Server

Struct Server 

Source
pub struct Server<const STACK_SIZE: usize> { /* private fields */ }
Expand description

A threadpool-based HTTP server

Implementations§

Source§

impl<const STACK_SIZE: usize> Server<STACK_SIZE>

Source

pub fn with_source_sink<F>(workers_max: usize, source_sink_handler: F) -> Self
where F: Fn(&mut Source, &mut Sink) -> bool + Send + Sync + 'static,

Creates a new server with the given connection handler

Source

pub fn with_request_response<F>( workers_max: usize, request_response_handler: F, ) -> Self
where F: Fn(Request<'_>) -> Response + Send + Sync + 'static,

Creates a new server with the given connection handler

Examples found in repository?
examples/helloworld.rs (lines 7-11)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |_| {
8        let mut response = Response::new_200_ok();
9        response.set_body_data(b"Hello world\r\n");
10        response
11    });
12
13    // Handle incoming connections
14    server.accept("[::]:9999").expect("server failed");
15}
More examples
Hide additional examples
examples/helloworld-nokeepalive.rs (lines 7-12)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |_| {
8        let mut response = Response::new_200_ok();
9        response.set_body_data(b"Hello world\r\n");
10        response.set_connection_close();
11        response
12    });
13
14    // Handle incoming connections
15    server.accept("[::]:9999").expect("server failed");
16}
examples/teapot.rs (lines 7-17)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |request| {
8        // Create the response body
9        let mut message = b"There are only teapots in ".to_vec();
10        message.extend_from_slice(&request.target);
11        message.extend_from_slice(b"\r\n");
12
13        // Send the response
14        let mut response = Response::new_status_reason(418, "I'm a teapot");
15        response.set_body_data(message);
16        response
17    });
18
19    // Handle incoming connections
20    server.accept("[::]:9999").expect("server failed");
21}
Source

pub fn dispatch(&self, source: Source, sink: Sink) -> Result<(), Error>

Manually dispatches a connection

Source

pub fn accept<A>(self, address: A) -> Result<Infallible, Error>
where A: ToSocketAddrs,

Listens on the given address and accepts forever

Examples found in repository?
examples/helloworld.rs (line 14)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |_| {
8        let mut response = Response::new_200_ok();
9        response.set_body_data(b"Hello world\r\n");
10        response
11    });
12
13    // Handle incoming connections
14    server.accept("[::]:9999").expect("server failed");
15}
More examples
Hide additional examples
examples/helloworld-nokeepalive.rs (line 15)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |_| {
8        let mut response = Response::new_200_ok();
9        response.set_body_data(b"Hello world\r\n");
10        response.set_connection_close();
11        response
12    });
13
14    // Handle incoming connections
15    server.accept("[::]:9999").expect("server failed");
16}
examples/teapot.rs (line 20)
2fn main() {
3    use ehttpd::Server;
4    use ehttpd::http::{Response, ResponseExt};
5
6    // Create a server that listens at [::]:9999 with up to 2048 worker threads under load if necessary
7    let server = Server::with_request_response(2048, |request| {
8        // Create the response body
9        let mut message = b"There are only teapots in ".to_vec();
10        message.extend_from_slice(&request.target);
11        message.extend_from_slice(b"\r\n");
12
13        // Send the response
14        let mut response = Response::new_status_reason(418, "I'm a teapot");
15        response.set_body_data(message);
16        response
17    });
18
19    // Handle incoming connections
20    server.accept("[::]:9999").expect("server failed");
21}

Auto Trait Implementations§

§

impl<const STACK_SIZE: usize> Freeze for Server<STACK_SIZE>

§

impl<const STACK_SIZE: usize> !RefUnwindSafe for Server<STACK_SIZE>

§

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

§

impl<const STACK_SIZE: usize> Sync for Server<STACK_SIZE>

§

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

§

impl<const STACK_SIZE: usize> !UnwindSafe for Server<STACK_SIZE>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.