WebSocketHandshake.h revision ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddb
1231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block/*
2231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Copyright (C) 2009 Google Inc.  All rights reserved.
3231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *
4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * Redistribution and use in source and binary forms, with or without
5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * modification, are permitted provided that the following conditions are
6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * met:
7231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *
8231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *     * Redistributions of source code must retain the above copyright
9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * notice, this list of conditions and the following disclaimer.
10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *     * Redistributions in binary form must reproduce the above
11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * copyright notice, this list of conditions and the following disclaimer
12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * in the documentation and/or other materials provided with the
13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * distribution.
14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *     * Neither the name of Google Inc. nor the names of its
15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * contributors may be used to endorse or promote products derived from
16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * this software without specific prior written permission.
17231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block *
18231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block */
30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
31231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#ifndef WebSocketHandshake_h
32231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#define WebSocketHandshake_h
33231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
34231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#if ENABLE(WEB_SOCKETS)
35231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
36231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "KURL.h"
37231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "PlatformString.h"
38dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#include "WebSocketHandshakeRequest.h"
3906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen#include "WebSocketHandshakeResponse.h"
40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
41231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blocknamespace WebCore {
42231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
43e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    class ScriptExecutionContext;
44231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
45ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddbBen Murdoch    class WebSocketHandshake {
46ab9e7a118cf1ea2e3a93dce683b2ded3e7291ddbBen Murdoch        WTF_MAKE_NONCOPYABLE(WebSocketHandshake);
47231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    public:
48231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        enum Mode {
49231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            Incomplete, Normal, Failed, Connected
50231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        };
51231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        WebSocketHandshake(const KURL&, const String& protocol, ScriptExecutionContext*);
52231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        ~WebSocketHandshake();
53231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
54231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const KURL& url() const;
55231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setURL(const KURL&);
56231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String host() const;
57231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
58231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& clientProtocol() const;
59231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setClientProtocol(const String& protocol);
60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
61231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        bool secure() const;
62231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
63231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String clientOrigin() const;
64231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String clientLocation() const;
65231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
66231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        CString clientHandshakeMessage() const;
67dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block        WebSocketHandshakeRequest clientHandshakeRequest() const;
68231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
69231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void reset();
70dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block        void clearScriptExecutionContext();
71231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
72231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        int readServerHandshake(const char* header, size_t len);
73231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        Mode mode() const;
74231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
75231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& serverWebSocketOrigin() const;
76231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setServerWebSocketOrigin(const String& webSocketOrigin);
77231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
78231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& serverWebSocketLocation() const;
79231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setServerWebSocketLocation(const String& webSocketLocation);
80231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
81231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& serverWebSocketProtocol() const;
82231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setServerWebSocketProtocol(const String& webSocketProtocol);
83231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
84231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& serverSetCookie() const;
85231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setServerSetCookie(const String& setCookie);
86231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const String& serverSetCookie2() const;
87231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setServerSetCookie2(const String& setCookie2);
88231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
8906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        const WebSocketHandshakeResponse& serverHandshakeResponse() const;
9006ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
91231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    private:
92231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        KURL httpURLForAuthenticationAndCookies() const;
93231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
9406ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
9506ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
96231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // Reads all headers except for the two predefined ones.
9706ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        const char* readHTTPHeaders(const char* start, const char* end);
9806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        void processHeaders();
99e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        bool checkResponseHeaders();
100231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
101231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        KURL m_url;
102231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_clientProtocol;
103231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        bool m_secure;
104231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        ScriptExecutionContext* m_context;
105231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
106231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        Mode m_mode;
107231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
108231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_wsOrigin;
109231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_wsLocation;
110231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_wsProtocol;
111231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_setCookie;
112231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_setCookie2;
113e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block
114e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        String m_secWebSocketKey1;
115e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        String m_secWebSocketKey2;
116e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        unsigned char m_key3[8];
117e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        unsigned char m_expectedChallengeResponse[16];
11806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
11906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        WebSocketHandshakeResponse m_response;
120231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    };
121231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
122d0825bca7fe65beaee391d30da42e937db621564Steve Block} // namespace WebCore
123231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
124d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // ENABLE(WEB_SOCKETS)
125231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
126d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // WebSocketHandshake_h
127