forked from LightenPan/HTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPWebsocket.cpp
262 lines (209 loc) · 6 KB
/
HTTPWebsocket.cpp
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
#include "HTTP.h"
#include <assert.h>
namespace HTTP
{
BYTE AllBitsSet(BYTE byte, BYTE bit)
{
return (byte & bit) == bit;
}
template<class T> T ByteSwap(T original)
{
union __swap
{
BYTE b[sizeof(T)];
T t;
};
__swap a;
__swap b;
a.t = original;
for (size_t i = 0; i < sizeof(T); ++i)
b.b[sizeof(T)-i-1] = a.b[i];
return b.t;
}
bool IsWebsocketRequest(_In_ const RequestHeader& req)
{
return
req.Header().find("Upgrade") != std::end(req.Header()) &&
req.Header().find("Sec-WebSocket-Key") != std::end(req.Header());
}
WS_RESPONSE_RESULT
BuildWebsocketRequestResponse(
_In_ const HTTP::RequestHeader& request,
_In_ HashFunc HashFunction,
_Out_ ResponseHeaderBuilder* responseHeader)
{
String wsKey = request.Header().find("Sec-WebSocket-Key")->second;
if (!wsKey.size())
return WS_RESPONSE_MISSING_KEY;
wsKey += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
/*
SHA1 hash;
hash << wsKey.c_str();
hash.Result(wsKeyHash);
*/
UINT wsKeyHash[5] = {0, 0, 0, 0, 0};
HashFunction(wsKey.c_str(), wsKeyHash);
for (int i = 0; i < 5; ++i)
wsKeyHash[i] = ByteSwap(wsKeyHash[i]);
wsKey = Base64Encode(wsKeyHash, sizeof(unsigned) * 5);
responseHeader->Protocol = HTTP::PROTOCOL_HTTP_1_1;
responseHeader->Code = HTTP::RESPONSE_SWITCHING_PROTOCOLS;
responseHeader->AddKey("Upgrade", "websocket");
responseHeader->AddKey("Connection", "Upgrade");
responseHeader->AddKey("Sec-WebSocket-Accept", wsKey);
// responseHeader->AddKey("Sec-WebSocket-Extensions", "");
responseHeader->AddKey("Sec-WebSocket-Version", "17");
return WS_RESPONSE_OK;
}
WS_FRAME_RESULT
ParseWebsocketFrame(
_In_reads_(DataLength) LPCVOID pData,
_In_ ULONGLONG DataLength,
_Out_ WS_FRAME_INFO* pFrameInfo,
_Out_ LPCVOID* pFramePayload)
{
if (!pData || DataLength < 2 || !pFrameInfo || !pFramePayload)
return WS_FRAME_ERROR;
LPCBYTE pFrameHeader = (LPCBYTE) pData;
ZeroMemory(pFrameInfo, sizeof(*pFrameInfo));
pFrameInfo->FinalPacket = AllBitsSet(pFrameHeader[0], 0x80);
pFrameInfo->RSV1 = AllBitsSet(pFrameHeader[0], 0x40);
pFrameInfo->RSV2 = AllBitsSet(pFrameHeader[0], 0x20);
pFrameInfo->RSV3 = AllBitsSet(pFrameHeader[0], 0x10);
pFrameInfo->OpCode = (WS_FRAME_OPCODE) (pFrameHeader[0] & 0x0F);
pFrameInfo->Masked = AllBitsSet(pFrameHeader[1], 0x80);
LPCBYTE pFrameData = pFrameHeader + 2;
BYTE payloadLengthBits = pFrameHeader[1] & 0x7F;
if (payloadLengthBits == 127)
{
pFrameInfo->PayloadLength = ByteSwap((* (ULONGLONG*) pFrameData) & 0x7FFFFFFFFFFFFFFF);
pFrameData += sizeof(ULONGLONG);
}
else if (payloadLengthBits == 126)
{
pFrameInfo->PayloadLength = ByteSwap(* (USHORT*) pFrameData);
pFrameData += sizeof(USHORT);
}
else
{
pFrameInfo->PayloadLength = payloadLengthBits;
}
if (pFrameInfo->Masked)
{
pFrameInfo->MaskingKey = * (DWORD*) pFrameData;
pFrameData += sizeof(DWORD);
}
*pFramePayload = pFrameData;
if ((pFrameInfo->OpCode & 0x80) && !pFrameInfo->FinalPacket)
return WS_FRAME_FRAGMENTED_OPCODE;
return WS_FRAME_OK;
}
WS_FRAME_RESULT
SetWebsocketFrame(
_In_ const WS_FRAME_INFO* pFrameInfo,
_Out_ WS_PACKED_FRAME_HEADER* pFrameHeader)
{
if (!pFrameInfo || !pFrameHeader)
return WS_FRAME_ERROR;
if ((pFrameInfo->OpCode & 0x80) && !pFrameInfo->FinalPacket)
return WS_FRAME_FRAGMENTED_OPCODE;
ZeroMemory(pFrameHeader, sizeof(WS_PACKED_FRAME_HEADER));
LPBYTE pOut = pFrameHeader->Data;
LPBYTE pExtraOut = pOut + 2;
pFrameHeader->Length = 2;
if (pFrameInfo->FinalPacket) pOut[0] |= 0x80;
if (pFrameInfo->RSV1) pOut[0] |= 0x40;
if (pFrameInfo->RSV2) pOut[0] |= 0x20;
if (pFrameInfo->RSV3) pOut[0] |= 0x10;
pOut[0] |= pFrameInfo->OpCode & 0xF;
if (pFrameInfo->Masked) pOut[1] |= 0x80;
if (pFrameInfo->PayloadLength > 0xFFFF)
{
pOut[1] |= 0x7F;
pFrameHeader->Length += sizeof(ULONGLONG);
(*(ULONGLONG*) pExtraOut) = pFrameInfo->PayloadLength;
pExtraOut += sizeof(ULONGLONG);
}
else if (pFrameInfo->PayloadLength > 0x7E)
{
pOut[1] |= 0x7E;
pFrameHeader->Length += sizeof(USHORT);
(*(USHORT*) pExtraOut) = (USHORT) pFrameInfo->PayloadLength;
pExtraOut += sizeof(USHORT);
}
else
{
pOut[1] |= (BYTE) pFrameInfo->PayloadLength & 0x7F;
}
if (pFrameInfo->Masked)
{
pFrameHeader->Length += sizeof(DWORD);
(*(DWORD*) pExtraOut) = pFrameInfo->MaskingKey;
pExtraOut += sizeof(DWORD);
}
assert(pFrameHeader->Length <= sizeof(pFrameHeader->Data));
return WS_FRAME_OK;
}
WS_FRAME_RESULT
SetWebsocketControlFrame(
_In_ WS_FRAME_OPCODE OpCode,
_In_ ULONGLONG PayloadLength,
_Out_ WS_PACKED_FRAME_HEADER* pFrameHeader)
{
WS_FRAME_INFO frameInfo;
ZeroMemory(&frameInfo, sizeof(frameInfo));
frameInfo.FinalPacket = 1;
frameInfo.OpCode = OpCode;
frameInfo.PayloadLength = PayloadLength;
return SetWebsocketFrame(
&frameInfo,
pFrameHeader);
}
WS_FRAME_RESULT
SetWebsocketCloseFrame(
_In_ WS_CLOSE_REASON Reason,
_In_ ULONGLONG PayloadLength,
_Out_ WS_PACKED_FRAME_HEADER* pFrameHeader)
{
WS_FRAME_INFO frameInfo;
ZeroMemory(&frameInfo, sizeof(frameInfo));
frameInfo.FinalPacket = 1;
frameInfo.OpCode = WS_FRAME_OPCODE_CONNECTION_CLOSE;
frameInfo.PayloadLength = PayloadLength + 2;
WS_FRAME_RESULT r = SetWebsocketFrame(
&frameInfo,
pFrameHeader);
if (r != WS_FRAME_OK)
return r;
assert(pFrameHeader->Length <= sizeof(pFrameHeader->Data) - 2);
BYTE i = pFrameHeader->Length;
pFrameHeader->Data[i++] = (BYTE) ((Reason & 0xFF00) >> 8);
pFrameHeader->Data[i++] = (BYTE) (Reason & 0xFF);
pFrameHeader->Length = i;
return WS_FRAME_OK;
}
void
MaskWebsocketPayload(
_In_reads_(DataLength) LPCVOID pData,
_In_ ULONGLONG DataLength,
_In_ DWORD MaskingKey,
_Out_writes_(DataLength) LPVOID pOutData)
{
LPCBYTE pInDataBytes = (LPCBYTE) pData;
LPCBYTE MaskBytes = (LPCBYTE) &MaskingKey;
LPBYTE pOutDataBytes = (LPBYTE) pOutData;
for (ULONGLONG i = 0; i < DataLength; ++i)
{
pOutDataBytes[i] = pInDataBytes[i] ^ MaskBytes[i % 4];
}
}
void
UnmaskWebsocketPayload(
_In_reads_(DataLength) LPCVOID pData,
_In_ ULONGLONG DataLength,
_In_ DWORD MaskingKey,
_Out_writes_(DataLength) LPVOID pOutData)
{
MaskWebsocketPayload(pData, DataLength, MaskingKey, pOutData);
}
}