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