trunk: add websocket class

This commit is contained in:
Marian Krivos 2012-02-05 12:29:46 +00:00
parent 7d7c02c579
commit d066518edd
4 changed files with 914 additions and 0 deletions

View File

@ -0,0 +1,265 @@
//
// WebSocket.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/WebSocket.h#1 $
//
// Library: Net
// Package: WebSocket
// Module: WebSocket
//
// Definition of the WebSocket class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Net_WebSocket_INCLUDED
#define Net_WebSocket_INCLUDED
#include "Poco/Net/Net.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/HTTPCredentials.h"
namespace Poco {
namespace Net {
class WebSocketImpl;
class HTTPServerRequest;
class HTTPServerResponse;
class HTTPClientSession;
class Net_API WebSocket: public StreamSocket
/// This class implements a WebSocket according
/// to the WebSocket protocol specification in RFC 6455.
///
/// Both client-side and server-side WebSockets
/// are supported.
///
/// Server-side WebSockets are usually created from within
/// a HTTPRequestHandler.
///
/// Client-side WebSockets are created using a HTTPClientSession.
///
/// Note that special frames like PING must be handled at
/// application level. In the case of a PING, a PONG message
/// must be returned.
{
public:
enum Mode
{
WS_SERVER, /// Server-side WebSocket.
WS_CLIENT /// Client-side WebSocket.
};
enum FrameFlags
/// Frame header flags.
{
FRAME_FLAG_FIN = 0x80, /// FIN bit: final fragment of a multi-fragment message.
FRAME_FLAG_RSV1 = 0x40, /// Reserved for future use. Must be zero.
FRAME_FLAG_RSV2 = 0x20, /// Reserved for future use. Must be zero.
FRAME_FLAG_RSV3 = 0x10, /// Reserved for future use. Must be zero.
};
enum FrameOpcodes
/// Frame header opcodes.
{
FRAME_OP_CONT = 0x00, /// Continuation frame.
FRAME_OP_TEXT = 0x01, /// Text frame.
FRAME_OP_BINARY = 0x02, /// Binary frame.
FRAME_OP_CLOSE = 0x08, /// Close connection.
FRAME_OP_PING = 0x09, /// Ping frame.
FRAME_OP_PONG = 0x0a, /// Pong frame.
FRAME_OP_BITMASK = 0x0f /// Bit mask for opcodes.
};
enum SendFlags
/// Combined header flags and opcodes for use with sendFrame().
{
FRAME_TEXT = FRAME_FLAG_FIN | FRAME_OP_TEXT,
/// Use this for sending a single text (UTF-8) payload frame.
FRAME_BINARY = FRAME_FLAG_FIN | FRAME_OP_BINARY
/// Use this for sending a single binary payload frame.
};
enum StatusCodes
/// StatusCodes for CLOSE frames sent with shutdown().
{
WS_NORMAL_CLOSE = 1000,
WS_ENDPOINT_GOING_AWAY = 1001,
WS_PROTOCOL_ERROR = 1002,
WS_PAYLOAD_NOT_ACCEPTABLE = 1003,
WS_RESERVED = 1004,
WS_RESERVED_NO_STATUS_CODE = 1005,
WS_RESERVED_ABNORMAL_CLOSE = 1006,
WS_MALFORMED_PAYLOAD = 1007,
WS_POLICY_VIOLATION = 1008,
WS_PAYLOAD_TOO_BIG = 1009,
WS_EXTENSION_REQUIRED = 1010,
WS_UNEXPECTED_CONDITION = 1011,
WS_RESERVED_TLS_FAILURE = 1015
};
enum ErrorCodes
/// These error codes can be obtained from a WebSocketException
/// to determine the exact cause of the error.
{
WS_ERR_NO_HANDSHAKE = 1,
/// No Connection: Upgrade or Upgrade: websocket header in handshake request.
WS_ERR_HANDSHAKE_NO_VERSION = 2,
/// No Sec-WebSocket-Version header in handshake request.
WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION = 3,
/// Unsupported WebSocket version requested by client.
WS_ERR_HANDSHAKE_NO_KEY = 4,
/// No Sec-WebSocket-Key header in handshake request.
WS_ERR_HANDSHAKE_ACCEPT = 5,
/// No Sec-WebSocket-Accept header or wrong value.
WS_ERR_UNAUTHORIZED = 6,
/// The server rejected the username or password for authentication.
WS_ERR_PAYLOAD_TOO_BIG = 10,
/// Payload too big for supplied buffer.
WS_ERR_INCOMPLETE_FRAME = 11
/// Incomplete frame received.
};
WebSocket(HTTPServerRequest& request, HTTPServerResponse& response);
/// Creates a server-side WebSocket from within a
/// HTTPRequestHandler.
///
/// First verifies that the request is a valid WebSocket upgrade
/// request. If so, completes the handshake by sending
/// a proper 101 response.
///
/// Throws an exception if the request is not a proper WebSocket
/// upgrade request.
WebSocket(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response);
/// Creates a client-side WebSocket, using the given
/// HTTPClientSession and HTTPRequest for the initial handshake
/// (HTTP Upgrade request).
///
/// Additional HTTP headers for the initial handshake request
/// (such as Origin or Sec-WebSocket-Protocol) can be given
/// in the request object.
///
/// The result of the handshake can be obtained from the response
/// object.
WebSocket(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials);
/// Creates a client-side WebSocket, using the given
/// HTTPClientSession and HTTPRequest for the initial handshake
/// (HTTP Upgrade request).
///
/// The given credentials are used for authentication
/// if requested by the server.
///
/// Additional HTTP headers for the initial handshake request
/// (such as Origin or Sec-WebSocket-Protocol) can be given
/// in the request object.
///
/// The result of the handshake can be obtained from the response
/// object.
virtual ~WebSocket();
/// Destroys the StreamSocket.
WebSocket& operator = (const Socket& socket);
/// Assignment operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
void shutdown();
/// Sends a Close control frame to the server end of
/// the connection to initiate an orderly shutdown
/// of the connection.
void shutdown(Poco::UInt16 statusCode, const std::string& statusMessage = "");
/// Sends a Close control frame to the server end of
/// the connection to initiate an orderly shutdown
/// of the connection.
int sendFrame(const void* buffer, int length, int flags = FRAME_TEXT);
/// Sends the contents of the given buffer through
/// the socket as a single frame.
///
/// Values from the FrameFlags, FrameOpcodes and SendFlags enumerations
/// can be specified in flags.
///
/// Returns the number of bytes sent, which may be
/// less than the number of bytes specified.
///
/// Certain socket implementations may also return a negative
/// value denoting a certain condition.
int receiveFrame(void* buffer, int length, int& flags);
/// Receives a frame from the socket and stores it
/// in buffer. Up to length bytes are received. If
/// the frame's payload is larger, a WebSocketException
/// is thrown and the WebSocket connection must be
/// terminated.
///
/// Returns the number of bytes received.
/// A return value of 0 means a graceful shutdown
/// of the connection from the peer.
///
/// Throws a TimeoutException if a receive timeout has
/// been set and nothing is received within that interval.
/// Throws a NetException (or a subclass) in case of other errors.
///
/// The frame flags and opcode (FrameFlags and FrameOpcodes)
/// is stored in flags.
Mode mode() const;
/// Returns WS_SERVER if the WebSocket is a server-side
/// WebSocket, or WS_CLIENT otherwise.
static const std::string WEBSOCKET_VERSION;
/// The WebSocket protocol version supported (13).
protected:
static WebSocketImpl* accept(HTTPServerRequest& request, HTTPServerResponse& response);
static WebSocketImpl* connect(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials);
static WebSocketImpl* completeHandshake(HTTPClientSession& cs, HTTPResponse& response, const std::string& key);
static std::string computeAccept(const std::string& key);
static std::string createKey();
private:
WebSocket();
static const std::string WEBSOCKET_GUID;
static HTTPCredentials _defaultCreds;
};
} } // namespace Poco::Net
#endif // Net_WebSocket_INCLUDED

