Expand description
This crate provides a lightweight mini-framework for writing HTTP/1.1 servers.
The Handler trait is the protagonist of this library.
It represents anything that can respond to an http Request by writing to an http Response.
There is no separate concept for “middleware”; everything that needs to interact with either the request or response is a handler.
§Usage
An echo server:
use claro::{Server, Request, Response, Status, handler};
let mut router = handler::PathRouter::new();
router.register(
"/hello/{name}",
|req: &mut Request, res: &mut Response| {
let name = &req.path_values["name"];
res.send_plain_text(&format!("Hello, {name}!"))
}
);
let (srv, shutdown) = Server::new("localhost:0", router).unwrap();
std::thread::spawn(|| srv.listen());
// Verify that it works, using minreq (an http client)
let response = minreq::get(&format!("http://localhost:{}/hello/friends", shutdown.address.port()))
.send()
.unwrap();
assert_eq!(response.as_str().unwrap(), "Hello, friends!");§Limitations
- No support for TLS/HTTPS
- No support for persistent connections (i.e. Connection: keep-alive); all responses have the “Connection: close” header.
- No support for non-ascii values in headers. This crate takes the position that header values should always be a subset of ASCII. While the spec recommends that “opaque” binary data outside of that subset be allowed as well, I do not heed this recommendation. Incoming header values that do not fall inside the valid ASCII subset cause request parsing to fail with a 400 Bad Request.
Re-exports§
pub use handler::Handler;
Modules§
- handler
- Composable http request handling
Structs§
- Headers
- Http headers
- Media
Type - The contents of a media-type header like
Content-Type. - Request
- An incoming http request
- Response
- An HTTP response.
- Server
- An HTTP server that is ready to listen.
- Shutdown
Handle - A handle for shutting down a listening HTTP server.
- Signal
- A wrapper around a Condvar and a mutex that allows a thread to wait for a notification.
- UrlEncoded
Params - Parsed key-value pairs from a url-encoded string.