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
7581bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverWebSocketOrigin() const;
7681bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverWebSocketLocation() const;
7781bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverWebSocketProtocol() const;
7881bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverSetCookie() const;
7981bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverSetCookie2() const;
8081bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverUpgrade() const;
8181bc750723a18f21cd17d1b173cd2a4dda9cea6eBen Murdoch        String serverConnection() const;
82231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
8306ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        const WebSocketHandshakeResponse& serverHandshakeResponse() const;
8406ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
85231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    private:
86231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        KURL httpURLForAuthenticationAndCookies() const;
87231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
8806ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
8906ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
90231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // Reads all headers except for the two predefined ones.
9106ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        const char* readHTTPHeaders(const char* start, const char* end);
9206ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        void processHeaders();
93e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        bool checkResponseHeaders();
94231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
95231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        KURL m_url;
96231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_clientProtocol;
97231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        bool m_secure;
98231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        ScriptExecutionContext* m_context;
99231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
100231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        Mode m_mode;
101231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
102e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        String m_secWebSocketKey1;
103e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        String m_secWebSocketKey2;
104e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        unsigned char m_key3[8];
105e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        unsigned char m_expectedChallengeResponse[16];
10606ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen
10706ea8e899e48f1f2f396b70e63fae369f2f23232Kristian Monsen        WebSocketHandshakeResponse m_response;
108231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    };
109231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
110d0825bca7fe65beaee391d30da42e937db621564Steve Block} // namespace WebCore
111231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
112d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // ENABLE(WEB_SOCKETS)
113231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
114d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // WebSocketHandshake_h
115