1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

optimize payload type detection

This commit is contained in:
Nikolay Kim 2017-12-24 14:29:19 -08:00
parent 9f9c75d832
commit ddd9c24bb2

View File

@ -20,7 +20,6 @@ use h1writer::{Writer, H1Writer};
use server::WorkerSettings; use server::WorkerSettings;
use httpcodes::HTTPNotFound; use httpcodes::HTTPNotFound;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use helpers::SharedHttpMessage;
use error::{ParseError, PayloadError, ResponseError}; use error::{ParseError, PayloadError, ResponseError};
use payload::{Payload, PayloadWriter, DEFAULT_BUFFER_SIZE}; use payload::{Payload, PayloadWriter, DEFAULT_BUFFER_SIZE};
@ -47,7 +46,6 @@ bitflags! {
} }
} }
pub(crate) enum Http1Result { pub(crate) enum Http1Result {
Done, Done,
Switch, Switch,
@ -583,58 +581,41 @@ impl Reader {
msg msg
}; };
let decoder = if upgrade(&msg) { let decoder = if let Some(len) = msg.get_ref().headers.get(header::CONTENT_LENGTH) {
Decoder::eof() // Content-Length
} else { if let Ok(s) = len.to_str() {
let has_len = msg.get_mut().headers.contains_key(header::CONTENT_LENGTH); if let Ok(len) = s.parse::<u64>() {
Some(Decoder::length(len))
// Chunked encoding
if chunked(&msg.get_mut().headers)? {
if has_len {
return Err(ParseError::Header)
}
Decoder::chunked()
} else {
if !has_len {
return Ok(Message::Http1(HttpRequest::from_message(msg), None))
}
// Content-Length
let len = msg.get_mut().headers.get(header::CONTENT_LENGTH).unwrap();
if let Ok(s) = len.to_str() {
if let Ok(len) = s.parse::<u64>() {
Decoder::length(len)
} else {
debug!("illegal Content-Length: {:?}", len);
return Err(ParseError::Header)
}
} else { } else {
debug!("illegal Content-Length: {:?}", len); debug!("illegal Content-Length: {:?}", len);
return Err(ParseError::Header) return Err(ParseError::Header)
} }
} else {
debug!("illegal Content-Length: {:?}", len);
return Err(ParseError::Header)
} }
}; } else if chunked(&msg.get_mut().headers)? {
// Chunked encoding
let (psender, payload) = Payload::new(false); Some(Decoder::chunked())
let info = PayloadInfo { } else if msg.get_ref().headers.contains_key(header::UPGRADE) ||
tx: PayloadType::new(&msg.get_mut().headers, psender),
decoder: decoder,
};
msg.get_mut().payload = Some(payload);
Ok(Message::Http1(HttpRequest::from_message(msg), Some(info)))
}
}
/// Check if request is UPGRADE
fn upgrade(msg: &SharedHttpMessage) -> bool {
if let Some(conn) = msg.get_ref().headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() {
s.to_lowercase().contains("upgrade")
} else {
msg.get_ref().method == Method::CONNECT msg.get_ref().method == Method::CONNECT
{
Some(Decoder::eof())
} else {
None
};
if let Some(decoder) = decoder {
let (psender, payload) = Payload::new(false);
let info = PayloadInfo {
tx: PayloadType::new(&msg.get_mut().headers, psender),
decoder: decoder,
};
msg.get_mut().payload = Some(payload);
Ok(Message::Http1(HttpRequest::from_message(msg), Some(info)))
} else {
Ok(Message::Http1(HttpRequest::from_message(msg), None))
} }
} else {
msg.get_ref().method == Method::CONNECT
} }
} }