forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.h
More file actions
265 lines (232 loc) · 9.13 KB
/
WebSocket.h
File metadata and controls
265 lines (232 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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