Home | History | Annotate | Download | only in websockets
      1 /*
      2  * Copyright (C) 2009 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 #if ENABLE(WEB_SOCKETS)
     35 
     36 #include "KURL.h"
     37 #include "PlatformString.h"
     38 #include "WebSocketHandshakeRequest.h"
     39 #include "WebSocketHandshakeResponse.h"
     40 
     41 namespace WebCore {
     42 
     43     class ScriptExecutionContext;
     44 
     45     class WebSocketHandshake {
     46         WTF_MAKE_NONCOPYABLE(WebSocketHandshake);
     47     public:
     48         enum Mode {
     49             Incomplete, Normal, Failed, Connected
     50         };
     51         WebSocketHandshake(const KURL&, const String& protocol, ScriptExecutionContext*);
     52         ~WebSocketHandshake();
     53 
     54         const KURL& url() const;
     55         void setURL(const KURL&);
     56         const String host() const;
     57 
     58         const String& clientProtocol() const;
     59         void setClientProtocol(const String& protocol);
     60 
     61         bool secure() const;
     62 
     63         String clientOrigin() const;
     64         String clientLocation() const;
     65 
     66         CString clientHandshakeMessage() const;
     67         WebSocketHandshakeRequest clientHandshakeRequest() const;
     68 
     69         void reset();
     70         void clearScriptExecutionContext();
     71 
     72         int readServerHandshake(const char* header, size_t len);
     73         Mode mode() const;
     74 
     75         String serverWebSocketOrigin() const;
     76         String serverWebSocketLocation() const;
     77         String serverWebSocketProtocol() const;
     78         String serverSetCookie() const;
     79         String serverSetCookie2() const;
     80         String serverUpgrade() const;
     81         String serverConnection() const;
     82 
     83         const WebSocketHandshakeResponse& serverHandshakeResponse() const;
     84 
     85     private:
     86         KURL httpURLForAuthenticationAndCookies() const;
     87 
     88         int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
     89 
     90         // Reads all headers except for the two predefined ones.
     91         const char* readHTTPHeaders(const char* start, const char* end);
     92         void processHeaders();
     93         bool checkResponseHeaders();
     94 
     95         KURL m_url;
     96         String m_clientProtocol;
     97         bool m_secure;
     98         ScriptExecutionContext* m_context;
     99 
    100         Mode m_mode;
    101 
    102         String m_secWebSocketKey1;
    103         String m_secWebSocketKey2;
    104         unsigned char m_key3[8];
    105         unsigned char m_expectedChallengeResponse[16];
    106 
    107         WebSocketHandshakeResponse m_response;
    108     };
    109 
    110 } // namespace WebCore
    111 
    112 #endif // ENABLE(WEB_SOCKETS)
    113 
    114 #endif // WebSocketHandshake_h
    115