Skip to content

Conversation

@tomasol
Copy link

@tomasol tomasol commented Feb 25, 2025

This is a proposal to add a HTTP proxy example as it seems to be a common use case.

Copy link
Member

@sunfishcode sunfishcode left a 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);
}
Copy link
Member

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?

Copy link
Author

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());
}
Copy link
Member

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?

Copy link
Author

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.

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);
Copy link
Member

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?

Copy link
Author

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");
Copy link
Member

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.

Copy link
Author

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);
Copy link
Member

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.

Copy link
Author

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.

@tomasol
Copy link
Author

tomasol commented Mar 13, 2025

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 http_server_proxy_simple.

@tomasol tomasol force-pushed the http-proxy-example branch from 38fa832 to 0c2b8ab Compare October 25, 2025 09:17
@tomasol
Copy link
Author

tomasol commented Oct 25, 2025

@pchickey I've attempted to upgrade the streaming proxy example to the latest main, but I haven't figured out how to convert UnsyncBoxBody<Bytes, Error> to Response<Body>. A pointer would be appreciated!

@tomasol tomasol marked this pull request as draft October 25, 2025 09:20
@pchickey
Copy link
Contributor

@tomasol
Copy link
Author

tomasol commented Oct 29, 2025

Thanks, that gave me the right direction. I couldn't figure out why the proxy was broken WRT grpc-web, but then I found out it is not forwarding response headers. Response::from_parts fixed that issue.

@tomasol tomasol marked this pull request as ready for review October 29, 2025 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants