3 releases (breaking)
0.3.0 | Aug 15, 2023 |
---|---|
0.2.0 | Jun 9, 2023 |
0.1.0 | Jun 6, 2023 |
#1507 in HTTP server
24 downloads per month
38KB
664 lines
Rust HTTP1.1 Server
A HTTP server library that complies with the 1.1 version of the HTTP protocol according to RFC 9112
and other related documents, written in Rust with ZERO dependencies.
This was originally meant to be an executable running a HTTP server, but it turns out it is way easier to implement a HTTP library, then use it in one's own executable.
Changelog
For more info check the change log
lib.rs
:
Note: The library is still in early Alpha/Beta
A lightweight server library for the HTTP/1.1 protocol
The aim of this crate is to create a library both easy to use and fast in intercepting incoming connections
Quick start
// Library imports
use oak_http_server::{Server, Status};
fn main() {
// Save server hostname and port as variables
let hostname = "localhost";
let port: u16 = 2300;
// Create a new HTTP server instance (must be mutable since appending handlers to the Server struct modifies its fields)
let mut server = Server::new(hostname, port);
// The following path handler responds to each response to the "/ping" path with "Pong!"
server.on("/ping", |_request, response| response.send("Pong!"));
// The following path handler responds only to GET requests on the "\headers" path
// and returns a list of the headers supplied in the corresponding HTTP request
server.on_get("/headers", |request, response| {
response.send(format!(
"Your browser sent the following headers with the request:\n{}",
request
.headers
.iter()
.map(|(name, value)| format!("{}: {}\n", name, value))
.collect::<String>(),
))
});
// Start the HTTP server. The provided closure/callback function will be called
// when a connection listener has been successfully established.
// Once this function is run, the server will begin listening to incoming HTTP requests
# #[cfg(not)]
server.start(|| {
println!("Successfully initiated server");
});
}