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#if ENABLE(WEB_SOCKETS)
35
36#include "KURL.h"
37#include "ResourceResponse.h"
38#include "WebSocketExtensionDispatcher.h"
39#include "WebSocketExtensionProcessor.h"
40#include <wtf/PassOwnPtr.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class ResourceRequest;
46class ScriptExecutionContext;
47
48class WebSocketHandshake {
49    WTF_MAKE_NONCOPYABLE(WebSocketHandshake); WTF_MAKE_FAST_ALLOCATED;
50public:
51    enum Mode {
52        Incomplete, Normal, Failed, Connected
53    };
54    WebSocketHandshake(const KURL&, const String& protocol, ScriptExecutionContext*);
55    ~WebSocketHandshake();
56
57    const KURL& url() const;
58    void setURL(const KURL&);
59    const String host() const;
60
61    const String& clientProtocol() const;
62    void setClientProtocol(const String&);
63
64    bool secure() const;
65
66    String clientOrigin() const;
67    String clientLocation() const;
68
69    CString clientHandshakeMessage() const;
70    ResourceRequest clientHandshakeRequest() const;
71
72    void reset();
73    void clearScriptExecutionContext();
74
75    int readServerHandshake(const char* header, size_t len);
76    Mode mode() const;
77    String failureReason() const; // Returns a string indicating the reason of failure if mode() == Failed.
78
79    String serverWebSocketProtocol() const;
80    String serverSetCookie() const;
81    String serverSetCookie2() const;
82    String serverUpgrade() const;
83    String serverConnection() const;
84    String serverWebSocketAccept() const;
85    String acceptedExtensions() const;
86
87    const ResourceResponse& serverHandshakeResponse() const;
88
89    void addExtensionProcessor(PassOwnPtr<WebSocketExtensionProcessor>);
90
91    static String getExpectedWebSocketAccept(const String& secWebSocketKey);
92
93private:
94    KURL httpURLForAuthenticationAndCookies() const;
95
96    int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
97
98    // Reads all headers except for the two predefined ones.
99    const char* readHTTPHeaders(const char* start, const char* end);
100    void processHeaders();
101    bool checkResponseHeaders();
102
103    KURL m_url;
104    String m_clientProtocol;
105    bool m_secure;
106    ScriptExecutionContext* m_context;
107
108    Mode m_mode;
109
110    ResourceResponse m_serverHandshakeResponse;
111
112    String m_failureReason;
113
114    String m_secWebSocketKey;
115    String m_expectedAccept;
116
117    WebSocketExtensionDispatcher m_extensionDispatcher;
118};
119
120} // namespace WebCore
121
122#endif // ENABLE(WEB_SOCKETS)
123
124#endif // WebSocketHandshake_h
125