<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>http_str</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<style>
.task-list-item { list-style-type: none; } .task-list-item-checkbox { margin-left: -20px; vertical-align: middle; }
</style>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', 'Ubuntu', 'Droid Sans', sans-serif;
font-size: 14px;
line-height: 1.6;
}
</style>
</head>
<body class="vscode-light">
<h1 id="httpstr">http_str</h1>
<p>This is a Rust libray that provides some types for parsing http request and create responses.</p>
<h2 id="usage">Usage</h2>
<pre><code class="language-rust"><div><span class="hljs-keyword">use</span> std::net::{TcpListener, TcpStream};
<span class="hljs-keyword">use</span> std::io::prelude::*;
<span class="hljs-keyword">use</span> std::io;
<span class="hljs-keyword">use</span> http_str::Request;
<span class="hljs-keyword">use</span> std::fs::File;
<span class="hljs-keyword">use</span> std::collections::BTreeMap;
<span class="hljs-keyword">use</span> std::<span class="hljs-built_in">str</span>::from_utf8;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() -> io::<span class="hljs-built_in">Result</span><()> {
<span class="hljs-keyword">let</span> tcp_listener = TcpListener::bind(<span class="hljs-string">"127.0.0.1:7878"</span>)?;
<span class="hljs-keyword">for</span> stream <span class="hljs-keyword">in</span> tcp_listener.incoming() {
<span class="hljs-keyword">let</span> stream = stream?;
handle_connection(stream)?;
}
}
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">handle_connection</span></span>(stream: TcpStream) -> io::<span class="hljs-built_in">Result</span><()> {
<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> buffer = [<span class="hljs-number">0</span>; <span class="hljs-number">512</span>];
stream.read_exact(&<span class="hljs-keyword">mut</span> buffer)?;
<span class="hljs-keyword">let</span> request = Request::new(&buffer[..]);
<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> body = [<span class="hljs-number">0</span>; <span class="hljs-number">512</span>];
<span class="hljs-keyword">let</span> file = File::open(request.uri)?;
file.read_exact(&<span class="hljs-keyword">mut</span> body)?;
<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> string = <span class="hljs-built_in">String</span>::new();
<span class="hljs-keyword">let</span> response = Response::new(request.version,
<span class="hljs-number">200</span>,
BTreeMap::new(), <span class="hljs-comment">// headers</span>
from_utf8(&body[..]).unwrap(),
&<span class="hljs-keyword">mut</span> string);
stream.write_all(response.as_bytes())?;
stream.flush()?;
<span class="hljs-literal">Ok</span>(())
}
</div></code></pre>
</body>
</html>