View File

@ -0,0 +1,126 @@
//
// WebSocketImpl.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/WebSocketImpl.h#1 $
//
// Library: Net
// Package: WebSocket
// Module: WebSocketImpl
//
// Definition of the StreamSocketImpl class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Net_WebSocketImpl_INCLUDED
#define Net_WebSocketImpl_INCLUDED
#include "Poco/Net/StreamSocketImpl.h"
#include "Poco/Random.h"
namespace Poco {
namespace Net {
class Net_API WebSocketImpl: public StreamSocketImpl
/// This class implements a WebSocket, according
/// to the WebSocket protocol described in RFC 6455.
{
public:
WebSocketImpl(StreamSocketImpl* pStreamSocketImpl, bool mustMaskPayload);
/// Creates a StreamSocketImpl using the given native socket.
// StreamSocketImpl
virtual int sendBytes(const void* buffer, int length, int flags);
/// Sends a WebSocket protocol frame.
virtual int receiveBytes(void* buffer, int length, int flags);
/// Receives a WebSocket protocol frame.
virtual SocketImpl* acceptConnection(SocketAddress& clientAddr);
virtual void connect(const SocketAddress& address);
virtual void connect(const SocketAddress& address, const Poco::Timespan& timeout);
virtual void connectNB(const SocketAddress& address, const Poco::Timespan& timeout);
virtual void bind(const SocketAddress& address, bool reuseAddress = false);
virtual void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false);
virtual void listen(int backlog = 64);
virtual void close();
virtual void shutdownReceive();
virtual void shutdownSend();
virtual void shutdown();
virtual int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
virtual int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
virtual void sendUrgent(unsigned char data);
virtual bool secure() const;
// Internal
int frameFlags() const;
/// Returns the frame flags of the most recently received frame.
bool mustMaskPayload() const;
/// Returns true if the payload must be masked.
protected:
enum
{
FRAME_FLAG_MASK = 0x80,
MAX_HEADER_LENGTH = 14
};
virtual ~WebSocketImpl();
private:
WebSocketImpl();
StreamSocketImpl* _pStreamSocketImpl;
int _frameFlags;
bool _mustMaskPayload;
Poco::Random _rnd;
};
//
// inlines
//
inline int WebSocketImpl::frameFlags() const
{
return _frameFlags;
}
inline bool WebSocketImpl::mustMaskPayload() const
{
return _mustMaskPayload;
}
} } // namespace Poco::Net
#endif // Net_WebSocketImpl_INCLUDED

