Trigger WebSocket error event

Trigger a WebSocket error after receiving an invalid message from the server
This commit is contained in:
Sam Gibson 2015-12-07 14:35:10 +11:00 committed by Ms2ger
parent b188cb542e
commit 4f736e6f7c
9 changed files with 23 additions and 46 deletions

View file

@ -107,7 +107,11 @@ pub fn init(connect: WebSocketCommunicate, connect_data: WebSocketConnectData, c
for message in receiver.incoming_messages() { for message in receiver.incoming_messages() {
let message: Message = match message { let message: Message = match message {
Ok(m) => m, Ok(m) => m,
Err(_) => break, Err(e) => {
debug!("Error receiving incoming WebSocket message: {:?}", e);
let _ = resource_event_sender.send(WebSocketNetworkEvent::Fail);
break;
}
}; };
let message = match message.opcode { let message = match message.opcode {
Type::Text => MessageData::Text(String::from_utf8_lossy(&message.payload).into_owned()), Type::Text => MessageData::Text(String::from_utf8_lossy(&message.payload).into_owned()),

View file

@ -141,7 +141,7 @@ pub fn close_the_websocket_connection(address: Trusted<WebSocket>,
code: Option<u16>, code: Option<u16>,
reason: String) { reason: String) {
let close_task = box CloseTask { let close_task = box CloseTask {
addr: address, address: address,
failed: false, failed: false,
code: code, code: code,
reason: Some(reason), reason: Some(reason),
@ -151,7 +151,7 @@ pub fn close_the_websocket_connection(address: Trusted<WebSocket>,
pub fn fail_the_websocket_connection(address: Trusted<WebSocket>, sender: Box<ScriptChan>) { pub fn fail_the_websocket_connection(address: Trusted<WebSocket>, sender: Box<ScriptChan>) {
let close_task = box CloseTask { let close_task = box CloseTask {
addr: address, address: address,
failed: true, failed: true,
code: Some(close_code::ABNORMAL), code: Some(close_code::ABNORMAL),
reason: None, reason: None,
@ -271,7 +271,7 @@ impl WebSocket {
match event { match event {
WebSocketNetworkEvent::ConnectionEstablished(headers, protocols) => { WebSocketNetworkEvent::ConnectionEstablished(headers, protocols) => {
let open_thread = box ConnectionEstablishedTask { let open_thread = box ConnectionEstablishedTask {
addr: moved_address.clone(), address: moved_address.clone(),
headers: headers, headers: headers,
protocols: protocols, protocols: protocols,
}; };
@ -324,7 +324,7 @@ impl WebSocket {
self.clearing_buffer.set(true); self.clearing_buffer.set(true);
let task = box BufferedAmountTask { let task = box BufferedAmountTask {
addr: address, address: address,
}; };
chan.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)).unwrap(); chan.send(CommonScriptMsg::RunnableMsg(WebSocketEvent, task)).unwrap();
@ -456,20 +456,20 @@ impl WebSocketMethods for WebSocket {
/// Task queued when *the WebSocket connection is established*. /// Task queued when *the WebSocket connection is established*.
struct ConnectionEstablishedTask { struct ConnectionEstablishedTask {
addr: Trusted<WebSocket>, address: Trusted<WebSocket>,
protocols: Vec<String>, protocols: Vec<String>,
headers: Headers, headers: Headers,
} }
impl Runnable for ConnectionEstablishedTask { impl Runnable for ConnectionEstablishedTask {
fn handler(self: Box<Self>) { fn handler(self: Box<Self>) {
let ws = self.addr.root(); let ws = self.address.root();
let global = ws.r().global(); let global = ws.r().global();
// Step 1: Protocols. // Step 1: Protocols.
if !self.protocols.is_empty() && self.headers.get::<WebSocketProtocol>().is_none() { if !self.protocols.is_empty() && self.headers.get::<WebSocketProtocol>().is_none() {
let sender = global.r().networking_task_source(); let sender = global.r().networking_task_source();
fail_the_websocket_connection(self.addr, sender); fail_the_websocket_connection(self.address, sender);
return; return;
} }
@ -502,7 +502,7 @@ impl Runnable for ConnectionEstablishedTask {
} }
struct BufferedAmountTask { struct BufferedAmountTask {
addr: Trusted<WebSocket>, address: Trusted<WebSocket>,
} }
impl Runnable for BufferedAmountTask { impl Runnable for BufferedAmountTask {
@ -512,7 +512,7 @@ impl Runnable for BufferedAmountTask {
// reaches step 1. In our implementation, the bytes will already have been sent on a background // reaches step 1. In our implementation, the bytes will already have been sent on a background
// thread. // thread.
fn handler(self: Box<Self>) { fn handler(self: Box<Self>) {
let ws = self.addr.root(); let ws = self.address.root();
ws.buffered_amount.set(0); ws.buffered_amount.set(0);
ws.clearing_buffer.set(false); ws.clearing_buffer.set(false);
@ -520,7 +520,7 @@ impl Runnable for BufferedAmountTask {
} }
struct CloseTask { struct CloseTask {
addr: Trusted<WebSocket>, address: Trusted<WebSocket>,
failed: bool, failed: bool,
code: Option<u16>, code: Option<u16>,
reason: Option<String>, reason: Option<String>,
@ -528,7 +528,7 @@ struct CloseTask {
impl Runnable for CloseTask { impl Runnable for CloseTask {
fn handler(self: Box<Self>) { fn handler(self: Box<Self>) {
let ws = self.addr.root(); let ws = self.address.root();
let ws = ws.r(); let ws = ws.r();
let global = ws.global(); let global = ws.global();
@ -545,9 +545,7 @@ impl Runnable for CloseTask {
// Step 2. // Step 2.
if self.failed { if self.failed {
ws.upcast().fire_event("error", ws.upcast().fire_simple_event("error");
EventBubbles::DoesNotBubble,
EventCancelable::Cancelable);
} }
// Step 3. // Step 3.

View file

@ -1,6 +1,5 @@
[010.html] [010.html]
type: testharness type: testharness
expected: TIMEOUT
[WebSockets: protocol in response but no requested protocol] [WebSockets: protocol in response but no requested protocol]
expected: TIMEOUT expected: FAIL

View file

@ -1,6 +0,0 @@
[011.html]
type: testharness
expected: TIMEOUT
[WebSockets: protocol mismatch]
expected: TIMEOUT

View file

@ -0,0 +1,4 @@
[005.html]
type: testharness
[WebSockets: setting HttpOnly cookies in ws response, checking ws request]
expected: FAIL

View file

@ -1,6 +0,0 @@
[015.html]
type: testharness
expected: TIMEOUT
[WebSockets: instanceof on events]
expected: TIMEOUT

View file

@ -1,6 +0,0 @@
[017.html]
type: testharness
expected: TIMEOUT
[WebSockets: this, e.target, e.currentTarget, e.eventPhase]
expected: TIMEOUT

View file

@ -1,9 +0,0 @@
[018.html]
type: testharness
expected: TIMEOUT
[error event]
expected: TIMEOUT
[close event]
expected: TIMEOUT

View file

@ -1,6 +1,5 @@
[005.html] [005.html]
type: testharness type: testharness
expected: TIMEOUT
[WebSockets: proper first line] [WebSockets: proper first line]
expected: TIMEOUT expected: FAIL