Name Description Size
byte_str.rs 2238
convert.rs 679
error.rs 3892
extensions.rs 6989
header
lib.rs 7424
method.rs The HTTP request method This module contains HTTP-method related structs and errors and such. The main type of this module, `Method`, is also reexported at the root of the crate as `http::Method` and is intended for import through that location primarily. # Examples ``` use http::Method; assert_eq!(Method::GET, Method::from_bytes(b"GET").unwrap()); assert!(Method::GET.is_idempotent()); assert_eq!(Method::POST.as_str(), "POST"); ``` 14388
request.rs HTTP request types. This module contains structs related to HTTP requests, notably the `Request` type itself as well as a builder to create requests. Typically you'll import the `http::Request` type rather than reaching into this module itself. # Examples Creating a `Request` to send ```no_run use http::{Request, Response}; let mut request = Request::builder() .uri("https://www.rust-lang.org/") .header("User-Agent", "my-awesome-agent/1.0"); if needs_awesome_header() { request = request.header("Awesome", "yes"); } let response = send(request.body(()).unwrap()); # fn needs_awesome_header() -> bool { # true # } # fn send(req: Request<()>) -> Response<()> { // ... # panic!() } ``` Inspecting a request to see what was sent. ``` use http::{Request, Response, StatusCode}; fn respond_to(req: Request<()>) -> http::Result<Response<()>> { if req.uri() != "/awesome-url" { return Response::builder() .status(StatusCode::NOT_FOUND) .body(()) } let has_awesome_header = req.headers().contains_key("Awesome"); let body = req.body(); // ... # panic!() } ``` 28871
response.rs HTTP response types. This module contains structs related to HTTP responses, notably the `Response` type itself as well as a builder to create responses. Typically you'll import the `http::Response` type rather than reaching into this module itself. # Examples Creating a `Response` to return ``` use http::{Request, Response, StatusCode}; fn respond_to(req: Request<()>) -> 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 http::{Request, Response, StatusCode}; fn not_found(_req: Request<()>) -> http::Result<Response<()>> { Response::builder() .status(StatusCode::NOT_FOUND) .body(()) } ``` Or otherwise inspecting the result of a request: ```no_run use http::{Request, Response}; fn get(url: &str) -> 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(); // ... ``` 21475
status.rs HTTP status codes This module contains HTTP-status code related structs an errors. The main type in this module is `StatusCode` which is not intended to be used through this module but rather the `http::StatusCode` type. # Examples ``` use http::StatusCode; assert_eq!(StatusCode::from_u16(200).unwrap(), StatusCode::OK); assert_eq!(StatusCode::NOT_FOUND, 404); assert!(StatusCode::OK.is_success()); ``` 21451
uri
version.rs HTTP version This module contains a definition of the `Version` type. The `Version` type is intended to be accessed through the root of the crate (`http::Version`) rather than this module. The `Version` type contains constants that represent the various versions of the HTTP protocol. # Examples ``` use http::Version; let http11 = Version::HTTP_11; let http2 = Version::HTTP_2; assert!(http11 != http2); println!("{:?}", http2); ``` 1686