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