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