#http #request-http #http-response #tcp #net #networking #request-response

已删除 kl-http

将TcpStream转换为http请求的辅助crate

使用旧版Rust 2015

0.2.1 2019年2月15日
0.2.0 2018年3月7日
0.1.1 2018年3月6日
0.1.0 2018年3月6日

#10#request-http

MIT 许可证

17KB
231

kl-http

Build Status Latest Version Rust Documentation codecov

一个轻量级的转换器,可以将TcpStream转换为http请求或http响应。

虽然像tokiohyper这样的crate提供了出色的功能和特性,但在处理Futures并将解析成可工作的HTTP请求或响应时,需要额外的工作。此crate专注于将TcpStream简单、易于使用的转换成HTTP请求。它使用http crate来构建http::Requesthttp::Response。它使用标准的Vec<u8>作为请求/响应的主体。


示例

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();

依赖项

~1MB
~17K SLoC