Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

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.

2 participants