Implement unspecified websocket close code (fixes issue #8158)

This commit is contained in:
Yanir Seroussi 2015-11-27 16:52:02 +11:00
parent bc618b0d53
commit ad9accf8e7
2 changed files with 21 additions and 10 deletions

View file

@ -126,6 +126,24 @@ const BLOCKED_PORTS_LIST: &'static [u16] = &[
6000, // x11
];
// Close codes defined in https://tools.ietf.org/html/rfc6455#section-7.4.1
// Names are from https://github.com/mozilla/gecko-dev/blob/master/netwerk/protocol/websocket/nsIWebSocketChannel.idl
#[allow(dead_code)]
mod close_code {
pub const NORMAL: u16 = 1000;
pub const GOING_AWAY: u16 = 1001;
pub const PROTOCOL_ERROR: u16 = 1002;
pub const UNSUPPORTED_DATATYPE: u16 = 1003;
pub const NO_STATUS: u16 = 1005;
pub const ABNORMAL: u16 = 1006;
pub const INVALID_PAYLOAD: u16 = 1007;
pub const POLICY_VIOLATION: u16 = 1008;
pub const TOO_LARGE: u16 = 1009;
pub const EXTENSION_MISSING: u16 = 1010;
pub const INTERNAL_ERROR: u16 = 1011;
pub const TLS_FAILED: u16 = 1015;
}
#[dom_struct]
pub struct WebSocket {
eventtarget: EventTarget,
@ -426,8 +444,8 @@ impl WebSocketMethods for WebSocket {
if let Some(code) = code {
//Check code is NOT 1000 NOR in the range of 3000-4999 (inclusive)
if code != 1000 && (code < 3000 || code > 4999) {
//Fail if the supplied code isn't normal and isn't reserved for libraries, frameworks, and applications
if code != close_code::NORMAL && (code < 3000 || code > 4999) {
return Err(Error::InvalidAccess);
}
}
@ -450,9 +468,7 @@ impl WebSocketMethods for WebSocket {
WebSocketRequestState::Open => {
//Closing handshake not started - still in open
//Start the closing by setting the code and reason if they exist
if let Some(code) = code {
self.code.set(code);
}
self.code.set(code.unwrap_or(close_code::NO_STATUS));
if let Some(reason) = reason {
*self.reason.borrow_mut() = reason.0;
}