Crates.io | micro-http |
lib.rs | micro-http |
version | 0.2.2 |
created_at | 2023-02-18 09:43:17.222474+00 |
updated_at | 2025-09-25 15:11:10.093298+00 |
description | the async micro http server |
homepage | https://github.com/foldright/micro-http |
repository | https://github.com/foldright/micro-http |
max_upload_size | |
id | 788096 |
size | 165,348 |
A lightweight, efficient, and modular HTTP/1.1 server implementation built on top of tokio.
Add this to your Cargo.toml
:
[dependencies]
micro-http = "0.1"
tokio = { version = "1", features = ["full"] }
http = "1"
http-body = "1"
tracing = "0.1"
tracing-subscriber = "0.3"
Here's a simple HTTP server that responds with "Hello World!":
use http::{Request, Response, StatusCode};
use http_body_util::BodyExt;
use std::error::Error;
use std::sync::Arc;
use tokio::net::TcpListener;
use micro_http::connection::HttpConnection;
use micro_http::handler::make_handler;
use micro_http::protocol::body::ReqBody;
use tracing::{error, info, warn, Level};
use tracing_subscriber::FmtSubscriber;
#[tokio::main]
async fn main() {
let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
info!(port = 8080, "start listening");
let tcp_listener = match TcpListener::bind("127.0.0.1:8080").await {
Ok(tcp_listener) => tcp_listener,
Err(e) => {
error!(cause = %e, "bind server error");
return;
}
};
let handler = make_handler(simple_handler);
let handler = Arc::new(handler);
loop {
let (tcp_stream, _remote_addr) = match tcp_listener.accept().await {
Ok(stream_and_addr) => stream_and_addr,
Err(e) => {
warn!(cause = %e, "failed to accept");
continue;
}
};
let handler = handler.clone();
// one connection per task
tokio::spawn(async move {
let (reader, writer) = tcp_stream.into_split();
let connection = HttpConnection::new(reader, writer);
match connection.process(handler).await {
Ok(_) => {
info!("finished process, connection shutdown");
}
Err(e) => {
error!("service has error, cause {}, connection shutdown", e);
}
}
});
}
}
async fn simple_handler(request: Request<ReqBody>) -> Result<Response<String>, Box<dyn Error + Send + Sync>> {
let path = request.uri().path().to_string();
info!("request path {}", path);
let (_header, body) = request.into_parts();
let body_bytes = body.collect().await?.to_bytes();
info!(body = std::str::from_utf8(&body_bytes[..]).unwrap(), "receiving request body");
let response_body = "Hello World!\r\n";
let response = Response::builder()
.status(StatusCode::OK)
.header(http::header::CONTENT_LENGTH, response_body.len())
.body(response_body.to_string())
.unwrap();
Ok(response)
}
The crate is organized into several key modules:
connection
: Core connection handling and lifecycle managementprotocol
: Protocol types and abstractionscodec
: Protocol encoding/decoding implementationhandler
: Request handler traits and utilitiesThe implementation focuses on performance through:
The crate uses unsafe code in a few well-documented places for performance optimization, particularly in header parsing. All unsafe usage is carefully reviewed and tested.
This project is licensed under the MIT License or Apache-2.0 License, pick one.
Contributions are welcome! Please feel free to submit a Pull Request.