mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Initial fetch refactor
This commit is contained in:
parent
9523283c14
commit
ecf02a3656
3 changed files with 104 additions and 77 deletions
|
@ -5,12 +5,13 @@
|
|||
#![feature(box_syntax)]
|
||||
#![feature(custom_attribute)]
|
||||
#![feature(custom_derive)]
|
||||
#![feature(box_raw)]
|
||||
#![feature(plugin)]
|
||||
#![feature(slice_patterns)]
|
||||
#![feature(step_by)]
|
||||
#![feature(vec_push_all)]
|
||||
|
||||
#![plugin(plugins, serde_macros)]
|
||||
#![feature(custom_attribute)]
|
||||
#![plugin(serde_macros, plugins)]
|
||||
#![plugin(regex_macros)]
|
||||
|
||||
#[macro_use]
|
||||
|
@ -30,10 +31,12 @@ use hyper::header::{ContentType, Headers};
|
|||
use hyper::http::RawStatus;
|
||||
use hyper::method::Method;
|
||||
use hyper::mime::{Attr, Mime};
|
||||
use hyper::status::StatusCode;
|
||||
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
|
||||
use msg::constellation_msg::{PipelineId};
|
||||
use regex::Regex;
|
||||
use serde::{Deserializer, Serializer};
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::thread;
|
||||
use url::Url;
|
||||
use util::mem::HeapSizeOf;
|
||||
|
@ -47,6 +50,80 @@ pub static IPV4_REGEX: Regex = regex!(
|
|||
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
|
||||
pub static IPV6_REGEX: Regex = regex!(r"^([a-fA-F0-9]{0,4}[:]?){1,8}(/\d{1,3})?$");
|
||||
|
||||
/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
|
||||
#[derive(Clone, PartialEq, Copy)]
|
||||
pub enum ResponseType {
|
||||
Basic,
|
||||
CORS,
|
||||
Default,
|
||||
Error,
|
||||
Opaque
|
||||
}
|
||||
|
||||
/// [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 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Image handling.
|
||||
///
|
||||
/// It may be surprising that this goes in the network crate as opposed to the graphics crate.
|
||||
|
@ -85,6 +162,11 @@ impl LoadData {
|
|||
}
|
||||
}
|
||||
|
||||
/// Interface for observing the final response for an asynchronous fetch operation.
|
||||
pub trait AsyncFetchListener {
|
||||
fn response_available(&self, response: Response);
|
||||
}
|
||||
|
||||
/// A listener for asynchronous network events. Cancelling the underlying request is unsupported.
|
||||
pub trait AsyncResponseListener {
|
||||
/// The response headers for a request have been received.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue