1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-27 12:01:15 +00: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,27 +581,11 @@ 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()
} else {
let has_len = msg.get_mut().headers.contains_key(header::CONTENT_LENGTH);
// 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 // Content-Length
let len = msg.get_mut().headers.get(header::CONTENT_LENGTH).unwrap();
if let Ok(s) = len.to_str() { if let Ok(s) = len.to_str() {
if let Ok(len) = s.parse::<u64>() { if let Ok(len) = s.parse::<u64>() {
Decoder::length(len) Some(Decoder::length(len))
} else { } else {
debug!("illegal Content-Length: {:?}", len); debug!("illegal Content-Length: {:?}", len);
return Err(ParseError::Header) return Err(ParseError::Header)
@ -612,9 +594,18 @@ impl Reader {
debug!("illegal Content-Length: {:?}", len); debug!("illegal Content-Length: {:?}", len);
return Err(ParseError::Header) return Err(ParseError::Header)
} }
} } else if chunked(&msg.get_mut().headers)? {
// Chunked encoding
Some(Decoder::chunked())
} else if msg.get_ref().headers.contains_key(header::UPGRADE) ||
msg.get_ref().method == Method::CONNECT
{
Some(Decoder::eof())
} else {
None
}; };
if let Some(decoder) = decoder {
let (psender, payload) = Payload::new(false); let (psender, payload) = Payload::new(false);
let info = PayloadInfo { let info = PayloadInfo {
tx: PayloadType::new(&msg.get_mut().headers, psender), tx: PayloadType::new(&msg.get_mut().headers, psender),
@ -622,19 +613,9 @@ impl Reader {
}; };
msg.get_mut().payload = Some(payload); msg.get_mut().payload = Some(payload);
Ok(Message::Http1(HttpRequest::from_message(msg), Some(info))) 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 { } else {
msg.get_ref().method == Method::CONNECT Ok(Message::Http1(HttpRequest::from_message(msg), None))
} }
} else {
msg.get_ref().method == Method::CONNECT
} }
} }