[−][src]Module http_io::server
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::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 { fn get( &self, uri: String, _stream: HttpBody<&mut I>, ) -> Result<HttpResponse<Box<dyn io::Read>>> { let path = self.file_root.join(uri.trim_start_matches("/")); Ok(HttpResponse::new( HttpStatus::OK, Box::new(std::fs::File::open(path)?), )) } fn put( &self, uri: String, mut stream: HttpBody<&mut I>, ) -> Result<HttpResponse<Box<dyn io::Read>>> { 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 handle: thread::JoinHandle<Result<()>> = thread::spawn(|| { let handler = FileHandler::new(std::env::current_dir()?); let socket = net::TcpListener::bind("127.0.0.1:8080")?; let server = HttpServer::new(socket, handler); server.serve_one()?; Ok(()) }); let mut body = http_io::client::get("http://localhost:8080/src/server.rs")?; io::copy(&mut body, &mut std::io::stdout())?; handle.join().unwrap()?; Ok(()) }
Structs
HttpServer | A simple HTTP server. Not suited for production workloads, better used in tests and small projects. |
Traits
HttpRequestHandler | Represents the ability to service and respond to HTTP requests. |
Listen | Represents the ability to accept a new abstract connection. |