mco_http/lib.rs
1#![doc(html_root_url = "https://docs.rs/mco_http/v0.10.16")]
2//#![cfg_attr(test, deny(missing_docs))]
3//#![cfg_attr(test, deny(warnings))]
4#![cfg_attr(all(test, feature = "nightly"), feature(test))]
5
6#![allow(unused_macros)]
7//! # Hyper
8//!
9//! Hyper is a fast, modern HTTP implementation written in and for Rust. It
10//! is a low-level typesafe abstraction over raw HTTP, providing an elegant
11//! layer over "stringly-typed" HTTP.
12//!
13//! Hyper offers both a [Client](client/index.html) and a
14//! [Server](server/index.html) which can be used to drive complex web
15//! applications written entirely in Rust.
16//!
17//! ## Internal Design
18//!
19//! Hyper is designed as a relatively low-level wrapper over raw HTTP. It should
20//! allow the implementation of higher-level abstractions with as little pain as
21//! possible, and should not irrevocably hide any information from its users.
22//!
23//! ### Common Functionality
24//!
25//! Functionality and code shared between the Server and Client implementations
26//! can be found in `src` directly - this includes `NetworkStream`s, `Method`s,
27//! `StatusCode`, and so on.
28//!
29//! #### Methods
30//!
31//! Methods are represented as a single `enum` to remain as simple as possible.
32//! Extension Methods are represented as raw `String`s. A method's safety and
33//! idempotence can be accessed using the `safe` and `idempotent` methods.
34//!
35//! #### StatusCode
36//!
37//! Status codes are also represented as a single, exhaustive, `enum`. This
38//! representation is efficient, typesafe, and ergonomic as it allows the use of
39//! `match` to disambiguate known status codes.
40//!
41//! #### Headers
42//!
43//! Hyper's [header](header/index.html) representation is likely the most
44//! complex API exposed by Hyper.
45//!
46//! Hyper's headers are an abstraction over an internal `HashMap` and provides a
47//! typesafe API for interacting with headers that does not rely on the use of
48//! "string-typing."
49//!
50//! Each HTTP header in Hyper has an associated type and implementation of the
51//! `Header` trait, which defines an HTTP headers name as a string, how to parse
52//! that header, and how to format that header.
53//!
54//! Headers are then parsed from the string representation lazily when the typed
55//! representation of a header is requested and formatted back into their string
56//! representation when headers are written back to the client.
57//!
58//! #### NetworkStream and NetworkAcceptor
59//!
60//! These are found in `src/net.rs` and define the interface that acceptors and
61//! streams must fulfill for them to be used within Hyper. They are by and large
62//! internal tools and you should only need to mess around with them if you want to
63//! mock or replace `TcpStream` and `TcpAcceptor`.
64//!
65//! ### Server
66//!
67//! Server-specific functionality, such as `Request` and `Response`
68//! representations, are found in in `src/server`.
69//!
70//! #### Handler + Server
71//!
72//! A `Handler` in Hyper accepts a `Request` and `Response`. This is where
73//! user-code can handle each connection. The server accepts connections in a
74//! task pool with a customizable number of threads, and passes the Request /
75//! Response to the handler.
76//!
77//! #### Request
78//!
79//! An incoming HTTP Request is represented as a struct containing
80//! a `Reader` over a `NetworkStream`, which represents the body, headers, a remote
81//! address, an HTTP version, and a `Method` - relatively standard stuff.
82//!
83//! `Request` implements `Reader` itself, meaning that you can ergonomically get
84//! the body out of a `Request` using standard `Reader` methods and helpers.
85//!
86//! #### Response
87//!
88//! An outgoing HTTP Response is also represented as a struct containing a `Writer`
89//! over a `NetworkStream` which represents the Response body in addition to
90//! standard items such as the `StatusCode` and HTTP version. `Response`'s `Writer`
91//! implementation provides a streaming interface for sending data over to the
92//! client.
93//!
94//! One of the traditional problems with representing outgoing HTTP Responses is
95//! tracking the write-status of the Response - have we written the status-line,
96//! the headers, the body, etc.? Hyper tracks this information statically using the
97//! type system and prevents you, using the type system, from writing headers after
98//! you have started writing to the body or vice versa.
99//!
100//! Hyper does this through a phantom type parameter in the definition of Response,
101//! which tracks whether you are allowed to write to the headers or the body. This
102//! phantom type can have two values `Fresh` or `Streaming`, with `Fresh`
103//! indicating that you can write the headers and `Streaming` indicating that you
104//! may write to the body, but not the headers.
105//!
106//! ### Client
107//!
108//! Client-specific functionality, such as `Request` and `Response`
109//! representations, are found in `src/client`.
110//!
111//! #### Request
112//!
113//! An outgoing HTTP Request is represented as a struct containing a `Writer` over
114//! a `NetworkStream` which represents the Request body in addition to the standard
115//! information such as headers and the request method.
116//!
117//! Outgoing Requests track their write-status in almost exactly the same way as
118//! outgoing HTTP Responses do on the Server, so we will defer to the explanation
119//! in the documentation for server Response.
120//!
121//! Requests expose an efficient streaming interface instead of a builder pattern,
122//! but they also provide the needed interface for creating a builder pattern over
123//! the API exposed by core Hyper.
124//!
125//! #### Response
126//!
127//! Incoming HTTP Responses are represented as a struct containing a `Reader` over
128//! a `NetworkStream` and contain headers, a status, and an http version. They
129//! implement `Reader` and can be read to get the data out of a `Response`.
130//!
131
132extern crate base64;
133extern crate time;
134#[macro_use] extern crate url;
135extern crate unicase;
136extern crate httparse;
137extern crate num_cpus;
138extern crate traitobject;
139extern crate typeable;
140
141#[cfg_attr(test, macro_use)]
142extern crate language_tags;
143
144#[macro_use]
145extern crate mime as mime_crate;
146
147#[macro_use]
148extern crate log;
149
150#[cfg(all(test, feature = "nightly"))]
151extern crate test;
152
153
154pub use url::Url;
155pub use crate::client::Client;
156pub use crate::error::{Result, Error};
157pub use crate::method::Method::{Get, Head, Post, Delete};
158pub use crate::status::StatusCode::{Ok, BadRequest, NotFound};
159pub use server::Server;
160pub use language_tags::LanguageTag;
161
162macro_rules! todo(
163 ($($arg:tt)*) => (if cfg!(not(ndebug)) {
164 trace!("TODO: {:?}", format_args!($($arg)*))
165 })
166);
167
168#[macro_use]
169pub mod mock;
170#[doc(hidden)]
171pub mod buffer;
172pub mod client;
173pub mod error;
174pub mod method;
175pub mod header;
176pub mod http;
177pub mod net;
178pub mod server;
179pub mod status;
180pub mod uri;
181pub mod version;
182
183pub mod multipart;
184pub mod json;
185pub mod path;
186pub mod query;
187pub mod route;
188pub mod runtime;
189
190/// Re-exporting the mime crate, for convenience.
191pub mod mime {
192 pub use mime_crate::*;
193}
194
195
196fn _assert_types() {
197 fn _assert_send<T: Send>() {}
198 fn _assert_sync<T: Sync>() {}
199
200 _assert_send::<Client>();
201 _assert_send::<client::Request<net::Fresh>>();
202 _assert_send::<client::Response>();
203 _assert_send::<error::Error>();
204 _assert_send::<crate::client::pool::Pool<crate::net::DefaultConnector>>();
205
206 _assert_sync::<Client>();
207 _assert_sync::<error::Error>();
208 _assert_sync::<crate::client::pool::Pool<crate::net::DefaultConnector>>();
209}