15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2011 Google Inc.  All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef WebSocketHandshake_h
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define WebSocketHandshake_h
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "modules/websockets/WebSocketExtensionDispatcher.h"
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "modules/websockets/WebSocketExtensionProcessor.h"
36197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "platform/heap/Handle.h"
3751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/network/WebSocketHandshakeRequest.h"
3851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/network/WebSocketHandshakeResponse.h"
3951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/weborigin/KURL.h"
4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/PassOwnPtr.h"
4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/text/WTFString.h"
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
43c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class Document;
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
47c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)class WebSocketHandshake : public GarbageCollectedFinalized<WebSocketHandshake> {
48197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    WTF_MAKE_NONCOPYABLE(WebSocketHandshake);
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
5093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // This enum is reused for histogram. When this needs to be modified, add a
5193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // new enum for histogram and convert mode values into values in the new
5293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // enum to keep new data consistent with old one.
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    enum Mode {
5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        Incomplete, Normal, Failed, Connected, ModeMax
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    };
5609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    WebSocketHandshake(const KURL&, const String& protocol, Document*);
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ~WebSocketHandshake();
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const KURL& url() const;
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void setURL(const KURL&);
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const String host() const;
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const String& clientProtocol() const;
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void setClientProtocol(const String&);
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool secure() const;
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String clientOrigin() const;
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String clientLocation() const;
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
71aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // Builds a WebSocket opening handshake string to send to the server.
72aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // Cookie headers will be added later by the platform code for security
73aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // reason.
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    CString clientHandshakeMessage() const;
75aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // Builds an object representing WebSocket opening handshake to pass to the
76aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch    // inspector.
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    PassRefPtr<WebSocketHandshakeRequest> clientHandshakeRequest() const;
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
7993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // We're collecting data for histogram in the destructor. Note that calling
8093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    // this method affects that.
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void reset();
8209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    void clearDocument();
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    int readServerHandshake(const char* header, size_t len);
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Mode mode() const;
86f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Returns a string indicating the reason of failure if mode() == Failed.
87f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    String failureReason() const;
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const AtomicString& serverWebSocketProtocol() const;
90a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const AtomicString& serverUpgrade() const;
91a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const AtomicString& serverConnection() const;
92a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const AtomicString& serverWebSocketAccept() const;
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String acceptedExtensions() const;
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const WebSocketHandshakeResponse& serverHandshakeResponse() const;
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void addExtensionProcessor(PassOwnPtr<WebSocketExtensionProcessor>);
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    static String getExpectedWebSocketAccept(const String& secWebSocketKey);
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
101197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    void trace(Visitor*);
102197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private:
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    KURL httpURLForAuthenticationAndCookies() const;
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    int readStatusLine(const char* header, size_t headerLength, int& statusCode, String& statusText);
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // Reads all headers except for the two predefined ones.
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const char* readHTTPHeaders(const char* start, const char* end);
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void processHeaders();
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool checkResponseHeaders();
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    KURL m_url;
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String m_clientProtocol;
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool m_secure;
116197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    RawPtrWillBeMember<Document> m_document;
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Mode m_mode;
1195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
120f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Stores a received server's handshake. The order of headers is not
121f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // guaranteed to be preserved. Duplicated headers may be dropped. Values may
122f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // be rebuilt after parsing, so they can be different from the original data
123f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // received from the server.
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    WebSocketHandshakeResponse m_response;
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String m_failureReason;
1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String m_secWebSocketKey;
1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    String m_expectedAccept;
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    WebSocketExtensionDispatcher m_extensionDispatcher;
1325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
1335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
134c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // WebSocketHandshake_h
137