0.1.3 |
|
---|---|
0.1.2 |
|
0.1.1 |
|
0.1.0 |
|
#100 in #pin
24KB
408 lines
http_str
This is a Rust libray that provides some types for parsing http request and create responses.
Usage
use std::net::{TcpListener, TcpStream};
use std::io::prelude::*;
use std::io;
use http_str::Request;
use std::fs::File;
use std::collections::BTreeMap;
use std::str::from_utf8;
fn main() -> io::Result<()> {
let tcp_listener = TcpListener::bind("127.0.0.1:7878")?;
for stream in tcp_listener.incoming() {
let stream = stream?;
handle_connection(stream)?;
}
}
fn handle_connection(stream: TcpStream) -> io::Result<()> {
let mut buffer = [0; 512];
stream.read_exact(&mut buffer)?;
let request = Request::new(&buffer[..]);
let mut body = [0; 512];
let file = File::open(request.uri)?;
file.read_exact(&mut body)?;
let mut string = String::new();
let response = Response::new(request.version,
200,
BTreeMap::new(), // headers
from_utf8(&body[..]).unwrap(),
&mut string);
stream.write_all(response.as_bytes())?;
stream.flush()?;
Ok(())
}
Dependencies
~10KB