Skip to content

Commit

Permalink
1. Add an Option<Pipeline_id> field to the LoadData struct, and a cor…
Browse files Browse the repository at this point in the history
…responding parameter to LoadData::new()

2. Change addEvent in the NetworkEventActor to add_request and add_response
  • Loading branch information
hsvalava authored and jdm committed May 5, 2015
1 parent 6e91ebb commit 01eb31a
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 150 deletions.
6 changes: 2 additions & 4 deletions components/devtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ path = "../msg"
[dependencies.util]
path = "../util"

[dependencies]
url = "0.2.16"
hyper = "0.3"

[dependencies]
time = "*"
rustc-serialize = "0.3"
url = "*"
hyper = "*"
110 changes: 42 additions & 68 deletions components/devtools/actors/network_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,14 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// Liberally derived from the [Firefox JS implementation](http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/webconsole.js).
/// Mediates interaction between the remote web console and equivalent functionality (object
/// inspection, JS evaluation, autocompletion) in Servo.
/// Handles interaction with the remote web console on network events (HTTP requests, responses) in Servo.

extern crate hyper;
extern crate url;

use actor::{Actor, ActorRegistry};
use protocol::JsonPacketStream;

use devtools_traits::DevtoolScriptControlMsg;
use msg::constellation_msg::PipelineId;
use devtools_traits::{DevtoolsControlMsg, NetworkEvent};

use collections::BTreeMap;
use core::cell::RefCell;
use std::fmt;
use rustc_serialize::json::{self, Json, ToJson};
use rustc_serialize::json;
use std::net::TcpStream;
use std::num::Float;
use std::sync::mpsc::{channel, Sender};
use std::borrow::IntoCow;

use url::Url;
use hyper::header::Headers;
use hyper::http::RawStatus;
Expand All @@ -43,12 +29,10 @@ struct HttpResponse {
body: Option<Vec<u8>>
}

