WebSocket constructor should not panic

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

Fixes #6082.
This commit is contained in:
Jinwoo Song 2015-05-16 14:48:27 +09:00 committed by Ms2ger
parent 74ef31cfc4
commit 877c369e0b
25 changed files with 557 additions and 41 deletions

View file

@ -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);