1/* 2 * Copyright (C) 2011 Apple Inc. All Rights Reserved. 3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies) 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#ifndef WebSocketServerConnection_h 28#define WebSocketServerConnection_h 29 30#if ENABLE(INSPECTOR_SERVER) 31 32#include <WebCore/SocketStreamHandleClient.h> 33#include <wtf/PassRefPtr.h> 34#include <wtf/RefPtr.h> 35#include <wtf/Vector.h> 36#include <wtf/text/WTFString.h> 37 38namespace WebCore { 39class HTTPHeaderMap; 40class SocketStreamHandle; 41} 42 43namespace WebKit { 44 45class HTTPRequest; 46class WebSocketServer; 47class WebSocketServerClient; 48 49class WebSocketServerConnection : public WebCore::SocketStreamHandleClient { 50public: 51 enum WebSocketServerMode { HTTP, WebSocket }; 52 WebSocketServerConnection(WebSocketServerClient*, WebSocketServer*); 53 virtual ~WebSocketServerConnection(); 54 55 unsigned identifier() const { return m_identifier; } 56 void setIdentifier(unsigned id) { m_identifier = id; } 57 void setSocketHandle(PassRefPtr<WebCore::SocketStreamHandle>); 58 59 // Sending data over the connection. 60 void sendWebSocketMessage(const String& message); 61 void sendHTTPResponseHeader(int statusCode, const String& statusText, const WebCore::HTTPHeaderMap& headerFields); 62 void sendRawData(const char* data, size_t length); 63 64 // Terminating the connection. 65 void shutdownNow(); 66 void shutdownAfterSendOrNow(); 67 68 // SocketStreamHandleClient implementation. 69 virtual void didCloseSocketStream(WebCore::SocketStreamHandle*); 70 virtual void didReceiveSocketStreamData(WebCore::SocketStreamHandle*, const char* data, int length); 71 virtual void didUpdateBufferedAmount(WebCore::SocketStreamHandle*, size_t bufferedAmount); 72 virtual void didFailSocketStream(WebCore::SocketStreamHandle*, const WebCore::SocketStreamError&); 73 74private: 75 // HTTP Mode. 76 void readHTTPMessage(); 77 78 // WebSocket Mode. 79 void upgradeToWebSocketServerConnection(PassRefPtr<HTTPRequest>); 80 void readWebSocketFrames(); 81 bool readWebSocketFrame(); 82 83protected: 84 unsigned m_identifier; 85 Vector<char> m_bufferedData; 86 WebSocketServerMode m_mode; 87 RefPtr<WebCore::SocketStreamHandle> m_socket; 88 WebSocketServer* m_server; 89 WebSocketServerClient* m_client; 90 bool m_shutdownAfterSend; 91}; 92 93} 94 95#endif // ENABLE(INSPECTOR_SERVER) 96 97#endif // WebSocketServerConnection_h 98