pub struct MicroHTTP { /* private fields */ }
Expand description

This is the main struct of the µHTTP server.

Implementations

Create a new MicroHTTP server on the given interface.microhttp

Internally, this just tries to create a TcpListener - nothing special. Returns the new MicroHTTP server or an std::io::Error on error.

Example
use micro_http_server::MicroHTTP;

let interface: &str = "127.0.0.1:3000";
let server = MicroHTTP::new(interface)
    .expect("Could not create server, maybe the port is already being used?");

Set whether or not the underlying TcpListener awaits connections in nonblocking mode

Return the next available client which is incoming at this server.

Returns either:

  • Some(client) if a client is available
  • None if no client is currently available (i.e. no one has reached out to the server yet)
  • std::io::Error if something is wrong with the server.
Example
use std::{io::{Read,Write},net::TcpStream};
use micro_http_server::MicroHTTP;

let server = MicroHTTP::new("127.0.0.1:3000").expect("Could not create server.");
println!("[Server] Waiting for a client @ 127.0.0.1:3000...");

loop {
    let result = server.next_client();
    if result.is_err() {
        println!("Something is wrong with the client: {:?}", result.unwrap_err());
        break;
    }

    match result.unwrap() {
        None => {
            // Here you can sleep or do something else.
            println!("Still waiting for clients...");
        },
        Some(client) => {
            println!("Got a new client from: {:?}", client.addr());
        }
    }
}

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.