http_request/
lib.rs

1//! http-request
2//!
3//! http-request is a lightweight, efficient library for building, sending,
4//! and handling HTTP/HTTPS requests in Rust applications.
5//! It provides a simple and intuitive API, allowing developers to easily
6//! interact with web services, whether they use the "HTTP" or "HTTPS" protocol.
7//! The library supports various HTTP methods, custom headers, request bodies,
8//! timeout, automatic handling of redirects (including detecting redirect loops),
9//! and enhanced response body decoding (both automatic and manual), enabling fast
10//! and secure communication. Whether working with secure "HTTPS" connections
11//! or standard "HTTP" requests, the library is optimized for performance,
12//! minimal resource usage, and easy integration into Rust projects.
13
14pub(crate) mod body;
15pub(crate) mod cfg;
16pub(crate) mod common;
17pub(crate) mod r#const;
18pub(crate) mod request;
19pub(crate) mod response;
20pub(crate) mod utils;
21
22pub use request::*;
23pub use response::*;
24
25pub use http_type::{HashMapXxHash3_64, hash_map_xx_hash3_64};
26pub use serde_json::{
27    Deserializer, Error, Map, Number, StreamDeserializer, Value, from_reader, from_slice, from_str,
28    from_value, to_string, to_string_pretty, to_value, to_vec, to_vec_pretty, to_writer,
29    to_writer_pretty, value,
30};
31
32pub(crate) use body::*;
33pub(crate) use common::*;
34pub(crate) use r#const::*;
35pub(crate) use utils::*;
36
37pub(crate) use std::{
38    borrow::Cow,
39    collections::{HashSet, VecDeque},
40    fmt::{self, Debug, Display, Formatter},
41    io::{Read, Write},
42    net::{Ipv4Addr, Ipv6Addr, TcpStream},
43    pin::Pin,
44    str::from_utf8,
45    sync::{
46        Arc, RwLock,
47        atomic::{AtomicBool, Ordering},
48    },
49    task::{Context, Poll},
50    time::{Duration, SystemTime, UNIX_EPOCH},
51    vec::IntoIter,
52};
53
54pub(crate) use futures::{Future, Sink, SinkExt, Stream, StreamExt};
55pub(crate) use http_type::{
56    ACCEPT, ACCEPT_ANY, BR_BYTES, COLON_U8, CONNECTION, CONTENT_LENGTH, CONTENT_TYPE, Compress,
57    ContentType, DEFAULT_BUFFER_SIZE, DEFAULT_HTTP_PATH, DEFAULT_MAX_REDIRECT_TIMES,
58    DEFAULT_TIMEOUT, EMPTY_STR, HOST, HTTP_BR_BYTES, HttpStatus, HttpUrlComponents, HttpVersion,
59    LOCATION, Method, Protocol, QUERY, RequestBody, RequestBodyString, RequestHeaders,
60    ResponseHeaders, ResponseStatusCode, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_VERSION, SPACE_U8,
61    TAB_U8, UPGRADE, USER_AGENT,
62    tokio::{
63        io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
64        net::TcpStream as AsyncTcpStream,
65        runtime::Runtime,
66        sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard},
67        time::timeout,
68    },
69};
70pub(crate) use rustls::{
71    ClientConfig, ClientConnection, RootCertStore, StreamOwned, pki_types::ServerName,
72};
73pub(crate) use serde::{Deserialize, Serialize, Serializer};
74pub(crate) use tokio_rustls::{TlsConnector, client::TlsStream};
75pub(crate) use tokio_tungstenite::{
76    MaybeTlsStream, WebSocketStream, client_async_with_config, connect_async_with_config,
77    tungstenite::Message, tungstenite::handshake::client::Request,
78};
79pub(crate) use webpki_roots::TLS_SERVER_ROOTS;
80
81#[cfg(test)]
82use std::{
83    sync::{Mutex, MutexGuard},
84    thread::{JoinHandle, spawn},
85    time::Instant,
86};
87
88#[cfg(test)]
89use http_type::tokio;
90#[cfg(test)]
91use serde_json::json;