#[derive(RustcEncodable)]
struct GetRequestHeadersReply {
from: String,
headers: String,
headerSize: u8,
rawHeaders: String
pub struct NetworkEventActor {
pub name: String,
request: HttpRequest,
response: HttpResponse,
}

#[derive(RustcEncodable)]
Expand All @@ -57,25 +41,27 @@ pub struct EventActor {
pub url: String,
pub method: String,
pub startedDateTime: String,
pub isXHR: String,
pub private: String
pub isXHR: bool,
pub private: bool
}

#[derive(RustcEncodable)]
pub struct ResponseStartMsg {
pub httpVersion: String,
pub remoteAddress: String,
pub remotePort: u8,
pub remotePort: u32,
pub status: String,
pub statusText: String,
pub headersSize: u8,
pub headersSize: u32,
pub discardResponseBody: bool,
}

pub struct NetworkEventActor {
pub name: String,
request: HttpRequest,
response: HttpResponse,
#[derive(RustcEncodable)]
struct GetRequestHeadersReply {
from: String,
headers: Vec<String>,
headerSize: u8,
rawHeaders: String
}

impl Actor for NetworkEventActor {
Expand All @@ -86,47 +72,35 @@ impl Actor for NetworkEventActor {
fn handle_message(&self,
_registry: &ActorRegistry,
msg_type: &str,
msg: &json::Object,
_msg: &json::Object,
stream: &mut TcpStream) -> Result<bool, ()> {
Ok(match msg_type {

"getRequestHeaders" => {
println!("getRequestHeaders");
// TODO: Pass the correct values for headers, headerSize, rawHeaders
let msg = GetRequestHeadersReply {
from: self.name(),
headers: "headers".to_string(),
headers: Vec::new(),
headerSize: 10,
rawHeaders: "Raw headers".to_string(),
rawHeaders: "Raw headers".to_string(),
};
stream.write_json_packet(&msg);
true
}

"getRequestCookies" => {
println!("getRequestCookies");
true
false
}

"getRequestPostData" => {
println!("getRequestPostData");
true
false
}

"getResponseHeaders" => {
println!("getResponseHeaders");
true
false
}

"getResponseCookies" => {
println!("getResponseCookies");
true
false
}

"getResponseContent" => {
println!("getResponseContent");
true
false
}

_ => false
})
}
Expand All @@ -147,37 +121,37 @@ impl NetworkEventActor {
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 add_request(&mut self, url: Url, method: Method, headers: Headers, body: Option<Vec<u8>>) {
self.request.url = url.serialize();
self.request.method = method.clone();
self.request.headers = headers.clone();
self.request.body = body;
}

pub fn add_response(&mut self, headers: Option<Headers>, status: Option<RawStatus>, body: Option<Vec<u8>>) {
self.response.headers = headers.clone();
self.response.status = status.clone();
self.response.body = body.clone();
}

pub fn get_event_actor(&self) -> EventActor {
// TODO: Send the correct values for startedDateTime, isXHR, private
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(),
isXHR: false,
private: false,
}
}

pub fn get_response_start(&self) -> ResponseStartMsg {
// TODO: Send the correct values for all these fields.
// This is a fake message.
ResponseStartMsg {
httpVersion: "HTTP/1.1".to_string(),
remoteAddress: "63.245.217.43".to_string(),
Expand Down
90 changes: 43 additions & 47 deletions components/devtools/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ extern crate rustc_serialize;
extern crate msg;
extern crate time;
extern crate util;
extern crate url;
extern crate hyper;
extern crate url;

use actor::{Actor, ActorRegistry};
use actors::console::ConsoleActor;
Expand All @@ -53,12 +53,6 @@ use std::net::{TcpListener, TcpStream, Shutdown};
use std::sync::{Arc, Mutex};
use time::precise_time_ns;

use url::Url;

use hyper::header::Headers;
use hyper::http::RawStatus;
use hyper::method::Method;

mod actor;
/// Corresponds to http://mxr.mozilla.org/mozilla-central/source/toolkit/devtools/server/actors/
mod actors {
Expand Down Expand Up @@ -279,72 +273,73 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
return console_actor_name;
}

fn find_first_console_actor(actors: Arc<Mutex<ActorRegistry>>) -> String {
let actors = actors.lock().unwrap();
let root = actors.find::<RootActor>("root");
let ref tab_actor_name = root.tabs[0];
let tab_actor = actors.find::<TabActor>(tab_actor_name);
let console_actor_name = tab_actor.console.clone();
return console_actor_name;
}

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();
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
}
}
}

fn handle_network_event(actors: Arc<Mutex<ActorRegistry>>,
mut accepted_connections: Vec<TcpStream>,
mut connections: Vec<TcpStream>,
actor_pipelines: &HashMap<PipelineId, String>,
actor_requests: &mut HashMap<String, String>,
pipeline_id: PipelineId,
request_id: String,
network_event: NetworkEvent) {

let console_actor_name = find_first_console_actor(actors.clone());
let console_actor_name = find_console_actor(actors.clone(), pipeline_id, actor_pipelines);
let netevent_actor_name = find_network_event_actor(actors.clone(), actor_requests, request_id.clone());
let mut actors = actors.lock().unwrap();
let actor = actors.find_mut::<NetworkEventActor>(&netevent_actor_name);

match network_event {
NetworkEvent::HttpRequest(..) => {
actor.addEvent(network_event);
NetworkEvent::HttpRequest(url, method, headers, body) => {
//Store the request information in the actor
actor.add_request(url, method, headers, body);

//Send a networkEvent message to the client
let msg = NetworkEventMsg {
from: console_actor_name,
__type__: "networkEvent".to_string(),
eventActor: actor.get_event_actor(),
};
for stream in accepted_connections.iter_mut() {
for stream in connections.iter_mut() {
stream.write_json_packet(&msg);
}
}
NetworkEvent::HttpResponse(headers, status, body) => {
//Store the response information in the actor
actor.add_response(headers, status, body);

NetworkEvent::HttpResponse(..) => {
println!("Network event response");
actor.addEvent(network_event);
//Send a networkEventUpdate (responseStart) to the client
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() {
for stream in connections.iter_mut() {
stream.write_json_packet(&msg);
}
}
//TODO: Send the other types of update messages at appropriate times
// requestHeaders, requestCookies, responseHeaders, securityInfo, etc
}
}

// Find the name of NetworkEventActor corresponding to request_id
// Create a new one if it does not exist, add it to the actor_requests hashmap
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();
match (*actor_requests).entry(request_id) {
Occupied(name) => {
//TODO: Delete from map like Firefox does?
name.into_mut().clone()
}
Vacant(entry) => {
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
}
}
}

Expand Down Expand Up @@ -372,18 +367,19 @@ fn run_server(sender: Sender<DevtoolsControlMsg>,
Ok(DevtoolsControlMsg::SendConsoleMessage(id, console_message)) =>
handle_console_message(actors.clone(), id, console_message,
&actor_pipelines),

Ok(DevtoolsControlMsg::NetworkEventMessage(request_id, network_event)) => {
// copy the accepted_connections vector
let mut connections = Vec::<TcpStream>::new();
for stream in accepted_connections.iter() {
connections.push(stream.try_clone().unwrap());
}
handle_network_event(actors.clone(), connections, &mut actor_requests, request_id, network_event);
//TODO: Get pipeline_id from NetworkEventMessage after fixing the send in http_loader
// For now, the id of the first pipeline is passed
handle_network_event(actors.clone(), connections, &actor_pipelines, &mut actor_requests,
PipelineId(0), request_id, network_event);
}
}
}

for connection in accepted_connections.iter_mut() {
let _ = connection.shutdown(Shutdown::Both);
}
Expand Down
6 changes: 2 additions & 4 deletions components/devtools_traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ path = "../msg"
[dependencies.util]
path = "../util"

[dependencies]
url = "0.2.16"
hyper = "0.3"

[dependencies]
time = "*"
rustc-serialize = "0.3"
url = "*"
hyper = "*"
1 change: 1 addition & 0 deletions components/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ regex = "0.1.14"
regex_macros = "0.1.8"
hyper = "0.3"
flate2 = "0.2.0"
uuid = "*"
Loading

0 comments on commit 01eb31a

Please sign in to comment.