Skip to content

Commit 2dc9768

Browse files
authored
feat(client): Add accessors to Connected fields (#2290)
1 parent 01103da commit 2dc9768

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/client/connect/mod.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
//! [`Connection`]: Connection
8282
use std::fmt;
8383

84-
use ::http::Response;
84+
use ::http::Extensions;
8585

8686
#[cfg(feature = "tcp")]
8787
pub mod dns;
@@ -149,6 +149,11 @@ impl Connected {
149149
self
150150
}
151151

152+
/// Determines if the connected transport is to an HTTP proxy.
153+
pub fn is_proxied(&self) -> bool {
154+
self.is_proxied
155+
}
156+
152157
/// Set extra connection information to be set in the extensions of every `Response`.
153158
pub fn extra<T: Clone + Send + Sync + 'static>(mut self, extra: T) -> Connected {
154159
if let Some(prev) = self.extra {
@@ -159,13 +164,24 @@ impl Connected {
159164
self
160165
}
161166

162-
/// Set that the connected transport negotiated HTTP/2 as it's
163-
/// next protocol.
167+
/// Copies the extra connection information into an `Extensions` map.
168+
pub fn get_extras(&self, extensions: &mut Extensions) {
169+
if let Some(extra) = &self.extra {
170+
extra.set(extensions);
171+
}
172+
}
173+
174+
/// Set that the connected transport negotiated HTTP/2 as its next protocol.
164175
pub fn negotiated_h2(mut self) -> Connected {
165176
self.alpn = Alpn::H2;
166177
self
167178
}
168179

180+
/// Determines if the connected transport negotiated HTTP/2 as its next protocol.
181+
pub fn is_negotiated_h2(&self) -> bool {
182+
self.alpn == Alpn::H2
183+
}
184+
169185
// Don't public expose that `Connected` is `Clone`, unsure if we want to
170186
// keep that contract...
171187
pub(super) fn clone(&self) -> Connected {
@@ -180,7 +196,7 @@ impl Connected {
180196
// ===== impl Extra =====
181197

182198
impl Extra {
183-
pub(super) fn set(&self, res: &mut Response<crate::Body>) {
199+
pub(super) fn set(&self, res: &mut Extensions) {
184200
self.0.set(res);
185201
}
186202
}
@@ -199,7 +215,7 @@ impl fmt::Debug for Extra {
199215

200216
trait ExtraInner: Send + Sync {
201217
fn clone_box(&self) -> Box<dyn ExtraInner>;
202-
fn set(&self, res: &mut Response<crate::Body>);
218+
fn set(&self, res: &mut Extensions);
203219
}
204220

205221
// This indirection allows the `Connected` to have a type-erased "extra" value,
@@ -216,8 +232,8 @@ where
216232
Box::new(self.clone())
217233
}
218234

219-
fn set(&self, res: &mut Response<crate::Body>) {
220-
res.extensions_mut().insert(self.0.clone());
235+
fn set(&self, res: &mut Extensions) {
236+
res.insert(self.0.clone());
221237
}
222238
}
223239

@@ -237,9 +253,9 @@ where
237253
Box::new(self.clone())
238254
}
239255

240-
fn set(&self, res: &mut Response<crate::Body>) {
256+
fn set(&self, res: &mut Extensions) {
241257
self.0.set(res);
242-
res.extensions_mut().insert(self.1.clone());
258+
res.insert(self.1.clone());
243259
}
244260
}
245261

@@ -340,13 +356,13 @@ mod tests {
340356
fn test_connected_extra() {
341357
let c1 = Connected::new().extra(Ex1(41));
342358

343-
let mut res1 = crate::Response::new(crate::Body::empty());
359+
let mut ex = ::http::Extensions::new();
344360

345-
assert_eq!(res1.extensions().get::<Ex1>(), None);
361+
assert_eq!(ex.get::<Ex1>(), None);
346362

347-
c1.extra.as_ref().expect("c1 extra").set(&mut res1);
363+
c1.extra.as_ref().expect("c1 extra").set(&mut ex);
348364

349-
assert_eq!(res1.extensions().get::<Ex1>(), Some(&Ex1(41)));
365+
assert_eq!(ex.get::<Ex1>(), Some(&Ex1(41)));
350366
}
351367

352368
#[test]
@@ -359,29 +375,29 @@ mod tests {
359375
.extra(Ex2("zoom"))
360376
.extra(Ex3("pew pew"));
361377

362-
let mut res1 = crate::Response::new(crate::Body::empty());
378+
let mut ex1 = ::http::Extensions::new();
363379

364-
assert_eq!(res1.extensions().get::<Ex1>(), None);
365-
assert_eq!(res1.extensions().get::<Ex2>(), None);
366-
assert_eq!(res1.extensions().get::<Ex3>(), None);
380+
assert_eq!(ex1.get::<Ex1>(), None);
381+
assert_eq!(ex1.get::<Ex2>(), None);
382+
assert_eq!(ex1.get::<Ex3>(), None);
367383

368-
c1.extra.as_ref().expect("c1 extra").set(&mut res1);
384+
c1.extra.as_ref().expect("c1 extra").set(&mut ex1);
369385

370-
assert_eq!(res1.extensions().get::<Ex1>(), Some(&Ex1(45)));
371-
assert_eq!(res1.extensions().get::<Ex2>(), Some(&Ex2("zoom")));
372-
assert_eq!(res1.extensions().get::<Ex3>(), Some(&Ex3("pew pew")));
386+
assert_eq!(ex1.get::<Ex1>(), Some(&Ex1(45)));
387+
assert_eq!(ex1.get::<Ex2>(), Some(&Ex2("zoom")));
388+
assert_eq!(ex1.get::<Ex3>(), Some(&Ex3("pew pew")));
373389

374390
// Just like extensions, inserting the same type overrides previous type.
375391
let c2 = Connected::new()
376392
.extra(Ex1(33))
377393
.extra(Ex2("hiccup"))
378394
.extra(Ex1(99));
379395

380-
let mut res2 = crate::Response::new(crate::Body::empty());
396+
let mut ex2 = ::http::Extensions::new();
381397

382-
c2.extra.as_ref().expect("c2 extra").set(&mut res2);
398+
c2.extra.as_ref().expect("c2 extra").set(&mut ex2);
383399

384-
assert_eq!(res2.extensions().get::<Ex1>(), Some(&Ex1(99)));
385-
assert_eq!(res2.extensions().get::<Ex2>(), Some(&Ex2("hiccup")));
400+
assert_eq!(ex2.get::<Ex1>(), Some(&Ex1(99)));
401+
assert_eq!(ex2.get::<Ex2>(), Some(&Ex2("hiccup")));
386402
}
387403
}

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ where
326326
let extra_info = pooled.conn_info.extra.clone();
327327
let fut = fut.map_ok(move |mut res| {
328328
if let Some(extra) = extra_info {
329-
extra.set(&mut res);
329+
extra.set(res.extensions_mut());
330330
}
331331
res
332332
});

0 commit comments

Comments
 (0)