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/WebSocketExtensionParser.h"
35 #include "wtf/ASCIICType.h"
36 #include "wtf/text/CString.h"
40 WebSocketExtensionParser::ParserStateBackup::~ParserStateBackup()
43 m_parser->m_current = m_current;
47 bool WebSocketExtensionParser::finished()
49 return m_current >= m_end;
52 bool WebSocketExtensionParser::parsedSuccessfully()
54 return m_current == m_end;
57 static bool isSeparator(char character)
59 static const char* separatorCharacters = "()<>@,;:\\\"/[]?={} \t";
60 const char* p = strchr(separatorCharacters, character);
64 void WebSocketExtensionParser::skipSpaces()
66 while (m_current < m_end && (*m_current == ' ' || *m_current == '\t'))
70 bool WebSocketExtensionParser::consumeToken()
72 ParserStateBackup backup(this);
74 const char* start = m_current;
75 while (m_current < m_end && isASCIIPrintable(*m_current) && !isSeparator(*m_current))
77 if (start < m_current) {
78 m_currentToken = String(start, m_current - start);
85 bool WebSocketExtensionParser::consumeQuotedString()
87 ParserStateBackup backup(this);
89 if (m_current >= m_end || *m_current != '"')
94 while (m_current < m_end && *m_current != '"') {
95 if (*m_current == '\\' && ++m_current >= m_end)
97 // RFC6455 requires that the value after quoted-string unescaping
98 // MUST conform to the 'token' ABNF.
99 if (!isASCIIPrintable(*m_current) || isSeparator(*m_current))
101 buffer.append(*m_current);
104 if (m_current >= m_end || *m_current != '"' || buffer.isEmpty())
106 m_currentToken = String::fromUTF8(buffer.data(), buffer.size());
112 bool WebSocketExtensionParser::consumeQuotedStringOrToken()
114 // This is ok because consumeQuotedString() restores m_current
116 return consumeQuotedString() || consumeToken();
119 bool WebSocketExtensionParser::consumeCharacter(char character)
121 ParserStateBackup backup(this);
123 if (m_current < m_end && *m_current == character) {
131 bool WebSocketExtensionParser::parseExtension(String& extensionToken, HashMap<String, String>& extensionParameters)
133 ParserStateBackup backup(this);
134 // Parse extension-token.
138 extensionToken = currentToken();
140 // Parse extension-parameters if exists.
141 while (consumeCharacter(';')) {
145 String parameterToken = currentToken();
146 if (consumeCharacter('=')) {
147 if (consumeQuotedStringOrToken())
148 extensionParameters.add(parameterToken, currentToken());
152 extensionParameters.add(parameterToken, String());
156 if (!finished() && !consumeCharacter(','))