3

我正在寻找Response使用以下代码设置 Iron 的标题:

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`

为什么我无法将标头传递到此元组中以供Request构建器修改?

4

1 回答 1

11

您可以修改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没有实现 trait iron::modifier::Modifier<iron::Response>。如果我们查看文档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)))
}

如果我们看一下for的实现ModifierHeader

fn modify(self, res: &mut Response) {
    res.headers.set(self.0);
}

它只是像我们上面所做的那样设置标题。


仅供参考,Rust 风格适用snake_case于变量和方法,Error::description(&why)通常写成why.description().

于 2016-05-23T20:38:08.957 回答