RESOLVED FIXED 65247
WebSocket: Accept multiple subprotocols
https://bugs.webkit.org/show_bug.cgi?id=65247
Summary WebSocket: Accept multiple subprotocols
Yuta Kitamura
Reported 2011-07-27 04:58:32 PDT
According to current WebSocket API specification, WebSocket constructor can accept an array of subprotocols: <http://dev.w3.org/html5/websockets/> [Constructor(in DOMString url, in optional DOMString protocols), Constructor(in DOMString url, in optional DOMString[] protocols)] interface WebSocket { ... } When WebSocket receives multiple subprotocols, it must send Sec-WebSocket-Protocol header field in the handshake request as in hybi protocol specification: <http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10> Sec-WebSocket-Protocol: chat, superchat
Attachments
Patch (28.10 KB, patch)
2011-08-08 19:38 PDT, Yuta Kitamura
no flags
Patch v2 (45.40 KB, patch)
2011-08-09 23:05 PDT, Yuta Kitamura
no flags
Yuta Kitamura
Comment 1 2011-08-08 19:38:17 PDT
Kent Tamura
Comment 2 2011-08-08 21:37:11 PDT
Comment on attachment 103327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=103327&action=review > Source/WebCore/websockets/WebSocket.cpp:59 > +static bool isSeparator(UChar character) This should be 'inline' > Source/WebCore/websockets/WebSocket.cpp:66 > + if (character == '(' || character == ')' || character == '<' || character == '>' || character == '@' > + || character == ',' || character == ';' || character == ':' || character == '\\' || character == '"' > + || character == '/' || character == '[' || character == ']' || character == '?' || character == '=' > + || character == '{' || character == '}' || character == ' ' || character == '\t') > + return true; > + return false; You can write just return character == '(' .... Test coverage for this code is very low. Let's improve the performance of this code by checking "character is smaller than the minimum separator character" and "character is larger than the minimum separator character". > Source/WebCore/websockets/WebSocket.cpp:75 > + for (size_t i = 0; i < protocol.length(); ++i) > + if (protocol[i] < 0x21 || protocol[i] > 0x7E || isSeparator(protocol[i])) > + return false; * You have to enclose the content of 'for' with {} because the content has two physical lines. * What are 0x21 and 0x7E? > Source/WebCore/websockets/WebSocket.cpp:182 > + // Emulate JavaScript's Array.toString() behavior. > + StringBuilder builder; I have a question. Should we support protocols-in-array in Hixie76? > Source/WebCore/websockets/WebSocket.cpp:206 > + for (size_t i = 0; i < protocols.size(); ++i) > + if (!isValidProtocolString(protocols[i])) { > + scriptExecutionContext()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, "Wrong protocol for WebSocket '" + encodeProtocolString(protocols[i]) + "'", 0, scriptExecutionContext()->securityOrigin()->toString(), 0); You have to enclose the 'for' content with {}. > Source/WebCore/websockets/WebSocket.cpp:218 > + for (size_t i = 0; i < protocols.size(); ++i) > + for (size_t j = i + 1; j < protocols.size(); ++j) > + if (protocols[i] == protocols[j]) { > + scriptExecutionContext()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, "WebSocket protocols contain duplicates: '" + encodeProtocolString(protocols[i]) + "'", 0, scriptExecutionContext()->securityOrigin()->toString(), 0); > + m_state = CLOSED; > + ec = SYNTAX_ERR; > + return; > + } O(N^2) algorithm is not acceptable. > Source/WebCore/websockets/WebSocket.cpp:228 > + if (!protocols.isEmpty()) { > + StringBuilder builder; > + for (size_t i = 0; i < protocols.size(); ++i) { > + if (i) > + builder.append(", "); > + builder.append(protocols[i]); > + } > + protocolString = builder.toString(); > + } This phrase appears twice in this function.
Yuta Kitamura
Comment 3 2011-08-09 04:20:47 PDT
Comment on attachment 103327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=103327&action=review >> Source/WebCore/websockets/WebSocket.cpp:182 >> + StringBuilder builder; > > I have a question. Should we support protocols-in-array in Hixie76? If you pass a non-string object, the value will be automatically converted to a string by calling object.toString(). Auto-generated IDL stubs follow this rule, and Web IDL spec seems to require this behavior. I admit passing an array is weird. I just tried hard to not lose the compatibility.
Yuta Kitamura
Comment 4 2011-08-09 23:05:12 PDT
Created attachment 103445 [details] Patch v2
Yuta Kitamura
Comment 5 2011-08-09 23:16:10 PDT
Comment on attachment 103327 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=103327&action=review >> Source/WebCore/websockets/WebSocket.cpp:59 >> +static bool isSeparator(UChar character) > > This should be 'inline' Fixed. >> Source/WebCore/websockets/WebSocket.cpp:66 >> + return false; > > You can write just > return character == '(' .... > > Test coverage for this code is very low. > > Let's improve the performance of this code by checking "character is smaller than the minimum separator character" and "character is larger than the minimum separator character". This code was rewritten. Added a new test to check this function. >> Source/WebCore/websockets/WebSocket.cpp:75 >> + return false; > > * You have to enclose the content of 'for' with {} because the content has two physical lines. > > * What are 0x21 and 0x7E? Added a brace. Added new constants to represent these values. >> Source/WebCore/websockets/WebSocket.cpp:206 >> + scriptExecutionContext()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, "Wrong protocol for WebSocket '" + encodeProtocolString(protocols[i]) + "'", 0, scriptExecutionContext()->securityOrigin()->toString(), 0); > > You have to enclose the 'for' content with {}. Fixed. >> Source/WebCore/websockets/WebSocket.cpp:218 >> + } > > O(N^2) algorithm is not acceptable. Fixed using HashSet<String>. >> Source/WebCore/websockets/WebSocket.cpp:228 >> + } > > This phrase appears twice in this function. Added a new function "joinStrings".
Kent Tamura
Comment 6 2011-08-10 00:39:43 PDT
Comment on attachment 103445 [details] Patch v2 looks ok
WebKit Review Bot
Comment 7 2011-08-10 02:07:34 PDT
Comment on attachment 103445 [details] Patch v2 Clearing flags on attachment: 103445 Committed r92757: <http://trac.webkit.org/changeset/92757>
WebKit Review Bot
Comment 8 2011-08-10 02:07:43 PDT
All reviewed patches have been landed. Closing bug.
Note You need to log in before you can comment on or make changes to this bug.