Auto merge of #13058 - malisas:malisa-responseAPI, r=Manishearth,jdm

Response API

<!-- Please describe your changes on the following line: -->
This PR adds the [dom::Response](https://fetch.spec.whatwg.org/#response-class) implementation and addresses #11896.

The relevant passing tests` expectations have been updated.

In order to allow non-UTF-8-encoded status messages, `net_traits::response::Response`'s `raw_status` field has been changed from type [`Option<RawStatus>`](https://doc.servo.org/hyper/http/struct.RawStatus.html) to type `Option<(u16, Vec<u8>)>`. As a result, a few other files which rely on the `raw_status` field were affected and updated.

TODOs:
- The `body` and `trailer` methods. Relies on implementation of `ReadableStream` and `Promise`s.
- Similarly, replace the dummy constructor `_body: Option<USVString>` argument with `body: ResponseBodyInit`.
- Currently, whenever `r's response's header list` or `r's Headers object` are mentioned, I always modify the `headers_reflector` field (of type dom::Headers, or `r's Headers object`) and not the corresponding hyper::Headers list in the net_traits::Response field. A completely accurate interpretation of the spec might consider making both of these lists the same thing via a reference. [Discussion](https://github.com/whatwg/fetch/issues/358) was [had](https://github.com/servo/servo/pull/12884).

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13058)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-09-08 18:58:05 -05:00 committed by GitHub
commit 5a5a76cc5d
30 changed files with 402 additions and 124 deletions

View file

@ -13,7 +13,6 @@ use hyper::header::{AccessControlRequestHeaders, AccessControlRequestMethod, Use
use hyper::header::{CacheControl, ContentLanguage, ContentLength, ContentType, Expires, LastModified};
use hyper::header::{Headers, HttpDate, Host, Referer as HyperReferer};
use hyper::header::{Location, SetCookie, Pragma, Encoding, qitem};
use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::mime::{Mime, TopLevel, SubLevel};
use hyper::server::{Handler, Listening, Server};
@ -27,7 +26,6 @@ use net::http_loader::HttpState;
use net_traits::FetchTaskTarget;
use net_traits::request::{Origin, RedirectMode, Referer, Request, RequestMode};
use net_traits::response::{CacheState, Response, ResponseBody, ResponseType};
use std::borrow::Cow;
use std::fs::File;
use std::io::Read;
use std::rc::Rc;
@ -834,7 +832,7 @@ fn test_fetch_with_devtools() {
let httpresponse = DevtoolsHttpResponse {
headers: Some(response_headers),
status: Some(RawStatus(200, Cow::Borrowed("OK"))),
status: Some((200, b"OK".to_vec())),
body: None,
pipeline_id: pipeline_id,
};

View file

@ -18,7 +18,6 @@ use hyper::http::RawStatus;
use hyper::method::Method;
use hyper::mime::{Mime, SubLevel, TopLevel};
use hyper::status::StatusCode;
use hyper_serde::Serde;
use msg::constellation_msg::{PipelineId, ReferrerPolicy};
use net::cookie::Cookie;
use net::cookie_storage::CookieStorage;
@ -80,7 +79,7 @@ fn respond_with_headers(body: Vec<u8>, mut headers: Headers) -> MockResponse {
MockResponse::new(
headers,
StatusCode::Ok,
RawStatus(200, Cow::Borrowed("Ok")),
RawStatus(200, Cow::Borrowed("OK")),
body
)
}
@ -537,7 +536,7 @@ fn test_request_and_response_data_with_network_messages() {
let httpresponse = DevtoolsHttpResponse {
headers: Some(response_headers),
status: Some(RawStatus(200, Cow::Borrowed("Ok"))),
status: Some((200, b"OK".to_vec())),
body: None,
pipeline_id: pipeline_id,
};
@ -623,7 +622,7 @@ fn test_redirected_request_to_devtools() {
assert!(devhttprequest.method == Method::Post);
assert!(devhttprequest.url == url);
assert!(devhttpresponse.status == Some(RawStatus(301, Cow::Borrowed("Moved Permanently"))));
assert!(devhttpresponse.status == Some((301, "Moved Permanently".as_bytes().to_vec())));
let devhttprequest = expect_devtools_http_request(&devtools_port);
let devhttpresponse = expect_devtools_http_response(&devtools_port);
@ -631,7 +630,7 @@ fn test_redirected_request_to_devtools() {
assert!(devhttprequest.method == Method::Get);
assert!(devhttprequest.url == url);
assert!(devhttpresponse.status == Some(RawStatus(200, Cow::Borrowed("Ok"))));
assert!(devhttpresponse.status == Some((200, b"OK".to_vec())));
}
@ -1586,7 +1585,7 @@ fn test_auth_ui_sets_header_on_401() {
Err(e) => panic!("response contained error {:?}", e),
Ok(response) => {
assert_eq!(response.metadata.status,
Some(Serde(RawStatus(200, Cow::Borrowed("Ok")))));
Some((200, b"OK".to_vec())));
}
}
}
@ -1621,7 +1620,7 @@ fn test_auth_ui_needs_www_auth() {
Err(e) => panic!("response contained error {:?}", e),
Ok(response) => {
assert_eq!(response.metadata.status,
Some(Serde(RawStatus(401, Cow::Borrowed("Unauthorized")))));
Some((401, "Unauthorized".as_bytes().to_vec())));
}
}
}
@ -1947,7 +1946,7 @@ fn load_request_for_custom_response(expected_body: Vec<u8>) -> (Metadata, String
fn test_custom_response() {
let expected_body = b"Yay!".to_vec();
let (metadata, body) = load_request_for_custom_response(expected_body.clone());
assert_eq!(metadata.status, Some(Serde(RawStatus(200, Cow::Borrowed("OK")))));
assert_eq!(metadata.status, Some((200, b"OK".to_vec())));
assert_eq!(body, String::from_utf8(expected_body).unwrap());
}