-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuWebSocketFrame.pas
415 lines (359 loc) · 10.7 KB
/
uWebSocketFrame.pas
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
unit uWebSocketFrame;
{
WEbSocket RFC https://tools.ietf.org/html/rfc6455
}
interface
uses
SysUtils, Classes,
//
uWebSocketConst;
type
TWebSocketFrame = record
private
FIsComplete: Boolean;
public
FIN: Boolean;
RSV1: Boolean;
RSV2: Boolean;
RSV3: Boolean;
Opcode: TWsOpcode;
Mask: Boolean;
PayloadLen: UInt64;
MaskingKey: UInt32;
PayloadData: RawByteString; // as was received
DecodedData: RawByteString; // decoded (inflated)
constructor Create(const ACode: TWsOpcode; const APayload, ADecoded: RawByteString;
const AMask: Boolean; const AFIN: Boolean = True);
constructor CreateFromBuffer(var ABuffer: RawByteString);
procedure LoadFromBuffer(var ABuffer: RawByteString);
procedure Clear;
procedure Assign(const A: TWebSocketFrame);
function IsValid: Boolean;
function IsValidOpcode: Boolean;
function IsIncomplete: Boolean;
function IsDeflated: Boolean;
procedure SetAsDeflated;
function ToBuffer: RawByteString;
procedure SetComplete(const A: Boolean = True);
property IsComplete: Boolean read FIsComplete;
end;
implementation
uses
//
AcedBinary;
type
PUInt16 = ^UInt16;
PUInt32 = ^UInt32;
UInt32Rec = packed record
case UInt32 of
0: (Lo, Hi: Word);
1: (Words: array [0..1] of Word);
2: (Bytes: array [0..3] of Byte);
end;
PUInt32Rec = ^UInt32Rec;
TArray4Byte = array[0..3] of UInt8;
// helpers
{$REGION 'helpers'}
function ByteSwap64(Value: UInt64): UInt64;
asm
{$IF Defined(CPUX86)}
mov edx, [ebp+$08]
mov eax, [ebp+$0c]
bswap edx
bswap eax
{$ELSEIF Defined(CPUX64)}
mov rax, rcx
bswap rax
{$ELSE}
{$Message Fatal 'ByteSwap64 has not been implemented for this architecture.'}
{$ENDIF}
end;
function ByteSwap32(Value: UInt32): UInt32;
begin
Result := G_BSwap(Value)
end;
function ByteSwap16(Value: UInt16): UInt16;
begin
Result := (Byte(Value and $FF) shl 8) or
(Byte((Value and $FF00) shr 8));
end;
function Random_UInt32: UInt32;
var
Overlay: packed record
a, b: UInt16;
end absolute Result;
begin
Assert(SizeOf(Overlay)=SizeOf(Result));
Overlay.a := Random($10000);
Overlay.b := Random($10000);
end;
procedure MaskingKeyToArray(const AKey: UInt32; var AArr: TArray4Byte);
begin
AArr[3] := AKey and $000000FF;
AArr[2] := (AKey shr 8) and $000000FF;
AArr[1] := (AKey shr 16) and $000000FF;
AArr[0] := (AKey shr 24) and $000000FF;
end;
procedure DoXorMasking(P: PByte; const AKey: UInt32; const ACount: Integer);
var
K: TArray4Byte;
j: Integer;
begin
MaskingKeyToArray(AKey, K);
j := 0;
while j < ACount do
begin
P^ := P^ xor K[j mod 4];
Inc(P);
Inc(j);
end;
end;
{$ENDREGION}
{ TWebSocketFrame }
procedure TWebSocketFrame.Assign(const A: TWebSocketFrame);
begin
// Self := A; ???
FIsComplete := A.FIsComplete;
FIN := A.FIN;
RSV1 := A.RSV1;
RSV2 := A.RSV2;
RSV3 := A.RSV3;
Opcode := A.Opcode;
Mask := A.Mask;
PayloadLen := A.PayloadLen;
MaskingKey := A.MaskingKey;
PayloadData := A.PayloadData;
DecodedData := A.DecodedData;
end;
procedure TWebSocketFrame.Clear;
begin
FIsComplete := False;
FIN := True;
RSV1 := False;
RSV2 := False;
RSV3 := False;
Opcode := wsNoFrame;
Mask := False;
PayloadLen := 0;
MaskingKey := 0;
PayloadData := '';
DecodedData := '';
end;
constructor TWebSocketFrame.Create(const ACode: TWsOpcode;
const APayload, ADecoded: RawByteString; const AMask, AFIN: Boolean);
begin
FIsComplete := True;
FIN := AFIN;
RSV1 := False;
RSV2 := False;
RSV3 := False;
Opcode := ACode;
PayloadLen := Length(APayload);
Mask := AMask; //and (PayloadLen > 0);
MaskingKey := 0;
PayloadData := APayload;
DecodedData := ADecoded;
end;
constructor TWebSocketFrame.CreateFromBuffer(var ABuffer: RawByteString);
begin
LoadFromBuffer(ABuffer);
end;
function TWebSocketFrame.IsDeflated: Boolean;
begin
Result := RSV1
end;
function TWebSocketFrame.IsIncomplete: Boolean;
begin
Result := (not FIsComplete)
end;
function TWebSocketFrame.IsValid: Boolean;
begin
Result := FIsComplete and IsValidOpcode() and (PayloadLen = Length(PayloadData))
end;
function TWebSocketFrame.IsValidOpcode: Boolean;
begin
Result := ($0 <= Opcode) and (Opcode <= $F)
end;
{ 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+ }
procedure TWebSocketFrame.LoadFromBuffer(var ABuffer: RawByteString);
var
b: Byte;
l, i, lmask: Cardinal;
begin
Clear();
l := Length(ABuffer);
// < header
if l < WS_FRAME_MIN_SIZE then
Exit;
// base header
i := 1;
b := Byte(ABuffer[i]);
Inc(i);
FIN := (b and 128) <> 0; // 1000 0000
RSV1 := (b and 64) <> 0; // 0100 0000
RSV2 := (b and 32) <> 0; // 0010 0000
RSV3 := (b and 16) <> 0; // 0001 0000
Opcode := b and 15; // 0000 1111
b := Byte(ABuffer[i]);
Inc(i);
// base header #2
Mask := (b and 128) <> 0; // 1000 0000
if Mask then
lmask := 4
else
lmask := 0;
PayloadLen := b and 127; // 0111 1111
if PayloadLen = 127 then
begin
// < header + Payload Len
if l < (2 + 8) then
Exit;
PayloadLen := ByteSwap64(PUInt64(@ABuffer[i])^); // network byte order -> x86 byte order
// < header + Payload Len + Mask + Payload data
if l < (2 + 8 + lmask + PayloadLen) then
Exit;
Inc(i, 8);
end
else
if PayloadLen = 126 then
begin
// < header + Payload Len
if l < (2 + 2) then
Exit;
PayloadLen := ByteSwap16(PUInt16(@ABuffer[i])^);
// < header + Payload Len + Mask + Payload data
if l < (2 + 2 + lmask + PayloadLen) then
Exit;
Inc(i, 2);
end
else
begin
// < header + data len + mask + data
if l < (2 + lmask + PayloadLen) then
Exit;
end;
//---
// masking key
if Mask then
begin
MaskingKey := ByteSwap32(PUInt32(@ABuffer[i])^);
Inc(i, 4)
end;
PayloadData := Copy(ABuffer, i, PayloadLen);
if Mask and (PayloadLen > 0) then
begin
DoXorMasking(@PayloadData[1], MaskingKey, PayloadLen)
end;
// delete frame from buffer
Inc(i, PayloadLen);
Delete(ABuffer, 1, i - 1);
//
DecodedData := PayloadData;
//
FIsComplete := True;
end;
procedure TWebSocketFrame.SetAsDeflated;
begin
RSV1 := True
end;
procedure TWebSocketFrame.SetComplete(const A: Boolean);
begin
FIsComplete := A;
FIN := A;
end;
{ 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+ }
function TWebSocketFrame.ToBuffer: RawByteString;
var
k: Integer;
h: UInt16;
extLen, maskKey: RawByteString;
maskKeyBE: UInt32;
begin
Result := '';
if not IsValid then
Exit;
h := 0;
if FIN then
h := h or $8000; // 10000000 00000000
if RSV1 then
h := h or $4000; // 01000000 00000000
if RSV2 then
h := h or $2000; // 00100000 00000000
if RSV3 then
h := h or $1000; // 00010000 00000000
// opcode
// isValid() Opcode := Opcode and $7F; 01111111
h := h or (UInt16(Opcode) shl 8); // 00001111 00000000
if Mask then
h := h or $80; // 00000000 10000000
extLen := '';
if PayloadLen <= 125 then
begin
h := h or PayloadLen; // 00000000 01111101
end
else
if PayloadLen <= $FFFF then
begin
h := h or 126; // 00000000 01111110
SetLength(extLen, 2);
PUInt16(@extLen[1])^ := ByteSwap16(UInt16(PayloadLen));
end
else
begin
h := h or 127; // 00000000 01111111
SetLength(extLen, 8);
PUInt64(@extLen[1])^ := ByteSwap64(PayloadLen);
end;
maskKey := '';
if Mask then
begin
if MaskingKey = 0 then
MaskingKey := Random_UInt32();
maskKeyBE := ByteSwap32(MaskingKey);
SetLength(maskKey, 4);
PUInt32(@maskKey[1])^ := maskKeyBE;
end;
SetLength(Result, 2);
PUInt16(@Result[1])^ := ByteSwap16(UInt16(h));
Result := Result + extLen + maskKey + PayloadData;
if Mask and (PayloadLen > 0) then
begin
k := 2 + Length(extLen) + Length(maskKey) + 1;
DoXorMasking(@Result[K], MaskingKey, PayloadLen)
end;
end;
end.