Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / websockets / WebSocketHandshake.h
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 Document;
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, Document*);
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     // Builds a WebSocket opening handshake string to send to the server.
71     // Cookie headers will be added later by the platform code for security
72     // reason.
73     CString clientHandshakeMessage() const;
74     // Builds an object representing WebSocket opening handshake to pass to the
75     // inspector.
76     PassRefPtr<WebSocketHandshakeRequest> clientHandshakeRequest() const;
77
78     // We're collecting data for histogram in the destructor. Note that calling
79     // this method affects that.
80     void reset();
81     void clearDocument();
82
83     int readServerHandshake(const char* header, size_t len);
84     Mode mode() const;
85     // Returns a string indicating the reason of failure if mode() == Failed.
86     String failureReason() const;
87
88     const AtomicString& serverWebSocketProtocol() const;
89     const AtomicString& serverUpgrade() const;
90     const AtomicString& serverConnection() const;
91     const AtomicString& serverWebSocketAccept() const;
92     String acceptedExtensions() const;
93
94     const WebSocketHandshakeResponse& serverHandshakeResponse() const;
95
96     void addExtensionProcessor(PassOwnPtr<WebSocketExtensionProcessor>);
97
98     static String getExpectedWebSocketAccept(const String& secWebSocketKey);
99
100 private:
101     KURL httpURLForAuthenticationAndCookies() const;
102
103     int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
104
105     // Reads all headers except for the two predefined ones.
106     const char* readHTTPHeaders(const char* start, const char* end);
107     void processHeaders();
108     bool checkResponseHeaders();
109
110     KURL m_url;
111     String m_clientProtocol;
112     bool m_secure;
113     Document* m_document;
114
115     Mode m_mode;
116
117     // Stores a received server's handshake. The order of headers is not
118     // guaranteed to be preserved. Duplicated headers may be dropped. Values may
119     // be rebuilt after parsing, so they can be different from the original data
120     // received from the server.
121     WebSocketHandshakeResponse m_response;
122
123     String m_failureReason;
124
125     String m_secWebSocketKey;
126     String m_expectedAccept;
127
128     WebSocketExtensionDispatcher m_extensionDispatcher;
129 };
130
131 } // namespace WebCore
132
133 #endif // WebSocketHandshake_h