Module hreq_h1::server [−][src]
Server implementation of the HTTP/1.1 protocol.
Example
Example
use hreq_h1::server; use std::error::Error; use async_std::net::TcpListener; use http::{Response, StatusCode}; #[async_std::main] async fn main() -> Result<(), Box<dyn Error>> { let mut listener = TcpListener::bind("127.0.0.1:3000").await?; // Accept all incoming TCP connections. loop { if let Ok((socket, _peer_addr)) = listener.accept().await { // Spawn a new task to process each connection individually async_std::task::spawn(async move { let mut h1 = server::handshake(socket); // Handle incoming requests from this socket, one by one. while let Some(request) = h1.accept().await { let (req, mut respond) = request.unwrap(); println!("Receive request: {:?}", req); // Build a response with no body, since // that is sent later. let response = Response::builder() .status(StatusCode::OK) .body(()) .unwrap(); // Send the response back to the client let mut send_body = respond .send_response(response, false).await.unwrap(); send_body.send_data(b"Hello world!", true) .await.unwrap(); } }); } } Ok(()) }
Structs
Connection | Server connection for accepting incoming requests. |
SendResponse | Handle to send a response and body back for a single request. |
Functions
handshake | “handshake” to create a connection. |