我希望使用以下代码设置 Iron Response
的 header :
extern crate iron; // 0.3.0
extern crate hyper; // 0.8.1
use iron::prelude::*;
use iron::status;
use hyper::header::{Headers, ContentType};
use hyper::mime::{Mime, TopLevel, SubLevel};
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main() {
fn hello_world(_: &mut Request) -> IronResult<Response> {
let mut headers = Headers::new();
let string = getFileAsString("./public/index.html");
headers.set(
ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))
);
Ok(Response::with((status::Ok, string, headers)))
}
Iron::new(hello_world).http("localhost:3000").unwrap();
println!("On 3000");
}
fn getFileAsString(fileStr: &str) -> String {
let path = Path::new(fileStr);
let display = path.display();
let mut fileContents = String::new();
let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
Ok(file) => file,
};
match file.read_to_string(&mut fileContents) {
Err(why) => panic!("couldn't read {}: {}", display, Error::description(&why)),
Ok(_) => fileContents
}
}
但是我得到了错误:
error[E0277]: the trait bound `iron::Headers: iron::modifier::Modifier<iron::Response>` is not satisfied
--> src/main.rs:24:12
|
24 | Ok(Response::with((status::Ok, string, headers)))
| ^^^^^^^^^^^^^^ the trait `iron::modifier::Modifier<iron::Response>` is not implemented for `iron::Headers`
|
= note: required because of the requirements on the impl of `iron::modifier::Modifier<iron::Response>` for `(hyper::status::StatusCode, std::string::String, iron::Headers)`
= note: required by `iron::Response::with`
为什么我无法将 header 传递到此元组中以供 Request
构建器修改?
最佳答案
您可以修改 Response
上的标题对象:
fn hello_world(_: &mut Request) -> IronResult<Response> {
let string = get_file_as_string("./public/index.html");
let mut resp = Response::with((status::Ok, string));
resp.headers.set(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])));
Ok(resp)
}
要找出原始错误,让我们检查错误消息:
error[E0277]: the trait bound `iron::Headers: iron::modifier::Modifier<iron::Response>` is not satisfied
--> src/main.rs:24:12
|
24 | Ok(Response::with((status::Ok, string, headers)))
| ^^^^^^^^^^^^^^ the trait `iron::modifier::Modifier<iron::Response>` is not implemented for `iron::Headers`
|
= note: required because of the requirements on the impl of `iron::modifier::Modifier<iron::Response>` for `(hyper::status::StatusCode, std::string::String, iron::Headers)`
= note: required by `iron::Response::with`
第一行告诉我们眼前的问题:iron::Headers
没有实现特征 iron::modifier::Modifier<iron::Response>
.如果我们检查 documentation for Headers
, 我们可以在 Trait Implementations 部分看到它确实没有实现 Modifier
.
然后我们可以从另一端看问题: 实现了 Modifier
是什么? ? Modifier
的文档,当与 Iron 一起构建时,请回答该问题。我们可以看到的一件事是:
impl<H> Modifier<Response> for Header<H>
where
H: Header + HeaderFormat,
这导致另一种可能性:
use iron::modifiers::Header;
fn hello_world(_: &mut Request) -> IronResult<Response> {
let string = get_file_as_string("./public/index.html");
let content_type = Header(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![])));
Ok(Response::with((status::Ok, string, content_type)))
}
如果我们看一下 the implementation of Modifier
for Header
:
fn modify(self, res: &mut Response) {
res.headers.set(self.0);
}
它只是像我们上面那样设置标题。
仅供引用,Rust 风格是 snake_case
对于变量和方法以及 Error::description(&why)
通常写成why.description()
.
关于rust - 无法设置 Iron 框架响应的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399635/