Module http_io::server

source ·
Expand description

A very simple HTTP server. It is not suitable for production workloads. Users should write their own request handler which implements the HttpRequestHandler trait.

File Server Example

use std::io;
use std::net;
use std::path::PathBuf;
use std::thread;

use http_io::error::{Error, Result};
use http_io::protocol::{HttpBody, HttpResponse, HttpStatus};
use http_io::server::{HttpRequestHandler, HttpServer};

struct FileHandler {
    file_root: PathBuf,
}

impl FileHandler {
    fn new<P: Into<PathBuf>>(file_root: P) -> Self {
        FileHandler {
            file_root: file_root.into(),
        }
    }
}

impl<I: io::Read> HttpRequestHandler<I> for FileHandler {
    type Error = Error;
    fn get<'a>(
        &'a mut self,
        uri: String,
    ) -> Result<HttpResponse<Box<dyn io::Read + 'a>>> {
        let path = self.file_root.join(uri.trim_start_matches("/"));
        Ok(HttpResponse::new(
            HttpStatus::OK,
            Box::new(std::fs::File::open(path)?),
        ))
    }

    fn put<'a>(
        &'a mut self,
        uri: String,
        mut stream: HttpBody<&mut I>,
    ) -> Result<HttpResponse<Box<dyn io::Read + 'a>>> {
        let path = self.file_root.join(uri.trim_start_matches("/"));
        let mut file = std::fs::File::create(path)?;
        io::copy(&mut stream, &mut file)?;
        Ok(HttpResponse::new(HttpStatus::OK, Box::new(io::empty())))
    }
}

fn main() -> Result<()> {
    let socket = net::TcpListener::bind("127.0.0.1:0")?;
    let port = socket.local_addr()?.port();
    let handle: thread::JoinHandle<Result<()>> = thread::spawn(move || {
        let handler = FileHandler::new(std::env::current_dir()?);
        let mut server = HttpServer::new(socket, handler);
        server.serve_one()?;
        Ok(())
    });

    let url = format!("http://localhost:{}/src/server.rs", port);
    let mut body = http_io::client::get(url.as_ref())?;
    io::copy(&mut body, &mut std::io::stdout())?;
    handle.join().unwrap()?;

    Ok(())
}

Structs

A simple HTTP server. Not suited for production workloads, better used in tests and small projects.

Traits

Represents the ability to service and respond to HTTP requests.
Represents the ability to accept a new abstract connection.