如何在运行时在Hyper中设置原始标头?

时间:2017-05-18 17:25:02

标签: rust hyper

我尝试在运行时在Hyper Response上设置标头。在实际代码中,标题来自一个文件,所以我不知道它们在编译时是什么。不幸的是,我已经通过文档和我能找到的所有示例挖掘但是没有看到有人这样做。这是代码:

extern crate hyper;
use std::collections::HashMap;
use std::io::{Read,Write};
use hyper::server::{Handler,Server,Response,Request};
use hyper::header::*;

fn main() {
    let headers = HashMap::new();
    headers.insert("X-Test-Header".to_string(), "test_value".to_string());
    let responder = Responder::new(headers);

    Server::http("127.0.0.1:1340")
    .unwrap()
    .handle(responder)
    .unwrap();
}

struct Responder {
    headers: HashMap<String, String>
}

impl Responder {
    pub fn new(headers: HashMap<String,String>) -> Self {
        Responder {
            headers: headers.clone()
        }
    }
    fn respond_success(&self, mut res: Response, content: &[u8]) {
        res.headers_mut()
            .set(ContentLength(content.len() as u64));
        for (key, val) in self.headers.iter() {
            res.headers_mut()
                .set_raw(key.as_str(), vec![val.into_bytes()])
        }
        let mut res_body = res.start().unwrap();
        res_body.write_all(content).unwrap();
    }
}

impl Handler for Responder {
    fn handle(&self, req: Request, res: Response) {
        let content = b"Hello World!";
        self.respond_success(res, content);
    }
}

错误:

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> src\main.rs:31:40
   |
31 |         for (key, val) in self.headers.iter() {
   |                                        ^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 28:65...
  --> src\main.rs:28:66
   |
28 |     fn respond_success(&self, mut res: Response, content: &[u8]) {
   |                                                                  ^
note: ...so that reference does not outlive borrowed content
  --> src\main.rs:31:27
   |
31 |         for (key, val) in self.headers.iter() {
   |                           ^^^^^^^^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that types are compatible (expected std::convert::From<&str>, found std::convert::From<&str>)
  --> src\main.rs:33:18
   |
33 |                 .set_raw(key.as_str(), vec![val.into_bytes()])
   |                  ^^^^^^^

看起来set_raw需要一些静态的东西,但我不确定我重构这个的最佳选择是什么。 responder必须是静态的吗?这似乎不对。

1 个答案:

答案 0 :(得分:2)

查看Headers::set_raw的签名:

fn set_raw<K>(&mut self, name: K, value: Vec<Vec<u8>>)
where 
    K: Into<Cow<'static, str>>

这表示name可以是任何可以转换为Cow<'static, str>的类型。这意味着它可以是&'static str a String

接下来,查看您尝试传递的类型:

.set_raw(key.as_str(), vec![val.into_bytes()])
  1. String::as_str返回一个字符串切片,只要它被调用String 'static生命周期。

    相反,您需要获得一个拥有的字符串。

  2. 您正在&String上拨打String::into_bytes,但是,正如Cannot move out of borrowed content中所述,您无法通过值接收接收器。

    相反,您需要获得字节的拥有副本。

  3. 一起看起来像:

    for (key, val) in self.headers.iter() {
        res.headers_mut()
            .set_raw(key.clone(), vec![val.clone().into_bytes()])
    }