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