From d46c844bb9777673dc9b894f438a8eaeb0811fc1 Mon Sep 17 00:00:00 2001 From: Ivan Zderadicka Date: Mon, 20 Mar 2023 18:52:38 +0100 Subject: [PATCH] examples: fix panic in tinyhttp example (#5328) As method in httparse result is no longer part of input buffer it needs to be handled separately. --- examples/tinyhttp.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/tinyhttp.rs b/examples/tinyhttp.rs index fa0bc6695..44534c6c1 100644 --- a/examples/tinyhttp.rs +++ b/examples/tinyhttp.rs @@ -18,7 +18,7 @@ use futures::SinkExt; use http::{header::HeaderValue, Request, Response, StatusCode}; #[macro_use] extern crate serde_derive; -use std::{env, error::Error, fmt, io}; +use std::{convert::TryFrom, env, error::Error, fmt, io}; use tokio::net::{TcpListener, TcpStream}; use tokio_stream::StreamExt; use tokio_util::codec::{Decoder, Encoder, Framed}; @@ -180,8 +180,11 @@ impl Decoder for Http { headers[i] = Some((k, v)); } + let method = http::Method::try_from(r.method.unwrap()) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + ( - toslice(r.method.unwrap().as_bytes()), + method, toslice(r.path.unwrap().as_bytes()), r.version.unwrap(), amt, @@ -195,7 +198,7 @@ impl Decoder for Http { } let data = src.split_to(amt).freeze(); let mut ret = Request::builder(); - ret = ret.method(&data[method.0..method.1]); + ret = ret.method(method); let s = data.slice(path.0..path.1); let s = unsafe { String::from_utf8_unchecked(Vec::from(s.as_ref())) }; ret = ret.uri(s);