Home | History | Annotate | Download | only in websockets
      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 "modules/websockets/WebSocketExtensionDispatcher.h"
     34 
     35 #include "modules/websockets/WebSocketExtensionParser.h"
     36 
     37 #include "wtf/ASCIICType.h"
     38 #include "wtf/HashMap.h"
     39 #include "wtf/text/CString.h"
     40 #include "wtf/text/StringHash.h"
     41 
     42 namespace WebCore {
     43 
     44 void WebSocketExtensionDispatcher::reset()
     45 {
     46     m_processors.clear();
     47 }
     48 
     49 void WebSocketExtensionDispatcher::addProcessor(PassOwnPtr<WebSocketExtensionProcessor> processor)
     50 {
     51     for (size_t i = 0; i < m_processors.size(); ++i) {
     52         if (m_processors[i]->extensionToken() == processor->extensionToken())
     53             return;
     54     }
     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);
     59 }
     60 
     61 const String WebSocketExtensionDispatcher::createHeaderValue() const
     62 {
     63     size_t numProcessors = m_processors.size();
     64     if (!numProcessors)
     65         return String();
     66 
     67     StringBuilder builder;
     68     builder.append(m_processors[0]->handshakeString());
     69     for (size_t i = 1; i < numProcessors; ++i) {
     70         builder.append(", ");
     71         builder.append(m_processors[i]->handshakeString());
     72     }
     73     return builder.toString();
     74 }
     75 
     76 void WebSocketExtensionDispatcher::appendAcceptedExtension(const String& extensionToken, HashMap<String, String>& extensionParameters)
     77 {
     78     if (!m_acceptedExtensionsBuilder.isEmpty())
     79         m_acceptedExtensionsBuilder.append(", ");
     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.append("; ");
     84         m_acceptedExtensionsBuilder.append(iterator->key);
     85         if (!iterator->value.isNull()) {
     86             m_acceptedExtensionsBuilder.append("=");
     87             m_acceptedExtensionsBuilder.append(iterator->value);
     88         }
     89     }
     90 }
     91 
     92 void WebSocketExtensionDispatcher::fail(const String& reason)
     93 {
     94     m_failureReason = reason;
     95     m_acceptedExtensionsBuilder.clear();
     96 }
     97 
     98 bool WebSocketExtensionDispatcher::processHeaderValue(const String& headerValue)
     99 {
    100     if (!headerValue.length())
    101         return true;
    102 
    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);
    106         return false;
    107     }
    108 
    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");
    116             return false;
    117         }
    118 
    119         size_t index;
    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);
    125                     break;
    126                 }
    127                 fail("Error in " + extensionToken + ": " + processor->failureReason());
    128                 return false;
    129             }
    130         }
    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");
    134             return false;
    135         }
    136     }
    137     return parser.parsedSuccessfully();
    138 }
    139 
    140 String WebSocketExtensionDispatcher::acceptedExtensions() const
    141 {
    142     if (m_acceptedExtensionsBuilder.isEmpty())
    143         return String();
    144     return m_acceptedExtensionsBuilder.toString();
    145 }
    146 
    147 String WebSocketExtensionDispatcher::failureReason() const
    148 {
    149     return m_failureReason;
    150 }
    151 
    152 } // namespace WebCore
    153