-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSockets.h
320 lines (268 loc) · 12.1 KB
/
WebSockets.h
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//---------------------------------------------------------------------------
#ifndef WebSocketsH
#define WebSocketsH
#include <System.SysUtils.hpp>
#include <IdIOHandler.hpp>
#include <IdContext.hpp>
#include <Idcustomhttpserver.hpp>
#include <IdHTTP.hpp>
#include <cstdint>
//---------------------------------------------------------------------------
namespace SvcApp {
//---------------------------------------------------------------------------
namespace WebSockets {
//---------------------------------------------------------------------------
enum class Opcode {
Continuation = 0,
Text = 1,
Binary = 2,
Close = 8,
Ping = 9,
Pong = 10,
};
enum class CloseStatus {
// indicates a normal closure, meaning that the purpose for
// which the connection was established has been fulfilled.
Normal = 1000,
// indicates that an endpoint is "going away", such as a server
// going down or a browser having navigated away from a page.
GoingAway = 1001,
// indicates that an endpoint is terminating the connection due
// to a protocol error.
ProtocolError = 1002,
// indicates that an endpoint is terminating the connection
// because it has received a type of data it cannot accept (e.g., an
// endpoint that understands only text data MAY send this if it
// receives a binary message).
UnhandledDataType = 1003,
// reserved, the specific meaning might be defined in the future.
Reserved = 1004,
// reserved, MUST NOT be set as a status code in a Close control frame
// by an endpoint. It is designated for use in applications expecting
// a status code to indicate that no status code was actually present.
ReservedNoStatus = 1005,
// is a reserved value and MUST NOT be set as a status code in a Close
// control frame by an endpoint. It is designated for use in applications
// expecting a status code to indicate that the connection was closed
// abnormally, e.g., without sending or receiving a Close control frame.
ReservedAbnormal = 1006,
// indicates that an endpoint is terminating the connection because it has
// received data within a message that was not consistent with the type
// of the message (e.g., non-UTF-8 [RFC3629] data within a text message).
InconsistentData = 1007,
// indicates that an endpoint is terminating the connection because
// it has received a message that violates its policy. This is a generic
// status code that can be returned when there is no other more suitable
// status code (e.g., 1003 or 1009) or if there is a need to hide specific
// details about the policy.
PolicyError = 1008,
// indicates that an endpoint is terminating the connection because
// it has received a message that is too big for it to process.
ToBigMessage = 1009,
// indicates that an endpoint (client) is terminating the connection
// because it has expected the server to negotiate one or more extension,
// but the server didn't return them in the response message of the
// WebSocket handshake. The list of extensions that are needed SHOULD
// appear in the /reason/ part of the Close frame. Note that this status
// code is not used by the server, because it can fail the WebSocket
// handshake instead.
MissingExtenstion = 1010,
// indicates that a server is terminating the connection because it
// encountered an unexpected condition that prevented it from fulfilling
//the request.
UnExpectedError = 1011,
// is a reserved value and MUST NOT be set as a status code in a Close
// control frame by an endpoint. It is designated for use in applications
// expecting a status code to indicate that the connection was closed
// due to a failure to perform a TLS handshake (e.g., the server
// certificate can't be verified).
ReservedTLSError = 1015,
};
extern bool ToCloseStatus( uint16_t Code, CloseStatus& Status );
extern String ToString( Opcode Type );
#pragma pack( push, 1 )
using MaskData =
union {
uint32_t d_;
uint8_t a_[4];
};
#pragma pack( pop )
class WebSocket {
public:
WebSocket() = default;
WebSocket( WebSocket const & ) = delete;
WebSocket& operator=( WebSocket const & ) = delete;
virtual ~WebSocket() = default;
TIdIOHandler& GetIOHandler() const { return DoGetIOHandler(); }
bool ReadFrame( TBytes& InBuffer, size_t& PayloadLen, size_t& PayloadPos,
CloseStatus& CloseReason, String& CloseText, int Timeout )
{
GetIOHandler().ReadTimeout = Timeout;
return DoReadFrame(
InBuffer, PayloadLen, PayloadPos, CloseReason, CloseText
);
}
bool ReadTextFrame( int Timeout, String& Text, String& CloseText,
CloseStatus& CloseReason );
String ReadTextFrame( int Timeout );
bool ReadMessage( Opcode& Type, TBytes& Data, CloseStatus& CloseReason,
String& CloseText, int Timeout )
{
GetIOHandler().ReadTimeout = Timeout;
return DoReadMessage( Type, Data, CloseReason, CloseText );
}
void SendEmptyFrame( Opcode Type ) { DoSendEmptyFrame( Type ); }
void SendFrame( Opcode Type, TBytes const & Data, bool Fin )
{
DoSendFrame( Type, Data, Fin );
}
void SendCloseFrame() { SendEmptyFrame( Opcode::Close ); }
void SendCloseFrame( CloseStatus CloseReason, TBytes const & CloseData );
void SendCloseFrame( CloseStatus CloseReason, String CloseText )
{
SendCloseFrame( CloseReason, TEncoding::UTF8->GetBytes( CloseText ) );
}
void SendPongFrame() { SendEmptyFrame( Opcode::Pong ); }
void SendPongFrame( TBytes const & Data )
{
SendFrame( Opcode::Pong, Data, true );
}
void SendPongFrame( String Text )
{
SendPongFrame( TEncoding::UTF8->GetBytes( Text ) );
}
void SendPingFrame() { SendEmptyFrame( Opcode::Ping ); }
void SendPingFrame( TBytes const & Data )
{
SendFrame( Opcode::Ping, Data, true );
}
void SendPingFrame( String Text )
{
SendPingFrame( TEncoding::UTF8->GetBytes( Text ) );
}
void SendFrame( String Text, bool Fin = true )
{
SendFrame( Opcode::Text, TEncoding::UTF8->GetBytes( Text ), Fin );
}
void SendFrame( TBytes const & Data, bool Fin = true )
{
SendFrame( Opcode::Binary, Data, Fin );
}
void SendMessage( Opcode Type, TBytes const & Data, CloseStatus& CloseReason,
String& CloseText, size_t MaxChunkSize, int Timeout )
{
GetIOHandler().ReadTimeout = Timeout;
DoSendMessage( Type, Data, CloseReason, CloseText, MaxChunkSize );
}
void SendMessage( String Text, CloseStatus& CloseReason, String& CloseText,
size_t MaxChunkSize, int Timeout )
{
SendMessage(
Opcode::Text, TEncoding::UTF8->GetBytes( Text ), CloseReason,
CloseText, MaxChunkSize, Timeout
);
}
void SendMessage( TBytes const & Data, CloseStatus& CloseReason, String& CloseText,
size_t MaxChunkSize, int Timeout )
{
SendMessage(
Opcode::Binary, Data, CloseReason, CloseText, MaxChunkSize, Timeout
);
}
bool IsWebSocket() const { return DoIsWebSocket(); }
protected:
virtual TIdIOHandler& DoGetIOHandler() const = 0;
virtual bool DoReadFrame( TBytes& InBuffer, size_t& PayloadLen,
size_t& PayloadPos, CloseStatus& Reason,
String& CloseText ) = 0;
virtual void DoSendEmptyFrame( Opcode Type ) = 0;
virtual void DoSendFrame( Opcode Type, TBytes const & Data, bool Fin ) = 0;
virtual bool DoReadMessage( Opcode& Type, TBytes& Data, CloseStatus& Reason,
String& CloseText );
virtual bool DoReadFragmentedData( TBytes& AData, CloseStatus& CloseReason,
String& CloseText );
virtual void DoTryToProcessControlFrame( TBytes& Buffer );
virtual void DoSendMessage( Opcode& Type, TBytes const & Data,
CloseStatus& CloseReason, String& CloseText,
size_t MaxChunkSize );
virtual bool DoIsWebSocket() const { return true; }
static bool ReadFrameHeader( TIdIOHandler& IOHandler, TBytes& InBuffer,
size_t& PayloadLen, size_t& PayloadPos,
CloseStatus& CloseReason, String& CloseText );
static TBytes BuildFrameHeader( Opcode Type, int DataLen, bool Fin, bool Masked );
static void SendFrame( TIdIOHandler& IOHandler, Opcode Type,
TBytes const & Data, bool Fin, bool Masked );
static Opcode GetOpcode( TBytes const & Data ) {
return static_cast<Opcode>( Data[0] & 15 );
}
static bool GetFin( TBytes const & Data ) { return Data[0] & 128; }
static bool GetMasked( TBytes const & Data ) { return Data[1] & 128; }
static System::Byte GetLen( TBytes const & Data ) { return Data[1] & 127; }
private:
};
//---------------------------------------------------------------------------
namespace Client {
//---------------------------------------------------------------------------
class WebSocket final : public WebSockets::WebSocket {
public:
WebSocket( TIdHTTP& IdHTTP, String URL,
Idheaderlist::TIdHeaderList* AdditionalCustomHeader = nullptr );
WebSocket( WebSocket const & ) = delete;
WebSocket& operator=( WebSocket const & ) = delete;
protected:
virtual TIdIOHandler& DoGetIOHandler() const override;
virtual bool DoReadFrame( TBytes& InBuffer, size_t& PayloadLen,
size_t& PayloadPos, CloseStatus& Reason,
String& CloseText ) override;
virtual void DoSendEmptyFrame( Opcode Type ) override;
virtual void DoSendFrame( Opcode Type, TBytes const & Data,
bool Fin ) override;
private:
TIdHTTP& idHTTP_;
void TryToUpgrade( String URL, Idheaderlist::TIdHeaderList* AdditionalCustomHeader );
uint32_t GenerateMask() const;
static String GenerateSecretKey();
};
//---------------------------------------------------------------------------
} // End of namespace Client
//---------------------------------------------------------------------------
namespace Server {
//---------------------------------------------------------------------------
class WebSocket final : public WebSockets::WebSocket {
public:
WebSocket( Idcontext::TIdContext* AThread,
Idcustomhttpserver::TIdHTTPRequestInfo* ARequestInfo,
Idcustomhttpserver::TIdHTTPResponseInfo* AResponseInfo )
: thread_( *AThread )
, requestInfo_( *ARequestInfo )
, responseInfo_( *AResponseInfo )
, isWebSocket_( TryToUpgrade() )
{}
WebSocket( WebSocket const & ) = delete;
WebSocket& operator=( WebSocket const & ) = delete;
Idcustomhttpserver::TIdHTTPRequestInfo& GetRequestInfo() const { return requestInfo_; }
Idcustomhttpserver::TIdHTTPResponseInfo& GetResponseInfo() const { return responseInfo_; }
protected:
virtual TIdIOHandler& DoGetIOHandler() const override;
virtual bool DoReadFrame( TBytes& InBuffer, size_t& PayloadLen,
size_t& PayloadPos, CloseStatus& Reason,
String& CloseText ) override;
virtual void DoSendEmptyFrame( Opcode Type ) override;
virtual void DoSendFrame( Opcode Type, TBytes const & Data,
bool Fin ) override;
bool DoIsWebSocket() const override { return isWebSocket_; }
private:
Idcontext::TIdContext& thread_;
Idcustomhttpserver::TIdHTTPRequestInfo& requestInfo_;
Idcustomhttpserver::TIdHTTPResponseInfo& responseInfo_;
bool isWebSocket_;
bool TryToUpgrade();
};
//---------------------------------------------------------------------------
} // End of namespace Server
//---------------------------------------------------------------------------
} // End of namespace WebSockets
//---------------------------------------------------------------------------
} // End of namespace SvcApp
//---------------------------------------------------------------------------
#endif