Closed flaviodelgrosso closed 2 weeks ago
You can convert Request<Incoming>
to Request<BoxBody<bytes::Bytes, hyper::Error>>
.
let req = if uri.host() == Some("my_interested_host_1") {
let new_req = inspect_request(req).await?;
new_req // <-------------------------------------------- here the problem
} else {
req.map(|b| b.boxed())
};
You can convert
Request<Incoming>
toRequest<BoxBody<bytes::Bytes, hyper::Error>>
.let req = if uri.host() == Some("my_interested_host_1") { let new_req = inspect_request(req).await?; new_req // <-------------------------------------------- here the problem } else { req.map(|b| b.boxed()) };
Totally right, I'm stupid :). Thanks for suggestion. It would be nice however to have a concrete implementation of Body struct that represents all body types. Something like:
#[derive(Debug)]
enum Internal {
BoxBody(BoxBody<Bytes, Error>),
Collected(Collected<Bytes>),
Empty(Empty<Bytes>),
Full(Full<Bytes>),
Incoming(Incoming),
String(String),
}
#[derive(Debug)]
pub struct Body {
inner: Internal,
}
// continue with implementation.....
First off, thank you for developing and maintaining this crate. I'm currently working on a feature that involves forwarding HTTP requests and responses between a client and an upstream server. Specifically, I need to copy data from the streams while forwarding them, allowing me to save the contents of both requests and responses in a buffer for later processing.
Problem:
When handling the response body from the upstream server, I'm able to forward it back to the client and clone the body successfully. However, I'm encountering difficulties when trying to apply the same logic to incoming requests.
For Responses:
I implemented a
BodyClone
struct (see code below) and managed to forward the response while cloning the body using the approach of boxing according to your examples:This works well for responses, allowing me to forward and clone the response body simultaneously.
For Requests:
The challenge arises with requests. The
.bind
method expects a service with a request of typeRequest<Incoming>
, so there's a mismatching types betweenRequest<Incoming>
andRequest<BoxBody<bytes::Bytes, hyper::Error>>
.Can you add some examples in the repo regarding this topic or maybe better solutions than mines? Thanks in advance.
That's the full example:
This is the implementation: