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