aquatic_http: move request parsing into protocol crate

This commit is contained in:
Joakim Frostegård 2020-07-20 20:13:56 +02:00
parent 2521c89109
commit 95e25710dc
6 changed files with 43 additions and 30 deletions

View file

@ -22,7 +22,6 @@ aquatic_http_protocol = { path = "../aquatic_http_protocol" }
either = "1"
flume = "0.7"
hashbrown = { version = "0.7", features = ["serde"] }
httparse = "1"
indexmap = "1"
itoa = "0.4"
log = "0.4"

View file

@ -9,7 +9,7 @@ use mio::net::TcpStream;
use native_tls::{TlsAcceptor, MidHandshakeTlsStream};
use aquatic_common_tcp::network::stream::Stream;
use aquatic_http_protocol::request::Request;
use aquatic_http_protocol::request::{Request, RequestParseError};
use crate::common::*;
@ -17,10 +17,9 @@ use crate::common::*;
#[derive(Debug)]
pub enum RequestReadError {
NeedMoreData,
Invalid(anyhow::Error),
StreamEnded,
Parse(anyhow::Error),
Io(::std::io::Error),
Parse(::httparse::Error),
}
@ -71,31 +70,20 @@ impl EstablishedConnection {
}
}
let mut headers = [httparse::EMPTY_HEADER; 16];
let mut http_request = httparse::Request::new(&mut headers);
match Request::from_bytes(&self.buf[..self.bytes_read]){
Ok(request) => {
self.clear_buffer();
match http_request.parse(&self.buf[..self.bytes_read]){
Ok(httparse::Status::Complete(_)) => {
if let Some(path) = http_request.path {
let res_request = Request::from_http_get_path(path);
self.clear_buffer();
res_request.map_err(RequestReadError::Invalid)
} else {
self.clear_buffer();
Err(RequestReadError::Invalid(anyhow::anyhow!("no http path")))
}
Ok(request)
},
Ok(httparse::Status::Partial) => {
Err(RequestParseError::NeedMoreData) => {
Err(RequestReadError::NeedMoreData)
},
Err(err) => {
Err(RequestParseError::Invalid(err)) => {
self.clear_buffer();
Err(RequestReadError::Parse(err))
}
},
}
}

View file

@ -227,8 +227,8 @@ pub fn handle_connection_read_event(
// Stop reading data (defer to later events)
break;
},
Err(RequestReadError::Invalid(err)) => {
info!("error reading request (invalid): {}", err);
Err(RequestReadError::Parse(err)) => {
info!("error reading request (invalid): {:#?}", err);
let meta = ConnectionMeta {
worker_index: socket_worker_index,
@ -253,11 +253,6 @@ pub fn handle_connection_read_event(
break
},
Err(RequestReadError::Parse(err)) => {
::log::info!("request httparse error: {}", err);
break
},
Err(RequestReadError::Io(err)) => {
::log::info!("error reading request (io): {}", err);