2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef WebSocketHandshake_h
32 #define WebSocketHandshake_h
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"
46 class WebSocketHandshake {
47 WTF_MAKE_NONCOPYABLE(WebSocketHandshake); WTF_MAKE_FAST_ALLOCATED;
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.
53 Incomplete, Normal, Failed, Connected, ModeMax
55 WebSocketHandshake(const KURL&, const String& protocol, Document*);
56 ~WebSocketHandshake();
58 const KURL& url() const;
59 void setURL(const KURL&);
60 const String host() const;
62 const String& clientProtocol() const;
63 void setClientProtocol(const String&);
67 String clientOrigin() const;
68 String clientLocation() const;
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
73 CString clientHandshakeMessage() const;
74 // Builds an object representing WebSocket opening handshake to pass to the
76 PassRefPtr<WebSocketHandshakeRequest> clientHandshakeRequest() const;
78 // We're collecting data for histogram in the destructor. Note that calling
79 // this method affects that.
83 int readServerHandshake(const char* header, size_t len);
85 // Returns a string indicating the reason of failure if mode() == Failed.
86 String failureReason() const;
88 const AtomicString& serverWebSocketProtocol() const;
89 const AtomicString& serverUpgrade() const;
90 const AtomicString& serverConnection() const;
91 const AtomicString& serverWebSocketAccept() const;
92 String acceptedExtensions() const;
94 const WebSocketHandshakeResponse& serverHandshakeResponse() const;
96 void addExtensionProcessor(PassOwnPtr<WebSocketExtensionProcessor>);
98 static String getExpectedWebSocketAccept(const String& secWebSocketKey);
101 KURL httpURLForAuthenticationAndCookies() const;
103 int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
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();
111 String m_clientProtocol;
113 Document* m_document;
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;
123 String m_failureReason;
125 String m_secWebSocketKey;
126 String m_expectedAccept;
128 WebSocketExtensionDispatcher m_extensionDispatcher;
131 } // namespace WebCore
133 #endif // WebSocketHandshake_h