Skip to main content

hyperlane_core/
lib.rs

1//! hyperlane-core
2//!
3//! A lightweight, high-performance, and cross-platform Rust
4//! HTTP server library built on Tokio. It simplifies modern
5//! web service development by providing built-in support
6//! for middleware, WebSocket, Server-Sent Events (SSE),
7//! and raw TCP communication. With a unified and ergonomic
8//! API across Windows, Linux, and MacOS, it enables developers
9//! to build robust, scalable, and event-driven network
10//! applications with minimal overhead and maximum flexibility.
11
12mod config;
13mod context;
14mod error;
15mod hook;
16mod listener;
17mod protocol;
18mod route;
19mod server;
20mod tls;
21
22#[cfg(test)]
23#[path = "../tests/mod.rs"]
24mod tests;
25
26pub use {
27    config::{ServerConfig, *},
28    context::*,
29    error::*,
30    hook::*,
31    route::*,
32    server::*,
33};
34
35pub use {
36    http_type::{Method, Request, Response, *},
37    inventory,
38};
39
40pub(crate) use {listener::*, protocol::*, tls::*};
41
42use std::{
43    any::Any,
44    cell::{RefCell, RefMut},
45    cmp::Ordering,
46    collections::{HashMap, HashSet, VecDeque, hash_map::Entry},
47    error::Error,
48    fmt,
49    future::{Future, poll_fn},
50    hash::{Hash, Hasher},
51    io::{self, ErrorKind, IoSlice, IoSliceMut, Write, stderr, stdout},
52    marker::PhantomPinned,
53    mem::{self, MaybeUninit},
54    net::{self, SocketAddr},
55    num::ParseIntError,
56    panic::AssertUnwindSafe,
57    pin::{Pin, pin},
58    process::abort,
59    ptr, slice,
60    str::{Split, SplitWhitespace, Utf8Error, from_utf8, from_utf8_unchecked},
61    sync::{
62        self, Arc, Mutex, MutexGuard, OnceLock,
63        atomic::{AtomicBool, AtomicUsize},
64    },
65    task::{self, Poll, Waker},
66    thread,
67    time::Duration,
68};
69
70use {
71    bytes::{Buf, Bytes},
72    futures::{FutureExt, StreamExt, stream::FuturesUnordered},
73    h2::{
74        self, Reason,
75        server::{Builder, SendResponse},
76    },
77    http::{
78        self, HeaderMap, HeaderName, HeaderValue, StatusCode, Uri,
79        request::Parts,
80        status::InvalidStatusCode,
81        uri::{Authority, Scheme},
82    },
83    inventory::collect,
84    lombok_macros::*,
85    quinn::{
86        self, AsyncUdpSocket, Chunk, Connection, ConnectionId, ConnectionIdGenerator, Endpoint,
87        EndpointConfig, Incoming, ReadError, RecvStream, Runtime, SendStream, TokioRuntime,
88        TransportConfig, UdpPoller, VarInt, WriteError,
89        crypto::rustls::{HandshakeData, NoInitialCipherSuite, QuicServerConfig},
90        udp::{BATCH_SIZE, RecvMeta, Transmit},
91    },
92    regex::Regex,
93    rustls::{
94        self, SupportedCipherSuite, SupportedProtocolVersion,
95        crypto::{
96            CryptoProvider, SecureRandom,
97            ring::{
98                Ticketer,
99                cipher_suite::{
100                    TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
101                    TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
102                    TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
103                    TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
104                    TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, TLS13_AES_128_GCM_SHA256,
105                    TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
106                },
107                default_provider,
108            },
109        },
110        pki_types::{
111            CertificateDer, PrivateKeyDer,
112            pem::{self, PemObject},
113        },
114        server::{ServerSessionMemoryCache, UnbufferedServerConnection},
115        unbuffered::{ConnectionState, EncodeError, EncryptError, UnbufferedStatus},
116        version::{TLS12, TLS13},
117    },
118    serde::{Deserialize, Serialize},
119    socket2::{Domain, SockRef, Socket, Type},
120    tokio::{
121        io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
122        net::{TcpListener, TcpStream, lookup_host},
123        runtime, spawn,
124        sync::watch::{Receiver, Sender, channel, error::SendError},
125        task::{JoinError, JoinHandle, spawn_blocking},
126        time::{Sleep, error::Elapsed, sleep, timeout},
127    },
128};
129
130#[cfg(not(target_os = "linux"))]
131use tokio::runtime::Handle;
132
133#[cfg(target_os = "linux")]
134use {
135    io_uring::{IoUring, opcode, squeue, types},
136    libc::{
137        AF_INET, AF_INET6, EAGAIN, EBUSY, ECANCELED, EFD_CLOEXEC, EFD_NONBLOCK, EINTR, EPIPE,
138        IP_TTL, IPPROTO_IP, IPPROTO_IPV6, IPV6_UNICAST_HOPS, MSG_MORE, MSG_WAITALL, POLLIN,
139        SHUT_WR, SOCK_CLOEXEC, SOCK_NONBLOCK, c_int, c_void, close, eventfd, read, setsockopt,
140        shutdown, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, write,
141    },
142    std::{
143        net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6},
144        os::fd::{AsRawFd, RawFd},
145        sync::mpsc::{SyncSender, sync_channel},
146        task::Wake,
147    },
148    tokio::runtime::{EnterGuard, Handle},
149};