1 | /* |
2 | * Copyright (C) 2012 Google Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are |
6 | * met: |
7 | * |
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 |
13 | * distribution. |
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. |
17 | * |
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. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | |
33 | #include "WebSocketExtensionDispatcher.h" |
34 | #include "WebSocketExtensionParser.h" |
35 | #include <wtf/ASCIICType.h> |
36 | #include <wtf/HashMap.h> |
37 | #include <wtf/text/CString.h> |
38 | #include <wtf/text/StringHash.h> |
39 | |
40 | namespace WebCore { |
41 | |
42 | void WebSocketExtensionDispatcher::reset() |
43 | { |
44 | m_processors.clear(); |
45 | } |
46 | |
47 | void WebSocketExtensionDispatcher::addProcessor(std::unique_ptr<WebSocketExtensionProcessor> processor) |
48 | { |
49 | for (auto& extensionProcessor : m_processors) { |
50 | if (extensionProcessor->extensionToken() == processor->extensionToken()) |
51 | return; |
52 | } |
53 | ASSERT(processor->handshakeString().length()); |
54 | ASSERT(!processor->handshakeString().contains('\n')); |
55 | ASSERT(!processor->handshakeString().contains(static_cast<UChar>('\0'))); |
56 | m_processors.append(WTFMove(processor)); |
57 | } |
58 | |
59 | const String WebSocketExtensionDispatcher::() const |
60 | { |
61 | size_t numProcessors = m_processors.size(); |
62 | if (!numProcessors) |
63 | return String(); |
64 | |
65 | StringBuilder builder; |
66 | builder.append(m_processors[0]->handshakeString()); |
67 | for (size_t i = 1; i < numProcessors; ++i) { |
68 | builder.appendLiteral(", " ); |
69 | builder.append(m_processors[i]->handshakeString()); |
70 | } |
71 | return builder.toString(); |
72 | } |
73 | |
74 | void WebSocketExtensionDispatcher::appendAcceptedExtension(const String& extensionToken, HashMap<String, String>& extensionParameters) |
75 | { |
76 | if (!m_acceptedExtensionsBuilder.isEmpty()) |
77 | m_acceptedExtensionsBuilder.appendLiteral(", " ); |
78 | m_acceptedExtensionsBuilder.append(extensionToken); |
79 | // FIXME: Should use ListHashSet to keep the order of the parameters. |
80 | for (auto& parameter : extensionParameters) { |
81 | m_acceptedExtensionsBuilder.appendLiteral("; " ); |
82 | m_acceptedExtensionsBuilder.append(parameter.key); |
83 | if (!parameter.value.isNull()) { |
84 | m_acceptedExtensionsBuilder.append('='); |
85 | m_acceptedExtensionsBuilder.append(parameter.value); |
86 | } |
87 | } |
88 | } |
89 | |
90 | void WebSocketExtensionDispatcher::fail(const String& reason) |
91 | { |
92 | m_failureReason = reason; |
93 | m_acceptedExtensionsBuilder.clear(); |
94 | } |
95 | |
96 | bool WebSocketExtensionDispatcher::(const String& ) |
97 | { |
98 | if (!headerValue.length()) |
99 | return true; |
100 | |
101 | // If we don't send Sec-WebSocket-Extensions header, the server should not return the header. |
102 | if (!m_processors.size()) { |
103 | fail("Received unexpected Sec-WebSocket-Extensions header" ); |
104 | return false; |
105 | } |
106 | |
107 | const CString = headerValue.utf8(); |
108 | WebSocketExtensionParser parser(headerValueData.data(), headerValueData.data() + headerValueData.length()); |
109 | while (!parser.finished()) { |
110 | String extensionToken; |
111 | HashMap<String, String> extensionParameters; |
112 | if (!parser.parseExtension(extensionToken, extensionParameters)) { |
113 | fail("Sec-WebSocket-Extensions header is invalid" ); |
114 | return false; |
115 | } |
116 | |
117 | size_t index = 0; |
118 | for (auto& processor : m_processors) { |
119 | if (extensionToken == processor->extensionToken()) { |
120 | if (processor->processResponse(extensionParameters)) { |
121 | appendAcceptedExtension(extensionToken, extensionParameters); |
122 | break; |
123 | } |
124 | fail(processor->failureReason()); |
125 | return false; |
126 | } |
127 | ++index; |
128 | } |
129 | // There is no extension which can process the response. |
130 | if (index == m_processors.size()) { |
131 | fail("Received unexpected extension: " + extensionToken); |
132 | return false; |
133 | } |
134 | } |
135 | return parser.parsedSuccessfully(); |
136 | } |
137 | |
138 | String WebSocketExtensionDispatcher::acceptedExtensions() const |
139 | { |
140 | if (m_acceptedExtensionsBuilder.isEmpty()) |
141 | return String(); |
142 | return m_acceptedExtensionsBuilder.toStringPreserveCapacity(); |
143 | } |
144 | |
145 | String WebSocketExtensionDispatcher::failureReason() const |
146 | { |
147 | return m_failureReason; |
148 | } |
149 | |
150 | } // namespace WebCore |
151 | |