[][src]Struct solicit::server::SimpleServer

pub struct SimpleServer<TS, H> where
    TS: TransportStream,
    H: FnMut(ServerRequest) -> Response<'static, 'static>, 
{ /* fields omitted */ }

The struct implements a simple HTTP/2 server that allows users to register a request handler (a callback taking a ServerRequest and returning a Response) which is run on all received requests.

The handle_next method needs to be called regularly in order to have the server process received frames, as well as send out the responses.

This is an exceedingly simple implementation of an HTTP/2 server and is mostly an example of how the solicit::http API can be used to make one.

Examples

extern crate solicit;
use std::str;
use std::net::{TcpListener, TcpStream};
use std::thread;

use solicit::server::SimpleServer;

use solicit::http::{Response, Header};

fn main() {
    fn handle_client(stream: TcpStream) {
        let mut server = SimpleServer::new(stream, |req| {
            println!("Received request:");
            for header in req.headers.iter() {
                println!("  {}: {}",
                str::from_utf8(header.name()).unwrap(),
                str::from_utf8(header.value()).unwrap());
            }
            println!("Body:\n{}", str::from_utf8(&req.body).unwrap());

            // Return a dummy response for every request
            Response {
                headers: vec![
                    Header::new(b":status", b"200"),
                    Header::new(b"x-solicit".to_vec(), b"Hello, World!".to_vec()),
                ],
                body: vec![65],
                stream_id: req.stream_id,
           }
        }).unwrap();
        while let Ok(_) = server.handle_next() {}
        println!("Server done (client disconnected)");
    }

    let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
    for stream in listener.incoming() {
        let stream = stream.unwrap();
        thread::spawn(move || {
            handle_client(stream)
        });
    }
}

Implementations

impl<TS, H> SimpleServer<TS, H> where
    TS: TransportStream,
    H: FnMut(ServerRequest) -> Response<'static, 'static>, 
[src]

pub fn new(stream: TS, handler: H) -> HttpResult<SimpleServer<TS, H>>[src]

Creates a new SimpleServer that will use the given TransportStream to communicate to the client. Assumes that the stream is fully uninitialized -- no preface sent or read yet.

pub fn handle_next(&mut self) -> HttpResult<()>[src]

Handles the next incoming frame, blocking to receive it if nothing is available on the underlying stream.

Handling the frame can trigger the handler callback. Any responses returned by the handler are immediately flushed out to the client (blocking the call until it's done).

Auto Trait Implementations

impl<TS, H> RefUnwindSafe for SimpleServer<TS, H> where
    H: RefUnwindSafe,
    TS: RefUnwindSafe

impl<TS, H> Send for SimpleServer<TS, H> where
    H: Send,
    TS: Send

impl<TS, H> Sync for SimpleServer<TS, H> where
    H: Sync,
    TS: Sync

impl<TS, H> Unpin for SimpleServer<TS, H> where
    H: Unpin,
    TS: Unpin

impl<TS, H> UnwindSafe for SimpleServer<TS, H> where
    H: UnwindSafe,
    TS: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.