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
14mod common;
15mod request;
16mod response;
17mod utils;
18
19pub use {request::*, response::*};
20
21pub use http_type::{HashMapXxHash3_64, RequestError, hash_map_xx_hash3_64};
22pub use serde_json::{
23    Deserializer, Error, Map, Number, StreamDeserializer, Value, from_reader, from_slice, from_str,
24    from_value, to_string, to_string_pretty, to_value, to_vec, to_vec_pretty, to_writer,
25    to_writer_pretty, value,
26};
27
28use {common::*, utils::*};
29
30use std::{
31    borrow::Cow,
32    collections::{HashSet, VecDeque},
33    fmt::{self, Debug, Display, Formatter},
34    io::{Read, Write},
35    net::{Ipv4Addr, Ipv6Addr, TcpStream},
36    pin::Pin,
37    str::from_utf8,
38    sync::{
39        Arc, RwLock,
40        atomic::{AtomicBool, Ordering},
41    },
42    task::{Context, Poll},
43    time::{Duration, SystemTime, UNIX_EPOCH},
44    vec::IntoIter,
45};
46#[cfg(test)]
47use std::{
48    sync::Mutex,
49    thread::{JoinHandle, spawn},
50    time::Instant,
51};
52
53use futures::{Future, Sink, SinkExt, Stream, StreamExt};
54#[cfg(test)]
55use http_type::tokio;
56use http_type::{
57    ACCEPT, ACCEPT_ANY, BR_BYTES, COLON_U8, CONNECTION, CONTENT_LENGTH, CONTENT_TYPE, Compress,
58    ContentType, DEFAULT_BUFFER_SIZE, DEFAULT_HTTP_PATH, DEFAULT_MAX_REDIRECT_TIMES,
59    DEFAULT_TIMEOUT, EMPTY_STR, HOST, HTTP_BR_BYTES, HttpStatus, HttpUrlComponents, HttpVersion,
60    LOCATION, Method, Protocol, QUERY, RequestBody, RequestBodyString, RequestHeaders,
61    ResponseHeaders, ResponseStatusCode, SEC_WEBSOCKET_KEY, SEC_WEBSOCKET_VERSION, SPACE_U8,
62    TAB_U8, UPGRADE, USER_AGENT,
63    tokio::{
64        io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
65        net::TcpStream as AsyncTcpStream,
66        runtime::Runtime,
67        sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard},
68        time::timeout,
69    },
70};
71use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned, pki_types::ServerName};
72use serde::{Serialize, Serializer};
73#[cfg(test)]
74use serde_json::json;
75use tokio_rustls::{TlsConnector, client::TlsStream};
76use tokio_tungstenite::{
77    MaybeTlsStream, WebSocketStream, client_async_with_config, connect_async_with_config,
78    tungstenite::Message, tungstenite::handshake::client::Request,
79};
80use webpki_roots::TLS_SERVER_ROOTS;