| 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 "WebSocketExtensionParser.h" |
| 34 | |
| 35 | #include <wtf/ASCIICType.h> |
| 36 | #include <wtf/text/CString.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | bool WebSocketExtensionParser::finished() |
| 41 | { |
| 42 | return m_current >= m_end; |
| 43 | } |
| 44 | |
| 45 | bool WebSocketExtensionParser::parsedSuccessfully() |
| 46 | { |
| 47 | return m_current == m_end; |
| 48 | } |
| 49 | |
| 50 | static bool isSeparator(char character) |
| 51 | { |
| 52 | static const char* separatorCharacters = "()<>@,;:\\\"/[]?={} \t" ; |
| 53 | const char* p = strchr(separatorCharacters, character); |
| 54 | return p && *p; |
| 55 | } |
| 56 | |
| 57 | void WebSocketExtensionParser::skipSpaces() |
| 58 | { |
| 59 | while (m_current < m_end && (*m_current == ' ' || *m_current == '\t')) |
| 60 | ++m_current; |
| 61 | } |
| 62 | |
| 63 | bool WebSocketExtensionParser::consumeToken() |
| 64 | { |
| 65 | skipSpaces(); |
| 66 | const char* start = m_current; |
| 67 | while (m_current < m_end && isASCIIPrintable(*m_current) && !isSeparator(*m_current)) |
| 68 | ++m_current; |
| 69 | if (start < m_current) { |
| 70 | m_currentToken = String(start, m_current - start); |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool WebSocketExtensionParser::consumeQuotedString() |
| 77 | { |
| 78 | skipSpaces(); |
| 79 | if (m_current >= m_end || *m_current != '"') |
| 80 | return false; |
| 81 | |
| 82 | Vector<char> buffer; |
| 83 | ++m_current; |
| 84 | while (m_current < m_end && *m_current != '"') { |
| 85 | if (*m_current == '\\' && ++m_current >= m_end) |
| 86 | return false; |
| 87 | buffer.append(*m_current); |
| 88 | ++m_current; |
| 89 | } |
| 90 | if (m_current >= m_end || *m_current != '"') |
| 91 | return false; |
| 92 | m_currentToken = String::fromUTF8(buffer.data(), buffer.size()); |
| 93 | if (m_currentToken.isNull()) |
| 94 | return false; |
| 95 | ++m_current; |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | bool WebSocketExtensionParser::consumeQuotedStringOrToken() |
| 100 | { |
| 101 | // This is ok because consumeQuotedString() doesn't update m_current or |
| 102 | // makes it same as m_end on failure. |
| 103 | return consumeQuotedString() || consumeToken(); |
| 104 | } |
| 105 | |
| 106 | bool WebSocketExtensionParser::consumeCharacter(char character) |
| 107 | { |
| 108 | skipSpaces(); |
| 109 | if (m_current < m_end && *m_current == character) { |
| 110 | ++m_current; |
| 111 | return true; |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | bool WebSocketExtensionParser::parseExtension(String& extensionToken, HashMap<String, String>& extensionParameters) |
| 117 | { |
| 118 | // Parse extension-token. |
| 119 | if (!consumeToken()) |
| 120 | return false; |
| 121 | |
| 122 | extensionToken = currentToken(); |
| 123 | |
| 124 | // Parse extension-parameters if exists. |
| 125 | while (consumeCharacter(';')) { |
| 126 | if (!consumeToken()) |
| 127 | return false; |
| 128 | |
| 129 | String parameterToken = currentToken(); |
| 130 | if (consumeCharacter('=')) { |
| 131 | if (consumeQuotedStringOrToken()) |
| 132 | extensionParameters.add(parameterToken, currentToken()); |
| 133 | else |
| 134 | return false; |
| 135 | } else |
| 136 | extensionParameters.add(parameterToken, String()); |
| 137 | } |
| 138 | if (!finished() && !consumeCharacter(',')) |
| 139 | return false; |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | } // namespace WebCore |
| 145 | |