Struct ehttpd::Server

source ·
pub struct Server<T = Body, const STACK_SIZE: usize = 65536> { /* private fields */ }
Expand description

A HTTP server

Architecture

The server creates a TcpListener bound to the given address. Then it creates a FIFO-queue to handle incoming connections, and spawns an acceptor-thread that accepts all incoming connections and sends them to the FIFO-queue. The exec-function loops forever over the queued connections and sends them together with the callback into a thread-pool to process them. If a connection is kept-alive, it is requeued into the FIFO-queue after the response has been sent to the client to be reprocessed again.

accept--->connection-queue--->threadpool
                 ^                |
                 |                |
             keep-alive           |
                 ^                |
                 |                v
close<----------------------thread-worker

Implementations§

source§

impl<T, const STACK_SIZE: usize> Server<T, STACK_SIZE>

source

pub fn new(
    address: &str,
    soft_limit: usize,
    hard_limit: usize
) -> Result<Self, Error>

Creates a new server bound on the given address

Examples found in repository?
examples/helloworld.rs (line 24)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |_: &mut Request| {
        let mut response = Response::new_200_ok();
        response.set_body_static(b"Hello world\r\n");
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}
More examples
Hide additional examples
examples/helloworld-nokeepalive.rs (line 25)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |_: &mut Request| {
        let mut response = Response::new_200_ok();
        response.set_body_static(b"Hello world\r\n");
        response.set_connection_close();
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}
examples/teapot.rs (line 30)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |request: &mut Request| {
        // Create the response body
        let mut message = b"There are only teapots in ".to_vec();
        message.extend_from_slice(&request.target);
        message.extend_from_slice(b"\r\n");

        // Send the response
        let mut response = Response::new_status_reason(418, "I'm a teapot");
        response.set_body_data(message);
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}
source

pub fn exec<F>(self, callback: F) -> Result<Infallible, Error>where
    F: Fn(&mut Request) -> Response<T> + Send + Sync + 'static,
    T: Read + 'static,

Starts the server

Examples found in repository?
examples/helloworld.rs (line 25)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |_: &mut Request| {
        let mut response = Response::new_200_ok();
        response.set_body_static(b"Hello world\r\n");
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}
More examples
Hide additional examples
examples/helloworld-nokeepalive.rs (line 26)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |_: &mut Request| {
        let mut response = Response::new_200_ok();
        response.set_body_static(b"Hello world\r\n");
        response.set_connection_close();
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}
examples/teapot.rs (line 31)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    // Log everything
    log::set_level(WARN);

    // Define our request handler
    let request_handler = |request: &mut Request| {
        // Create the response body
        let mut message = b"There are only teapots in ".to_vec();
        message.extend_from_slice(&request.target);
        message.extend_from_slice(b"\r\n");

        // Send the response
        let mut response = Response::new_status_reason(418, "I'm a teapot");
        response.set_body_data(message);
        response
    };

    // Create a server that listens at [::]:9999, keeps up to 64 worker threads *permanently* and can spawn up to 1024
    // temporary worker threads under high load
    let server: Server = Server::new("[::]:9999", 64, 4096).expect("failed to start server");
    server.exec(request_handler).expect("server failed");
}

Auto Trait Implementations§

§

impl<T, const STACK_SIZE: usize> RefUnwindSafe for Server<T, STACK_SIZE>

§

impl<T, const STACK_SIZE: usize> Send for Server<T, STACK_SIZE>

§

impl<T = Body, const STACK_SIZE: usize = 65536> !Sync for Server<T, STACK_SIZE>

§

impl<T, const STACK_SIZE: usize> Unpin for Server<T, STACK_SIZE>

§

impl<T, const STACK_SIZE: usize> UnwindSafe for Server<T, STACK_SIZE>

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.