244
Net/src/WebSocket.cpp Normal file
View File

@ -0,0 +1,244 @@
//
// WebSocket.cpp
//
// $Id: //poco/1.4/Net/src/WebSocket.cpp#4 $
//
// Library: Net
// Package: WebSocket
// Module: WebSocket
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/WebSocketImpl.h"
#include "Poco/Net/HTTPServerRequestImpl.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/NetException.h"
#include "Poco/Buffer.h"
#include "Poco/MemoryStream.h"
#include "Poco/NullStream.h"
#include "Poco/BinaryWriter.h"
#include "Poco/SHA1Engine.h"
#include "Poco/Base64Encoder.h"
#include "Poco/String.h"
#include "Poco/Random.h"
#include "Poco/StreamCopier.h"
#include <sstream>
namespace Poco {
namespace Net {
const std::string WebSocket::WEBSOCKET_GUID("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
const std::string WebSocket::WEBSOCKET_VERSION("13");
HTTPCredentials WebSocket::_defaultCreds;
WebSocket::WebSocket(HTTPServerRequest& request, HTTPServerResponse& response):
StreamSocket(accept(request, response))
{
}
WebSocket::WebSocket(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response):
StreamSocket(connect(cs, request, response, _defaultCreds))
{
}
WebSocket::WebSocket(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials):
StreamSocket(connect(cs, request, response, credentials))
{
}
WebSocket::~WebSocket()
{
}
WebSocket& WebSocket::operator = (const Socket& socket)
{
if (dynamic_cast<WebSocketImpl*>(socket.impl()))
Socket::operator = (socket);
else
throw InvalidArgumentException("Cannot assign incompatible socket");
return *this;
}
void WebSocket::shutdown()
{
shutdown(WS_NORMAL_CLOSE);
}
void WebSocket::shutdown(Poco::UInt16 statusCode, const std::string& statusMessage)
{
Poco::Buffer<char> buffer(statusMessage.size() + 2);
Poco::MemoryOutputStream ostr(buffer.begin(), buffer.size());
Poco::BinaryWriter writer(ostr, Poco::BinaryWriter::NETWORK_BYTE_ORDER);
writer << statusCode;
writer.writeRaw(statusMessage);
sendFrame(buffer.begin(), static_cast<int>(ostr.charsWritten()), FRAME_FLAG_FIN | FRAME_OP_CLOSE);
}
int WebSocket::sendFrame(const void* buffer, int length, int flags)
{
return static_cast<WebSocketImpl*>(impl())->sendBytes(buffer, length, flags);
}
int WebSocket::receiveFrame(void* buffer, int length, int& flags)
{
int n = static_cast<WebSocketImpl*>(impl())->receiveBytes(buffer, length, 0);
flags = static_cast<WebSocketImpl*>(impl())->frameFlags();
return n;
}
WebSocket::Mode WebSocket::mode() const
{
return static_cast<WebSocketImpl*>(impl())->mustMaskPayload() ? WS_CLIENT : WS_SERVER;
}
WebSocketImpl* WebSocket::accept(HTTPServerRequest& request, HTTPServerResponse& response)
{
if (icompare(request.get("Connection", ""), "Upgrade") == 0 &&
icompare(request.get("Upgrade", ""), "websocket") == 0)
{
std::string version = request.get("Sec-WebSocket-Version", "");
if (version.empty()) throw WebSocketException("Missing Sec-WebSocket-Version in handshake request", WS_ERR_HANDSHAKE_NO_VERSION);
if (version != WEBSOCKET_VERSION) throw WebSocketException("Unsupported WebSocket version requested", version, WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION);
std::string key = request.get("Sec-WebSocket-Key", "");
Poco::trimInPlace(key);
if (key.empty()) throw WebSocketException("Missing Sec-WebSocket-Key in handshake request", WS_ERR_HANDSHAKE_NO_KEY);
response.setStatusAndReason(HTTPResponse::HTTP_SWITCHING_PROTOCOLS);
response.set("Upgrade", "websocket");
response.set("Connection", "Upgrade");
response.set("Sec-WebSocket-Accept", computeAccept(key));
response.setContentLength(0);
response.send().flush();
return new WebSocketImpl(static_cast<StreamSocketImpl*>(static_cast<HTTPServerRequestImpl&>(request).detachSocket().impl()), false);
}
else throw WebSocketException("No WebSocket handshake", WS_ERR_NO_HANDSHAKE);
}
WebSocketImpl* WebSocket::connect(HTTPClientSession& cs, HTTPRequest& request, HTTPResponse& response, HTTPCredentials& credentials)
{
if (!cs.getProxyHost().empty() && !cs.secure())
{
cs.proxyTunnel();
}
std::string key = createKey();
request.set("Connection", "Upgrade");
request.set("Upgrade", "websocket");
request.set("Sec-WebSocket-Version", WEBSOCKET_VERSION);
request.set("Sec-WebSocket-Key", key);
request.setChunkedTransferEncoding(false);
cs.setKeepAlive(true);
cs.sendRequest(request);
std::istream& istr = cs.receiveResponse(response);
if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS)
{
return completeHandshake(cs, response, key);
}
else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED)
{
Poco::NullOutputStream null;
Poco::StreamCopier::copyStream(istr, null);
credentials.authenticate(request, response);
if (!cs.getProxyHost().empty() && !cs.secure())
{
cs.reset();
cs.proxyTunnel();
}
cs.sendRequest(request);
cs.receiveResponse(response);
if (response.getStatus() == HTTPResponse::HTTP_SWITCHING_PROTOCOLS)
{
return completeHandshake(cs, response, key);
}
else if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED)
{
throw WebSocketException("Not authorized", WS_ERR_UNAUTHORIZED);
}
}
throw WebSocketException("Cannot upgrade to WebSocket connection", response.getReason(), WS_ERR_NO_HANDSHAKE);
}
WebSocketImpl* WebSocket::completeHandshake(HTTPClientSession& cs, HTTPResponse& response, const std::string& key)
{
std::string connection = response.get("Connection", "");
if (Poco::icompare(connection, "Upgrade") != 0)
throw WebSocketException("No Connection: Upgrade header in handshake response", WS_ERR_NO_HANDSHAKE);
std::string upgrade = response.get("Upgrade", "");
if (Poco::icompare(upgrade, "websocket") != 0)
throw WebSocketException("No Upgrade: websocket header in handshake response", WS_ERR_NO_HANDSHAKE);
std::string accept = response.get("Sec-WebSocket-Accept", "");
if (accept != computeAccept(key))
throw WebSocketException("Invalid or missing Sec-WebSocket-Accept header in handshake response", WS_ERR_NO_HANDSHAKE);
return new WebSocketImpl(static_cast<StreamSocketImpl*>(cs.socket().impl()), true);
}
std::string WebSocket::createKey()
{
Poco::Random rnd;
std::ostringstream ostr;
Poco::Base64Encoder base64(ostr);
Poco::BinaryWriter writer(base64);
writer << rnd.next() << rnd.next() << rnd.next() << rnd.next();
base64.close();
return ostr.str();
}
std::string WebSocket::computeAccept(const std::string& key)
{
std::string accept(key);
accept += WEBSOCKET_GUID;
Poco::SHA1Engine sha1;
sha1.update(accept);
Poco::DigestEngine::Digest d = sha1.digest();
std::ostringstream ostr;
Poco::Base64Encoder base64(ostr);
base64.write(reinterpret_cast<const char*>(&d[0]), d.size());
base64.close();
return ostr.str();
}
} } // namespace Poco::Net

