Coverage Report

Created: 2024-12-17 06:15

/rust/registry/src/index.crates.io-6f17d22bba15001f/http-0.2.12/src/response.rs
Line
Count
Source (jump to first uncovered line)
1
//! HTTP response types.
2
//!
3
//! This module contains structs related to HTTP responses, notably the
4
//! `Response` type itself as well as a builder to create responses. Typically
5
//! you'll import the `http::Response` type rather than reaching into this
6
//! module itself.
7
//!
8
//! # Examples
9
//!
10
//! Creating a `Response` to return
11
//!
12
//! ```
13
//! use http::{Request, Response, StatusCode};
14
//!
15
//! fn respond_to(req: Request<()>) -> http::Result<Response<()>> {
16
//!     let mut builder = Response::builder()
17
//!         .header("Foo", "Bar")
18
//!         .status(StatusCode::OK);
19
//!
20
//!     if req.headers().contains_key("Another-Header") {
21
//!         builder = builder.header("Another-Header", "Ack");
22
//!     }
23
//!
24
//!     builder.body(())
25
//! }
26
//! ```
27
//!
28
//! A simple 404 handler
29
//!
30
//! ```
31
//! use http::{Request, Response, StatusCode};
32
//!
33
//! fn not_found(_req: Request<()>) -> http::Result<Response<()>> {
34
//!     Response::builder()
35
//!         .status(StatusCode::NOT_FOUND)
36
//!         .body(())
37
//! }
38
//! ```
39
//!
40
//! Or otherwise inspecting the result of a request:
41
//!
42
//! ```no_run
43
//! use http::{Request, Response};
44
//!
45
//! fn get(url: &str) -> http::Result<Response<()>> {
46
//!     // ...
47
//! # panic!()
48
//! }
49
//!
50
//! let response = get("https://www.rust-lang.org/").unwrap();
51
//!
52
//! if !response.status().is_success() {
53
//!     panic!("failed to get a successful response status!");
54
//! }
55
//!
56
//! if let Some(date) = response.headers().get("Date") {
57
//!     // we've got a `Date` header!
58
//! }
59
//!
60
//! let body = response.body();
61
//! // ...
62
//! ```
63
64
use std::any::Any;
65
use std::convert::TryFrom;
66
use std::fmt;
67
68
use crate::header::{HeaderMap, HeaderName, HeaderValue};
69
use crate::status::StatusCode;
70
use crate::version::Version;
71
use crate::{Extensions, Result};
72
73
/// Represents an HTTP response
74
///
75
/// An HTTP response consists of a head and a potentially optional body. The body
76
/// component is generic, enabling arbitrary types to represent the HTTP body.
77
/// For example, the body could be `Vec<u8>`, a `Stream` of byte chunks, or a
78
/// value that has been deserialized.
79
///
80
/// Typically you'll work with responses on the client side as the result of
81
/// sending a `Request` and on the server you'll be generating a `Response` to
82
/// send back to the client.
83
///
84
/// # Examples
85
///
86
/// Creating a `Response` to return
87
///
88
/// ```
89
/// use http::{Request, Response, StatusCode};
90
///
91
/// fn respond_to(req: Request<()>) -> http::Result<Response<()>> {
92
///     let mut builder = Response::builder()
93
///         .header("Foo", "Bar")
94
///         .status(StatusCode::OK);
95
///
96
///     if req.headers().contains_key("Another-Header") {
97
///         builder = builder.header("Another-Header", "Ack");
98
///     }
99
///
100
///     builder.body(())
101
/// }
102
/// ```
103
///
104
/// A simple 404 handler
105
///
106
/// ```
107
/// use http::{Request, Response, StatusCode};
108
///
109
/// fn not_found(_req: Request<()>) -> http::Result<Response<()>> {
110
///     Response::builder()
111
///         .status(StatusCode::NOT_FOUND)
112
///         .body(())
113
/// }
114
/// ```
115
///
116
/// Or otherwise inspecting the result of a request:
117
///
118
/// ```no_run
119
/// use http::{Request, Response};
120
///
121
/// fn get(url: &str) -> http::Result<Response<()>> {
122
///     // ...
123
/// # panic!()
124
/// }
125
///
126
/// let response = get("https://www.rust-lang.org/").unwrap();
127
///
128
/// if !response.status().is_success() {
129
///     panic!("failed to get a successful response status!");
130
/// }
131
///
132
/// if let Some(date) = response.headers().get("Date") {
133
///     // we've got a `Date` header!
134
/// }
135
///
136
/// let body = response.body();
137
/// // ...
138
/// ```
139
///
140
/// Deserialize a response of bytes via json:
141
///
142
/// ```
143
/// # extern crate serde;
144
/// # extern crate serde_json;
145
/// # extern crate http;
146
/// use http::Response;
147
/// use serde::de;
148
///
149
/// fn deserialize<T>(res: Response<Vec<u8>>) -> serde_json::Result<Response<T>>
150
///     where for<'de> T: de::Deserialize<'de>,
151
/// {
152
///     let (parts, body) = res.into_parts();
153
///     let body = serde_json::from_slice(&body)?;
154
///     Ok(Response::from_parts(parts, body))
155
/// }
156
/// #
157
/// # fn main() {}
158
/// ```
159
///
160
/// Or alternatively, serialize the body of a response to json
161
///
162
/// ```
163
/// # extern crate serde;
164
/// # extern crate serde_json;
165
/// # extern crate http;
166
/// use http::Response;
167
/// use serde::ser;
168
///
169
/// fn serialize<T>(res: Response<T>) -> serde_json::Result<Response<Vec<u8>>>
170
///     where T: ser::Serialize,
171
/// {
172
///     let (parts, body) = res.into_parts();
173
///     let body = serde_json::to_vec(&body)?;
174
///     Ok(Response::from_parts(parts, body))
175
/// }
176
/// #
177
/// # fn main() {}
178
/// ```
179
pub struct Response<T> {
180
    head: Parts,
181
    body: T,
182
}
183
184
/// Component parts of an HTTP `Response`
185
///
186
/// The HTTP response head consists of a status, version, and a set of
187
/// header fields.
188
pub struct Parts {
189
    /// The response's status
190
    pub status: StatusCode,
191
192
    /// The response's version
193
    pub version: Version,
194
195
    /// The response's headers
196
    pub headers: HeaderMap<HeaderValue>,
197
198
    /// The response's extensions
199
    pub extensions: Extensions,
200
201
    _priv: (),
202
}
203
204
/// An HTTP response builder
205
///
206
/// This type can be used to construct an instance of `Response` through a
207
/// builder-like pattern.
208
#[derive(Debug)]
209
pub struct Builder {
210
    inner: Result<Parts>,
211
}
212
213
impl Response<()> {
214
    /// Creates a new builder-style object to manufacture a `Response`
215
    ///
216
    /// This method returns an instance of `Builder` which can be used to
217
    /// create a `Response`.
218
    ///
219
    /// # Examples
220
    ///
221
    /// ```
222
    /// # use http::*;
223
    /// let response = Response::builder()
224
    ///     .status(200)
225
    ///     .header("X-Custom-Foo", "Bar")
226
    ///     .body(())
227
    ///     .unwrap();
228
    /// ```
229
    #[inline]
230
1.03k
    pub fn builder() -> Builder {
231
1.03k
        Builder::new()
232
1.03k
    }
Unexecuted instantiation: <http::response::Response<()>>::builder
Unexecuted instantiation: <http::response::Response<()>>::builder
<http::response::Response<()>>::builder
Line
Count
Source
230
1.03k
    pub fn builder() -> Builder {
231
1.03k
        Builder::new()
232
1.03k
    }
Unexecuted instantiation: <http::response::Response<()>>::builder
Unexecuted instantiation: <http::response::Response<()>>::builder
233
}
234
235
impl<T> Response<T> {
236
    /// Creates a new blank `Response` with the body
237
    ///
238
    /// The component ports of this response will be set to their default, e.g.
239
    /// the ok status, no headers, etc.
240
    ///
241
    /// # Examples
242
    ///
243
    /// ```
244
    /// # use http::*;
245
    /// let response = Response::new("hello world");
246
    ///
247
    /// assert_eq!(response.status(), StatusCode::OK);
248
    /// assert_eq!(*response.body(), "hello world");
249
    /// ```
250
    #[inline]
251
1.92M
    pub fn new(body: T) -> Response<T> {
252
1.92M
        Response {
253
1.92M
            head: Parts::new(),
254
1.92M
            body: body,
255
1.92M
        }
256
1.92M
    }
Unexecuted instantiation: <http::response::Response<_>>::new
<http::response::Response<linkerd_http_box::body::BoxBody>>::new
Line
Count
Source
251
151k
    pub fn new(body: T) -> Response<T> {
252
151k
        Response {
253
151k
            head: Parts::new(),
254
151k
            body: body,
255
151k
        }
256
151k
    }
<http::response::Response<hyper::body::body::Body>>::new
Line
Count
Source
251
1.76M
    pub fn new(body: T) -> Response<T> {
252
1.76M
        Response {
253
1.76M
            head: Parts::new(),
254
1.76M
            body: body,
255
1.76M
        }
256
1.76M
    }
Unexecuted instantiation: <http::response::Response<()>>::new
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::new
257
258
    /// Creates a new `Response` with the given head and body
259
    ///
260
    /// # Examples
261
    ///
262
    /// ```
263
    /// # use http::*;
264
    /// let response = Response::new("hello world");
265
    /// let (mut parts, body) = response.into_parts();
266
    ///
267
    /// parts.status = StatusCode::BAD_REQUEST;
268
    /// let response = Response::from_parts(parts, body);
269
    ///
270
    /// assert_eq!(response.status(), StatusCode::BAD_REQUEST);
271
    /// assert_eq!(*response.body(), "hello world");
272
    /// ```
273
    #[inline]
274
538k
    pub fn from_parts(parts: Parts, body: T) -> Response<T> {
275
538k
        Response {
276
538k
            head: parts,
277
538k
            body: body,
278
538k
        }
279
538k
    }
Unexecuted instantiation: <http::response::Response<_>>::from_parts
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::from_parts
<http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>, linkerd_app_core::classify::Eos>>>::from_parts
Line
Count
Source
274
538k
    pub fn from_parts(parts: Parts, body: T) -> Response<T> {
275
538k
        Response {
276
538k
            head: parts,
277
538k
            body: body,
278
538k
        }
279
538k
    }
Unexecuted instantiation: <http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Eos>>>::from_parts
Unexecuted instantiation: <http::response::Response<()>>::from_parts
Unexecuted instantiation: <http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<hyper_balance::PendingUntilFirstDataBody<tower::load::peak_ewma::Handle, hyper::body::body::Body>, linkerd_app_core::classify::Eos>>>::from_parts
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, tonic::status::Status>>>::from_parts
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::from_parts
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::from_parts
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::from_parts
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::from_parts
280
281
    /// Returns the `StatusCode`.
282
    ///
283
    /// # Examples
284
    ///
285
    /// ```
286
    /// # use http::*;
287
    /// let response: Response<()> = Response::default();
288
    /// assert_eq!(response.status(), StatusCode::OK);
289
    /// ```
290
    #[inline]
291
1.07M
    pub fn status(&self) -> StatusCode {
292
1.07M
        self.head.status
293
1.07M
    }
Unexecuted instantiation: <http::response::Response<_>>::status
<http::response::Response<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>>>::status
Line
Count
Source
291
538k
    pub fn status(&self) -> StatusCode {
292
538k
        self.head.status
293
538k
    }
Unexecuted instantiation: <http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>, linkerd_app_core::classify::Eos>>>::status
Unexecuted instantiation: <http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>>::status
Unexecuted instantiation: <http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>>::status
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::status
<http::response::Response<hyper::body::body::Body>>::status
Line
Count
Source
291
538k
    pub fn status(&self) -> StatusCode {
292
538k
        self.head.status
293
538k
    }
Unexecuted instantiation: <http::response::Response<()>>::status
Unexecuted instantiation: <http::response::Response<hyper_balance::PendingUntilFirstDataBody<tower::load::peak_ewma::Handle, hyper::body::body::Body>>>::status
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::status
294
295
    /// Returns a mutable reference to the associated `StatusCode`.
296
    ///
297
    /// # Examples
298
    ///
299
    /// ```
300
    /// # use http::*;
301
    /// let mut response: Response<()> = Response::default();
302
    /// *response.status_mut() = StatusCode::CREATED;
303
    /// assert_eq!(response.status(), StatusCode::CREATED);
304
    /// ```
305
    #[inline]
306
1.38M
    pub fn status_mut(&mut self) -> &mut StatusCode {
307
1.38M
        &mut self.head.status
308
1.38M
    }
Unexecuted instantiation: <http::response::Response<_>>::status_mut
<http::response::Response<linkerd_http_box::body::BoxBody>>::status_mut
Line
Count
Source
306
151k
    pub fn status_mut(&mut self) -> &mut StatusCode {
307
151k
        &mut self.head.status
308
151k
    }
<http::response::Response<hyper::body::body::Body>>::status_mut
Line
Count
Source
306
1.23M
    pub fn status_mut(&mut self) -> &mut StatusCode {
307
1.23M
        &mut self.head.status
308
1.23M
    }
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::status_mut
309
310
    /// Returns a reference to the associated version.
311
    ///
312
    /// # Examples
313
    ///
314
    /// ```
315
    /// # use http::*;
316
    /// let response: Response<()> = Response::default();
317
    /// assert_eq!(response.version(), Version::HTTP_11);
318
    /// ```
319
    #[inline]
320
538k
    pub fn version(&self) -> Version {
321
538k
        self.head.version
322
538k
    }
Unexecuted instantiation: <http::response::Response<_>>::version
Unexecuted instantiation: <http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>>::version
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::version
<http::response::Response<hyper::body::body::Body>>::version
Line
Count
Source
320
538k
    pub fn version(&self) -> Version {
321
538k
        self.head.version
322
538k
    }
323
324
    /// Returns a mutable reference to the associated version.
325
    ///
326
    /// # Examples
327
    ///
328
    /// ```
329
    /// # use http::*;
330
    /// let mut response: Response<()> = Response::default();
331
    /// *response.version_mut() = Version::HTTP_2;
332
    /// assert_eq!(response.version(), Version::HTTP_2);
333
    /// ```
334
    #[inline]
335
1.23M
    pub fn version_mut(&mut self) -> &mut Version {
336
1.23M
        &mut self.head.version
337
1.23M
    }
Unexecuted instantiation: <http::response::Response<_>>::version_mut
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::version_mut
<http::response::Response<hyper::body::body::Body>>::version_mut
Line
Count
Source
335
1.23M
    pub fn version_mut(&mut self) -> &mut Version {
336
1.23M
        &mut self.head.version
337
1.23M
    }
338
339
    /// Returns a reference to the associated header field map.
340
    ///
341
    /// # Examples
342
    ///
343
    /// ```
344
    /// # use http::*;
345
    /// let response: Response<()> = Response::default();
346
    /// assert!(response.headers().is_empty());
347
    /// ```
348
    #[inline]
349
0
    pub fn headers(&self) -> &HeaderMap<HeaderValue> {
350
0
        &self.head.headers
351
0
    }
Unexecuted instantiation: <http::response::Response<_>>::headers
Unexecuted instantiation: <http::response::Response<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>>>::headers
Unexecuted instantiation: <http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>>::headers
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::headers
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::headers
Unexecuted instantiation: <http::response::Response<()>>::headers
Unexecuted instantiation: <http::response::Response<hyper_balance::PendingUntilFirstDataBody<tower::load::peak_ewma::Handle, hyper::body::body::Body>>>::headers
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::headers
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::headers
352
353
    /// Returns a mutable reference to the associated header field map.
354
    ///
355
    /// # Examples
356
    ///
357
    /// ```
358
    /// # use http::*;
359
    /// # use http::header::*;
360
    /// let mut response: Response<()> = Response::default();
361
    /// response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
362
    /// assert!(!response.headers().is_empty());
363
    /// ```
364
    #[inline]
365
1.76M
    pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {
366
1.76M
        &mut self.head.headers
367
1.76M
    }
Unexecuted instantiation: <http::response::Response<_>>::headers_mut
Unexecuted instantiation: <http::response::Response<()>>::headers_mut
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::headers_mut
<http::response::Response<hyper::body::body::Body>>::headers_mut
Line
Count
Source
365
1.76M
    pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {
366
1.76M
        &mut self.head.headers
367
1.76M
    }
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::headers_mut
Unexecuted instantiation: <http::response::Response<()>>::headers_mut
Unexecuted instantiation: <http::response::Response<()>>::headers_mut
368
369
    /// Returns a reference to the associated extensions.
370
    ///
371
    /// # Examples
372
    ///
373
    /// ```
374
    /// # use http::*;
375
    /// let response: Response<()> = Response::default();
376
    /// assert!(response.extensions().get::<i32>().is_none());
377
    /// ```
378
    #[inline]
379
538k
    pub fn extensions(&self) -> &Extensions {
380
538k
        &self.head.extensions
381
538k
    }
Unexecuted instantiation: <http::response::Response<_>>::extensions
<http::response::Response<hyper::body::body::Body>>::extensions
Line
Count
Source
379
538k
    pub fn extensions(&self) -> &Extensions {
380
538k
        &self.head.extensions
381
538k
    }
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::extensions
382
383
    /// Returns a mutable reference to the associated extensions.
384
    ///
385
    /// # Examples
386
    ///
387
    /// ```
388
    /// # use http::*;
389
    /// # use http::header::*;
390
    /// let mut response: Response<()> = Response::default();
391
    /// response.extensions_mut().insert("hello");
392
    /// assert_eq!(response.extensions().get(), Some(&"hello"));
393
    /// ```
394
    #[inline]
395
1.23M
    pub fn extensions_mut(&mut self) -> &mut Extensions {
396
1.23M
        &mut self.head.extensions
397
1.23M
    }
Unexecuted instantiation: <http::response::Response<_>>::extensions_mut
Unexecuted instantiation: <http::response::Response<()>>::extensions_mut
<http::response::Response<hyper::body::body::Body>>::extensions_mut
Line
Count
Source
395
1.23M
    pub fn extensions_mut(&mut self) -> &mut Extensions {
396
1.23M
        &mut self.head.extensions
397
1.23M
    }
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::extensions_mut
398
399
    /// Returns a reference to the associated HTTP body.
400
    ///
401
    /// # Examples
402
    ///
403
    /// ```
404
    /// # use http::*;
405
    /// let response: Response<String> = Response::default();
406
    /// assert!(response.body().is_empty());
407
    /// ```
408
    #[inline]
409
0
    pub fn body(&self) -> &T {
410
0
        &self.body
411
0
    }
Unexecuted instantiation: <http::response::Response<_>>::body
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::body
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::body
412
413
    /// Returns a mutable reference to the associated HTTP body.
414
    ///
415
    /// # Examples
416
    ///
417
    /// ```
418
    /// # use http::*;
419
    /// let mut response: Response<String> = Response::default();
420
    /// response.body_mut().push_str("hello world");
421
    /// assert!(!response.body().is_empty());
422
    /// ```
423
    #[inline]
424
0
    pub fn body_mut(&mut self) -> &mut T {
425
0
        &mut self.body
426
0
    }
Unexecuted instantiation: <http::response::Response<_>>::body_mut
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::body_mut
427
428
    /// Consumes the response, returning just the body.
429
    ///
430
    /// # Examples
431
    ///
432
    /// ```
433
    /// # use http::Response;
434
    /// let response = Response::new(10);
435
    /// let body = response.into_body();
436
    /// assert_eq!(body, 10);
437
    /// ```
438
    #[inline]
439
691k
    pub fn into_body(self) -> T {
440
691k
        self.body
441
691k
    }
Unexecuted instantiation: <http::response::Response<_>>::into_body
<http::response::Response<hyper::body::body::Body>>::into_body
Line
Count
Source
439
691k
    pub fn into_body(self) -> T {
440
691k
        self.body
441
691k
    }
442
443
    /// Consumes the response returning the head and body parts.
444
    ///
445
    /// # Examples
446
    ///
447
    /// ```
448
    /// # use http::*;
449
    /// let response: Response<()> = Response::default();
450
    /// let (parts, body) = response.into_parts();
451
    /// assert_eq!(parts.status, StatusCode::OK);
452
    /// ```
453
    #[inline]
454
1.76M
    pub fn into_parts(self) -> (Parts, T) {
455
1.76M
        (self.head, self.body)
456
1.76M
    }
Unexecuted instantiation: <http::response::Response<_>>::into_parts
Unexecuted instantiation: <http::response::Response<()>>::into_parts
<http::response::Response<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>>>::into_parts
Line
Count
Source
454
538k
    pub fn into_parts(self) -> (Parts, T) {
455
538k
        (self.head, self.body)
456
538k
    }
<http::response::Response<linkerd_http_box::body::BoxBody>>::into_parts
Line
Count
Source
454
691k
    pub fn into_parts(self) -> (Parts, T) {
455
691k
        (self.head, self.body)
456
691k
    }
<http::response::Response<hyper::body::body::Body>>::into_parts
Line
Count
Source
454
538k
    pub fn into_parts(self) -> (Parts, T) {
455
538k
        (self.head, self.body)
456
538k
    }
Unexecuted instantiation: <http::response::Response<hyper_balance::PendingUntilFirstDataBody<tower::load::peak_ewma::Handle, hyper::body::body::Body>>>::into_parts
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::into_parts
Unexecuted instantiation: <http::response::Response<()>>::into_parts
Unexecuted instantiation: <http::response::Response<()>>::into_parts
457
458
    /// Consumes the response returning a new response with body mapped to the
459
    /// return type of the passed in function.
460
    ///
461
    /// # Examples
462
    ///
463
    /// ```
464
    /// # use http::*;
465
    /// let response = Response::builder().body("some string").unwrap();
466
    /// let mapped_response: Response<&[u8]> = response.map(|b| {
467
    ///   assert_eq!(b, "some string");
468
    ///   b.as_bytes()
469
    /// });
470
    /// assert_eq!(mapped_response.body(), &"some string".as_bytes());
471
    /// ```
472
    #[inline]
473
4.31M
    pub fn map<F, U>(self, f: F) -> Response<U>
474
4.31M
    where
475
4.31M
        F: FnOnce(T) -> U,
476
4.31M
    {
477
4.31M
        Response {
478
4.31M
            body: f(self.body),
479
4.31M
            head: self.head,
480
4.31M
        }
481
4.31M
    }
Unexecuted instantiation: <http::response::Response<_>>::map::<_, _>
<http::response::Response<linkerd_http_retain::RetainBody<linkerd_idle_cache::Cached<linkerd_stack::gate::Gate<tower::buffer::service::Buffer<tower::util::boxed::sync::BoxService<http::request::Request<linkerd_http_box::body::BoxBody>, http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>, http::request::Request<linkerd_http_box::body::BoxBody>>>>, linkerd_http_box::body::BoxBody>>>::map::<<linkerd_http_box::body::BoxBody>::new<linkerd_http_retain::RetainBody<linkerd_idle_cache::Cached<linkerd_stack::gate::Gate<tower::buffer::service::Buffer<tower::util::boxed::sync::BoxService<http::request::Request<linkerd_http_box::body::BoxBody>, http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>, http::request::Request<linkerd_http_box::body::BoxBody>>>>, linkerd_http_box::body::BoxBody>>, linkerd_http_box::body::BoxBody>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
<http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>, linkerd_app_core::classify::Eos>>>::map::<<linkerd_http_box::body::BoxBody>::new<linkerd_http_metrics::requests::service::ResponseBody<linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>, linkerd_app_core::classify::Eos>>, linkerd_http_box::body::BoxBody>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
Unexecuted instantiation: <http::response::Response<linkerd_http_metrics::requests::service::ResponseBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Eos>>>::map::<<linkerd_http_box::body::BoxBody>::new<linkerd_http_metrics::requests::service::ResponseBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Eos>>, linkerd_http_box::body::BoxBody>
<http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>>::map::<<linkerd_proxy_tap::service::TapHttp<linkerd_error_respond::RespondService<linkerd_app_core::errors::respond::NewRespond<linkerd_app_inbound::http::router::ClientRescue>, linkerd_reconnect::Reconnect<linkerd_app_inbound::http::router::Http, linkerd_app_core::svc::AlwaysReconnect, linkerd_stack::new_service::FromMakeService<linkerd_stack::on_service::OnService<tower_layer::layer_fn::LayerFn<<linkerd_stack::map_err::MapErr<(), linkerd_proxy_http::client::Client<linkerd_stack::map_target::MapTargetService<linkerd_transport_metrics::client::Client<linkerd_app_core::transport::Metrics, linkerd_stack::box_future::BoxFuture<linkerd_stack::connect::MakeConnectionService<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>>, <linkerd_app_inbound::Inbound<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>::push_http_router<linkerd_app_inbound::http::fuzz::Target, linkerd_app_test::resolver::Resolver<linkerd_addr::Addr, core::option::Option<linkerd_service_profiles::Receiver>>>::{closure#0}::{closure#1}>, linkerd_app_inbound::http::router::Http, linkerd_proxy_tap::service::Body<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>, linkerd_proxy_tap::grpc::server::TapRequestPayload>>>>::layer::{closure#0}>, linkerd_proxy_http::client::MakeClient<(), linkerd_stack::map_target::MapTargetService<linkerd_transport_metrics::client::Client<linkerd_app_core::transport::Metrics, linkerd_stack::box_future::BoxFuture<linkerd_stack::connect::MakeConnectionService<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>>, <linkerd_app_inbound::Inbound<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>::push_http_router<linkerd_app_inbound::http::fuzz::Target, linkerd_app_test::resolver::Resolver<linkerd_addr::Addr, core::option::Option<linkerd_service_profiles::Receiver>>>::{closure#0}::{closure#1}>, linkerd_proxy_tap::service::Body<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>, linkerd_proxy_tap::grpc::server::TapRequestPayload>>>>>>, linkerd_app_inbound::http::router::Logical, linkerd_proxy_tap::grpc::server::Tap> as tower_service::Service<http::request::Request<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>>>>::call::{closure#1}::{closure#1}, linkerd_proxy_tap::service::Body<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>, linkerd_proxy_tap::grpc::server::TapResponsePayload>>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
<http::response::Response<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>>::map::<<linkerd_http_box::body::BoxBody>::new<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>, linkerd_http_box::body::BoxBody>
Line
Count
Source
473
540k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
540k
    where
475
540k
        F: FnOnce(T) -> U,
476
540k
    {
477
540k
        Response {
478
540k
            body: f(self.body),
479
540k
            head: self.head,
480
540k
        }
481
540k
    }
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::map::<<hyper::proto::h2::client::ClientTask<linkerd_proxy_tap::service::Body<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>, linkerd_proxy_tap::grpc::server::TapRequestPayload>>>::poll_pipe::{closure#2}::{closure#1}, hyper::body::body::Body>
Unexecuted instantiation: <http::response::Response<linkerd_http_box::body::BoxBody>>::map::<<linkerd_http_box::body::BoxBody>::new<linkerd_http_box::body::BoxBody>, linkerd_http_box::body::BoxBody>
<http::response::Response<linkerd_http_box::body::BoxBody>>::map::<<linkerd_http_retain::Retain<linkerd_idle_cache::Cached<linkerd_stack::gate::Gate<tower::buffer::service::Buffer<tower::util::boxed::sync::BoxService<http::request::Request<linkerd_http_box::body::BoxBody>, http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>, http::request::Request<linkerd_http_box::body::BoxBody>>>>, linkerd_http_box::body::BoxBody> as tower_service::Service<http::request::Request<linkerd_http_box::body::BoxBody>>>::call::{closure#0}::{closure#0}, linkerd_http_retain::RetainBody<linkerd_idle_cache::Cached<linkerd_stack::gate::Gate<tower::buffer::service::Buffer<tower::util::boxed::sync::BoxService<http::request::Request<linkerd_http_box::body::BoxBody>, http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>, http::request::Request<linkerd_http_box::body::BoxBody>>>>, linkerd_http_box::body::BoxBody>>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
<http::response::Response<linkerd_http_box::body::BoxBody>>::map::<<linkerd_app_core::errors::respond::Respond<linkerd_app_inbound::http::router::ClientRescue> as linkerd_error_respond::Respond<http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>>::respond::{closure#0}, linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
<http::response::Response<linkerd_http_box::body::BoxBody>>::map::<<linkerd_app_core::errors::respond::Respond<linkerd_app_inbound::http::server::ServerRescue> as linkerd_error_respond::Respond<http::response::Response<linkerd_http_box::body::BoxBody>, alloc::boxed::Box<dyn core::error::Error + core::marker::Sync + core::marker::Send>>>::respond::{closure#0}, linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
<http::response::Response<hyper::body::body::Body>>::map::<<linkerd_http_box::body::BoxBody>::new<hyper::body::body::Body>, linkerd_http_box::body::BoxBody>
Line
Count
Source
473
538k
    pub fn map<F, U>(self, f: F) -> Response<U>
474
538k
    where
475
538k
        F: FnOnce(T) -> U,
476
538k
    {
477
538k
        Response {
478
538k
            body: f(self.body),
479
538k
            head: self.head,
480
538k
        }
481
538k
    }
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::map::<<linkerd_proxy_http::orig_proto::Upgrade<linkerd_stack::map_target::MapTargetService<linkerd_transport_metrics::client::Client<linkerd_app_core::transport::Metrics, linkerd_stack::box_future::BoxFuture<linkerd_stack::connect::MakeConnectionService<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>>, <linkerd_app_inbound::Inbound<linkerd_stack::map_target::MapTargetService<linkerd_app_test::connect::Connect<linkerd_proxy_transport::addrs::Remote<linkerd_proxy_transport::addrs::ServerAddr>>, linkerd_app_inbound::http::fuzz::build_fuzz_server<tokio::io::util::mem::DuplexStream>::{closure#0}>>>::push_http_router<linkerd_app_inbound::http::fuzz::Target, linkerd_app_test::resolver::Resolver<linkerd_addr::Addr, core::option::Option<linkerd_service_profiles::Receiver>>>::{closure#0}::{closure#1}>, linkerd_app_inbound::http::router::Http, linkerd_proxy_tap::service::Body<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>, linkerd_proxy_tap::grpc::server::TapRequestPayload>> as tower_service::Service<http::request::Request<linkerd_proxy_tap::service::Body<linkerd_http_metrics::requests::service::RequestBody<linkerd_http_box::body::BoxBody, linkerd_app_core::classify::Class>, linkerd_proxy_tap::grpc::server::TapRequestPayload>>>>::call::{closure#1}::{closure#1}, linkerd_http_box::body::BoxBody>
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::map::<<hyper::proto::h2::client::ClientTask<linkerd_http_metrics::requests::service::RequestBody<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, tonic::status::Status>, linkerd_app_core::classify::Class>>>::poll_pipe::{closure#2}::{closure#1}, hyper::body::body::Body>
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body>>::map::<<hyper_balance::PendingUntilFirstData as tower::load::completion::TrackCompletion<tower::load::peak_ewma::Handle, http::response::Response<hyper::body::body::Body>>>::track_completion::{closure#0}, hyper_balance::PendingUntilFirstDataBody<tower::load::peak_ewma::Handle, hyper::body::body::Body>>
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::map::<axum_core::body::boxed<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>, http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::map::<tonic::body::boxed<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>, http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, tonic::status::Status>>
Unexecuted instantiation: <http::response::Response<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>>::map::<<axum::routing::route::RouteFuture<hyper::body::body::Body, core::convert::Infallible> as core::future::future::Future>::poll::{closure#0}, http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, axum_core::error::Error>>
Unexecuted instantiation: <http::response::Response<h2::share::RecvStream>>::map::<<hyper::proto::h2::client::ClientTask<http_body::combinators::box_body::UnsyncBoxBody<bytes::bytes::Bytes, tonic::status::Status>>>::poll_pipe::{closure#2}::{closure#1}, hyper::body::body::Body>
482
}
483
484
impl<T: Default> Default for Response<T> {
485
    #[inline]
486
151k
    fn default() -> Response<T> {
487
151k
        Response::new(T::default())
488
151k
    }
Unexecuted instantiation: <http::response::Response<_> as core::default::Default>::default
<http::response::Response<linkerd_http_box::body::BoxBody> as core::default::Default>::default
Line
Count
Source
486
151k
    fn default() -> Response<T> {
487
151k
        Response::new(T::default())
488
151k
    }
489
}
490
491
impl<T: fmt::Debug> fmt::Debug for Response<T> {
492
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
493
0
        f.debug_struct("Response")
494
0
            .field("status", &self.status())
495
0
            .field("version", &self.version())
496
0
            .field("headers", self.headers())
497
0
            // omits Extensions because not useful
498
0
            .field("body", self.body())
499
0
            .finish()
500
0
    }
Unexecuted instantiation: <http::response::Response<_> as core::fmt::Debug>::fmt
Unexecuted instantiation: <http::response::Response<hyper::body::body::Body> as core::fmt::Debug>::fmt
501
}
502
503
impl Parts {
504
    /// Creates a new default instance of `Parts`
505
1.92M
    fn new() -> Parts {
506
1.92M
        Parts {
507
1.92M
            status: StatusCode::default(),
508
1.92M
            version: Version::default(),
509
1.92M
            headers: HeaderMap::default(),
510
1.92M
            extensions: Extensions::default(),
511
1.92M
            _priv: (),
512
1.92M
        }
513
1.92M
    }
514
}
515
516
impl fmt::Debug for Parts {
517
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
518
0
        f.debug_struct("Parts")
519
0
            .field("status", &self.status)
520
0
            .field("version", &self.version)
521
0
            .field("headers", &self.headers)
522
0
            // omits Extensions because not useful
523
0
            // omits _priv because not useful
524
0
            .finish()
525
0
    }
526
}
527
528
impl Builder {
529
    /// Creates a new default instance of `Builder` to construct either a
530
    /// `Head` or a `Response`.
531
    ///
532
    /// # Examples
533
    ///
534
    /// ```
535
    /// # use http::*;
536
    ///
537
    /// let response = response::Builder::new()
538
    ///     .status(200)
539
    ///     .body(())
540
    ///     .unwrap();
541
    /// ```
542
    #[inline]
543
1.03k
    pub fn new() -> Builder {
544
1.03k
        Builder::default()
545
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::new
Unexecuted instantiation: <http::response::Builder>::new
<http::response::Builder>::new
Line
Count
Source
543
1.03k
    pub fn new() -> Builder {
544
1.03k
        Builder::default()
545
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::new
Unexecuted instantiation: <http::response::Builder>::new
546
547
    /// Set the HTTP status for this response.
548
    ///
549
    /// By default this is `200`.
550
    ///
551
    /// # Examples
552
    ///
553
    /// ```
554
    /// # use http::*;
555
    ///
556
    /// let response = Response::builder()
557
    ///     .status(200)
558
    ///     .body(())
559
    ///     .unwrap();
560
    /// ```
561
1.03k
    pub fn status<T>(self, status: T) -> Builder
562
1.03k
    where
563
1.03k
        StatusCode: TryFrom<T>,
564
1.03k
        <StatusCode as TryFrom<T>>::Error: Into<crate::Error>,
565
1.03k
    {
566
1.03k
        self.and_then(move |mut head| {
567
1.03k
            head.status = TryFrom::try_from(status).map_err(Into::into)?;
568
1.03k
            Ok(head)
569
1.03k
        })
Unexecuted instantiation: <http::response::Builder>::status::<_>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::status::<http::status::StatusCode>::{closure#0}
<http::response::Builder>::status::<http::status::StatusCode>::{closure#0}
Line
Count
Source
566
1.03k
        self.and_then(move |mut head| {
567
1.03k
            head.status = TryFrom::try_from(status).map_err(Into::into)?;
568
1.03k
            Ok(head)
569
1.03k
        })
Unexecuted instantiation: <http::response::Builder>::status::<http::status::StatusCode>::{closure#0}
570
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::status::<_>
Unexecuted instantiation: <http::response::Builder>::status::<http::status::StatusCode>
<http::response::Builder>::status::<http::status::StatusCode>
Line
Count
Source
561
1.03k
    pub fn status<T>(self, status: T) -> Builder
562
1.03k
    where
563
1.03k
        StatusCode: TryFrom<T>,
564
1.03k
        <StatusCode as TryFrom<T>>::Error: Into<crate::Error>,
565
1.03k
    {
566
1.03k
        self.and_then(move |mut head| {
567
            head.status = TryFrom::try_from(status).map_err(Into::into)?;
568
            Ok(head)
569
1.03k
        })
570
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::status::<http::status::StatusCode>
571
572
    /// Set the HTTP version for this response.
573
    ///
574
    /// By default this is HTTP/1.1
575
    ///
576
    /// # Examples
577
    ///
578
    /// ```
579
    /// # use http::*;
580
    ///
581
    /// let response = Response::builder()
582
    ///     .version(Version::HTTP_2)
583
    ///     .body(())
584
    ///     .unwrap();
585
    /// ```
586
1.03k
    pub fn version(self, version: Version) -> Builder {
587
1.03k
        self.and_then(move |mut head| {
588
1.03k
            head.version = version;
589
1.03k
            Ok(head)
590
1.03k
        })
591
1.03k
    }
592
593
    /// Appends a header to this response builder.
594
    ///
595
    /// This function will append the provided key/value as a header to the
596
    /// internal `HeaderMap` being constructed. Essentially this is equivalent
597
    /// to calling `HeaderMap::append`.
598
    ///
599
    /// # Examples
600
    ///
601
    /// ```
602
    /// # use http::*;
603
    /// # use http::header::HeaderValue;
604
    ///
605
    /// let response = Response::builder()
606
    ///     .header("Content-Type", "text/html")
607
    ///     .header("X-Custom-Foo", "bar")
608
    ///     .header("content-length", 0)
609
    ///     .body(())
610
    ///     .unwrap();
611
    /// ```
612
2.07k
    pub fn header<K, V>(self, key: K, value: V) -> Builder
613
2.07k
    where
614
2.07k
        HeaderName: TryFrom<K>,
615
2.07k
        <HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
616
2.07k
        HeaderValue: TryFrom<V>,
617
2.07k
        <HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
618
2.07k
    {
619
2.07k
        self.and_then(move |mut head| {
620
2.07k
            let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
621
2.07k
            let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
622
2.07k
            head.headers.try_append(name, value)?;
623
2.07k
            Ok(head)
624
2.07k
        })
Unexecuted instantiation: <http::response::Builder>::header::<_, _>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::header::<http::header::name::HeaderName, &http::header::value::HeaderValue>::{closure#0}
<http::response::Builder>::header::<http::header::name::HeaderName, &str>::{closure#0}
Line
Count
Source
619
2.07k
        self.and_then(move |mut head| {
620
2.07k
            let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
621
2.07k
            let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
622
2.07k
            head.headers.try_append(name, value)?;
623
2.07k
            Ok(head)
624
2.07k
        })
Unexecuted instantiation: <http::response::Builder>::header::<&str, &str>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::header::<&str, http::header::value::HeaderValue>::{closure#0}
625
2.07k
    }
Unexecuted instantiation: <http::response::Builder>::header::<_, _>
Unexecuted instantiation: <http::response::Builder>::header::<http::header::name::HeaderName, &http::header::value::HeaderValue>
<http::response::Builder>::header::<http::header::name::HeaderName, &str>
Line
Count
Source
612
2.07k
    pub fn header<K, V>(self, key: K, value: V) -> Builder
613
2.07k
    where
614
2.07k
        HeaderName: TryFrom<K>,
615
2.07k
        <HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
616
2.07k
        HeaderValue: TryFrom<V>,
617
2.07k
        <HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
618
2.07k
    {
619
2.07k
        self.and_then(move |mut head| {
620
            let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
621
            let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
622
            head.headers.try_append(name, value)?;
623
            Ok(head)
624
2.07k
        })
625
2.07k
    }
Unexecuted instantiation: <http::response::Builder>::header::<&str, &str>
Unexecuted instantiation: <http::response::Builder>::header::<&str, http::header::value::HeaderValue>
626
627
    /// Get header on this response builder.
628
    ///
629
    /// When builder has error returns None.
630
    ///
631
    /// # Example
632
    ///
633
    /// ```
634
    /// # use http::Response;
635
    /// # use http::header::HeaderValue;
636
    /// let res = Response::builder()
637
    ///     .header("Accept", "text/html")
638
    ///     .header("X-Custom-Foo", "bar");
639
    /// let headers = res.headers_ref().unwrap();
640
    /// assert_eq!( headers["Accept"], "text/html" );
641
    /// assert_eq!( headers["X-Custom-Foo"], "bar" );
642
    /// ```
643
0
    pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> {
644
0
        self.inner.as_ref().ok().map(|h| &h.headers)
645
0
    }
646
647
    /// Get header on this response builder.
648
    /// when builder has error returns None
649
    ///
650
    /// # Example
651
    ///
652
    /// ```
653
    /// # use http::*;
654
    /// # use http::header::HeaderValue;
655
    /// # use http::response::Builder;
656
    /// let mut res = Response::builder();
657
    /// {
658
    ///   let headers = res.headers_mut().unwrap();
659
    ///   headers.insert("Accept", HeaderValue::from_static("text/html"));
660
    ///   headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
661
    /// }
662
    /// let headers = res.headers_ref().unwrap();
663
    /// assert_eq!( headers["Accept"], "text/html" );
664
    /// assert_eq!( headers["X-Custom-Foo"], "bar" );
665
    /// ```
666
0
    pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>> {
667
0
        self.inner.as_mut().ok().map(|h| &mut h.headers)
668
0
    }
669
670
    /// Adds an extension to this builder
671
    ///
672
    /// # Examples
673
    ///
674
    /// ```
675
    /// # use http::*;
676
    ///
677
    /// let response = Response::builder()
678
    ///     .extension("My Extension")
679
    ///     .body(())
680
    ///     .unwrap();
681
    ///
682
    /// assert_eq!(response.extensions().get::<&'static str>(),
683
    ///            Some(&"My Extension"));
684
    /// ```
685
0
    pub fn extension<T>(self, extension: T) -> Builder
686
0
    where
687
0
        T: Any + Send + Sync + 'static,
688
0
    {
689
0
        self.and_then(move |mut head| {
690
0
            head.extensions.insert(extension);
691
0
            Ok(head)
692
0
        })
693
0
    }
694
695
    /// Get a reference to the extensions for this response builder.
696
    ///
697
    /// If the builder has an error, this returns `None`.
698
    ///
699
    /// # Example
700
    ///
701
    /// ```
702
    /// # use http::Response;
703
    /// let res = Response::builder().extension("My Extension").extension(5u32);
704
    /// let extensions = res.extensions_ref().unwrap();
705
    /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
706
    /// assert_eq!(extensions.get::<u32>(), Some(&5u32));
707
    /// ```
708
0
    pub fn extensions_ref(&self) -> Option<&Extensions> {
709
0
        self.inner.as_ref().ok().map(|h| &h.extensions)
710
0
    }
711
712
    /// Get a mutable reference to the extensions for this response builder.
713
    ///
714
    /// If the builder has an error, this returns `None`.
715
    ///
716
    /// # Example
717
    ///
718
    /// ```
719
    /// # use http::Response;
720
    /// let mut res = Response::builder().extension("My Extension");
721
    /// let mut extensions = res.extensions_mut().unwrap();
722
    /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
723
    /// extensions.insert(5u32);
724
    /// assert_eq!(extensions.get::<u32>(), Some(&5u32));
725
    /// ```
726
0
    pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
727
0
        self.inner.as_mut().ok().map(|h| &mut h.extensions)
728
0
    }
729
730
    /// "Consumes" this builder, using the provided `body` to return a
731
    /// constructed `Response`.
732
    ///
733
    /// # Errors
734
    ///
735
    /// This function may return an error if any previously configured argument
736
    /// failed to parse or get converted to the internal representation. For
737
    /// example if an invalid `head` was specified via `header("Foo",
738
    /// "Bar\r\n")` the error will be returned when this function is called
739
    /// rather than when `header` was called.
740
    ///
741
    /// # Examples
742
    ///
743
    /// ```
744
    /// # use http::*;
745
    ///
746
    /// let response = Response::builder()
747
    ///     .body(())
748
    ///     .unwrap();
749
    /// ```
750
1.03k
    pub fn body<T>(self, body: T) -> Result<Response<T>> {
751
1.03k
        self.inner.map(move |head| {
752
1.03k
            Response {
753
1.03k
                head,
754
1.03k
                body,
755
1.03k
            }
756
1.03k
        })
Unexecuted instantiation: <http::response::Builder>::body::<_>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::body::<()>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::body::<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>::{closure#0}
<http::response::Builder>::body::<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>::{closure#0}
Line
Count
Source
751
1.03k
        self.inner.map(move |head| {
752
1.03k
            Response {
753
1.03k
                head,
754
1.03k
                body,
755
1.03k
            }
756
1.03k
        })
Unexecuted instantiation: <http::response::Builder>::body::<()>::{closure#0}
Unexecuted instantiation: <http::response::Builder>::body::<()>::{closure#0}
757
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::body::<_>
Unexecuted instantiation: <http::response::Builder>::body::<()>
Unexecuted instantiation: <http::response::Builder>::body::<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::router::ClientRescue, linkerd_http_box::body::BoxBody>>
<http::response::Builder>::body::<linkerd_app_core::errors::respond::ResponseBody<linkerd_app_inbound::http::server::ServerRescue, linkerd_http_box::body::BoxBody>>
Line
Count
Source
750
1.03k
    pub fn body<T>(self, body: T) -> Result<Response<T>> {
751
1.03k
        self.inner.map(move |head| {
752
            Response {
753
                head,
754
                body,
755
            }
756
1.03k
        })
757
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::body::<()>
Unexecuted instantiation: <http::response::Builder>::body::<()>
758
759
    // private
760
761
4.14k
    fn and_then<F>(self, func: F) -> Self
762
4.14k
    where
763
4.14k
        F: FnOnce(Parts) -> Result<Parts>
764
4.14k
    {
765
4.14k
        Builder {
766
4.14k
            inner: self.inner.and_then(func),
767
4.14k
        }
768
4.14k
    }
<http::response::Builder>::and_then::<<http::response::Builder>::version::{closure#0}>
Line
Count
Source
761
1.03k
    fn and_then<F>(self, func: F) -> Self
762
1.03k
    where
763
1.03k
        F: FnOnce(Parts) -> Result<Parts>
764
1.03k
    {
765
1.03k
        Builder {
766
1.03k
            inner: self.inner.and_then(func),
767
1.03k
        }
768
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::and_then::<<http::response::Builder>::status<http::status::StatusCode>::{closure#0}>
Unexecuted instantiation: <http::response::Builder>::and_then::<<http::response::Builder>::header<http::header::name::HeaderName, &http::header::value::HeaderValue>::{closure#0}>
<http::response::Builder>::and_then::<<http::response::Builder>::header<http::header::name::HeaderName, &str>::{closure#0}>
Line
Count
Source
761
2.07k
    fn and_then<F>(self, func: F) -> Self
762
2.07k
    where
763
2.07k
        F: FnOnce(Parts) -> Result<Parts>
764
2.07k
    {
765
2.07k
        Builder {
766
2.07k
            inner: self.inner.and_then(func),
767
2.07k
        }
768
2.07k
    }
Unexecuted instantiation: <http::response::Builder>::and_then::<<http::response::Builder>::header<&str, &str>::{closure#0}>
Unexecuted instantiation: <http::response::Builder>::and_then::<<http::response::Builder>::header<&str, http::header::value::HeaderValue>::{closure#0}>
<http::response::Builder>::and_then::<<http::response::Builder>::status<http::status::StatusCode>::{closure#0}>
Line
Count
Source
761
1.03k
    fn and_then<F>(self, func: F) -> Self
762
1.03k
    where
763
1.03k
        F: FnOnce(Parts) -> Result<Parts>
764
1.03k
    {
765
1.03k
        Builder {
766
1.03k
            inner: self.inner.and_then(func),
767
1.03k
        }
768
1.03k
    }
Unexecuted instantiation: <http::response::Builder>::and_then::<<http::response::Builder>::status<http::status::StatusCode>::{closure#0}>
769
}
770
771
impl Default for Builder {
772
    #[inline]
773
1.03k
    fn default() -> Builder {
774
1.03k
        Builder {
775
1.03k
            inner: Ok(Parts::new()),
776
1.03k
        }
777
1.03k
    }
Unexecuted instantiation: <http::response::Builder as core::default::Default>::default
Unexecuted instantiation: <http::response::Builder as core::default::Default>::default
<http::response::Builder as core::default::Default>::default
Line
Count
Source
773
1.03k
    fn default() -> Builder {
774
1.03k
        Builder {
775
1.03k
            inner: Ok(Parts::new()),
776
1.03k
        }
777
1.03k
    }
Unexecuted instantiation: <http::response::Builder as core::default::Default>::default
Unexecuted instantiation: <http::response::Builder as core::default::Default>::default
778
}
779
780
#[cfg(test)]
781
mod tests {
782
    use super::*;
783
784
    #[test]
785
    fn it_can_map_a_body_from_one_type_to_another() {
786
        let response = Response::builder().body("some string").unwrap();
787
        let mapped_response = response.map(|s| {
788
            assert_eq!(s, "some string");
789
            123u32
790
        });
791
        assert_eq!(mapped_response.body(), &123u32);
792
    }
793
}