Skip to content

Commit

Permalink
examples: fix tinyhttp (tokio-rs#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkbai authored and carllerche committed Dec 3, 2019
1 parent 07451f8 commit 38c3617
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
13 changes: 11 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ edition = "2018"
[dev-dependencies]
tokio = { version = "0.2.0", path = "../tokio", features = ["full"] }
tokio-util = { version = "0.2.0", path = "../tokio-util", features = ["full"] }

bytes = "0.5.0"
bytes = "0.5"
futures = "0.3.0"
http = "0.2"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
httparse = "1.0"
time = "0.1"

[[example]]
name = "chat"
Expand Down Expand Up @@ -50,3 +55,7 @@ path = "udp-client.rs"
[[example]]
name = "udp-codec"
path = "udp-codec.rs"

[[example]]
name = "tinyhttp"
path = "tinyhttp.rs"
33 changes: 18 additions & 15 deletions examples/tinyhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
use bytes::BytesMut;
use futures::{SinkExt, StreamExt};
use http::{header::HeaderValue, Request, Response, StatusCode};
use serde::Serialize;
#[macro_use]
extern crate serde_derive;
use serde_json;
use std::{env, error::Error, fmt, io};
use tokio::{
codec::{Decoder, Encoder, Framed},
net::{TcpListener, TcpStream},
};
use tokio::net::{TcpListener, TcpStream};
use tokio_util::codec::{Decoder, Encoder, Framed};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Parse the arguments, bind the TCP socket we'll be listening to, spin up
// our worker threads, and start shipping sockets to those worker threads.
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());

let mut incoming = TcpListener::bind(&addr).await?.incoming();
let mut server = TcpListener::bind(&addr).await?;
let mut incoming = server.incoming();
println!("Listening on: {}", addr);

while let Some(Ok(stream)) = incoming.next().await {
Expand Down Expand Up @@ -63,11 +63,11 @@ async fn respond(req: Request<()>) -> Result<Response<String>, Box<dyn Error>> {
let mut response = Response::builder();
let body = match req.uri().path() {
"/plaintext" => {
response.header("Content-Type", "text/plain");
response = response.header("Content-Type", "text/plain");
"Hello, World!".to_string()
}
"/json" => {
response.header("Content-Type", "application/json");
response = response.header("Content-Type", "application/json");

#[derive(Serialize)]
struct Message {
Expand All @@ -78,7 +78,7 @@ async fn respond(req: Request<()>) -> Result<Response<String>, Box<dyn Error>> {
})?
}
_ => {
response.status(StatusCode::NOT_FOUND);
response = response.status(StatusCode::NOT_FOUND);
String::new()
}
};
Expand Down Expand Up @@ -196,16 +196,19 @@ impl Decoder for Http {
}
let data = src.split_to(amt).freeze();
let mut ret = Request::builder();
ret.method(&data[method.0..method.1]);
ret.uri(data.slice(path.0, path.1));
ret.version(http::Version::HTTP_11);
ret = ret.method(&data[method.0..method.1]);
let s = data.slice(path.0..path.1);
let s = unsafe { String::from_utf8_unchecked(Vec::from(s.as_ref())) };
ret = ret.uri(s);
ret = ret.version(http::Version::HTTP_11);
for header in headers.iter() {
let (k, v) = match *header {
Some((ref k, ref v)) => (k, v),
None => break,
};
let value = unsafe { HeaderValue::from_shared_unchecked(data.slice(v.0, v.1)) };
ret.header(&data[k.0..k.1], value);
let value = HeaderValue::from_bytes(data.slice(v.0..v.1).as_ref())
.map_err(|_| io::Error::new(io::ErrorKind::Other, "header decode error"))?;
ret = ret.header(&data[k.0..k.1], value);
}

let req = ret
Expand Down

0 comments on commit 38c3617

Please sign in to comment.