1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
use std::{any::Any, convert::Infallible};
use thiserror::Error;
use crate::{header::HeaderMap, status::StatusCodeError, Body, Extensions, StatusCode, Version};
/// Represents an HTTP response
///
/// An HTTP response consists of a head and a potentially optional body. The body
/// component is generic, enabling arbitrary types to represent the HTTP body.
/// For example, the body could be `Vec<u8>`, a `Stream` of byte chunks, or a
/// value that has been deserialized.
///
/// Typically you'll work with responses on the client side as the result of
/// sending a `Request` and on the server you'll be generating a `Response` to
/// send back to the client.
///
/// # Examples
///
/// Creating a `Response` to return
///
/// ```
/// use axol_http::{Request, Response, StatusCode};
///
/// fn respond_to(req: Request<()>) -> axol_http::Result<Response<()>> {
/// let mut builder = Response::builder()
/// .header("Foo", "Bar")
/// .status(StatusCode::OK);
///
/// if req.headers().contains_key("Another-Header") {
/// builder = builder.header("Another-Header", "Ack");
/// }
///
/// builder.body(())
/// }
/// ```
///
/// A simple 404 handler
///
/// ```
/// use axol_http::{Request, Response, StatusCode};
///
/// fn not_found(_req: Request<()>) -> axol_http::Result<Response<()>> {
/// Response::builder()
/// .status(StatusCode::NOT_FOUND)
/// .body(())
/// }
/// ```
///
/// Or otherwise inspecting the result of a request:
///
/// ```no_run
/// use axol_http::{Request, Response};
///
/// fn get(url: &str) -> axol_http::Result<Response<()>> {
/// // ...
/// # panic!()
/// }
///
/// let response = get("https://www.rust-lang.org/").unwrap();
///
/// if !response.status().is_success() {
/// panic!("failed to get a successful response status!");
/// }
///
/// if let Some(date) = response.headers().get("Date") {
/// // we've got a `Date` header!
/// }
///
/// let body = response.body();
/// // ...
/// ```
///
/// Deserialize a response of bytes via json:
///
/// ```
/// # extern crate serde;
/// # extern crate serde_json;
/// # extern crate http;
/// use axol_http::Response;
/// use serde::de;
///
/// fn deserialize<T>(res: Response<Vec<u8>>) -> serde_json::Result<Response<T>>
/// where for<'de> T: de::Deserialize<'de>,
/// {
/// let (parts, body) = res.into_parts();
/// let body = serde_json::from_slice(&body)?;
/// Ok(Response::from_parts(parts, body))
/// }
/// #
/// # fn main() {}
/// ```
///
/// Or alternatively, serialize the body of a response to json
///
/// ```
/// # extern crate serde;
/// # extern crate serde_json;
/// # extern crate http;
/// use axol_http::Response;
/// use serde::ser;
///
/// fn serialize<T>(res: Response<T>) -> serde_json::Result<Response<Vec<u8>>>
/// where T: ser::Serialize,
/// {
/// let (parts, body) = res.into_parts();
/// let body = serde_json::to_vec(&body)?;
/// Ok(Response::from_parts(parts, body))
/// }
/// #
/// # fn main() {}
/// ```
#[derive(Debug, Default)]
pub struct Response {
/// The response's status
pub status: StatusCode,
/// The response's version
pub version: Version,
/// The response's headers
pub headers: HeaderMap,
/// The response's extensions
pub extensions: Extensions,
/// The response's body
pub body: Body,
}
/// Component parts of an HTTP `Response`
///
/// The HTTP response head consists of a status, version, and a set of
/// header fields.
#[derive(Debug, Default)]
pub struct ResponseParts {
/// The response's status
pub status: StatusCode,
/// The response's version
pub version: Version,
/// The response's headers
pub headers: HeaderMap,
/// The response's extensions
pub extensions: Extensions,
}
impl ResponseParts {
pub fn as_ref(&mut self) -> ResponsePartsRef<'_> {
ResponsePartsRef {
status: &mut self.status,
version: &mut self.version,
headers: &mut self.headers,
extensions: &mut self.extensions,
}
}
}
impl Response {
pub fn parts_mut(&mut self) -> ResponsePartsRef<'_> {
ResponsePartsRef {
status: &mut self.status,
version: &mut self.version,
headers: &mut self.headers,
extensions: &mut self.extensions,
}
}
}
#[derive(Debug)]
pub struct ResponsePartsRef<'a> {
/// The response's status
pub status: &'a mut StatusCode,
/// The response's version
pub version: &'a mut Version,
/// The response's headers
pub headers: &'a mut HeaderMap,
/// The response's extensions
pub extensions: &'a mut Extensions,
}
impl ResponseParts {
/// Creates a new default instance of `ResponseParts`
pub fn new() -> ResponseParts {
Default::default()
}
}
#[derive(Error, Debug)]
pub enum ResponseBuilderError {
#[error("")]
Infallible(#[from] Infallible),
#[error("status code error: {0}")]
StatusCode(#[from] StatusCodeError),
// #[error("http error: {0}")]
// Http(#[from] HttpError),
}
/// An HTTP response builder
///
/// This type can be used to construct an instance of `Response` through a
/// builder-like pattern.
#[derive(Debug)]
pub struct Builder {
inner: Result<Response, ResponseBuilderError>,
}
impl Response {
/// Creates a new builder-style object to manufacture a `Response`
///
/// This method returns an instance of `Builder` which can be used to
/// create a `Response`.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
/// let response = Response::builder()
/// .status(200)
/// .header("X-Custom-Foo", "Bar")
/// .body(())
/// .unwrap();
/// ```
pub fn builder() -> Builder {
Builder::new()
}
/// Creates a new blank `Response` with the body
///
/// The component ports of this response will be set to their default, e.g.
/// the ok status, no headers, etc.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
/// let response = Response::new("hello world");
///
/// assert_eq!(response.status(), StatusCode::OK);
/// assert_eq!(*response.body(), "hello world");
/// ```
pub fn new(body: impl Into<Body>) -> Response {
Self::from_parts(Default::default(), body)
}
/// Creates a new `Response` with the given head and body
///
/// # Examples
///
/// ```
/// # use axol_http::*;
/// let response = Response::new("hello world");
/// let (mut parts, body) = response.into_parts();
///
/// parts.status = StatusCode::BAD_REQUEST;
/// let response = Response::from_parts(parts, body);
///
/// assert_eq!(response.status(), StatusCode::BAD_REQUEST);
/// assert_eq!(*response.body(), "hello world");
/// ```
pub fn from_parts(parts: ResponseParts, body: impl Into<Body>) -> Response {
Response {
status: parts.status,
version: parts.version,
headers: parts.headers,
extensions: parts.extensions,
body: body.into(),
}
}
/// Consumes the response returning the head and body parts.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
/// let response: Response<()> = Response::default();
/// let (parts, body) = response.into_parts();
/// assert_eq!(parts.status, StatusCode::OK);
/// ```
pub fn into_parts(self) -> (ResponseParts, Body) {
(
ResponseParts {
status: self.status,
version: self.version,
headers: self.headers,
extensions: self.extensions,
},
self.body,
)
}
/// Sets status code of response
pub fn with_status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
}
impl Builder {
/// Creates a new default instance of `Builder` to construct either a
/// `Head` or a `Response`.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
///
/// let response = response::Builder::new()
/// .status(200)
/// .body(())
/// .unwrap();
/// ```
pub fn new() -> Builder {
Builder::default()
}
/// Set the HTTP status for this response.
///
/// This function will configure the HTTP status code of the `Response` that
/// will be returned from `Builder::build`.
///
/// By default this is `200`.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
///
/// let response = Response::builder()
/// .status(200)
/// .body(())
/// .unwrap();
/// ```
pub fn status<T>(self, status: T) -> Builder
where
StatusCode: TryFrom<T>,
<StatusCode as TryFrom<T>>::Error: Into<StatusCodeError>,
{
self.and_then(move |mut head| {
head.status = TryFrom::try_from(status).map_err(Into::into)?;
Ok(head)
})
}
/// Set the HTTP version for this response.
///
/// This function will configure the HTTP version of the `Response` that
/// will be returned from `Builder::build`.
///
/// By default this is HTTP/1.1
///
/// # Examples
///
/// ```
/// # use axol_http::*;
///
/// let response = Response::builder()
/// .version(Version::HTTP_2)
/// .body(())
/// .unwrap();
/// ```
pub fn version(self, version: Version) -> Builder {
self.and_then(move |mut head| {
head.version = version;
Ok(head)
})
}
/// Appends a header to this response builder.
///
/// This function will append the provided key/value as a header to the
/// internal `HeaderMap` being constructed. Essentially this is equivalent
/// to calling `HeaderMap::append`.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
/// # use axol_http::header::HeaderValue;
///
/// let response = Response::builder()
/// .header("Content-Type", "text/html")
/// .header("X-Custom-Foo", "bar")
/// .header("content-length", 0)
/// .body(())
/// .unwrap();
/// ```
pub fn header(self, name: impl AsRef<str>, value: impl Into<String>) -> Builder {
self.and_then(move |mut head| {
head.headers.insert(name, value);
Ok(head)
})
}
/// Get header on this response builder.
///
/// When builder has error returns None.
///
/// # Example
///
/// ```
/// # use axol_http::Response;
/// # use axol_http::header::HeaderValue;
/// let res = Response::builder()
/// .header("Accept", "text/html")
/// .header("X-Custom-Foo", "bar");
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_ref(&self) -> Option<&HeaderMap> {
self.inner.as_ref().ok().map(|h| &h.headers)
}
/// Get header on this response builder.
/// when builder has error returns None
///
/// # Example
///
/// ```
/// # use axol_http::*;
/// # use axol_http::header::HeaderValue;
/// # use axol_http::response::Builder;
/// let mut res = Response::builder();
/// {
/// let headers = res.headers_mut().unwrap();
/// headers.insert("Accept", HeaderValue::from_static("text/html"));
/// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
/// }
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap> {
self.inner.as_mut().ok().map(|h| &mut h.headers)
}
/// Adds an extension to this builder
///
/// # Examples
///
/// ```
/// # use axol_http::*;
///
/// let response = Response::builder()
/// .extension("My Extension")
/// .body(())
/// .unwrap();
///
/// assert_eq!(response.extensions().get::<&'static str>(),
/// Some(&"My Extension"));
/// ```
pub fn extension<T>(self, extension: T) -> Builder
where
T: Any + Send + Sync + 'static,
{
self.and_then(move |head| {
head.extensions.insert(extension);
Ok(head)
})
}
/// Get a reference to the extensions for this response builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use axol_http::Response;
/// let res = Response::builder().extension("My Extension").extension(5u32);
/// let extensions = res.extensions_ref().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_ref(&self) -> Option<&Extensions> {
self.inner.as_ref().ok().map(|h| &h.extensions)
}
/// Get a mutable reference to the extensions for this response builder.
///
/// If the builder has an error, this returns `None`.
///
/// # Example
///
/// ```
/// # use axol_http::Response;
/// let mut res = Response::builder().extension("My Extension");
/// let mut extensions = res.extensions_mut().unwrap();
/// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension"));
/// extensions.insert(5u32);
/// assert_eq!(extensions.get::<u32>(), Some(&5u32));
/// ```
pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
self.inner.as_mut().ok().map(|h| &mut h.extensions)
}
/// "Consumes" this builder, using the provided `body` to return a
/// constructed `Response`.
///
/// # Errors
///
/// This function may return an error if any previously configured argument
/// failed to parse or get converted to the internal representation. For
/// example if an invalid `head` was specified via `header("Foo",
/// "Bar\r\n")` the error will be returned when this function is called
/// rather than when `header` was called.
///
/// # Examples
///
/// ```
/// # use axol_http::*;
///
/// let response = Response::builder()
/// .body(())
/// .unwrap();
/// ```
pub fn body(self, body: impl Into<Body>) -> Result<Response, ResponseBuilderError> {
self.inner.map(move |mut head| {
head.body = body.into();
head
})
}
// private
fn and_then<F>(self, func: F) -> Self
where
F: FnOnce(Response) -> Result<Response, ResponseBuilderError>,
{
Builder {
inner: self.inner.and_then(func),
}
}
}
impl Default for Builder {
fn default() -> Builder {
Builder {
inner: Ok(Default::default()),
}
}
}