Skip to content

Commit

Permalink
feat: pass CONNECT requests through handler
Browse files Browse the repository at this point in the history
  • Loading branch information
omjadas committed Aug 9, 2022
1 parent e6432db commit 35aca05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
48 changes: 22 additions & 26 deletions src/proxy/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,10 @@ where
client_addr = %self.client_addr,
)
)]
pub(crate) async fn proxy(self, req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
if req.method() == Method::CONNECT {
self.process_connect(req).await
} else {
self.process_request(req).await
}
}

#[instrument(skip_all)]
async fn process_request(mut self, req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
pub(crate) async fn proxy(
mut self,
req: Request<Body>,
) -> Result<Response<Body>, hyper::Error> {
let ctx = HttpContext {
client_addr: self.client_addr,
};
Expand All @@ -94,24 +88,26 @@ where
RequestOrResponse::Response(res) => return Ok(res),
};

if hyper_tungstenite::is_upgrade_request(&req) {
return Ok(self.upgrade_websocket(req));
}

let res = self
.client
.request(normalize_request(req))
.instrument(info_span!("proxy_request"))
.await?;
if req.method() == Method::CONNECT {
self.process_connect(req)
} else if hyper_tungstenite::is_upgrade_request(&req) {
Ok(self.upgrade_websocket(req))
} else {
let res = self
.client
.request(normalize_request(req))
.instrument(info_span!("proxy_request"))
.await?;

Ok(self
.http_handler
.handle_response(&ctx, res)
.instrument(info_span!("handle_response"))
.await)
Ok(self
.http_handler
.handle_response(&ctx, res)
.instrument(info_span!("handle_response"))
.await)
}
}

async fn process_connect(self, mut req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
fn process_connect(self, mut req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let span = info_span!("process_connect");
let fut = async move {
match hyper::upgrade::on(&mut req).await {
Expand Down Expand Up @@ -288,7 +284,7 @@ where
req = Request::from_parts(parts, body);
};

self.clone().process_request(req)
self.clone().proxy(req)
});

Http::new()
Expand Down
4 changes: 2 additions & 2 deletions tests/openssl_ca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn https_rustls() {
.unwrap();

assert_eq!(res.status(), 200);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 1);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 2);
assert_eq!(http_handler.response_counter.load(Ordering::Relaxed), 1);

stop_server.send(()).unwrap();
Expand All @@ -61,7 +61,7 @@ async fn https_native_tls() {
.unwrap();

assert_eq!(res.status(), 200);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 1);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 2);
assert_eq!(http_handler.response_counter.load(Ordering::Relaxed), 1);

stop_server.send(()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/rcgen_ca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn https_rustls() {
.unwrap();

assert_eq!(res.status(), 200);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 1);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 2);
assert_eq!(http_handler.response_counter.load(Ordering::Relaxed), 1);

stop_server.send(()).unwrap();
Expand All @@ -67,7 +67,7 @@ async fn https_native_tls() {
.unwrap();

assert_eq!(res.status(), 200);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 1);
assert_eq!(http_handler.request_counter.load(Ordering::Relaxed), 2);
assert_eq!(http_handler.response_counter.load(Ordering::Relaxed), 1);

stop_server.send(()).unwrap();
Expand Down

0 comments on commit 35aca05

Please sign in to comment.