使用旧版Rust 2015
0.2.1 |
|
---|---|
0.2.0 |
|
0.1.1 |
|
0.1.0 |
|
#10 在 #request-http
17KB
231 行
kl-http
一个轻量级的转换器,可以将TcpStream转换为http请求或http响应。
虽然像tokio
或hyper
这样的crate提供了出色的功能和特性,但在处理Futures
并将解析成可工作的HTTP请求或响应时,需要额外的工作。此crate专注于将TcpStream简单、易于使用的转换成HTTP请求。它使用http
crate来构建http::Request
和http::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