ยตHTTP ๐ฆ๐๐
A fast, tiny library for writing HTTP servers in Rust designed for humans:
-
Simple: Inspired by Go's standard library HTTP server.
-
Fast: High performance, multi-threaded implementation built on top of Tokio that competes with the fastest Rust HTTP servers.
-
Flexible: Simple interface that enables many use cases. It can be used directly or to act as a base for frameworks to build on top of.
Installation
Available on crates.io, install with:
cargo add uhttp
Benchmarks
Running on my desktop PC with a 7950x
oha -n 500000 -c 500 --latency-correction http://localhost:8080
---
config:
xyChart:
showTick: true
---
xychart-beta
title "Requests Per Second"
x-axis ["hyper", "uhttp", "Go HTTP", "Node.js (uws library)", "Node.js (std)"]
y-axis "Requests Per Second" 0 --> 500000
bar [497432, 493671, 412250, 140929, 79966]
Usage
Get Request
use io;
use Server;
use HttpWriter;
use Request;
use Response;
async
async
Request Body
use io;
use Server;
use Request;
use Response;
async
async
Routing
The URL is passed into the handler as a String and can be used to match request paths to routes. You can use simple if statements or a third party URL matching library to handle routing.
TODO: Adding a basic router
use io;
use Server;
use HttpWriter;
use Request;
use Response;
async
async
Serving a File
use io;
use PathBuf;
use fs;
use Server;
use HttpWriter;
use Request;
use Response;
const CARGO_HOME: &str = env!;
async
async
Constants
Provided are some constants to make responses more consistent
use io;
use Write;
use Server;
use Request;
use Response;
use c;
Performance
Setting Headers Explicitly
Setting the Content-Type, Content-Length or Transfer-Encoding explicitly will improve performance as the server does not need to detect them automatically.
TODO
- Provide compressor utils for
Content-Encoding:gzipandbr Transfer-Encoding: chunked- Server Sent Events (use this instead of WebSocket)
- HTTP/2
- More performance improvements
Out of Scope
Though feel free to raise a PR to add support
- WebSocket