#http

fibers_http_server

A tiny HTTP/1.1 server framework

14 releases

0.2.0 Jul 6, 2020
0.1.12 Dec 5, 2019
0.1.11 Aug 18, 2019
0.1.10 Feb 4, 2019
0.1.0 Apr 30, 2018

#72 in HTTP server

Download history 23/week @ 2020-11-06 53/week @ 2020-11-13 66/week @ 2020-11-20 45/week @ 2020-11-27 41/week @ 2020-12-04 18/week @ 2020-12-11 8/week @ 2020-12-18 1/week @ 2020-12-25 18/week @ 2021-01-01 36/week @ 2021-01-08 53/week @ 2021-01-15 44/week @ 2021-01-22 50/week @ 2021-01-29 95/week @ 2021-02-05 100/week @ 2021-02-12 52/week @ 2021-02-19

412 downloads per month
Used in 5 crates

MIT license

72KB
1.5K SLoC

fibers_http_server

fibers_http_server Documentation Build Status Code Coverage License: MIT

A tiny HTTP/1.1 server framework for Rust.

Documentation

Examples

use std::io::{Read, Write};
use std::net::TcpStream;
use std::thread;
use std::time::Duration;
use bytecodec::bytes::Utf8Encoder;
use bytecodec::value::NullDecoder;
use fibers::{Executor, Spawn, InPlaceExecutor};
use fibers_http_server::{HandleRequest, Reply, Req, Res, ServerBuilder, Status};
use futures::future::{ok, Future};
use httpcodec::{BodyDecoder, BodyEncoder};

// Request handler
struct Hello;
impl HandleRequest for Hello {
    const METHOD: &'static str = "GET";
    const PATH: &'static str = "/hello";

    type ReqBody = ();
    type ResBody = String;
    type Decoder = BodyDecoder<NullDecoder>;
    type Encoder = BodyEncoder<Utf8Encoder>;
    type Reply = Reply<Self::ResBody>;

    fn handle_request(&self, _req: Req<Self::ReqBody>) -> Self::Reply {
        Box::new(ok(Res::new(Status::Ok, "hello".to_owned())))
    }
}

let addr = "127.0.0.1:14758".parse().unwrap();

// HTTP server
thread::spawn(move || {
    let executor = InPlaceExecutor::new().unwrap();
    let mut builder = ServerBuilder::new(addr);
    builder.add_handler(Hello).unwrap();
    let server = builder.finish(executor.handle());
    executor.spawn(server.map_err(|e| panic!("{}", e)));
    executor.run().unwrap()
});
thread::sleep(Duration::from_millis(100));

// HTTP client
let mut client = TcpStream::connect(addr).unwrap();
client
    .write_all(b"GET /hello HTTP/1.1\r\nContent-Length: 0\r\n\r\n")
    .unwrap();
thread::sleep(Duration::from_millis(100));

let mut buf = [0; 1024];
let size = client.read(&mut buf).unwrap();
assert_eq!(
    &buf[..size],
    b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello".as_ref()
);

Dependencies

~4MB
~106K SLoC