Home | History | Annotate | Download | only in websockets
      1 /*
      2  * Copyright (C) 2011 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 #ifndef WebSocketHandshake_h
     32 #define WebSocketHandshake_h
     33 
     34 #include "modules/websockets/WebSocketExtensionDispatcher.h"
     35 #include "modules/websockets/WebSocketExtensionProcessor.h"
     36 #include "platform/heap/Handle.h"
     37 #include "platform/network/WebSocketHandshakeRequest.h"
     38 #include "platform/network/WebSocketHandshakeResponse.h"
     39 #include "platform/weborigin/KURL.h"
     40 #include "wtf/PassOwnPtr.h"
     41 #include "wtf/text/WTFString.h"
     42 
     43 namespace blink {
     44 
     45 class Document;
     46 
     47 class WebSocketHandshake : public GarbageCollectedFinalized<WebSocketHandshake> {
     48     WTF_MAKE_NONCOPYABLE(WebSocketHandshake);
     49 public:
     50     // This enum is reused for histogram. When this needs to be modified, add a
     51     // new enum for histogram and convert mode values into values in the new
     52     // enum to keep new data consistent with old one.
     53     enum Mode {
     54         Incomplete, Normal, Failed, Connected, ModeMax
     55     };
     56     WebSocketHandshake(const KURL&, const String& protocol, Document*);
     57     ~WebSocketHandshake();
     58 
     59     const KURL& url() const;
     60     void setURL(const KURL&);
     61     const String host() const;
     62 
     63     const String& clientProtocol() const;
     64     void setClientProtocol(const String&);
     65 
     66     bool secure() const;
     67 
     68     String clientOrigin() const;
     69     String clientLocation() const;
     70 
     71     // Builds a WebSocket opening handshake string to send to the server.
     72     // Cookie headers will be added later by the platform code for security
     73     // reason.
     74     CString clientHandshakeMessage() const;
     75     // Builds an object representing WebSocket opening handshake to pass to the
     76     // inspector.
     77     PassRefPtr<WebSocketHandshakeRequest> clientHandshakeRequest() const;
     78 
     79     // We're collecting data for histogram in the destructor. Note that calling
     80     // this method affects that.
     81     void reset();
     82     void clearDocument();
     83 
     84     int readServerHandshake(const char* header, size_t len);
     85     Mode mode() const;
     86     // Returns a string indicating the reason of failure if mode() == Failed.
     87     String failureReason() const;
     88 
     89     const AtomicString& serverWebSocketProtocol() const;
     90     const AtomicString& serverUpgrade() const;
     91     const AtomicString& serverConnection() const;
     92     const AtomicString& serverWebSocketAccept() const;
     93     String acceptedExtensions() const;
     94 
     95     const WebSocketHandshakeResponse& serverHandshakeResponse() const;
     96 
     97     void addExtensionProcessor(PassOwnPtr<WebSocketExtensionProcessor>);
     98 
     99     static String getExpectedWebSocketAccept(const String& secWebSocketKey);
    100 
    101     void trace(Visitor*);
    102 
    103 private:
    104     KURL httpURLForAuthenticationAndCookies() const;
    105 
    106     int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
    107 
    108     // Reads all headers except for the two predefined ones.
    109     const char* readHTTPHeaders(const char* start, const char* end);
    110     void processHeaders();
    111     bool checkResponseHeaders();
    112 
    113     KURL m_url;
    114     String m_clientProtocol;
    115     bool m_secure;
    116     RawPtrWillBeMember<Document> m_document;
    117 
    118     Mode m_mode;
    119 
    120     // Stores a received server's handshake. The order of headers is not
    121     // guaranteed to be preserved. Duplicated headers may be dropped. Values may
    122     // be rebuilt after parsing, so they can be different from the original data
    123     // received from the server.
    124     WebSocketHandshakeResponse m_response;
    125 
    126     String m_failureReason;
    127 
    128     String m_secWebSocketKey;
    129     String m_expectedAccept;
    130 
    131     WebSocketExtensionDispatcher m_extensionDispatcher;
    132 };
    133 
    134 } // namespace blink
    135 
    136 #endif // WebSocketHandshake_h
    137