Rust 检查正文时,将正文和标头从hyper HTTP请求复制到新请求
我想使用hyper创建一个小型的Rust HTTP代理,它接受请求、转发请求并转储请求+正文 基于,代理部分工作正常 但是,我不能简单地复制和打印请求正文。我的主要问题是请求主体不能简单地复制到像Vec这样的东西中。我无法解构请求以读取正文,然后稍后再创建它,因为解构的标题无法添加到新请求中 以下代码显示了我的最小HTTP代理示例:Rust 检查正文时,将正文和标头从hyper HTTP请求复制到新请求,rust,hyper,Rust,Hyper,我想使用hyper创建一个小型的Rust HTTP代理,它接受请求、转发请求并转储请求+正文 基于,代理部分工作正常 但是,我不能简单地复制和打印请求正文。我的主要问题是请求主体不能简单地复制到像Vec这样的东西中。我无法解构请求以读取正文,然后稍后再创建它,因为解构的标题无法添加到新请求中 以下代码显示了我的最小HTTP代理示例: extern crate futures; extern crate hyper; extern crate tokio_core; use futures::{
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use futures::{Future, Stream};
use hyper::{Body, Client, StatusCode};
use hyper::client::HttpConnector;
use hyper::header::{ContentLength, ContentType};
use hyper::server::{Http, Request, Response, Service};
use tokio_core::reactor::Core;
type HTTPClient = Client<HttpConnector, Body>;
struct Server {
client: HTTPClient,
}
impl Server {
pub fn new(client: HTTPClient) -> Server {
Server { client: client }
}
}
impl Service for Server {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, mut req: Request) -> Self::Future {
let req_uri_str = {
let uri = req.uri();
format!(
"http://localhost{}?{}",
uri.path(),
uri.query().unwrap_or_default()
)
};
req.set_uri(req_uri_str.parse().unwrap());
// Try to create a copy of the new request
/*
let (method, uri, version, headers, body) = req.deconstruct();
let mut req_copy: Request<hyper::Body> = Request::new(method, uri);
// Main problem: How can the request body be copied?
// >>> let body_bytes: Vec<u8> = ...
req_copy.set_body(body);
req_copy.set_version(version);
// Try to copy the headers
for header in headers.iter() {
req_copy.headers_mut().set(header.value().unwrap());
}
*/
// This works if the request is not deconstructed
let work = self.client
.request(req)
.and_then(|res| futures::future::ok(res))
.or_else(|err| {
let body = format!("{}\n", err);
futures::future::ok(
Response::new()
.with_status(StatusCode::BadRequest)
.with_header(ContentType::plaintext())
.with_header(ContentLength(body.len() as u64))
.with_body(body),
)
});
Box::new(work)
}
}
fn main() {
// Create HTTP client core + handles
let mut core = Core::new().unwrap();
let handle = core.handle();
let handle_clone = handle.clone();
// Create HTTP server
let server_addr = "127.0.0.1:9999".parse().unwrap();
let server = Http::new()
.serve_addr_handle(&server_addr, &handle, move || {
Ok(Server::new(Client::new(&handle_clone)))
})
.unwrap();
// Connect HTTP client with server
let handle_clone2 = handle.clone();
handle.spawn(
server
.for_each(move |conn| {
handle_clone2.spawn(conn.map(|_| ()).map_err(|err| println!("Error: {:?}", err)));
Ok(())
})
.map_err(|_| ()),
);
core.run(futures::future::empty::<(), ()>()).unwrap();
}
如果您在端口80上运行任何HTTP服务,使用浏览器连接到端口9999将完美地转发任何响应和请求
但是,如果您重新启用有关构建新的复制请求的行,我的方法将失败,因为我不知道如何复制头。此外,在复制请求主体时,这并没有真正帮助我
我知道这里也有类似的问题,但没有一个符合我的要求,在查看请求主体后重新使用它,或者根本没有答案
请求主体不能简单地复制到Vec之类的东西中
当然可以。在Rust标准库中,值得记住迭代器特性的功能。在处理未来问题时,你还应该记住和的能力
例如,hyper的实现流。这意味着您可以使用以下方法:
将流的所有结果连接到单个可扩展目标,返回表示最终结果的未来
这将创建一个可以转换为Vec的大数据块:
同样,Vec可以转换回实体
因为解构的头不能添加到新请求中
总而言之:
fn create_localhost_request(req: Request) -> (Request, Body) {
let (method, uri, version, headers, body) = req.deconstruct();
let req_uri_str = {
format!(
"http://localhost{}?{}",
uri.path(),
uri.query().unwrap_or_default()
)
};
let uri = req_uri_str.parse().unwrap();
let mut req_copy = Request::new(method, uri);
req_copy.set_version(version);
req_copy.headers_mut().extend(headers.iter());
(req_copy, body)
}
fn perform_proxy_request(
client: HttpClient,
req: Request,
) -> Box<Future<Item = Response, Error = hyper::Error>> {
Box::new(client.request(req).or_else(|err| {
let body = format!("{}\n", err);
Ok(Response::new()
.with_status(StatusCode::BadRequest)
.with_header(ContentType::plaintext())
.with_header(ContentLength(body.len() as u64))
.with_body(body))
}))
}
impl Service for Server {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, req: Request) -> Self::Future {
let (mut req, body) = create_localhost_request(req);
let client = self.client.clone();
let work = body
.concat2()
.map(|chunk| chunk.to_vec())
// Do whatever we need with the body here, but be careful
// about doing any synchronous work.
.map(move |body| {
req.set_body(body);
req
})
.and_then(|req| perform_proxy_request(client, req));
Box::new(work)
}
}
请求主体不能简单地复制到Vec之类的东西中
当然可以。在Rust标准库中,值得记住迭代器特性的功能。在处理未来问题时,你还应该记住和的能力
例如,hyper的实现流。这意味着您可以使用以下方法:
将流的所有结果连接到单个可扩展目标,返回表示最终结果的未来
这将创建一个可以转换为Vec的大数据块:
同样,Vec可以转换回实体
因为解构的头不能添加到新请求中
总而言之:
fn create_localhost_request(req: Request) -> (Request, Body) {
let (method, uri, version, headers, body) = req.deconstruct();
let req_uri_str = {
format!(
"http://localhost{}?{}",
uri.path(),
uri.query().unwrap_or_default()
)
};
let uri = req_uri_str.parse().unwrap();
let mut req_copy = Request::new(method, uri);
req_copy.set_version(version);
req_copy.headers_mut().extend(headers.iter());
(req_copy, body)
}
fn perform_proxy_request(
client: HttpClient,
req: Request,
) -> Box<Future<Item = Response, Error = hyper::Error>> {
Box::new(client.request(req).or_else(|err| {
let body = format!("{}\n", err);
Ok(Response::new()
.with_status(StatusCode::BadRequest)
.with_header(ContentType::plaintext())
.with_header(ContentLength(body.len() as u64))
.with_body(body))
}))
}
impl Service for Server {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, req: Request) -> Self::Future {
let (mut req, body) = create_localhost_request(req);
let client = self.client.clone();
let work = body
.concat2()
.map(|chunk| chunk.to_vec())
// Do whatever we need with the body here, but be careful
// about doing any synchronous work.
.map(move |body| {
req.set_body(body);
req
})
.and_then(|req| perform_proxy_request(client, req));
Box::new(work)
}
}
@Shepmaster是的,我已经阅读了所有的指南,并查看了存储库中提供的示例;这些都没有涉及到这个问题。@Shepmaster我在我的问题中包括了一个完整的、可运行的例子。我已经尝试将其最小化,但运行HTTP服务器和客户端只需要一些样板代码。请随意缩短它,无论你看到任何潜在的地方。@Shepmaster是的,我已经阅读了所有的指南,并查看了存储库中提供的示例;这些都没有涉及到这个问题。@Shepmaster我在我的问题中包括了一个完整的、可运行的例子。我已经尝试将其最小化,但运行HTTP服务器和客户端只需要一些样板代码。请随意缩短它,无论您在哪里看到任何潜力。我必须承认,我应该考虑使用我在示例中看到的concat2迭代器功能简单地处理请求体。非常感谢,这正是我想要的@迭代器不提供PhilippLudwig concat2;它是由Stream提供的。上面有一个链接,指向相应的文档。我明白了。多亏了你的详细解释,我也很容易地检查了回复。不过,我必须仔细阅读Streams和Futures。有什么原因导致这个代码位从未在请求对象上成为克隆特征?@adv我不知道是否有任何超级维护人员看到过这一点,或者是否有人提交了添加它的请求。也许你可以!我必须承认,我应该考虑使用我在示例中看到的concat2迭代器功能来处理请求体。非常感谢,这正是我想要的@迭代器不提供PhilippLudwig concat2;它是由Stream提供的。上面有一个链接,指向相应的文档。我明白了。多亏了你的详细解释,我也很容易地检查了回复。不过,我必须仔细阅读Streams和Futures。有什么原因导致这个代码位从未在请求对象上成为克隆特征?@adv我不知道是否有任何超级维护人员看到过这一点,或者是否有人提交了添加它的请求。也许你可以!
fn create_localhost_request(req: Request) -> (Request, Body) {
let (method, uri, version, headers, body) = req.deconstruct();
let req_uri_str = {
format!(
"http://localhost{}?{}",
uri.path(),
uri.query().unwrap_or_default()
)
};
let uri = req_uri_str.parse().unwrap();
let mut req_copy = Request::new(method, uri);
req_copy.set_version(version);
req_copy.headers_mut().extend(headers.iter());
(req_copy, body)
}
fn perform_proxy_request(
client: HttpClient,
req: Request,
) -> Box<Future<Item = Response, Error = hyper::Error>> {
Box::new(client.request(req).or_else(|err| {
let body = format!("{}\n", err);
Ok(Response::new()
.with_status(StatusCode::BadRequest)
.with_header(ContentType::plaintext())
.with_header(ContentLength(body.len() as u64))
.with_body(body))
}))
}
impl Service for Server {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
fn call(&self, req: Request) -> Self::Future {
let (mut req, body) = create_localhost_request(req);
let client = self.client.clone();
let work = body
.concat2()
.map(|chunk| chunk.to_vec())
// Do whatever we need with the body here, but be careful
// about doing any synchronous work.
.map(move |body| {
req.set_body(body);
req
})
.and_then(|req| perform_proxy_request(client, req));
Box::new(work)
}
}