mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Initial fetch refactor
This commit is contained in:
parent
9523283c14
commit
ecf02a3656
3 changed files with 104 additions and 77 deletions
|
@ -4,65 +4,18 @@
|
|||
|
||||
use hyper::header::Headers;
|
||||
use hyper::status::StatusCode;
|
||||
use net_traits::{Response, ResponseBody, ResponseType};
|
||||
use std::ascii::AsciiExt;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use url::Url;
|
||||
|
||||
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
|
||||
#[derive(Clone, PartialEq, Copy)]
|
||||
pub enum ResponseType {
|
||||
Basic,
|
||||
CORS,
|
||||
Default,
|
||||
Error,
|
||||
Opaque
|
||||
pub trait ResponseMethods {
|
||||
fn new() -> Response;
|
||||
fn to_filtered(self, ResponseType) -> Response;
|
||||
}
|
||||
|
||||
/// [Response termination reason](https://fetch.spec.whatwg.org/#concept-response-termination-reason)
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum TerminationReason {
|
||||
EndUserAbort,
|
||||
Fatal,
|
||||
Timeout
|
||||
}
|
||||
|
||||
/// The response body can still be pushed to after fetch
|
||||
/// This provides a way to store unfinished response bodies
|
||||
#[derive(Clone)]
|
||||
pub enum ResponseBody {
|
||||
Empty, // XXXManishearth is this necessary, or is Done(vec![]) enough?
|
||||
Receiving(Vec<u8>),
|
||||
Done(Vec<u8>),
|
||||
}
|
||||
|
||||
pub enum ResponseMsg {
|
||||
Chunk(Vec<u8>),
|
||||
Finished,
|
||||
Errored
|
||||
}
|
||||
|
||||
pub struct ResponseLoader {
|
||||
response: Response,
|
||||
chan: Receiver<ResponseMsg>
|
||||
}
|
||||
|
||||
/// A [Response](https://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec
|
||||
#[derive(Clone)]
|
||||
pub struct Response {
|
||||
pub response_type: ResponseType,
|
||||
pub termination_reason: Option<TerminationReason>,
|
||||
pub url: Option<Url>,
|
||||
/// `None` can be considered a StatusCode of `0`.
|
||||
pub status: Option<StatusCode>,
|
||||
pub headers: Headers,
|
||||
pub body: ResponseBody,
|
||||
/// [Internal response](https://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response
|
||||
/// is a filtered response
|
||||
pub internal_response: Option<Box<Response>>,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new() -> Response {
|
||||
impl ResponseMethods for Response {
|
||||
fn new() -> Response {
|
||||
Response {
|
||||
response_type: ResponseType::Default,
|
||||
termination_reason: None,
|
||||
|
@ -74,28 +27,9 @@ impl Response {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn network_error() -> Response {
|
||||
Response {
|
||||
response_type: ResponseType::Error,
|
||||
termination_reason: None,
|
||||
url: None,
|
||||
status: None,
|
||||
headers: Headers::new(),
|
||||
body: ResponseBody::Empty,
|
||||
internal_response: None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_network_error(&self) -> bool {
|
||||
match self.response_type {
|
||||
ResponseType::Error => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert to a filtered response, of type `filter_type`.
|
||||
/// Do not use with type Error or Default
|
||||
pub fn to_filtered(self, filter_type: ResponseType) -> Response {
|
||||
fn to_filtered(self, filter_type: ResponseType) -> Response {
|
||||
assert!(filter_type != ResponseType::Error);
|
||||
assert!(filter_type != ResponseType::Default);
|
||||
if self.is_network_error() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue