mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-28 01:52:57 +01:00
optimize payload type detection
This commit is contained in:
parent
9f9c75d832
commit
ddd9c24bb2
45
src/h1.rs
45
src/h1.rs
@ -20,7 +20,6 @@ use h1writer::{Writer, H1Writer};
|
||||
use server::WorkerSettings;
|
||||
use httpcodes::HTTPNotFound;
|
||||
use httprequest::HttpRequest;
|
||||
use helpers::SharedHttpMessage;
|
||||
use error::{ParseError, PayloadError, ResponseError};
|
||||
use payload::{Payload, PayloadWriter, DEFAULT_BUFFER_SIZE};
|
||||
|
||||
@ -47,7 +46,6 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(crate) enum Http1Result {
|
||||
Done,
|
||||
Switch,
|
||||
@ -583,27 +581,11 @@ impl Reader {
|
||||
msg
|
||||
};
|
||||
|
||||
let decoder = if upgrade(&msg) {
|
||||
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))
|
||||
}
|
||||
|
||||
let decoder = if let Some(len) = msg.get_ref().headers.get(header::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(len) = s.parse::<u64>() {
|
||||
Decoder::length(len)
|
||||
Some(Decoder::length(len))
|
||||
} else {
|
||||
debug!("illegal Content-Length: {:?}", len);
|
||||
return Err(ParseError::Header)
|
||||
@ -612,9 +594,18 @@ impl Reader {
|
||||
debug!("illegal Content-Length: {:?}", len);
|
||||
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 info = PayloadInfo {
|
||||
tx: PayloadType::new(&msg.get_mut().headers, psender),
|
||||
@ -622,19 +613,9 @@ impl Reader {
|
||||
};
|
||||
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
|
||||
Ok(Message::Http1(HttpRequest::from_message(msg), None))
|
||||
}
|
||||
} else {
|
||||
msg.get_ref().method == Method::CONNECT
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user