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