mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Uses hyper's ContentEncoding instead of get_raw
This commit is contained in:
parent
5a60fdf4ca
commit
d6e1fab278
1 changed files with 37 additions and 34 deletions
|
@ -23,7 +23,7 @@ use hyper::header::StrictTransportSecurity;
|
||||||
use hyper::header::{AcceptEncoding, Accept, ContentLength, ContentType, Host, Location, qitem, Quality, QualityItem};
|
use hyper::header::{AcceptEncoding, Accept, ContentLength, ContentType, Host, Location, qitem, Quality, QualityItem};
|
||||||
use hyper::client::{Request, Response};
|
use hyper::client::{Request, Response};
|
||||||
use hyper::header::{AcceptEncoding, Accept, ContentLength, ContentType, Host, Location, qitem, StrictTransportSecurity};
|
use hyper::header::{AcceptEncoding, Accept, ContentLength, ContentType, Host, Location, qitem, StrictTransportSecurity};
|
||||||
use hyper::header::{Quality, QualityItem, Headers};
|
use hyper::header::{Quality, QualityItem, Headers, ContentEncoding, Encoding};
|
||||||
use hyper::Error as HttpError;
|
use hyper::Error as HttpError;
|
||||||
use hyper::method::Method;
|
use hyper::method::Method;
|
||||||
use hyper::http::RawStatus;
|
use hyper::http::RawStatus;
|
||||||
|
@ -109,6 +109,9 @@ fn load_for_consumer(load_data: LoadData,
|
||||||
Err(LoadError::MaxRedirects(url)) => {
|
Err(LoadError::MaxRedirects(url)) => {
|
||||||
send_error(url, "too many redirects".to_string(), start_chan)
|
send_error(url, "too many redirects".to_string(), start_chan)
|
||||||
}
|
}
|
||||||
|
Err(LoadError::UnsupportedContentEncodings(url)) => {
|
||||||
|
send_error(url, "no valid content encodings supported".to_string(), start_chan)
|
||||||
|
}
|
||||||
Err(LoadError::Cors(url, msg)) |
|
Err(LoadError::Cors(url, msg)) |
|
||||||
Err(LoadError::InvalidRedirect(url, msg)) |
|
Err(LoadError::InvalidRedirect(url, msg)) |
|
||||||
Err(LoadError::Decoding(url, msg)) => {
|
Err(LoadError::Decoding(url, msg)) => {
|
||||||
|
@ -259,7 +262,8 @@ pub enum LoadError {
|
||||||
Ssl(Url, String),
|
Ssl(Url, String),
|
||||||
InvalidRedirect(Url, String),
|
InvalidRedirect(Url, String),
|
||||||
Decoding(Url, String),
|
Decoding(Url, String),
|
||||||
MaxRedirects(Url)
|
MaxRedirects(Url),
|
||||||
|
UnsupportedContentEncodings(Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -514,21 +518,6 @@ pub fn load<A>(mut load_data: LoadData,
|
||||||
metadata.headers = Some(adjusted_headers);
|
metadata.headers = Some(adjusted_headers);
|
||||||
metadata.status = Some(response.status_raw().clone());
|
metadata.status = Some(response.status_raw().clone());
|
||||||
|
|
||||||
let mut encoding_str: Option<String> = None;
|
|
||||||
|
|
||||||
//TODO: This is now in hyper, just need to implement
|
|
||||||
//FIXME: Implement Content-Encoding Header https://github.com/hyperium/hyper/issues/391
|
|
||||||
if let Some(encodings) = response.headers().get_raw("content-encoding") {
|
|
||||||
for encoding in encodings.iter() {
|
|
||||||
if let Ok(encodings) = String::from_utf8(encoding.clone()) {
|
|
||||||
if encodings == "gzip" || encodings == "deflate" {
|
|
||||||
encoding_str = Some(encodings);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Tell devtools that we got a response
|
// --- Tell devtools that we got a response
|
||||||
// Send an HttpResponse message to devtools with the corresponding request_id
|
// Send an HttpResponse message to devtools with the corresponding request_id
|
||||||
// TODO: Send this message only if load_data has a pipeline_id that is not None
|
// TODO: Send this message only if load_data has a pipeline_id that is not None
|
||||||
|
@ -542,27 +531,41 @@ pub fn load<A>(mut load_data: LoadData,
|
||||||
net_event_response))).unwrap();
|
net_event_response))).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Stream the results depending on the encoding type.
|
let selected_content_encoding = match response.headers().get::<ContentEncoding>() {
|
||||||
match encoding_str {
|
Some(&ContentEncoding(ref encodings)) => {
|
||||||
Some(encoding) => {
|
if encodings.contains(&Encoding::Gzip) {
|
||||||
if encoding == "gzip" {
|
Some(Encoding::Gzip)
|
||||||
let result = GzDecoder::new(response);
|
} else if encodings.contains(&Encoding::Deflate) {
|
||||||
match result {
|
Some(Encoding::Deflate)
|
||||||
Ok(response_decoding) => {
|
} else {
|
||||||
return Ok((Box::new(response_decoding), metadata));
|
return Err(LoadError::UnsupportedContentEncodings(url.clone()))
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
return Err(LoadError::Decoding(metadata.final_url, err.to_string()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if encoding == "deflate" {
|
|
||||||
let response_decoding = DeflateDecoder::new(response);
|
|
||||||
return Ok((Box::new(response_decoding), metadata));
|
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
None => {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match selected_content_encoding {
|
||||||
|
Some(Encoding::Gzip) => {
|
||||||
|
let result = GzDecoder::new(response);
|
||||||
|
match result {
|
||||||
|
Ok(response_decoding) => {
|
||||||
|
return Ok((Box::new(response_decoding), metadata));
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(LoadError::Decoding(metadata.final_url, err.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Encoding::Deflate) => {
|
||||||
|
let response_decoding = DeflateDecoder::new(response);
|
||||||
|
return Ok((Box::new(response_decoding), metadata));
|
||||||
|
}
|
||||||
None => {
|
None => {
|
||||||
return Ok((Box::new(response), metadata));
|
return Ok((Box::new(response), metadata));
|
||||||
}
|
}
|
||||||
|
_ => return Err(LoadError::UnsupportedContentEncodings(url.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue