convert net crate to use hyper

This commit is contained in:
Sean McArthur 2014-11-14 11:57:32 -08:00 committed by Manish Goregaokar
parent 92a8c7a80c
commit 12727d4dd0
10 changed files with 164 additions and 93 deletions

View file

@ -9,7 +9,7 @@
//! This library will eventually become the core of the Fetch crate
//! with CORSRequest being expanded into FetchRequest (etc)
use http::method::Method;
use hyper::method::Method;
use std::ascii::AsciiExt;
use std::comm::{Sender, Receiver, channel};
use time;

View file

@ -3,8 +3,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use url::Url;
use http::method::{Get, Method};
use http::headers::request::HeaderCollection;
use hyper::method::{Get, Method};
use hyper::mime::{Mime, Text, Html, Charset, Utf8};
use hyper::header::Headers;
use hyper::header::common::ContentType;
use fetch::cors_cache::CORSCache;
use fetch::response::Response;
@ -58,7 +60,7 @@ pub enum ResponseTainting {
pub struct Request {
pub method: Method,
pub url: Url,
pub headers: HeaderCollection,
pub headers: Headers,
pub unsafe_request: bool,
pub body: Option<Vec<u8>>,
pub preserve_content_codings: bool,
@ -87,7 +89,7 @@ impl Request {
Request {
method: Get,
url: url,
headers: HeaderCollection::new(),
headers: Headers::new(),
unsafe_request: false,
body: None,
preserve_content_codings: false,
@ -116,7 +118,7 @@ impl Request {
"about" => match self.url.non_relative_scheme_data() {
Some(s) if s.as_slice() == "blank" => {
let mut response = Response::new();
let _ = response.headers.insert_raw("Content-Type".to_string(), b"text/html;charset=utf-8");
response.headers.set(ContentType(Mime(Text, Html, vec![(Charset, Utf8)])));
response
},
_ => Response::network_error()

View file

@ -3,11 +3,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use url::Url;
use http::status::{Status, UnregisteredStatus};
use http::status::Ok as StatusOk;
use http::headers::HeaderEnum;
use http::headers::response::HeaderCollection;
use std::ascii::OwnedAsciiExt;
use hyper::status::StatusCode;
use hyper::status::Ok as StatusOk;
use hyper::header::Headers;
use std::ascii::AsciiExt;
use std::comm::Receiver;
/// [Response type](http://fetch.spec.whatwg.org/#concept-response-type)
@ -57,8 +56,9 @@ pub struct Response {
pub response_type: ResponseType,
pub termination_reason: Option<TerminationReason>,
pub url: Option<Url>,
pub status: Status,
pub headers: HeaderCollection,
/// `None` can be considered a StatusCode of `0`.
pub status: Option<StatusCode>,
pub headers: Headers,
pub body: ResponseBody,
/// [Internal response](http://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response is a filtered response
pub internal_response: Option<Box<Response>>,
@ -70,8 +70,8 @@ impl Response {
response_type: Default,
termination_reason: None,
url: None,
status: StatusOk,
headers: HeaderCollection::new(),
status: Some(StatusOk),
headers: Headers::new(),
body: Empty,
internal_response: None
}
@ -82,8 +82,8 @@ impl Response {
response_type: Error,
termination_reason: None,
url: None,
status: UnregisteredStatus(0, "".to_string()),
headers: HeaderCollection::new(),
status: None,
headers: Headers::new(),
body: Empty,
internal_response: None
}
@ -110,32 +110,30 @@ impl Response {
match filter_type {
Default | Error => unreachable!(),
Basic => {
let mut headers = HeaderCollection::new();
for h in old_headers.iter() {
match h.header_name().into_ascii_lower().as_slice() {
"set-cookie" | "set-cookie2" => {},
_ => headers.insert(h)
let headers = old_headers.iter().filter(|header| {
match header.name().to_ascii_lower().as_slice() {
"set-cookie" | "set-cookie2" => false,
_ => true
}
}
}).collect();
response.headers = headers;
response.response_type = filter_type;
},
CORS => {
let mut headers = HeaderCollection::new();
for h in old_headers.iter() {
match h.header_name().into_ascii_lower().as_slice() {
let headers = old_headers.iter().filter(|header| {
match header.name().to_ascii_lower().as_slice() {
"cache-control" | "content-language" |
"content-type" | "expires" | "last-modified" | "Pragma" => {},
"content-type" | "expires" | "last-modified" | "Pragma" => false,
// XXXManishearth handle Access-Control-Expose-Headers
_ => headers.insert(h)
_ => true
}
}
}).collect();
response.headers = headers;
response.response_type = filter_type;
},
Opaque => {
response.headers = HeaderCollection::new();
response.status = UnregisteredStatus(0, "".to_string());
response.headers = Headers::new();
response.status = None;
response.body = Empty;
}
}