Struct HttpServer

Source
pub struct HttpServer { /* private fields */ }
Expand description

HTTP Server implementation using Unix Domain Sockets and EPOLL to handle multiple connections on the same thread.

The function that handles incoming connections, parses incoming requests and sends responses for awaiting requests is requests. It can be called in a loop, which will render the thread that the server runs on incapable of performing other operations, or it can be used in another EPOLL structure, as it provides its epoll, which is a wrapper over the file descriptor of the epoll structure used within the server, and it can be added to another one using the EPOLLIN flag. Whenever there is a notification on that fd, requests should be called once.

§Example

§Starting and running the server

use dbs_uhttp::{HttpServer, Response, StatusCode};

let path_to_socket = "/tmp/example.sock";
std::fs::remove_file(path_to_socket).unwrap_or_default();

// Start the server.
let mut server = HttpServer::new(path_to_socket).unwrap();
server.start_server().unwrap();

// Connect a client to the server so it doesn't block in our example.
let mut socket = std::os::unix::net::UnixStream::connect(path_to_socket).unwrap();

// Server loop processing requests.
loop {
    for request in server.requests().unwrap() {
        let response = request.process(|request| {
            // Your code here.
            Response::new(request.http_version(), StatusCode::NoContent)
        });
        server.respond(response);
    }
    // Break this example loop.
    break;
}

Implementations§

Source§

impl HttpServer

Source

pub fn new<P: AsRef<Path>>(path_to_socket: P) -> Result<Self, ServerError>

Constructor for HttpServer.

Returns the newly formed HttpServer.

§Errors

Returns an IOError when binding or epoll::create fails.

Source

pub fn new_from_fd(socket_fd: RawFd) -> Result<Self, ServerError>

Constructor for HttpServer.

Note that this function requires the socket_fd to be solely owned and not be associated with another File in the caller as it uses the unsafe UnixListener::from_raw_fd method.

Returns the newly formed HttpServer.

§Errors

Returns an IOError when epoll::create fails.

Source

pub fn set_payload_max_size(&mut self, request_payload_max_size: usize)

This function sets the limit for PUT/PATCH requests. It overwrites the default limit of 0.05MiB with the one allowed by server.

Source

pub fn start_server(&mut self) -> Result<(), ServerError>

Starts the HTTP Server.

Source

pub fn requests(&mut self) -> Result<Vec<ServerRequest>, ServerError>

This function is responsible for the data exchange with the clients and should be called when we are either notified through epoll that we need to exchange data with at least a client or when we don’t need to perform any other operations on this thread and we can afford to call it in a loop.

Note that this function will block the current thread if there are no notifications to be handled by the server.

Returns a collection of complete and valid requests to be processed by the user of the server. Once processed, responses should be sent using enqueue_responses().

§Errors

IOError is returned when read, write or epoll::ctl operations fail. ServerFull is returned when a client is trying to connect to the server, but full capacity has already been reached. InvalidWrite is returned when the server attempted to perform a write operation on a connection on which it is not possible.

Source

pub fn flush_outgoing_writes(&mut self)

This function is responsible with flushing any remaining outgoing requests on the server.

Note that this function can block the thread on write, since the operation is blocking.

Source

pub fn epoll(&self) -> &Poll

The file descriptor of the epoll structure can enable the server to become a non-blocking structure in an application.

Returns a reference to the instance of the server’s internal Poll structure.

Source

pub fn enqueue_responses( &mut self, responses: Vec<ServerResponse>, ) -> Result<(), ServerError>

Enqueues the provided responses in the outgoing connection.

§Errors

IOError is returned when an epoll::ctl operation fails.

Source

pub fn respond(&mut self, response: ServerResponse) -> Result<(), ServerError>

Adds the provided response to the outgoing buffer in the corresponding connection.

§Errors

IOError is returned when an epoll::ctl operation fails. Underflow is returned when enqueue_response fails.

Auto Trait Implementations§

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.