diff --git a/Cargo.lock b/Cargo.lock index 0da8ef92757..68ff52b5fb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2373,6 +2373,7 @@ dependencies = [ "tinyfiledialogs 2.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "utf-8 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "webrender_traits 0.39.0 (git+https://github.com/servo/webrender)", "webvr 0.0.1", diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index bc6c17a8871..346ab0ab165 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -89,6 +89,7 @@ swapper = "0.0.4" time = "0.1.12" unicode-segmentation = "1.1.0" url = {version = "1.2", features = ["heap_size", "query_encoding"]} +utf-8 = "0.7" uuid = {version = "0.4", features = ["v4"]} xml5ever = {version = "0.7", features = ["unstable"]} webrender_traits = {git = "https://github.com/servo/webrender", features = ["ipc"]} diff --git a/components/script/dom/eventsource.rs b/components/script/dom/eventsource.rs index 7945eec0b7d..81a2bc87584 100644 --- a/components/script/dom/eventsource.rs +++ b/components/script/dom/eventsource.rs @@ -16,8 +16,6 @@ use dom::eventtarget::EventTarget; use dom::globalscope::GlobalScope; use dom::messageevent::MessageEvent; use dom_struct::dom_struct; -use encoding::Encoding; -use encoding::all::UTF_8; use euclid::length::Length; use hyper::header::{Accept, qitem}; use ipc_channel::ipc; @@ -39,6 +37,7 @@ use std::str::{Chars, FromStr}; use std::sync::{Arc, Mutex}; use task_source::TaskSource; use timers::OneshotTimerCallback; +use utf8; header! { (LastEventId, "Last-Event-ID") => [String] } @@ -76,6 +75,8 @@ enum ParserState { } struct EventSourceContext { + incomplete_utf8: Option, + event_source: Trusted, gen_id: GenerationId, action_sender: ipc::IpcSender, @@ -293,12 +294,41 @@ impl FetchResponseListener for EventSourceContext { } fn process_response_chunk(&mut self, chunk: Vec) { - let mut stream = String::new(); - UTF_8.raw_decoder().raw_feed(&chunk, &mut stream); - self.parse(stream.chars()) + let mut input = &*chunk; + if let Some(mut incomplete) = self.incomplete_utf8.take() { + match incomplete.try_complete(input) { + None => return, + Some((result, remaining_input)) => { + self.parse(result.unwrap_or("\u{FFFD}").chars()); + input = remaining_input; + } + } + } + + while !input.is_empty() { + match utf8::decode(&input) { + Ok(s) => { + self.parse(s.chars()); + return + } + Err(utf8::DecodeError::Invalid { valid_prefix, remaining_input, .. }) => { + self.parse(valid_prefix.chars()); + self.parse("\u{FFFD}".chars()); + input = remaining_input; + } + Err(utf8::DecodeError::Incomplete { valid_prefix, incomplete_suffix }) => { + self.parse(valid_prefix.chars()); + self.incomplete_utf8 = Some(incomplete_suffix); + return + } + } + } } fn process_response_eof(&mut self, _response: Result<(), NetworkError>) { + if let Some(_) = self.incomplete_utf8.take() { + self.parse("\u{FFFD}".chars()); + } self.reestablish_the_connection(); } } @@ -378,6 +408,8 @@ impl EventSource { // Step 14 let (action_sender, action_receiver) = ipc::channel().unwrap(); let context = EventSourceContext { + incomplete_utf8: None, + event_source: Trusted::new(&ev), gen_id: ev.generation_id.get(), action_sender: action_sender.clone(), diff --git a/components/script/lib.rs b/components/script/lib.rs index 6e0b75d7ef9..1dc74905cc9 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -102,6 +102,7 @@ extern crate time; extern crate tinyfiledialogs; extern crate unicode_segmentation; extern crate url; +extern crate utf8; extern crate uuid; extern crate webrender_traits; extern crate webvr_traits;