diff options
author | bors-servo <metajack+bors@gmail.com> | 2015-08-06 14:00:26 -0600 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2015-08-06 14:00:26 -0600 |
commit | 889ef3aef02144ce797266e99e0cdb66ea38871a (patch) | |
tree | a03d5979850c74804deca530d804a1f3c99cae5c | |
parent | 522ebe7a3bd55d4572b670fa32ffc9979b3f7a74 (diff) | |
parent | 8943f11e5483bc777d1a6445361495ff2ced038c (diff) | |
download | servo-889ef3aef02144ce797266e99e0cdb66ea38871a.tar.gz servo-889ef3aef02144ce797266e99e0cdb66ea38871a.zip |
Auto merge of #7010 - HarryLovesCode:master, r=jdm
Fixes #6879 (Improper connections to the devtools server)
Modifies how we behave in the case that something attempts to communicate with the devtools server improperly. This includes...
- Invalid encoding (Non `UTF8`) of the packet length / error parsing / none specified
- JSON encoding error (such as a `Parser::SyntaxError` or a `Parser::IoError`)
Happy to make changes if anyone has an issue with this or feels another way is more idiomatic!
<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7010)
<!-- Reviewable:end -->
-rw-r--r-- | components/devtools/lib.rs | 4 | ||||
-rw-r--r-- | components/devtools/protocol.rs | 35 |
2 files changed, 27 insertions, 12 deletions
diff --git a/components/devtools/lib.rs b/components/devtools/lib.rs index f268934ccbd..c791a9c46f2 100644 --- a/components/devtools/lib.rs +++ b/components/devtools/lib.rs @@ -172,8 +172,8 @@ fn run_server(sender: Sender<DevtoolsControlMsg>, println!("error: EOF"); break 'outer } - Err(e) => { - println!("error: {}", e.description()); + Err(err_msg) => { + println!("error: {}", err_msg); break 'outer } } diff --git a/components/devtools/protocol.rs b/components/devtools/protocol.rs index daa7c19fba6..1b54b92d01a 100644 --- a/components/devtools/protocol.rs +++ b/components/devtools/protocol.rs @@ -8,12 +8,14 @@ use rustc_serialize::{json, Encodable}; use rustc_serialize::json::Json; -use std::io::{self, Read, Write}; +use rustc_serialize::json::ParserError::{IoError, SyntaxError}; +use std::error::Error; +use std::io::{Read, Write}; use std::net::TcpStream; pub trait JsonPacketStream { fn write_json_packet<'a, T: Encodable>(&mut self, obj: &T); - fn read_json_packet(&mut self) -> io::Result<Option<Json>>; + fn read_json_packet(&mut self) -> Result<Option<Json>, String>; } impl JsonPacketStream for TcpStream { @@ -25,25 +27,38 @@ impl JsonPacketStream for TcpStream { self.write_all(s.as_bytes()).unwrap(); } - fn read_json_packet<'a>(&mut self) -> io::Result<Option<Json>> { + fn read_json_packet<'a>(&mut self) -> Result<Option<Json>, String> { // https://wiki.mozilla.org/Remote_Debugging_Protocol_Stream_Transport // In short, each JSON packet is [ascii length]:[JSON data of given length] let mut buffer = vec!(); loop { let mut buf = [0]; - let byte = match try!(self.read(&mut buf)) { - 0 => return Ok(None), // EOF - 1 => buf[0], - _ => unreachable!(), + let byte = match self.read(&mut buf) { + Ok(0) => return Ok(None), // EOF + Ok(1) => buf[0], + Ok(_) => unreachable!(), + Err(e) => return Err(e.description().to_string()), }; match byte { b':' => { - let packet_len_str = String::from_utf8(buffer).unwrap(); - let packet_len = u64::from_str_radix(&packet_len_str, 10).unwrap(); + let packet_len_str = match String::from_utf8(buffer) { + Ok(packet_len) => packet_len, + Err(_) => return Err("nonvalid UTF8 in packet length".to_string()), + }; + let packet_len = match u64::from_str_radix(&packet_len_str, 10) { + Ok(packet_len) => packet_len, + Err(_) => return Err("packet length missing / not parsable".to_string()), + }; let mut packet = String::new(); self.take(packet_len).read_to_string(&mut packet).unwrap(); println!("{}", packet); - return Ok(Some(Json::from_str(&packet).unwrap())) + return match Json::from_str(&packet) { + Ok(json) => Ok(Some(json)), + Err(err) => match err { + IoError(ioerr) => return Err(ioerr.description().to_string()), + SyntaxError(_, l, c) => return Err(format!("syntax at {}:{}", l, c)), + }, + }; }, c => buffer.push(c), } |