Struct oxhttp::Server

source ·
pub struct Server { /* private fields */ }
Available on crate feature server only.
Expand description

An HTTP server.

It uses a very simple threading mechanism: a new thread is started on each connection and closed when the client connection is closed. To avoid crashes it is possible to set an upper bound to the number of concurrent connections using the Server::with_max_concurrent_connections function.

use std::net::{Ipv4Addr, Ipv6Addr};
use oxhttp::Server;
use oxhttp::model::{Response, Status};
use std::time::Duration;

// Builds a new server that returns a 404 everywhere except for "/" where it returns the body 'home'
let mut server = Server::new(|request| {
    if request.url().path() == "/" {
        Response::builder(Status::OK).with_body("home")
    } else {
        Response::builder(Status::NOT_FOUND).build()
    }
});
// We bind the server to localhost on both IPv4 and v6
server = server.bind((Ipv4Addr::LOCALHOST, 8080)).bind((Ipv6Addr::LOCALHOST, 8080));
// Raise a timeout error if the client does not respond after 10s.
server = server.with_global_timeout(Duration::from_secs(10));
// Limits the number of concurrent connections to 128.
server = server.with_max_concurrent_connections(128);
// We spawn the server and block on it
server.spawn()?.join()?;

Implementations§

source§

impl Server

source

pub fn new( on_request: impl Fn(&mut Request) -> Response + Send + Sync + 'static ) -> Self

Builds the server using the given on_request method that builds a Response from a given Request.

source

pub fn bind(self, addr: impl Into<SocketAddr>) -> Self

Ask the server to listen to a given socket when spawned.

source

pub fn with_server_name( self, server: impl Into<String> ) -> Result<Self, InvalidHeader>

Sets the default value for the Server header.

source

pub fn with_global_timeout(self, timeout: Duration) -> Self

Sets the global timeout value (applies to both read and write).

source

pub fn with_max_concurrent_connections(self, max_num_thread: usize) -> Self

Sets the number maximum number of threads this server can spawn.

source

pub fn spawn(self) -> Result<ListeningServer>

Spawns the server by listening to the given addresses.

Note that this is not blocking. To wait for the server to terminate indefinitely, call join on the result.

Auto Trait Implementations§

§

impl Freeze for Server

§

impl !RefUnwindSafe for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl !UnwindSafe for Server

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>,

§

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>,

§

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.