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 {config::*, context::*, error::*, hook::*, route::*, server::*};
27
28pub use {http_type::*, inventory};
29
30pub(crate) use {listener::*, protocol::*, tls::*};
31
32use std::{
33    any::Any,
34    cell::{RefCell, RefMut},
35    cmp::Ordering,
36    collections::{HashMap, HashSet, VecDeque},
37    error::Error,
38    fmt,
39    future::{Future, poll_fn},
40    hash::{Hash, Hasher},
41    io::{self, ErrorKind, IoSlice, IoSliceMut, Write, stderr, stdout},
42    marker::PhantomPinned,
43    mem::{self, MaybeUninit},
44    net::{self, SocketAddr},
45    num::ParseIntError,
46    panic::AssertUnwindSafe,
47    pin::{Pin, pin},
48    process::abort,
49    ptr, slice,
50    str::{Split, SplitWhitespace, Utf8Error, from_utf8},
51    sync::{
52        self, Arc, Mutex, MutexGuard, OnceLock,
53        atomic::{AtomicBool, AtomicUsize},
54    },
55    task::{self, Poll, Waker},
56    thread,
57    time::Duration,
58};
59
60use {
61    bytes::Bytes,
62    futures::{FutureExt, StreamExt, stream::FuturesUnordered},
63    h2::{
64        Reason,
65        server::{Builder, SendResponse},
66    },
67    http::{
68        HeaderMap, HeaderName, HeaderValue, StatusCode, Uri,
69        request::Parts,
70        status::InvalidStatusCode,
71        uri::{Authority, Scheme},
72    },
73    inventory::collect,
74    lombok_macros::*,
75    quinn::{
76        self, AsyncUdpSocket, Chunk, Connection, ConnectionId, ConnectionIdGenerator, Endpoint,
77        EndpointConfig, Incoming, ReadError, RecvStream, Runtime, SendStream, TokioRuntime,
78        TransportConfig, UdpPoller, VarInt, WriteError,
79        crypto::rustls::{HandshakeData, NoInitialCipherSuite, QuicServerConfig},
80        udp::{BATCH_SIZE, RecvMeta, Transmit},
81    },
82    regex::Regex,
83    rustls::{
84        SupportedCipherSuite,
85        crypto::{
86            CryptoProvider, SecureRandom,
87            ring::{
88                Ticketer,
89                cipher_suite::{
90                    TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
91                    TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
92                    TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
93                    TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
94                    TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, TLS13_AES_128_GCM_SHA256,
95                    TLS13_AES_256_GCM_SHA384, TLS13_CHACHA20_POLY1305_SHA256,
96                },
97                default_provider,
98            },
99        },
100        pki_types::{
101            CertificateDer, PrivateKeyDer,
102            pem::{self, PemObject},
103        },
104        server::{ServerSessionMemoryCache, UnbufferedServerConnection},
105        unbuffered::{ConnectionState, EncodeError, EncryptError, UnbufferedStatus},
106        version::{TLS12, TLS13},
107    },
108    serde::{Deserialize, Serialize},
109    socket2::{Domain, SockRef, Socket, Type},
110    tokio::{
111        io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
112        net::{TcpListener, TcpStream, lookup_host},
113        runtime, spawn,
114        sync::watch::{Receiver, Sender, channel, error::SendError},
115        task::{JoinError, JoinHandle, spawn_blocking},
116        time::{Sleep, error::Elapsed, sleep, timeout},
117    },
118};
119
120#[cfg(not(target_os = "linux"))]
121use tokio::runtime::Handle;
122
123#[cfg(target_os = "linux")]
124use {
125    io_uring::{IoUring, opcode, squeue, types},
126    libc::{
127        AF_INET, AF_INET6, EAGAIN, EBUSY, ECANCELED, EFD_CLOEXEC, EFD_NONBLOCK, EINTR, EPIPE,
128        IP_TTL, IPPROTO_IP, IPPROTO_IPV6, IPV6_UNICAST_HOPS, MSG_MORE, MSG_WAITALL, POLLIN,
129        SHUT_WR, SOCK_CLOEXEC, SOCK_NONBLOCK, c_int, c_void, close, eventfd, read, setsockopt,
130        shutdown, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, write,
131    },
132    std::{
133        net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6},
134        os::fd::{AsRawFd, RawFd},
135        sync::mpsc::{SyncSender, sync_channel},
136        task::Wake,
137    },
138    tokio::runtime::{EnterGuard, Handle},
139};