mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 22:49:21 +02:00
use custom headers map; more optimizations
This commit is contained in:
10
src/info.rs
10
src/info.rs
@ -33,7 +33,7 @@ impl ConnectionInfo {
|
||||
let peer = None;
|
||||
|
||||
// load forwarded header
|
||||
for hdr in req.headers.get_all(header::FORWARDED) {
|
||||
for hdr in req.headers.get_all(&header::FORWARDED) {
|
||||
if let Ok(val) = hdr.to_str() {
|
||||
for pair in val.split(';') {
|
||||
for el in pair.split(',') {
|
||||
@ -69,7 +69,7 @@ impl ConnectionInfo {
|
||||
if scheme.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
scheme = h.split(',').next().map(|v| v.trim());
|
||||
@ -87,14 +87,14 @@ impl ConnectionInfo {
|
||||
if host.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
host = h.split(',').next().map(|v| v.trim());
|
||||
}
|
||||
}
|
||||
if host.is_none() {
|
||||
if let Some(h) = req.headers.get(header::HOST) {
|
||||
if let Some(h) = req.headers.get(&header::HOST) {
|
||||
host = h.to_str().ok();
|
||||
}
|
||||
if host.is_none() {
|
||||
@ -110,7 +110,7 @@ impl ConnectionInfo {
|
||||
if remote.is_none() {
|
||||
if let Some(h) = req
|
||||
.headers
|
||||
.get(HeaderName::from_lowercase(X_FORWARDED_FOR).unwrap())
|
||||
.get(&HeaderName::from_lowercase(X_FORWARDED_FOR).unwrap())
|
||||
{
|
||||
if let Ok(h) = h.to_str() {
|
||||
remote = h.split(',').next().map(|v| v.trim());
|
||||
|
@ -109,7 +109,7 @@ where
|
||||
|
||||
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
|
||||
// negotiate content-encoding
|
||||
let encoding = if let Some(val) = req.headers().get(ACCEPT_ENCODING) {
|
||||
let encoding = if let Some(val) = req.headers().get(&ACCEPT_ENCODING) {
|
||||
if let Ok(enc) = val.to_str() {
|
||||
AcceptEncoding::parse(enc, self.encoding)
|
||||
} else {
|
||||
|
@ -584,7 +584,7 @@ struct Inner {
|
||||
|
||||
impl Inner {
|
||||
fn validate_origin(&self, req: &RequestHead) -> Result<(), CorsError> {
|
||||
if let Some(hdr) = req.headers().get(header::ORIGIN) {
|
||||
if let Some(hdr) = req.headers().get(&header::ORIGIN) {
|
||||
if let Ok(origin) = hdr.to_str() {
|
||||
return match self.origins {
|
||||
AllOrSome::All => Ok(()),
|
||||
@ -608,7 +608,7 @@ impl Inner {
|
||||
AllOrSome::All => {
|
||||
if self.send_wildcard {
|
||||
Some(HeaderValue::from_static("*"))
|
||||
} else if let Some(origin) = req.headers().get(header::ORIGIN) {
|
||||
} else if let Some(origin) = req.headers().get(&header::ORIGIN) {
|
||||
Some(origin.clone())
|
||||
} else {
|
||||
None
|
||||
@ -617,7 +617,7 @@ impl Inner {
|
||||
AllOrSome::Some(ref origins) => {
|
||||
if let Some(origin) =
|
||||
req.headers()
|
||||
.get(header::ORIGIN)
|
||||
.get(&header::ORIGIN)
|
||||
.filter(|o| match o.to_str() {
|
||||
Ok(os) => origins.contains(os),
|
||||
_ => false,
|
||||
@ -632,7 +632,7 @@ impl Inner {
|
||||
}
|
||||
|
||||
fn validate_allowed_method(&self, req: &RequestHead) -> Result<(), CorsError> {
|
||||
if let Some(hdr) = req.headers().get(header::ACCESS_CONTROL_REQUEST_METHOD) {
|
||||
if let Some(hdr) = req.headers().get(&header::ACCESS_CONTROL_REQUEST_METHOD) {
|
||||
if let Ok(meth) = hdr.to_str() {
|
||||
if let Ok(method) = Method::try_from(meth) {
|
||||
return self
|
||||
@ -653,7 +653,7 @@ impl Inner {
|
||||
AllOrSome::All => Ok(()),
|
||||
AllOrSome::Some(ref allowed_headers) => {
|
||||
if let Some(hdr) =
|
||||
req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS)
|
||||
req.headers().get(&header::ACCESS_CONTROL_REQUEST_HEADERS)
|
||||
{
|
||||
if let Ok(headers) = hdr.to_str() {
|
||||
let mut hdrs = HashSet::new();
|
||||
@ -720,7 +720,7 @@ where
|
||||
.unwrap(),
|
||||
)
|
||||
} else if let Some(hdr) =
|
||||
req.headers().get(header::ACCESS_CONTROL_REQUEST_HEADERS)
|
||||
req.headers().get(&header::ACCESS_CONTROL_REQUEST_HEADERS)
|
||||
{
|
||||
Some(hdr.clone())
|
||||
} else {
|
||||
@ -759,7 +759,7 @@ where
|
||||
.into_body();
|
||||
|
||||
Either::A(ok(req.into_response(res)))
|
||||
} else if req.headers().contains_key(header::ORIGIN) {
|
||||
} else if req.headers().contains_key(&header::ORIGIN) {
|
||||
// Only check requests with a origin header.
|
||||
if let Err(e) = self.inner.validate_origin(req.head()) {
|
||||
return Either::A(ok(req.error_response(e)));
|
||||
@ -790,7 +790,7 @@ where
|
||||
}
|
||||
if inner.vary_header {
|
||||
let value =
|
||||
if let Some(hdr) = res.headers_mut().get(header::VARY) {
|
||||
if let Some(hdr) = res.headers_mut().get(&header::VARY) {
|
||||
let mut val: Vec<u8> =
|
||||
Vec::with_capacity(hdr.as_bytes().len() + 8);
|
||||
val.extend(hdr.as_bytes());
|
||||
@ -893,20 +893,20 @@ mod tests {
|
||||
assert_eq!(
|
||||
&b"*"[..],
|
||||
resp.headers()
|
||||
.get(header::ACCESS_CONTROL_ALLOW_ORIGIN)
|
||||
.get(&header::ACCESS_CONTROL_ALLOW_ORIGIN)
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
);
|
||||
assert_eq!(
|
||||
&b"3600"[..],
|
||||
resp.headers()
|
||||
.get(header::ACCESS_CONTROL_MAX_AGE)
|
||||
.get(&header::ACCESS_CONTROL_MAX_AGE)
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
);
|
||||
let hdr = resp
|
||||
.headers()
|
||||
.get(header::ACCESS_CONTROL_ALLOW_HEADERS)
|
||||
.get(&header::ACCESS_CONTROL_ALLOW_HEADERS)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
|
@ -131,11 +131,11 @@ where
|
||||
// set response headers
|
||||
for (key, value) in inner.headers.iter() {
|
||||
if !res.headers().contains_key(key) {
|
||||
res.headers_mut().insert(key, value.clone());
|
||||
res.headers_mut().insert(key.clone(), value.clone());
|
||||
}
|
||||
}
|
||||
// default content-type
|
||||
if inner.ct && !res.headers().contains_key(CONTENT_TYPE) {
|
||||
if inner.ct && !res.headers().contains_key(&CONTENT_TYPE) {
|
||||
res.headers_mut().insert(
|
||||
CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/octet-stream"),
|
||||
|
@ -14,6 +14,7 @@ use time;
|
||||
|
||||
use crate::dev::{BodySize, MessageBody, ResponseBody};
|
||||
use crate::error::{Error, Result};
|
||||
use crate::http::{HeaderName, HttpTryFrom};
|
||||
use crate::service::{ServiceRequest, ServiceResponse};
|
||||
use crate::HttpResponse;
|
||||
|
||||
@ -288,8 +289,12 @@ impl Format {
|
||||
|
||||
if let Some(key) = cap.get(2) {
|
||||
results.push(match cap.get(3).unwrap().as_str() {
|
||||
"i" => FormatText::RequestHeader(key.as_str().to_owned()),
|
||||
"o" => FormatText::ResponseHeader(key.as_str().to_owned()),
|
||||
"i" => FormatText::RequestHeader(
|
||||
HeaderName::try_from(key.as_str()).unwrap(),
|
||||
),
|
||||
"o" => FormatText::ResponseHeader(
|
||||
HeaderName::try_from(key.as_str()).unwrap(),
|
||||
),
|
||||
"e" => FormatText::EnvironHeader(key.as_str().to_owned()),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
@ -332,8 +337,8 @@ pub enum FormatText {
|
||||
TimeMillis,
|
||||
RemoteAddr,
|
||||
UrlPath,
|
||||
RequestHeader(String),
|
||||
ResponseHeader(String),
|
||||
RequestHeader(HeaderName),
|
||||
ResponseHeader(HeaderName),
|
||||
EnvironHeader(String),
|
||||
}
|
||||
|
||||
|
@ -286,10 +286,10 @@ mod tests {
|
||||
{
|
||||
let cookies = req.cookies().unwrap();
|
||||
assert_eq!(cookies.len(), 2);
|
||||
assert_eq!(cookies[0].name(), "cookie1");
|
||||
assert_eq!(cookies[0].value(), "value1");
|
||||
assert_eq!(cookies[1].name(), "cookie2");
|
||||
assert_eq!(cookies[1].value(), "value2");
|
||||
assert_eq!(cookies[0].name(), "cookie2");
|
||||
assert_eq!(cookies[0].value(), "value2");
|
||||
assert_eq!(cookies[1].name(), "cookie1");
|
||||
assert_eq!(cookies[1].value(), "value1");
|
||||
}
|
||||
|
||||
let cookie = req.cookie("cookie1");
|
||||
|
@ -209,7 +209,7 @@ where
|
||||
};
|
||||
|
||||
let mut len = None;
|
||||
if let Some(l) = req.headers().get(CONTENT_LENGTH) {
|
||||
if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
|
||||
if let Ok(s) = l.to_str() {
|
||||
if let Ok(l) = s.parse::<usize>() {
|
||||
len = Some(l)
|
||||
|
@ -297,7 +297,7 @@ where
|
||||
}
|
||||
|
||||
let mut len = None;
|
||||
if let Some(l) = req.headers().get(CONTENT_LENGTH) {
|
||||
if let Some(l) = req.headers().get(&CONTENT_LENGTH) {
|
||||
if let Ok(s) = l.to_str() {
|
||||
if let Ok(l) = s.parse::<usize>() {
|
||||
len = Some(l)
|
||||
|
@ -313,7 +313,7 @@ where
|
||||
/// Create `MessageBody` for request.
|
||||
pub fn new(req: &mut T) -> HttpMessageBody<T> {
|
||||
let mut len = None;
|
||||
if let Some(l) = req.headers().get(header::CONTENT_LENGTH) {
|
||||
if let Some(l) = req.headers().get(&header::CONTENT_LENGTH) {
|
||||
if let Ok(s) = l.to_str() {
|
||||
if let Ok(l) = s.parse::<usize>() {
|
||||
len = Some(l)
|
||||
|
Reference in New Issue
Block a user