2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "modules/websockets/WebSocketExtensionDispatcher.h"
35 #include "modules/websockets/WebSocketExtensionParser.h"
37 #include "wtf/ASCIICType.h"
38 #include "wtf/HashMap.h"
39 #include "wtf/text/CString.h"
40 #include "wtf/text/StringHash.h"
44 void WebSocketExtensionDispatcher::reset()
49 void WebSocketExtensionDispatcher::addProcessor(PassOwnPtr<WebSocketExtensionProcessor> processor)
51 for (size_t i = 0; i < m_processors.size(); ++i) {
52 if (m_processors[i]->extensionToken() == processor->extensionToken())
55 ASSERT(processor->handshakeString().length());
56 ASSERT(!processor->handshakeString().contains('\n'));
57 ASSERT(!processor->handshakeString().contains(static_cast<UChar>('\0')));
58 m_processors.append(processor);
61 const String WebSocketExtensionDispatcher::createHeaderValue() const
63 size_t numProcessors = m_processors.size();
67 StringBuilder builder;
68 builder.append(m_processors[0]->handshakeString());
69 for (size_t i = 1; i < numProcessors; ++i) {
70 builder.appendLiteral(", ");
71 builder.append(m_processors[i]->handshakeString());
73 return builder.toString();
76 void WebSocketExtensionDispatcher::appendAcceptedExtension(const String& extensionToken, HashMap<String, String>& extensionParameters)
78 if (!m_acceptedExtensionsBuilder.isEmpty())
79 m_acceptedExtensionsBuilder.appendLiteral(", ");
80 m_acceptedExtensionsBuilder.append(extensionToken);
81 // FIXME: Should use ListHashSet to keep the order of the parameters.
82 for (HashMap<String, String>::const_iterator iterator = extensionParameters.begin(); iterator != extensionParameters.end(); ++iterator) {
83 m_acceptedExtensionsBuilder.appendLiteral("; ");
84 m_acceptedExtensionsBuilder.append(iterator->key);
85 if (!iterator->value.isNull()) {
86 m_acceptedExtensionsBuilder.append('=');
87 m_acceptedExtensionsBuilder.append(iterator->value);
92 void WebSocketExtensionDispatcher::fail(const String& reason)
94 m_failureReason = reason;
95 m_acceptedExtensionsBuilder.clear();
98 bool WebSocketExtensionDispatcher::processHeaderValue(const String& headerValue)
100 if (!headerValue.length())
103 // If we don't send Sec-WebSocket-Extensions header, the server should not return the header.
104 if (!m_processors.size()) {
105 fail("Response must not include 'Sec-WebSocket-Extensions' header if not present in request: " + headerValue);
109 const CString headerValueData = headerValue.utf8();
110 WebSocketExtensionParser parser(headerValueData.data(), headerValueData.data() + headerValueData.length());
111 while (!parser.finished()) {
112 String extensionToken;
113 HashMap<String, String> extensionParameters;
114 if (!parser.parseExtension(extensionToken, extensionParameters)) {
115 fail("Invalid 'Sec-WebSocket-Extensions' header");
120 for (index = 0; index < m_processors.size(); ++index) {
121 WebSocketExtensionProcessor* processor = m_processors[index].get();
122 if (extensionToken == processor->extensionToken()) {
123 if (processor->processResponse(extensionParameters)) {
124 appendAcceptedExtension(extensionToken, extensionParameters);
127 fail("Error in " + extensionToken + ": " + processor->failureReason());
131 // There is no extension which can process the response.
132 if (index == m_processors.size()) {
133 fail("Found an unsupported extension '" + extensionToken + "' in 'Sec-WebSocket-Extensions' header");
137 return parser.parsedSuccessfully();
140 String WebSocketExtensionDispatcher::acceptedExtensions() const
142 if (m_acceptedExtensionsBuilder.isEmpty())
144 return m_acceptedExtensionsBuilder.toString();
147 String WebSocketExtensionDispatcher::failureReason() const
149 return m_failureReason;