xitca_http/lib.rs
1//! Http module for [Service](xitca_service::Service) trait oriented http handling.
2//!
3//! This crate tries to serve both low overhead and ease of use purpose.
4//! All http protocols can be used separately with corresponding feature flag or work together
5//! for handling different protocols in one place.
6//!
7//! # Examples
8//! ```no_run
9//! use std::convert::Infallible;
10//!
11//! use xitca_http::{
12//!     http::{IntoResponse, Request, RequestExt, Response},
13//!     HttpServiceBuilder,
14//!     RequestBody,
15//!     ResponseBody
16//! };
17//! use xitca_service::{fn_service, Service, ServiceExt};
18//!
19//! # fn main() -> std::io::Result<()> {
20//! // xitca-http has to run inside a tcp/udp server.
21//! xitca_server::Builder::new()
22//!     // create http service with given name, socket address and service logic.
23//!     .bind("xitca-http", "localhost:0",
24//!         // a simple async function service produce hello world string as http response.
25//!         fn_service(|req: Request<RequestExt<RequestBody>>| async {
26//!             Ok::<Response<ResponseBody>, Infallible>(req.into_response("Hello,World!"))
27//!         })
28//!         // http service builder is a middleware take control of above function service
29//!         // and bridge tcp/udp transport with the http service.
30//!         .enclosed(HttpServiceBuilder::new())
31//!     )?
32//!     .build()
33//! # ; Ok(())
34//! # }
35//! ```
36
37#![forbid(unsafe_code)]
38
39mod tls;
40
41#[cfg(feature = "runtime")]
42mod builder;
43#[cfg(feature = "runtime")]
44mod service;
45#[cfg(feature = "runtime")]
46mod version;
47
48pub mod body;
49pub mod config;
50pub mod error;
51pub mod http;
52pub mod util;
53
54#[cfg(feature = "runtime")]
55pub mod date;
56#[cfg(feature = "http1")]
57pub mod h1;
58#[cfg(feature = "http2")]
59pub mod h2;
60#[cfg(feature = "http3")]
61pub mod h3;
62
63/// re-export bytes crate as module.
64pub use xitca_io::bytes;
65
66pub use self::{
67    body::{RequestBody, ResponseBody},
68    error::{BodyError, HttpServiceError},
69    http::{Request, Response},
70};
71
72#[cfg(feature = "runtime")]
73pub use self::builder::HttpServiceBuilder;
74
75// TODO: enable this conflict feature check.
76// temporary compile error for conflicted feature combination.
77// #[cfg(not(feature = "http1"))]
78// #[cfg(all(feature = "http2", feature = "native-tls"))]
79// compile_error!("http2 feature can not use native-tls");
80
81pub(crate) fn unspecified_socket_addr() -> core::net::SocketAddr {
82    core::net::SocketAddr::V4(core::net::SocketAddrV4::new(std::net::Ipv4Addr::UNSPECIFIED, 0))
83}