Skip to content
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

fix(examples): send only path of URI in request #3438

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/client.rs
Expand Up @@ -54,9 +54,10 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
});

let authority = url.authority().unwrap().clone();
let uri = url.path_and_query().map(|p| p.as_str()).unwrap_or("/");

let req = Request::builder()
.uri(url)
.uri(uri)
.header(hyper::header::HOST, authority.as_str())
.body(Empty::<Bytes>::new())?;

Expand Down
3 changes: 2 additions & 1 deletion examples/client_json.rs
Expand Up @@ -43,10 +43,11 @@ async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
});

let authority = url.authority().unwrap().clone();
let uri = url.path_and_query().map(|p| p.as_str()).unwrap_or("/");

// Fetch the url...
let req = Request::builder()
.uri(url)
.uri(uri)
.header(hyper::header::HOST, authority.as_str())
.body(Empty::<Bytes>::new())?;

Expand Down
7 changes: 6 additions & 1 deletion examples/http_proxy.rs
Expand Up @@ -3,6 +3,7 @@
use std::net::SocketAddr;

use bytes::Bytes;
use http::{uri, Uri};
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full};
use hyper::client::conn::http1::Builder;
use hyper::server::conn::http1;
Expand Down Expand Up @@ -49,7 +50,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

async fn proxy(
req: Request<hyper::body::Incoming>,
mut req: Request<hyper::body::Incoming>,
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, hyper::Error> {
println!("req: {:?}", req);

Expand Down Expand Up @@ -105,6 +106,10 @@ async fn proxy(
}
});

let mut uri_parts = uri::Parts::default();
uri_parts.path_and_query = req.uri().path_and_query().cloned();
*req.uri_mut() = Uri::from_parts(uri_parts).unwrap();

let resp = sender.send_request(req).await?;
Ok(resp.map(|b| b.boxed()))
}
Expand Down