Skip to content

Commit 6987ee5

Browse files
committed
feat: pass context to should_intercept
1 parent 4165da0 commit 6987ee5

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub trait HttpHandler: Clone + Send + Sync + 'static {
115115
}
116116

117117
/// Whether a CONNECT request should be intercepted. Defaults to `true` for all requests.
118-
async fn should_intercept(&mut self, _req: &Request<Body>) -> bool {
118+
async fn should_intercept(&mut self, _ctx: &HttpContext, _req: &Request<Body>) -> bool {
119119
true
120120
}
121121
}

src/proxy/internal.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ where
6969
H: HttpHandler,
7070
W: WebSocketHandler,
7171
{
72+
fn context(&self) -> HttpContext {
73+
HttpContext {
74+
client_addr: self.client_addr,
75+
}
76+
}
77+
7278
#[instrument(
7379
skip_all,
7480
fields(
@@ -82,9 +88,7 @@ where
8288
mut self,
8389
req: Request<Body>,
8490
) -> Result<Response<Body>, hyper::Error> {
85-
let ctx = HttpContext {
86-
client_addr: self.client_addr,
87-
};
91+
let ctx = self.context();
8892

8993
let req = match self
9094
.http_handler
@@ -136,7 +140,11 @@ where
136140
bytes::Bytes::copy_from_slice(buffer[..bytes_read].as_ref()),
137141
);
138142

139-
if self.http_handler.should_intercept(&req).await {
143+
if self
144+
.http_handler
145+
.should_intercept(&self.context(), &req)
146+
.await
147+
{
140148
if buffer == *b"GET " {
141149
if let Err(e) =
142150
self.serve_stream(upgraded, Scheme::HTTP, authority).await

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl HttpHandler for TestHandler {
305305
decode_response(res).unwrap()
306306
}
307307

308-
async fn should_intercept(&mut self, _req: &Request<Body>) -> bool {
308+
async fn should_intercept(&mut self, _ctx: &HttpContext, _req: &Request<Body>) -> bool {
309309
self.should_intercept
310310
}
311311
}

0 commit comments

Comments
 (0)