Crates.io | micro_http_server |
lib.rs | micro_http_server |
version | 0.0.5 |
source | src |
created_at | 2018-07-14 12:59:30.432146 |
updated_at | 2022-04-15 09:43:54.074112 |
description | A very simple HTTP server without Futures or other complicated stuff. |
homepage | https://github.com/philippludwig/micro-http-server |
repository | https://github.com/philippludwig/micro-http-server.git |
max_upload_size | |
id | 74189 |
size | 19,131 |
The µHTTP server is a very small HTTP server implementation for Rust without the needed for complicated stuff like Futures.
For various small projects, I usually need a tiny HTTP server which just answers a few very small API-like requests. Since the web frameworks that are available for Rust all are heavily undocumented beside some small "getting started" examples, I turned to supposedly low-level frameworks like hyper, which also is severly lacking in documentation and examples; therefore I created µHTTP which does everything I need.
That's it. If you need more, feel free to open an Issue or a PR.
Creating a server is as simple as this:
let server = MicroHTTP::new("127.0.0.1:3000")
.expect("Could not create server.");
Now you can check if a client has connected:
let client = server.next_client().unwrap();
if client.is_some() {
// New connection
} else {
// No one talked to the server :(
}
To retrieve the client's request, call request()
:
let request_str = client.request()
.expect("Client didn't request anything!);
To respond, use respond_ok
for HTTP 200 or
respond
for a more custom response:
client.respond_ok("I got your request".as_bytes());
...
client.respond("200 OK",
"Here is my custom response".as_bytes(),
vec!(
"Some-Header: Some Value",
"Some other Header: Some other Value"
));
#![deny(missing_docs)]
, since everything should be documented - otherwise it is useless.