A modular HTTP server library in Rust.
forbid(unsafe_code)
- Threaded request handlers:
FnOnce(Request) -> Response + 'static + Clone + Send + Sync
- Uses async code internally for excellent performance under load
- JSON
- Server-Sent Events (SSE)
- Saves large request bodies to temp files
- Sends 100-Continue
- Limits number of threads and connections
- Modular: roll your own logging, write custom versions of internal methods, etc.
- No macros or complicated type params
- Good test coverage (63%)
- New, not proven in production.
- To do:
- Request timeouts
chunked
transfer-encoding for request bodies- gzip
- brotli
- TLS
- automatically getting TLS certs via ACME
- Drop idle connections when approaching connection limit.
- Denial-of-Service mitigation: source throttling, minimum throughput
- Complete functional test suite
- Missing load tests
- Disk space usage limits
Complete examples: examples/
.
Simple example:
use serde::Deserialize;
use serde_json::json;
use servlin::{
socket_addr_127_0_0_1,
Error,
HttpServerBuilder,
Request,
Response
};
use servlin::log::log_request_and_response;
use std::sync::Arc;
use temp_dir::TempDir;
struct State {}
fn hello(_state: Arc<State>, req: Request) -> Result<Response, Error> {
#[derive(Deserialize)]
struct Input {
name: String,
}
let input: Input = req.json()?;
Ok(Response::json(200, json!({"message": format!("Hello, {}!", input.name)}))?)
}
fn handle_req(state: Arc<State>, req: Request) -> Result<Response, Error> {
match (req.method(), req.url().path()) {
("GET", "/ping") => Ok(Response::text(200, "ok")),
("POST", "/hello") => hello(state, req),
_ => Ok(Response::text(404, "Not found")),
}
}
let state = Arc::new(State {});
let request_handler = move |req: Request| {
log_request_and_response(req, |req| handle_req(state, req)).unwrap()
};
let cache_dir = TempDir::new().unwrap();
safina::timer::start_timer_thread();
let executor = safina::executor::Executor::new(1, 9).unwrap();
executor.block_on(
HttpServerBuilder::new()
.listen_addr(socket_addr_127_0_0_1(8271))
.max_conns(1000)
.small_body_len(64 * 1024)
.receive_large_bodies(cache_dir.path())
.spawn_and_join(request_handler)
).unwrap();
Metric output format: x/y
x = unsafe code used by the build
y = total unsafe code found in the crate
Symbols:
π = No `unsafe` usage found, declares #![forbid(unsafe_code)]
β = No `unsafe` usage found, missing #![forbid(unsafe_code)]
β’οΈ = `unsafe` usage found
Functions Expressions Impls Traits Methods Dependency
0/0 0/0 0/0 0/0 0/0 π servlin 0.7.0
0/0 0/0 0/0 0/0 0/0 π βββ safina 0.6.0
0/0 0/0 0/0 0/0 0/0 π β βββ safina-macros 0.1.3
0/0 0/0 0/0 0/0 0/0 π β βββ safe-proc-macro2 1.0.67
0/0 4/4 0/0 0/0 0/0 β’οΈ β β βββ unicode-ident 1.0.13
0/0 0/0 0/0 0/0 0/0 π β βββ safe-quote 1.0.15
0/0 0/0 0/0 0/0 0/0 π β βββ safe-proc-macro2 1.0.67
0/0 0/0 0/0 0/0 0/0 π βββ async-fs 2.1.2
4/4 222/222 40/40 0/0 13/13 β’οΈ β βββ async-lock 3.4.0
0/0 2/2 0/0 0/0 0/0 β’οΈ β β βββ event-listener-strategy 0.5.2
0/0 39/49 8/12 0/0 2/2 β’οΈ β β β βββ event-listener 5.3.1
0/0 183/183 2/2 0/0 1/1 β’οΈ β β β β βββ concurrent-queue 2.5.0
4/4 12/75 4/16 0/0 0/3 β’οΈ β β β β β βββ crossbeam-utils 0.8.20
0/0 0/0 0/0 0/0 0/0 π β β β β βββ parking 2.2.1
0/0 11/191 0/0 0/0 2/2 β’οΈ β β β β βββ pin-project-lite 0.2.14
0/0 11/191 0/0 0/0 2/2 β’οΈ β β β βββ pin-project-lite 0.2.14
0/0 39/49 8/12 0/0 2/2 β’οΈ β β βββ event-listener 5.3.1
0/0 11/191 0/0 0/0 2/2 β’οΈ β β βββ pin-project-lite 0.2.14
0/0 0/0 0/0 0/0 0/0 π β βββ blocking 1.6.1
0/0 0/0 0/0 0/0 0/0 π β β βββ async-channel 2.3.1
0/0 183/183 2/2 0/0 1/1 β’οΈ β β β βββ concurrent-queue 2.5.0
0/0 2/2 0/0 0/0 0/0 β’οΈ β β β βββ event-listener-strategy 0.5.2
0/0 36/36 2/2 0/0 0/0 β’οΈ β β β βββ futures-core 0.3.31
0/0 11/191 0/0 0/0 2/2 β’οΈ β β β βββ pin-project-lite 0.2.14
1/1 860/866 4/4 0/0 13/13 β’οΈ β β βββ async-task 4.7.1
0/0 0/0 0/0 0/0 0/0 β β β βββ futures-io 0.3.31
0/0 0/0 0/0 0/0 0/0 β β β βββ futures-lite 2.3.0
0/0 0/0 0/0 0/0 0/0 π β β β βββ fastrand 2.1.1
0/0 36/36 2/2 0/0 0/0 β’οΈ β β β βββ futures-core 0.3.31
0/0 0/0 0/0 0/0 0/0 β β β β βββ futures-io 0.3.31
27/41 1973/2421 2/2 0/0 109/147 β’οΈ β β β βββ memchr 2.7.4
0/0 0/0 0/0 0/0 0/0 π β β β βββ parking 2.2.1
0/0 11/191 0/0 0/0 2/2 β’οΈ β β β βββ pin-project-lite 0.2.14
0/0 28/28 2/2 0/0 0/0 β’οΈ β β βββ piper 0.2.4
0/0 32/32 2/2 0/0 0/0 β’οΈ β β β βββ atomic-waker 1.1.2
0/0 0/0 0/0 0/0 0/0 π β β β βββ fastrand 2.1.1
0/0 0/0 0/0 0/0 0/0 β β β β βββ futures-io 0.3.31
0/0 14/14 1/1 0/0 0/0 β’οΈ β β βββ tracing 0.1.40
0/0 11/191 0/0 0/0 2/2 β’οΈ β β βββ pin-project-lite 0.2.14
0/0 96/96 5/5 0/0 2/2 β’οΈ β β βββ tracing-core 0.1.32
0/0 0/117 0/9 0/0 0/4 β β β βββ once_cell 1.20.2
0/0 0/0 0/0 0/0 0/0 β β βββ futures-lite 2.3.0
0/0 0/0 0/0 0/0 0/0 π βββ async-net 2.0.0
0/0 68/114 19/22 1/1 4/8 β’οΈ β βββ async-io 2.3.4
4/4 222/222 40/40 0/0 13/13 β’οΈ β β βββ async-lock 3.4.0
0/0 0/0 0/0 0/0 0/0 β β β βββ cfg-if 1.0.0
0/0 183/183 2/2 0/0 1/1 β’οΈ β β βββ concurrent-queue 2.5.0
0/0 0/0 0/0 0/0 0/0 β β β βββ futures-io 0.3.31
0/0 0/0 0/0 0/0 0/0 β β β βββ futures-lite 2.3.0
0/0 0/0 0/0 0/0 0/0 π β β βββ parking 2.2.1
0/2 34/420 5/20 1/4 5/14 β’οΈ β β βββ polling 3.7.3
0/0 0/0 0/0 0/0 0/0 β β β β βββ cfg-if 1.0.0
53/389 2527/7252 6/8 1/1 20/35 β’οΈ β β β βββ rustix 0.38.37
0/0 0/0 0/0 0/0 0/0 β β β β β βββ bitflags 2.6.0
0/0 5/5 0/0 0/0 0/0 β’οΈ β β β β β βββ serde 1.0.210
0/0 0/0 0/0 0/0 0/0 β β β β β β βββ serde_derive 1.0.210
0/0 15/15 0/0 0/0 3/3 β’οΈ β β β β β βββ proc-macro2 1.0.88
0/0 4/4 0/0 0/0 0/0 β’οΈ β β β β β β βββ unicode-ident 1.0.13
0/0 0/0 0/0 0/0 0/0 β β β β β β βββ quote 1.0.37
0/0 15/15 0/0 0/0 3/3 β’οΈ β β β β β β βββ proc-macro2 1.0.88
0/0 88/88 3/3 0/0 2/2 β’οΈ β β β β β βββ syn 2.0.82
0/0 15/15 0/0 0/0 3/3 β’οΈ β β β β β βββ proc-macro2 1.0.88
0/0 0/0 0/0 0/0 0/0 β β β β β β βββ quote 1.0.37
0/0 4/4 0/0 0/0 0/0 β’οΈ β β β β β βββ unicode-ident 1.0.13
0/0 35/103 0/0 0/0 0/0 β’οΈ β β β β βββ errno 0.3.9
1/90 10/596 0/2 0/0 5/69 β’οΈ β β β β β βββ libc 0.2.161
0/0 7/7 0/0 0/0 0/0 β’οΈ β β β β βββ itoa 1.0.11
1/90 10/596 0/2 0/0 5/69 β’οΈ β β β β βββ libc 0.2.161
0/0 14/14 1/1 0/0 0/0 β’οΈ β β β βββ tracing 0.1.40
53/389 2527/7252 6/8 1/1 20/35 β’οΈ β β βββ rustix 0.38.37
0/0 23/23 0/0 0/0 3/3 β’οΈ β β βββ slab 0.4.9
0/0 5/5 0/0 0/0 0/0 β’οΈ β β β βββ serde 1.0.210
0/0 14/14 1/1 0/0 0/0 β’οΈ β β βββ tracing 0.1.40
0/0 0/0 0/0 0/0 0/0 π β βββ blocking 1.6.1
0/0 0/0 0/0 0/0 0/0 β β βββ futures-lite 2.3.0
0/0 0/0 0/0 0/0 0/0 π βββ fixed-buffer 1.0.0
0/0 0/0 0/0 0/0 0/0 β β βββ futures-io 0.3.31
0/0 0/0 0/0 0/0 0/0 β βββ futures-io 0.3.31
0/0 0/0 0/0 0/0 0/0 β βββ futures-lite 2.3.0
0/0 0/0 0/0 0/0 0/0 β βββ include_dir 0.7.4
0/0 0/0 0/0 0/0 0/0 β β βββ include_dir_macros 0.7.4
0/0 15/15 0/0 0/0 3/3 β’οΈ β βββ proc-macro2 1.0.88
0/0 0/0 0/0 0/0 0/0 β β βββ quote 1.0.37
0/0 0/0 0/0 0/0 0/0 π βββ permit 0.2.1
0/0 12/32 0/0 0/0 0/0 β’οΈ βββ rand 0.8.5
1/90 10/596 0/2 0/0 5/69 β’οΈ β βββ libc 0.2.161
0/0 2/2 0/0 0/0 0/0 β’οΈ β βββ rand_core 0.6.4
3/6 51/192 0/1 0/0 1/3 β’οΈ β β βββ getrandom 0.2.15
0/0 0/0 0/0 0/0 0/0 β β β β βββ cfg-if 1.0.0
1/90 10/596 0/2 0/0 5/69 β’οΈ β β β βββ libc 0.2.161
0/0 5/5 0/0 0/0 0/0 β’οΈ β β βββ serde 1.0.210
0/0 5/5 0/0 0/0 0/0 β’οΈ β βββ serde 1.0.210
0/0 0/0 0/0 0/0 0/0 π βββ safe-regex 0.3.0
0/0 0/0 0/0 0/0 0/0 π β βββ safe-regex-macro 0.3.0
0/0 0/0 0/0 0/0 0/0 π β βββ safe-proc-macro2 1.0.67
0/0 0/0 0/0 0/0 0/0 π β βββ safe-regex-compiler 0.3.0
0/0 0/0 0/0 0/0 0/0 π β βββ safe-proc-macro2 1.0.67
0/0 0/0 0/0 0/0 0/0 π β βββ safe-quote 1.0.15
0/0 5/5 0/0 0/0 0/0 β’οΈ βββ serde 1.0.210
0/0 66/69 0/0 0/0 0/0 β’οΈ βββ serde_json 1.0.132
0/0 7/7 0/0 0/0 0/0 β’οΈ β βββ itoa 1.0.11
27/41 1973/2421 2/2 0/0 109/147 β’οΈ β βββ memchr 2.7.4
7/9 572/702 0/0 0/0 2/2 β’οΈ β βββ ryu 1.0.18
0/0 5/5 0/0 0/0 0/0 β’οΈ β βββ serde 1.0.210
0/0 0/0 0/0 0/0 0/0 π βββ serde_urlencoded 0.7.1
0/0 2/2 0/0 0/0 0/0 β’οΈ β βββ form_urlencoded 1.2.1
0/0 8/8 0/0 0/0 0/0 β’οΈ β β βββ percent-encoding 2.3.1
0/0 7/7 0/0 0/0 0/0 β’οΈ β βββ itoa 1.0.11
7/9 572/702 0/0 0/0 2/2 β’οΈ β βββ ryu 1.0.18
0/0 5/5 0/0 0/0 0/0 β’οΈ β βββ serde 1.0.210
0/0 0/0 0/0 0/0 0/0 π βββ temp-dir 0.1.14
0/0 0/0 0/0 0/0 0/0 π βββ temp-file 0.1.9
0/0 0/0 0/0 0/0 0/0 β βββ url 2.5.2
0/0 2/2 0/0 0/0 0/0 β’οΈ βββ form_urlencoded 1.2.1
0/0 0/0 0/0 0/0 0/0 β βββ idna 0.5.0
0/0 5/5 0/0 0/0 0/0 β’οΈ β βββ unicode-bidi 0.3.17
0/0 5/5 0/0 0/0 0/0 β’οΈ β β βββ serde 1.0.210
1/1 24/24 0/0 0/0 0/0 β’οΈ β βββ unicode-normalization 0.1.24
0/0 0/0 0/0 0/0 0/0 π β βββ tinyvec 1.8.0
0/0 5/5 0/0 0/0 0/0 β’οΈ β βββ serde 1.0.210
0/0 0/0 0/0 0/0 0/0 π β βββ tinyvec_macros 0.1.1
0/0 8/8 0/0 0/0 0/0 β’οΈ βββ percent-encoding 2.3.1
0/0 5/5 0/0 0/0 0/0 β’οΈ βββ serde 1.0.210
101/547 7066/13995 105/153 3/6 187/326
See rust-webserver-comparison.md.
- v0.7.0 2024-11-06
log_request_and_response
to logduration_ms
tag.- Fix typo in function name
Response::internal_server_errror_500
. - Close connection on 5xx error.
- Add
Response::too_many_requests_429
. - Implement
Into<TagList>
for arrays.
- v0.6.0 2024-11-02
- Remove
servlin::reexports
module. - Use
safina
v0.6.0.
- Remove
- v0.5.1 2024-10-26 - Remove dependency on
once_cell
. - v0.5.0 2024-10-21 - Remove
LogFileWriterBuilder
. - v0.4.3 - Implement
From<Cow<'_, str>>
andFrom<&Path>
forTagValue
. - v0.4.2 - Implement
Seek
forBodyReader
. - v0.4.1
- Add
Request::opt_json
. - Implement
From<LoggerStoppedError>
forError
.
- Add
- v0.4.0
- Changed
Response::json
to returnResult<Response, Error>
. - Changed
log_request_and_response
to returnResult
. - Added
Response::unprocessable_entity_422
.
- Changed
- v0.3.2 - Fix bug in
Response::include_dir
redirects. - v0.3.1
- Add
Response::redirect_301
Response::include_dir
to redirect from/somedir
to/somedir/
so relative URLs will work.
- Add
- v0.3.0 - Changed
Response::include_dir
to take&Request
and look forindex.html
in dirs. - v0.2.0
- Added:
log_request_and_response
and other logging toolingResponse::ok_200()
Response::unauthorized_401()
Response::forbidden_403()
Response::internal_server_errror_500()
Response::not_implemented_501()
Response::service_unavailable_503()
EventSender::is_connected()
PORT_env()
- Removed
print_log_response
andRequestBody::length_is_known
- Changed
RequestBody::len
andis_empty
to returnOption
. - Bugfixes
- Added:
- v0.1.1 - Add
EventSender::unconnected
. - v0.1.0 - Rename library to Servlin.
- Fix limitations above
- Support HEAD responses that have Content-Length set and no body.
- Add a server-wide limit on upload body size.
- Limit disk usage for caching uploads.
- Update
rust-webserver-comparison.md
- Add missing data
- Add other servers from https://www.arewewebyet.org/topics/frameworks/
- Rearrange
- Generate geiger reports for each web server
License: MIT OR Apache-2.0