gpt4 book ai didi

rust - 在检查正文时将正文和 header 从超 HTTP 请求复制到新请求

转载 作者:行者123 更新时间:2023-11-29 08:12:19 41 4
gpt4 key购买 nike

我想使用 hyper 创建一个小型 Rust HTTP 代理,它接受请求、转发它们并转储请求 + 正文。

基于 this example ,代理部分工作正常。

但是,我不能简单地复制和打印请求正文。我的主要问题是请求正文不能简单地复制到类似 Vec<u8> 的内容中。 .我不能deconstruct读取正文然后稍后创建它的请求,因为无法将解构的 header 添加到新请求中。

以下代码显示了我的最小 HTTP 代理示例:

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 将完美地转发任何响应和请求。

但是,如果您重新启用有关构建新的复制请求的行,我的方法就会失败,因为我不明白如何复制 header 。 (此外,这在复制请求正文时并没有真正帮助我)

我知道这里有类似的问题,但没有一个符合我在查看请求体后重新使用请求体的要求(或者根本没有答案)。

最佳答案

the request body can't be simply copied into something like an Vec<u8>

当然可以。在 Rust 标准库中,值得记住 Iterator 的功能特征。和 future 打交道,也要记住 Future 的能力和 Stream .

例如,hyper 的 Body 工具 Stream .这意味着您可以使用 Stream::concat2 方法:

Concatenate all results of a stream into a single extendable destination, returning a future representing the end result.

这会创建一个大的 Chunk可以转换为 Vec :

extern crate hyper; // 0.11.22
extern crate futures; // 0.1.18

use futures::{Future, Stream};

fn example(req: hyper::Request) {
req.body().concat2().map(|chunk| {
let body = chunk.to_vec();
println!("{:?}", body);
()
});
// Use this future somehow!
}

同样,一个 Vec<u8>可以转换回 Body .

since the deconstructed headers can't be added to a new request.

req_copy.headers_mut().extend(headers.iter());

一起:

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)
}
}

关于rust - 在检查正文时将正文和 header 从超 HTTP 请求复制到新请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49379145/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com