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;
21pub(crate) mod websocket;
22
23pub use request::*;
24pub use response::*;
25pub use websocket::*;
26
27pub use http_type::{
28    HashMapXxHash3_64, JsonDeserializer, JsonError, JsonMap, JsonNumber, JsonResult,
29    JsonSerializer, JsonStreamDeserializer, JsonValue, hash_map_xx_hash3_64, json_from_reader,
30    json_from_slice, json_from_str, json_from_value, json_to_string, json_to_string_pretty,
31    json_to_value, json_to_vec, json_to_vec_pretty, json_to_writer, json_to_writer_pretty,
32    json_value,
33};
34
35pub(crate) use body::*;
36pub(crate) use common::*;
37pub(crate) use r#const::*;
38pub(crate) use utils::*;
39
40pub(crate) use futures::{Future, Sink, SinkExt, Stream, StreamExt};
41pub(crate) use http_type::{
42    ACCEPT, ACCEPT_ANY, BR_BYTES, CONNECTION, CONTENT_LENGTH, CONTENT_TYPE, Compress, ContentType,
43    DEFAULT_BUFFER_SIZE, DEFAULT_HTTP_PATH, DEFAULT_MAX_REDIRECT_TIMES, DEFAULT_TIMEOUT, EMPTY_STR,
44    HOST, HTTP_BR_BYTES, HttpStatus, HttpUrlComponents, HttpVersion, LOCATION, Method, Protocol,
45    QUERY_SYMBOL, RequestBody, RequestBodyString, RequestError, RequestHeaders, ResponseHeaders,
46    ResponseStatusCode, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_VERSION, SPACE_U8, TAB_U8, UPGRADE,
47    USER_AGENT,
48    tokio::{
49        io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
50        net::TcpStream as AsyncTcpStream,
51        runtime::Runtime,
52        sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard},
53        time::timeout,
54    },
55};
56pub(crate) use rustls::{
57    ClientConfig, ClientConnection, RootCertStore, StreamOwned, pki_types::ServerName,
58};
59pub(crate) use serde::{Serialize, Serializer};
60pub(crate) use std::{
61    borrow::Cow,
62    collections::{HashSet, VecDeque},
63    fmt::{self, Debug, Display, Formatter},
64    io::{Read, Write},
65    net::{Ipv4Addr, Ipv6Addr, TcpStream},
66    pin::Pin,
67    str::from_utf8,
68    sync::{
69        Arc, RwLock,
70        atomic::{AtomicBool, Ordering},
71    },
72    task::{Context, Poll},
73    time::{Duration, SystemTime, UNIX_EPOCH},
74    vec::IntoIter,
75};
76pub(crate) use tokio_rustls::{TlsConnector, client::TlsStream};
77pub(crate) use tokio_tungstenite::{
78    MaybeTlsStream, WebSocketStream, client_async_with_config, connect_async_with_config,
79    tungstenite::Message, tungstenite::handshake::client::Request,
80};
81pub(crate) use webpki_roots::TLS_SERVER_ROOTS;
82
83#[cfg(test)]
84use std::{
85    sync::{Mutex, MutexGuard},
86    thread::{JoinHandle, spawn},
87    time::Instant,
88};
89
90#[cfg(test)]
91use http_type::tokio;