279
Net/src/WebSocketImpl.cpp Normal file
View File

@ -0,0 +1,279 @@
//
// WebSocketImpl.cpp
//
// $Id: //poco/1.4/Net/src/WebSocketImpl.cpp#2 $
//
// Library: Net
// Package: WebSocket
// Module: WebSocketImpl
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Net/WebSocketImpl.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Buffer.h"
#include "Poco/BinaryWriter.h"
#include "Poco/BinaryReader.h"
#include "Poco/MemoryStream.h"
#include "Poco/Format.h"
#include <cstring>
namespace Poco {
namespace Net {
WebSocketImpl::WebSocketImpl(StreamSocketImpl* pStreamSocketImpl, bool mustMaskPayload):
StreamSocketImpl(pStreamSocketImpl->sockfd()),
_pStreamSocketImpl(pStreamSocketImpl),
_frameFlags(0),
_mustMaskPayload(mustMaskPayload)
{
poco_check_ptr(pStreamSocketImpl);
_pStreamSocketImpl->duplicate();
}
WebSocketImpl::~WebSocketImpl()
{
_pStreamSocketImpl->release();
reset();
}
int WebSocketImpl::sendBytes(const void* buffer, int length, int flags)
{
Poco::Buffer<char> frame(length + MAX_HEADER_LENGTH);
Poco::MemoryOutputStream ostr(frame.begin(), frame.size());
Poco::BinaryWriter writer(ostr, Poco::BinaryWriter::NETWORK_BYTE_ORDER);
writer << static_cast<Poco::UInt8>(flags);
Poco::UInt8 lengthByte(0);
if (_mustMaskPayload)
{
lengthByte |= FRAME_FLAG_MASK;
}
if (length < 126)
{
lengthByte |= static_cast<Poco::UInt8>(length);
writer << lengthByte;
}
else if (length < 65536)
{
lengthByte |= 126;
writer << lengthByte << static_cast<Poco::UInt16>(length);
}
else
{
lengthByte |= 127;
writer << lengthByte << static_cast<Poco::UInt64>(length);
}
if (_mustMaskPayload)
{
const Poco::UInt32 mask = _rnd.next();
const char* m = reinterpret_cast<const char*>(&mask);
const char* b = reinterpret_cast<const char*>(buffer);
writer.writeRaw(m, 4);
char* p = frame.begin() + ostr.charsWritten();
for (int i = 0; i < length; i++)
{
p[i] = b[i] ^ m[i % 4];
}
}
else
{
std::memcpy(frame.begin() + ostr.charsWritten(), buffer, length);
}
_pStreamSocketImpl->sendBytes(frame.begin(), length + static_cast<int>(ostr.charsWritten()));
return length;
}
int WebSocketImpl::receiveBytes(void* buffer, int length, int)
{
char header[MAX_HEADER_LENGTH];
int n = _pStreamSocketImpl->receiveBytes(header, MAX_HEADER_LENGTH);
if (n > 0)
{
Poco::MemoryInputStream istr(header, n);
Poco::BinaryReader reader(istr, Poco::BinaryReader::NETWORK_BYTE_ORDER);
Poco::UInt8 flags;
Poco::UInt8 lengthByte;
char mask[4];
reader >> flags >> lengthByte;
_frameFlags = flags;
int payloadLength = 0;
int payloadOffset = 2;
if ((lengthByte & 0x7f) == 127)
{
Poco::UInt64 l;
reader >> l;
if (l > length) throw WebSocketException(Poco::format("Insufficient buffer for payload size %Lu", l), WebSocket::WS_ERR_PAYLOAD_TOO_BIG);
payloadLength = static_cast<int>(l);
payloadOffset += 8;
}
else if ((lengthByte & 0x7f) == 126)
{
Poco::UInt16 l;
reader >> l;
if (l > length) throw WebSocketException(Poco::format("Insufficient buffer for payload size %hu", l), WebSocket::WS_ERR_PAYLOAD_TOO_BIG);
payloadLength = static_cast<int>(l);
payloadOffset += 2;
}
else
{
Poco::UInt8 l = lengthByte & 0x7f;
if (l > length) throw WebSocketException(Poco::format("Insufficient buffer for payload size %u", unsigned(l)), WebSocket::WS_ERR_PAYLOAD_TOO_BIG);
payloadLength = static_cast<int>(l);
}
if (lengthByte & FRAME_FLAG_MASK)
{
reader.readRaw(mask, 4);
payloadOffset += 4;
}
int received = 0;
if (payloadOffset < n)
{
std::memcpy(buffer, header + payloadOffset, n - payloadOffset);
received = n - payloadOffset;
}
while (received < payloadLength)
{
n = _pStreamSocketImpl->receiveBytes(reinterpret_cast<char*>(buffer) + received, payloadLength - received);
if (n > 0)
received += n;
else
throw WebSocketException("Incomplete frame received", WebSocket::WS_ERR_INCOMPLETE_FRAME);
}
if (lengthByte & FRAME_FLAG_MASK)
{
char* p = reinterpret_cast<char*>(buffer);
for (int i = 0; i < received; i++)
{
p[i] ^= mask[i % 4];
}
}
return received;
}
return n;
}
SocketImpl* WebSocketImpl::acceptConnection(SocketAddress& clientAddr)
{
throw Poco::InvalidAccessException("Cannot acceptConnection() on a WebSocketImpl");
}
void WebSocketImpl::connect(const SocketAddress& address)
{
throw Poco::InvalidAccessException("Cannot connect() a WebSocketImpl");
}
void WebSocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
{
throw Poco::InvalidAccessException("Cannot connect() a WebSocketImpl");
}
void WebSocketImpl::connectNB(const SocketAddress& address, const Poco::Timespan& timeout)
{
throw Poco::InvalidAccessException("Cannot connectNB() a WebSocketImpl");
}
void WebSocketImpl::bind(const SocketAddress& address, bool reuseAddress)
{
throw Poco::InvalidAccessException("Cannot bind() a WebSocketImpl");
}
void WebSocketImpl::bind6(const SocketAddress& address, bool reuseAddress, bool ipV6Only)
{
throw Poco::InvalidAccessException("Cannot bind6() a WebSocketImpl");
}
void WebSocketImpl::listen(int backlog)
{
throw Poco::InvalidAccessException("Cannot listen() on a WebSocketImpl");
}
void WebSocketImpl::close()
{
_pStreamSocketImpl->close();
reset();
}
void WebSocketImpl::shutdownReceive()
{
_pStreamSocketImpl->shutdownReceive();
}
void WebSocketImpl::shutdownSend()
{
_pStreamSocketImpl->shutdownSend();
}
void WebSocketImpl::shutdown()
{
_pStreamSocketImpl->shutdown();
}
int WebSocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags)
{
throw Poco::InvalidAccessException("Cannot sendTo() on a WebSocketImpl");
}
int WebSocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags)
{
throw Poco::InvalidAccessException("Cannot receiveFrom() on a WebSocketImpl");
}
void WebSocketImpl::sendUrgent(unsigned char data)
{
throw Poco::InvalidAccessException("Cannot sendUrgent() on a WebSocketImpl");
}
bool WebSocketImpl::secure() const
{
return _pStreamSocketImpl->secure();
}
} } // namespace Poco::Net