mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
1. Add an enum type NetworkEventMessage for handling both HttpRequest and HttpResponse messages
2. Change run_server to handle network events 3. Add a unique id to track request-actor associations 4. Update the network event actor
This commit is contained in:
parent
9f4a88bc48
commit
6e91ebb1fe
5 changed files with 230 additions and 72 deletions
|
@ -14,31 +14,68 @@ use protocol::JsonPacketStream;
|
||||||
|
|
||||||
use devtools_traits::DevtoolScriptControlMsg;
|
use devtools_traits::DevtoolScriptControlMsg;
|
||||||
use msg::constellation_msg::PipelineId;
|
use msg::constellation_msg::PipelineId;
|
||||||
|
use devtools_traits::{DevtoolsControlMsg, NetworkEvent};
|
||||||
|
|
||||||
use collections::BTreeMap;
|
use collections::BTreeMap;
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
use rustc_serialize::json::{self, Json, ToJson};
|
use rustc_serialize::json::{self, Json, ToJson};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::num::Float;
|
use std::num::Float;
|
||||||
use std::sync::mpsc::{channel, Sender};
|
use std::sync::mpsc::{channel, Sender};
|
||||||
|
use std::borrow::IntoCow;
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use hyper::header::Headers;
|
use hyper::header::Headers;
|
||||||
use hyper::http::RawStatus;
|
use hyper::http::RawStatus;
|
||||||
use hyper::method::Method;
|
use hyper::method::Method;
|
||||||
|
|
||||||
#[derive(RustcEncodable)]
|
struct HttpRequest {
|
||||||
pub struct HttpRequest {
|
url: String,
|
||||||
pub url: Url,
|
method: Method,
|
||||||
//method: Method,
|
headers: Headers,
|
||||||
//headers: Headers,
|
body: Option<Vec<u8>>,
|
||||||
pub body: Option<Vec<u8>>,
|
}
|
||||||
|
|
||||||
|
struct HttpResponse {
|
||||||
|
headers: Option<Headers>,
|
||||||
|
status: Option<RawStatus>,
|
||||||
|
body: Option<Vec<u8>>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcEncodable)]
|
#[derive(RustcEncodable)]
|
||||||
|
struct GetRequestHeadersReply {
|
||||||
|
from: String,
|
||||||
|
headers: String,
|
||||||
|
headerSize: u8,
|
||||||
|
rawHeaders: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(RustcEncodable)]
|
||||||
|
pub struct EventActor {
|
||||||
|
pub actor: String,
|
||||||
|
pub url: String,
|
||||||
|
pub method: String,
|
||||||
|
pub startedDateTime: String,
|
||||||
|
pub isXHR: String,
|
||||||
|
pub private: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(RustcEncodable)]
|
||||||
|
pub struct ResponseStartMsg {
|
||||||
|
pub httpVersion: String,
|
||||||
|
pub remoteAddress: String,
|
||||||
|
pub remotePort: u8,
|
||||||
|
pub status: String,
|
||||||
|
pub statusText: String,
|
||||||
|
pub headersSize: u8,
|
||||||
|
pub discardResponseBody: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct NetworkEventActor {
|
pub struct NetworkEventActor {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub request: HttpRequest,
|
request: HttpRequest,
|
||||||
|
response: HttpResponse,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Actor for NetworkEventActor {
|
impl Actor for NetworkEventActor {
|
||||||
|
@ -54,11 +91,39 @@ impl Actor for NetworkEventActor {
|
||||||
Ok(match msg_type {
|
Ok(match msg_type {
|
||||||
|
|
||||||
"getRequestHeaders" => {
|
"getRequestHeaders" => {
|
||||||
//stream.write_json_packet(&msg);
|
println!("getRequestHeaders");
|
||||||
|
let msg = GetRequestHeadersReply {
|
||||||
|
from: self.name(),
|
||||||
|
headers: "headers".to_string(),
|
||||||
|
headerSize: 10,
|
||||||
|
rawHeaders: "Raw headers".to_string(),
|
||||||
|
};
|
||||||
|
stream.write_json_packet(&msg);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
"getRequestCookies" => {
|
"getRequestCookies" => {
|
||||||
|
println!("getRequestCookies");
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
"getRequestPostData" => {
|
||||||
|
println!("getRequestPostData");
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
"getResponseHeaders" => {
|
||||||
|
println!("getResponseHeaders");
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
"getResponseCookies" => {
|
||||||
|
println!("getResponseCookies");
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
"getResponseContent" => {
|
||||||
|
println!("getResponseContent");
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,3 +131,61 @@ impl Actor for NetworkEventActor {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl NetworkEventActor {
|
||||||
|
pub fn new(name: String) -> NetworkEventActor {
|
||||||
|
NetworkEventActor {
|
||||||
|
name: name,
|
||||||
|
request: HttpRequest {
|
||||||
|
url: String::new(),
|
||||||
|
method: Method::Get,
|
||||||
|
headers: Headers::new(),
|
||||||
|
body: None
|
||||||
|
},
|
||||||
|
response: HttpResponse {
|
||||||
|
headers: None,
|
||||||
|
status: None,
|
||||||
|
body: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn addEvent(&mut self, network_event: NetworkEvent) {
|
||||||
|
match network_event {
|
||||||
|
NetworkEvent::HttpRequest(url, method, headers, body) => {
|
||||||
|
self.request.url = url.serialize();
|
||||||
|
self.request.method = method.clone();
|
||||||
|
self.request.headers = headers.clone();
|
||||||
|
self.request.body = body;
|
||||||
|
}
|
||||||
|
NetworkEvent::HttpResponse(headers, status, body) => {
|
||||||
|
self.response.headers = headers.clone();
|
||||||
|
self.response.status = status.clone();
|
||||||
|
self.response.body = body.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_event_actor(&self) -> EventActor {
|
||||||
|
EventActor {
|
||||||
|
actor: self.name(),
|
||||||
|
url: self.request.url.clone(),
|
||||||
|
method: format!("{}", self.request.method),
|
||||||
|
startedDateTime: "2015-04-22T20:47:08.545Z".to_string(),
|
||||||
|
isXHR: "false".to_string(),
|
||||||
|
private: "false".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_response_start(&self) -> ResponseStartMsg {
|
||||||
|
ResponseStartMsg {
|
||||||
|
httpVersion: "HTTP/1.1".to_string(),
|
||||||
|
remoteAddress: "63.245.217.43".to_string(),
|
||||||
|
remotePort: 443,
|
||||||
|
status: "200".to_string(),
|
||||||
|
statusText: "OK".to_string(),
|
||||||
|
headersSize: 337,
|
||||||
|
discardResponseBody: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -30,8 +30,7 @@ extern crate hyper;
|
||||||
|
|
||||||
use actor::{Actor, ActorRegistry};
|
use actor::{Actor, ActorRegistry};
|
||||||
use actors::console::ConsoleActor;
|
use actors::console::ConsoleActor;
|
||||||
use actors::network_event::NetworkEventActor;
|
use actors::network_event::{NetworkEventActor, EventActor, ResponseStartMsg};
|
||||||
use actors::network_event::{HttpRequest};
|
|
||||||
use actors::worker::WorkerActor;
|
use actors::worker::WorkerActor;
|
||||||
use actors::inspector::InspectorActor;
|
use actors::inspector::InspectorActor;
|
||||||
use actors::root::RootActor;
|
use actors::root::RootActor;
|
||||||
|
@ -39,7 +38,7 @@ use actors::tab::TabActor;
|
||||||
use actors::timeline::TimelineActor;
|
use actors::timeline::TimelineActor;
|
||||||
use protocol::JsonPacketStream;
|
use protocol::JsonPacketStream;
|
||||||
|
|
||||||
use devtools_traits::{ConsoleMessage, DevtoolsControlMsg};
|
use devtools_traits::{ConsoleMessage, DevtoolsControlMsg, NetworkEvent};
|
||||||
use devtools_traits::{DevtoolsPageInfo, DevtoolScriptControlMsg};
|
use devtools_traits::{DevtoolsPageInfo, DevtoolScriptControlMsg};
|
||||||
use msg::constellation_msg::{PipelineId, WorkerId};
|
use msg::constellation_msg::{PipelineId, WorkerId};
|
||||||
use util::task::spawn_named;
|
use util::task::spawn_named;
|
||||||
|
@ -47,6 +46,7 @@ use util::task::spawn_named;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender, RecvError};
|
use std::sync::mpsc::{channel, Receiver, Sender, RecvError};
|
||||||
use std::net::{TcpListener, TcpStream, Shutdown};
|
use std::net::{TcpListener, TcpStream, Shutdown};
|
||||||
|
@ -99,12 +99,11 @@ struct NetworkEventMsg {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcEncodable)]
|
#[derive(RustcEncodable)]
|
||||||
struct EventActor {
|
struct NetworkEventUpdateMsg {
|
||||||
actor: NetworkEventActor,
|
from: String,
|
||||||
url: String,
|
__type__: String,
|
||||||
method: String,
|
updateType: String,
|
||||||
startedDateTime: String,
|
response: ResponseStartMsg,
|
||||||
isXHR: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spin up a devtools server that listens for connections on the specified port.
|
/// Spin up a devtools server that listens for connections on the specified port.
|
||||||
|
@ -138,6 +137,7 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
|
||||||
let mut accepted_connections: Vec<TcpStream> = Vec::new();
|
let mut accepted_connections: Vec<TcpStream> = Vec::new();
|
||||||
|
|
||||||
let mut actor_pipelines: HashMap<PipelineId, String> = HashMap::new();
|
let mut actor_pipelines: HashMap<PipelineId, String> = HashMap::new();
|
||||||
|
let mut actor_requests: HashMap<String, String> = HashMap::new();
|
||||||
|
|
||||||
let mut actor_workers: HashMap<(PipelineId, WorkerId), String> = HashMap::new();
|
let mut actor_workers: HashMap<(PipelineId, WorkerId), String> = HashMap::new();
|
||||||
|
|
||||||
|
@ -279,46 +279,72 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
|
||||||
return console_actor_name;
|
return console_actor_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_network_event(actors: Arc<Mutex<ActorRegistry>>,
|
fn find_first_console_actor(actors: Arc<Mutex<ActorRegistry>>) -> String {
|
||||||
connections: RefCell<Vec<TcpStream>>,
|
let actors = actors.lock().unwrap();
|
||||||
url: Url,
|
let root = actors.find::<RootActor>("root");
|
||||||
method: Method,
|
let ref tab_actor_name = root.tabs[0];
|
||||||
headers: Headers,
|
let tab_actor = actors.find::<TabActor>(tab_actor_name);
|
||||||
body: Option<Vec<u8>>) {
|
let console_actor_name = tab_actor.console.clone();
|
||||||
|
return console_actor_name;
|
||||||
//println!("handle_network_event");
|
}
|
||||||
|
|
||||||
|
fn find_network_event_actor(actors: Arc<Mutex<ActorRegistry>>,
|
||||||
|
actor_requests: &mut HashMap<String, String>,
|
||||||
|
request_id: String) -> String {
|
||||||
let mut actors = actors.lock().unwrap();
|
let mut actors = actors.lock().unwrap();
|
||||||
|
match (*actor_requests).entry(request_id) {
|
||||||
|
Occupied(name) => {
|
||||||
|
name.into_mut().clone()
|
||||||
|
}
|
||||||
|
Vacant(entry) => {
|
||||||
|
println!("not found");
|
||||||
|
let actor_name = actors.new_name("netevent");
|
||||||
|
let actor = NetworkEventActor::new(actor_name.clone());
|
||||||
|
entry.insert(actor_name.clone());
|
||||||
|
actors.register(box actor);
|
||||||
|
actor_name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* TODO: Maintain a HashMap that maps request/response ID to actor name.
|
fn handle_network_event(actors: Arc<Mutex<ActorRegistry>>,
|
||||||
* Check if the map contains the ID of the request/response message.
|
mut accepted_connections: Vec<TcpStream>,
|
||||||
* If no actor exists, create a new one.
|
actor_requests: &mut HashMap<String, String>,
|
||||||
* Store to stream(s) to the actor and retrieve them.
|
request_id: String,
|
||||||
*/
|
network_event: NetworkEvent) {
|
||||||
|
|
||||||
let actor = NetworkEventActor {
|
let console_actor_name = find_first_console_actor(actors.clone());
|
||||||
name: actors.new_name("network_event"),
|
let netevent_actor_name = find_network_event_actor(actors.clone(), actor_requests, request_id.clone());
|
||||||
request: HttpRequest {
|
let mut actors = actors.lock().unwrap();
|
||||||
url: url.clone(),
|
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);
|
||||||
//method: method.clone(),
|
|
||||||
//headers: headers.clone(),
|
|
||||||
body: body.clone()
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let msg = NetworkEventMsg {
|
match network_event {
|
||||||
from: actor.name.clone(),
|
NetworkEvent::HttpRequest(..) => {
|
||||||
__type__: "networkEvent".to_string(),
|
actor.addEvent(network_event);
|
||||||
eventActor: EventActor {
|
let msg = NetworkEventMsg {
|
||||||
actor: actor,
|
from: console_actor_name,
|
||||||
url: url.serialize(),
|
__type__: "networkEvent".to_string(),
|
||||||
method: "".to_string(),
|
eventActor: actor.get_event_actor(),
|
||||||
startedDateTime: "".to_string(),
|
};
|
||||||
isXHR: "false".to_string(),
|
for stream in accepted_connections.iter_mut() {
|
||||||
},
|
stream.write_json_packet(&msg);
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for stream in connections.borrow_mut().iter_mut() {
|
NetworkEvent::HttpResponse(..) => {
|
||||||
stream.write_json_packet(&msg);
|
println!("Network event response");
|
||||||
|
actor.addEvent(network_event);
|
||||||
|
let msg = NetworkEventUpdateMsg {
|
||||||
|
from: netevent_actor_name,
|
||||||
|
__type__: "networkEventUpdate".to_string(),
|
||||||
|
updateType: "responseStart".to_string(),
|
||||||
|
response: actor.get_response_start()
|
||||||
|
};
|
||||||
|
|
||||||
|
for stream in accepted_connections.iter_mut() {
|
||||||
|
stream.write_json_packet(&msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,14 +373,14 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
|
||||||
handle_console_message(actors.clone(), id, console_message,
|
handle_console_message(actors.clone(), id, console_message,
|
||||||
&actor_pipelines),
|
&actor_pipelines),
|
||||||
|
|
||||||
Ok(DevtoolsControlMsg::HttpRequest(url, method, headers, body)) => {
|
Ok(DevtoolsControlMsg::NetworkEventMessage(request_id, network_event)) => {
|
||||||
//println!("run_server: HttpRequest");
|
// copy the accepted_connections vector
|
||||||
let connections = RefCell::new(Vec::<TcpStream>::new());
|
let mut connections = Vec::<TcpStream>::new();
|
||||||
let mut stream = accepted_connections.get_mut(0).unwrap();
|
for stream in accepted_connections.iter() {
|
||||||
connections.borrow_mut().push(stream.try_clone().unwrap());
|
connections.push(stream.try_clone().unwrap());
|
||||||
handle_network_event(actors.clone(), connections, url, method, headers, body);
|
}
|
||||||
|
handle_network_event(actors.clone(), connections, &mut actor_requests, request_id, network_event);
|
||||||
}
|
}
|
||||||
_ => break,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,7 @@ pub enum DevtoolsControlMsg {
|
||||||
NewGlobal((PipelineId, Option<WorkerId>), Sender<DevtoolScriptControlMsg>, DevtoolsPageInfo),
|
NewGlobal((PipelineId, Option<WorkerId>), Sender<DevtoolScriptControlMsg>, DevtoolsPageInfo),
|
||||||
SendConsoleMessage(PipelineId, ConsoleMessage),
|
SendConsoleMessage(PipelineId, ConsoleMessage),
|
||||||
ServerExitMsg,
|
ServerExitMsg,
|
||||||
HttpRequest(Url, Method, Headers, Option<Vec<u8>>),
|
NetworkEventMessage(String, NetworkEvent),
|
||||||
HttpResponse(Option<Headers>, RawStatus, Vec<u8>)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialized JS return values
|
/// Serialized JS return values
|
||||||
|
@ -152,6 +151,12 @@ pub enum ConsoleMessage {
|
||||||
//WarnMessage(String),
|
//WarnMessage(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum NetworkEvent {
|
||||||
|
HttpRequest(Url, Method, Headers, Option<Vec<u8>>),
|
||||||
|
HttpResponse(Option<Headers>, Option<RawStatus>, Option<Vec<u8>>)
|
||||||
|
}
|
||||||
|
|
||||||
impl TimelineMarker {
|
impl TimelineMarker {
|
||||||
pub fn new(name: String, metadata: TracingMetadata) -> TimelineMarker {
|
pub fn new(name: String, metadata: TracingMetadata) -> TimelineMarker {
|
||||||
TimelineMarker {
|
TimelineMarker {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
use net_traits::{ControlMsg, CookieSource, LoadData, Metadata, LoadConsumer};
|
use net_traits::{ControlMsg, CookieSource, LoadData, Metadata, LoadConsumer};
|
||||||
use net_traits::ProgressMsg::{Payload, Done};
|
use net_traits::ProgressMsg::{Payload, Done};
|
||||||
use devtools_traits::{DevtoolsControlMsg};
|
use devtools_traits::{DevtoolsControlMsg, NetworkEvent};
|
||||||
use mime_classifier::MIMEClassifier;
|
use mime_classifier::MIMEClassifier;
|
||||||
use resource_task::{start_sending_opt, start_sending_sniffed_opt};
|
use resource_task::{start_sending_opt, start_sending_sniffed_opt};
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ use util::resource_files::resources_dir_path;
|
||||||
use util::opts;
|
use util::opts;
|
||||||
use url::{Url, UrlParser};
|
use url::{Url, UrlParser};
|
||||||
|
|
||||||
|
use uuid;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::boxed::FnBox;
|
use std::boxed::FnBox;
|
||||||
|
|
||||||
|
@ -199,16 +200,14 @@ reason: \"certificate verify failed\" }]))";
|
||||||
info!("{:?}", load_data.data);
|
info!("{:?}", load_data.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
let request_id = uuid::Uuid::new_v4().to_simple_string();
|
||||||
match devtools_chan {
|
let net_event = NetworkEvent::HttpRequest(
|
||||||
Some(chan) => chan.send(DevtoolsControlMsg::HttpRequest(load_data.url.clone(), load_data.method.clone(), load_data.headers.clone(), load_data.data.clone())).unwrap(),
|
load_data.url.clone(),
|
||||||
None => {}
|
load_data.method.clone(),
|
||||||
}
|
load_data.headers.clone(),
|
||||||
*/
|
load_data.data.clone()
|
||||||
|
);
|
||||||
println!("load");
|
devtools_chan.as_ref().map(|chan| chan.send(DevtoolsControlMsg::NetworkEventMessage(request_id.clone(), net_event)));
|
||||||
devtools_chan.as_ref().map(|chan| chan.send(DevtoolsControlMsg::HttpRequest(load_data.url.clone(), load_data.method.clone(), load_data.headers.clone(), load_data.data.clone())).unwrap());
|
|
||||||
|
|
||||||
|
|
||||||
// Avoid automatically sending request body if a redirect has occurred.
|
// Avoid automatically sending request body if a redirect has occurred.
|
||||||
let writer = match load_data.data {
|
let writer = match load_data.data {
|
||||||
|
@ -342,6 +341,10 @@ reason: \"certificate verify failed\" }]))";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("Http loader Response");
|
||||||
|
let net_event_response = NetworkEvent::HttpResponse(metadata.headers.clone(), metadata.status.clone(), None);
|
||||||
|
devtools_chan.as_ref().map(|chan| chan.send(DevtoolsControlMsg::NetworkEventMessage(request_id, net_event_response)));
|
||||||
|
|
||||||
match encoding_str {
|
match encoding_str {
|
||||||
Some(encoding) => {
|
Some(encoding) => {
|
||||||
if encoding == "gzip" {
|
if encoding == "gzip" {
|
||||||
|
|
|
@ -29,6 +29,7 @@ extern crate rustc_serialize;
|
||||||
extern crate util;
|
extern crate util;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
|
extern crate uuid;
|
||||||
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue