Crate oak_http_server

source ·
Expand description

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
   server.start(|| {
       println!("Successfully initiated server");
   });
}

Modules

  • Includes various handlers provided by the library

Structs

  • A struct representing a HTTP connection between a client and the server
  • A HTTP request
  • A HTTP response for the server to reply to the client
  • The “heart” of the module; the server struct
  • Represents a HTTP URL (named Target for formality reasons)
  • The HTTP version of a request or a response

Enums

Type Definitions