kl-http

Crates.iokl-http
lib.rskl-http
version0.2.1
sourcesrc
created_at2018-03-06 07:42:01.451524
updated_at2019-02-15 18:15:18.208541
descriptionHelper crate for getting TcpStream into a http Request.
homepage
repository
max_upload_size
id54080
size17,840
Kurt Lawrence (kurtlawrence)

documentation

README

kl-http

Build Status Latest Version Rust Documentation codecov

A lightweight converter for taking a TcpStream and converting into a http::Request or http::Response.

While crates such as tokio or hyper offer great functionality and features, there is extra work in handling Futures and parsing into a workable HTTP request or response. This crate is focused on a simple, easy-to-use conversion of a TcpStream into a HTTP request. It uses http crate to construct the http::Request and http::Response. It uses a standard Vec<u8> as the body of the requests/response.


Example

extern crate http;
extern crate kl_http;

use kl_http::{HttpRequest, HttpSerialise};
use std::io::BufReader;
use std::io::Write;

let incoming_request = b"GET / HTTP/1.1\r\nuser-agent: Dart/2.0 (dart:io)\r\ncontent-type: text/plain; charset=utf-8\r\naccept-encoding: gzip\r\ncontent-length: 11\r\nhost: 10.0.2.2:8080\r\n\r\nHello world";

let listener = ::std::net::TcpListener::bind("127.0.0.1:8080").unwrap();
let mut http_request = HttpRequest::from_tcp_stream(listener.accept().unwrap()).unwrap();

println!("{}", String::from_utf8_lossy(&http_request.request.to_http()));

let mut response = http::Response::builder();
response.status(http::StatusCode::OK);
let response = response.body("hello me".as_bytes().to_vec()).unwrap();
http_request.respond(response).unwrap();
Commit count: 0

cargo fmt