-
Notifications
You must be signed in to change notification settings - Fork 11
Add a http server proxy example #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
e3aeefa
to
2142719
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks or this example! I agree a proxy is great example to have. I just have a few comments to simpliy the code:
// Copy headers from server request to the client request. | ||
for (key, value) in server_req.headers() { | ||
client_req = client_req.header(key, value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of copying the headers one at a time, could this do what the http server example does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the simplified example.
.headers_mut() | ||
.unwrap() | ||
.append(key, value.clone()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And similarly, could this avoid copying headers individually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the simplified example.
examples/http_server_proxy.rs
Outdated
let server_req_to_client_req = async { | ||
let res = copy(server_req.body_mut(), &mut client_request_body).await; | ||
// TODO: Convert to io error if necessary | ||
let _ = Client::finish(client_request_body, None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, can we map io errors to ErrorVariant::BodyIo
error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am constructing a std::io::Error
now.
let (mut client_request_body, client_resp) = client | ||
.start_request(client_req) | ||
.await | ||
.expect("client.start_request failed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using start_request
works here, though is a little more verbose than needed for a simple proxy. If you change the request above to use .body(server_req.into_body())
, then you can use plain send
instead of start_request
and manually copying the body.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the simplified example.
} | ||
// Start sending the server response. | ||
let server_resp = server_resp.body(BodyForthcoming).unwrap(); | ||
let mut server_resp = responder.start_response(server_resp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to above, this can do server_resp.body(client_resp.into_body())
and plain respond
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in the simplified example.
Thanks for the code review. I wanted to show a streaming example, as it took me a while to get it, but I understand that having something more simple might be useful as well. I have fixed the TODO and created |
This is a proposal to add a HTTP proxy example as it seems to be a common use case.