Skip to content
Permalink
Browse files

WebSocket constructor should not panic

Make an early return when the WebSocket connection fails in the constructor.
Also let the WebSocket connection to be closed when the connection could
not be established.

Fixes #6082.
  • Loading branch information...
Jinwoo-Song authored and Ms2ger committed May 16, 2015
1 parent 4435e6f commit 7781f331d3407a895f1dadeeff3a74ad0c7f5ca8
@@ -141,7 +141,16 @@ impl WebSocket {

// TODO Client::connect does not conform to RFC 6455
// see https://github.com/cyderize/rust-websocket/issues/38
let request = Client::connect(parsed_url).unwrap();
let request = match Client::connect(parsed_url) {
Ok(request) => request,
Err(_) => {
let global_root = ws_root.global.root();
let address = Trusted::new(global_root.r().get_cx(), ws_root, global_root.r().script_chan().clone());
let task = box WebSocketTaskHandler::new(address, WebSocketTask::Close);
global_root.r().script_chan().send(ScriptMsg::RunnableMsg(task)).unwrap();
return Ok(Temporary::from_rooted(ws_root));
}
};
let response = request.send().unwrap();
response.validate().unwrap();
ws_root.ready_state.set(WebSocketRequestState::Open);
@@ -1,3 +1,6 @@
[progress.html]
type: testharness
expected: TIMEOUT
[W3C WebSocket API - Create WebSocket - Pass a URL with a non ws/wss scheme - SYNTAX_ERR is thrown]
expected: FAIL

@@ -1,3 +1,5 @@
[Create-Secure-blocked-port.htm]
type: testharness
expected: CRASH
[W3C WebSocket API - Create Secure WebSocket - Pass a URL with a blocked port - SECURITY_ERR should be thrown]
expected: FAIL

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,3 +1,5 @@
[021.html]
type: testharness
expected: CRASH
[WebSockets: Same sub protocol twice]
expected: FAIL

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,3 +1,6 @@
[020.html]
type: testharness
expected: CRASH
expected: TIMEOUT
[WebSockets: error events]
expected: TIMEOUT

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -539,6 +539,12 @@
"url": "/_mozilla/mozilla/union.html"
}
],
"mozilla/websocket_connection_fail.html": [
{
"path": "mozilla/websocket_connection_fail.html",
"url": "/_mozilla/mozilla/websocket_connection_fail.html"
}
],
"mozilla/window.html": [
{
"path": "mozilla/window.html",
@@ -0,0 +1,19 @@
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
async_test(function() {
var onclose = 0;
var ws = new WebSocket("ws://wrong_url");
ws.onclose = this.step_func_done(function(ev) {
onclose++;
assert_equals(onclose, 1);
});
});
</script>
</body>
</html>

0 comments on commit 7781f33

Please sign in to comment.
You can’t perform that action at this time.