forked from remittor/delphi-iocp-server-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuHPServClient.pas
572 lines (502 loc) · 15.3 KB
/
uHPServClient.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
unit uHPServClient;
(*******************************************************************************
Author: Sergey N. Naberegnyh
Created: August, 05, 2008
Updated: January, 23, 2009
// Cmd.exe -> netsh winsock reset ???
*******************************************************************************)
interface
uses
Windows, Messages, SysUtils, Classes,
HPSockApi, HPScktSrvr, WinSock; // JwaWinCrypt;
type
THttpMethod = (METHOD_UNKNOWN, METHOD_GET, METHOD_HEAD);
TAuthType = (atNone, atBasic, atDigest);
EDigestAuthInitError = Exception;
TNonceCountMask = array[0..7] of Cardinal;
PNonceCountMask = ^TNonceCountMask;
TStringList = Class(Classes.TStringList);
TNonceBuffer = packed record
Timestamp: TSystemTime;
Hash: array[0..31] of Byte;
end;
THttpSrvClient = class(THPServerClient)
private
FNotProcessed: integer;
FCmdAccepted: boolean;
FHttpMethod: THttpMethod;
FVersion: String;
FObjName: String;
FParams: String;
FRequest: TStringList;
FResponse: String;
FFile: THandle;
FkeepAlive: boolean;
function BuildAuthResponse(const sNonce: AnsiString): AnsiString;
procedure ParseCmdLine(const CommandLine: AnsiString);
procedure SendError(ErrCode: integer; const sAuthInfo: AnsiString = '');
procedure ProcessRequest();
function CheckUserPassword(const Auth: AnsiString): boolean;
protected
FAuthorized: boolean;
FAuthType: TAuthType;
procedure SendResponse(PData: Pointer; DataLen: integer); virtual;
procedure SendResponse2(PData: Pointer; DataLen: integer); virtual;
procedure CloseFile;
property FileHandle: THandle read FFile;
public
constructor Create; override;
destructor Destroy; override;
procedure Initialize; virtual;
procedure Finalize; virtual;
procedure DoWriteComplete(BytesTransfered: Cardinal; CompletionKey, Error: Integer); virtual;
procedure ProcessBuffer(PBuf: PAnsiChar; BufLen: integer); virtual;
property KeepAlive: boolean read FkeepAlive write FKeepAlive;
property RequestHeader: TStringList read FRequest;
end;
implementation
uses
uHPServCon, StrUtils, Math;
const
cOpaque = '07D0A5F7D91D43DBB30C0211F9FFDB3C';
Current_Zone = 'localhost zone';
MICROSOFT_DIGEST_NAME = 'Microsoft Digest Security Protocol Provider';
WDIGEST_SP_NAME = 'WDigest';
Session_Timeout = 5/(24 * 60);
function DecodeBase64(pSource: Pointer; SourceLen: DWORD; pDest: Pointer; var DestLen: DWORD): BOOL;
const
BBase64: array [0..79] of Byte =
($3E, $40, $40, $40, $3F, $34, $35, $36, $37, $38,
$39, $3A, $3B, $3C, $3D, $40, $40, $40, $40, $40,
$40, $40, $00, $01, $02, $03, $04, $05, $06, $07,
$08, $09, $0A, $0B, $0C, $0D, $0E, $0F, $10, $11,
$12, $13, $14, $15, $16, $17, $18, $19, $40, $40,
$40, $40, $40, $40, $1A, $1B, $1C, $1D, $1E, $1F,
$20, $21, $22, $23, $24, $25, $26, $27, $28, $29,
$2A, $2B, $2C, $2D, $2E, $2F, $30, $31, $32, $33);
var
i, k, d, dd, Len: DWORD;
b: byte;
PD, PS: PAnsiChar;
begin
Result:= (@DestLen <> nil) and (pSource <> nil);
if not Result then begin
SetLastError(ERROR_INVALID_PARAMETER);
Exit;
end;
Len:= (SourceLen div 4) * 3 + (SourceLen mod 4);
if (pDest = nil) or (Len = 0) then begin
DestLen:= Len;
Exit;
end;
if Len > DestLen then begin
Result:= false;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
Exit;
end;
i:= 0; d:= 0; dd:= 0;
PD:= pDest; PS:= pSource;
repeat
k:= Ord(PS[i]);
if k in [43..43 + High(BBase64)] then b:= BBase64[k - 43] else b:= $40;
if b = $40 then begin
Inc(i);
Continue;
end;
PD[d]:= Chr(Ord(b) shl 2);
dd:= 1;
Inc(i); Inc(d);
if i >= SourceLen then Break;
k:= Ord(PS[i]);
if k in [43..43 + High(BBase64)] then b:= BBase64[k - 43] else b:= $40;
if b = $40 then begin
Inc(i);
Continue;
end;
PD[d - 1]:= Chr(Ord(PD[d - 1]) or ((b shr 4) and 3));
PD[d]:= Chr((b shl 4) and $F0);
Inc(i); Inc(d);
if i >= SourceLen then Break;
k:= Ord(PS[i]);
if k in [43..43 + High(BBase64)] then b:= BBase64[k - 43] else b:= $40;
if b = $40 then begin
Inc(i);
Continue;
end;
PD[d - 1]:= Chr(Ord(PD[d - 1]) or ((b shr 2) and $0F));
PD[d]:= Chr((b shl 6) and $FC);
Inc(i); Inc(d);
if i >= SourceLen then Break;
k:= Ord(PS[i]);
if k in [43..43 + High(BBase64)] then b:= BBase64[k - 43] else b:= $40;
if b = $40 then begin
Inc(i);
Continue;
end;
PD[d - 1]:= Chr(Ord(PD[d - 1]) or (b and $3F));
Inc(i); dd:= 0;
until i >= SourceLen;
DestLen:= d - dd;
end;
function DecodeBase64Str(const Source: AnsiString): AnsiString;
var
L, L2: Cardinal;
begin
Result := '';
if Source = '' then Exit;
if not DecodeBase64(@Source[1], Length(Source), nil, L) then Exit;
Setlength(Result, L);
L2 := L;
if not DecodeBase64(@Source[1], Length(Source), @Result[1], L) then begin
Result := '';
end else begin
if L2 <> L then SetLength(Result, L);
end;
end;
function BinToHex(Buffer: PByteArray; BufSize: Integer): AnsiString;
const
Convert: array[0..15] of Char = '0123456789abcdef';
var
I: Integer;
P: PAnsiChar;
begin
SetLength(Result, BufSize * 2);
P:= PAnsiChar(result);
for I := 0 to BufSize - 1 do begin
P[0] := Convert[Byte(Buffer[I]) shr 4];
P[1] := Convert[Byte(Buffer[I]) and $F];
Inc(P, 2);
end;
end;
{ THttpSrvClient }
function THttpSrvClient.BuildAuthResponse(const sNonce: AnsiString): AnsiString;
const
cDigestAuth = 'WWW-Authenticate: Digest realm="localhost zone",' +
' qop="auth",' +
' nonce="%s", opaque="%s"';
begin
Result := Format(cDigestAuth, [sNonce, cOpaque]);
end;
function THttpSrvClient.CheckUserPassword(const Auth: AnsiString): boolean;
var
k: integer;
ds, Usr, Psw: AnsiString;
begin
Result := false;
ds := DecodeBase64Str(Auth);
k := Pos(':', ds);
if k < 1 then exit;
Usr := UpperCase(Copy(ds, 1, k - 1));
Psw := Copy(ds, k + 1, 255);
Result := (Usr = 'TEST_USER') and (Psw = 'mypsw');
end;
procedure THttpSrvClient.CloseFile;
begin
if FFile <> 0 then begin
CloseHandle(FFile);
FFile := 0;
end;
end;
constructor THttpSrvClient.Create;
begin
inherited;
FRequest := TStringList.Create;
FRequest.Sorted := True;
end;
destructor THttpSrvClient.Destroy;
begin
Finalize;
FRequest.Free;
inherited;
end;
procedure THttpSrvClient.DoWriteComplete(BytesTransfered: Cardinal; CompletionKey, Error: Integer);
var
WsaBuf: TWsaBuf;
begin
CloseFile;
FResponse := '';
if KeepAlive then begin
WsaBuf.cLength := RecvBufSize;
WsaBuf.pBuffer := RecvBuf;
if ReadBuffer(WsaBuf, 1, 0) <> 0 then Disconnect($60);
end else begin
Disconnect($61);
end;
end;
procedure THttpSrvClient.Finalize;
begin
FRequest.Clear;
CloseFile;
end;
procedure THttpSrvClient.Initialize;
begin
FNotProcessed := 0;
FCmdAccepted := false;
FkeepAlive := false;
FAuthorized := false;
FAuthType := atNone; // atDigest; //
FVersion := '';
FObjName := '';
FParams := '';
end;
procedure THttpSrvClient.ParseCmdLine(const CommandLine: AnsiString);
var
Command: AnsiString;
i, L, n: integer;
begin
Server.LogMsg(1, 1, ClientID, 'ParseCmdLine - Line '+CommandLine);
FHttpMethod := METHOD_UNKNOWN;
i := Pos(' ', CommandLine);
if i = 0 then exit;
Command := Copy(CommandLine, 1, i - 1);
if Command = 'GET' then FHttpMethod := METHOD_GET else
if Command = 'HEAD' then FHttpMethod := METHOD_HEAD;
Inc(i);
L := 0;
for n:= Length(CommandLine) downto i do begin
if CommandLine[n] = ' ' then begin
L:= n + 1;
Break;
end;
end;
if L = 0 then Exit;
FVersion := Copy(CommandLine, L, MaxInt);
if i >= L - 1 then Exit;
FObjName := Trim(Copy(CommandLine, i, L - i - 1));
i := Pos('?', FObjName);
if i <> 0 then begin
FParams := Copy(FObjName, i + 1, MaxInt);
SetLength(FObjName, i - 1);
end;
end;
procedure THttpSrvClient.ProcessBuffer(PBuf: PAnsiChar; BufLen: integer);
var
SrcPos, DstPos, LastLine: integer;
db, s: AnsiString;
WsaBuf: TWsaBuf;
begin
Server.LogMsg(1, 1, ClientID, 'ProcessBuffer - IN');
SetLength(db, 3);
db[1] := '$';
SetLength(s, MAX_LINE_LEN);
SrcPos := 0;
DstPos := 1;
LastLine := 0;
Inc(BufLen, FNotProcessed);
while SrcPos < BufLen do begin
if DstPos > MAX_LINE_LEN then begin
if FRequest.Count = 0 then SendError(414) else SendError(413);
Exit;
end;
case PBuf[SrcPos] of
#10:
begin
Inc(SrcPos);
LastLine:= SrcPos;
if DstPos > 1 then begin
if FCmdAccepted then begin
FRequest.Add(Copy(s, 1, DstPos - 1));
if FRequest.Count > 16 then begin
SendError(413);
Exit;
end;
end else begin
FCmdAccepted:= true;
ParseCmdLine(Trim(Copy(s, 1, DstPos - 1)));
end;
DstPos := 1;
end else begin
Server.LogMsg(1, 1, ClientID, 'ProcessBuffer - complete');
FCmdAccepted := False;
ProcessRequest;
Exit;
end;
end;
#13:
begin
Inc(SrcPos);
end;
'%':
begin
if SrcPos + 2 < BufLen then begin
Inc(SrcPos);
db[2] := PBuf[SrcPos];
Inc(SrcPos);
db[3] := PBuf[SrcPos];
Inc(SrcPos);
s[DstPos] := Chr(StrToInt(db));
Inc(DstPos);
end else begin
Break;
end;
end;
else begin
s[DstPos] := PBuf[SrcPos];
Inc(SrcPos);
Inc(DstPos);
end;
end;
end;
FNotProcessed := BufLen - LastLine;
if FNotProcessed > 0 then Move(PBuf[LastLine], PBuf[0], FNotProcessed);
WsaBuf.cLength := RecvBufSize - FNotProcessed;
WsaBuf.pBuffer := RecvBuf;
Inc(WsaBuf.pBuffer, FNotProcessed);
ReadBuffer(WsaBuf, 1, 0);
end;
procedure THttpSrvClient.ProcessRequest;
const
cBasicHeader = 'Authorization: Basic ';
cDigestHeader = 'Authorization: Digest ';
var
Size: LARGE_INTEGER;
s: AnsiString;
n: integer;
AuthHeader: AnsiString;
sAuthInfo: AnsiString;
AuthResult: Cardinal;
NewClient: boolean;
begin
Server.LogMsg(1, 1, ClientID, 'ProcessRequest - IN');
try
if FHttpMethod = METHOD_UNKNOWN then begin
SendError(501);
Exit;
end;
Server.LogMsg(1, 1, ClientID, 'ProcessRequest - Object '+FObjName);
if FObjName = '' then begin
SendError(400);
Exit;
end;
if Pos('test.htm', FObjName) > 0 then begin
FKeepAlive := True;
SendError(999);
Exit;
end;
if FObjName[1] = '/' then FObjName[1] := '\';
if FObjName = '\' then FObjName := RootDir + '\Index.htm'
else FObjName := RootDir + FObjName;
AuthResult := 401;
FAuthorized := FAuthorized or (FAuthType = atNone);
if not FAuthorized then begin
if FAuthType = atBasic then AuthHeader:= cBasicHeader else AuthHeader:= cDigestHeader;
NewClient := true;
for n:= 0 to Pred(FRequest.Count) do begin
if AnsiStartsText(AuthHeader, FRequest[n]) then begin
NewClient := false;
s := Copy(FRequest[n], Length(AuthHeader) + 1, MaxInt);
if FAuthType = atBasic then begin
FAuthorized:= CheckUserPassword(s);
if not FAuthorized then AuthResult:= 401;
end else begin
//AuthResult:= DigestSecurity.CheckDigestAuth(s, 'GET', sAuthInfo);
//FAuthorized:= AuthResult = 200;
end;
Break;
end;
end;
//if not FAuthorized and NewClient then
// sAuthInfo:= BuildAuthResponse(DigestSecurity.NewNonceString);
end;
if not FAuthorized then
begin
SendError(AuthResult, sAuthInfo);
Exit;
end;
FkeepAlive:= false;
for n:= 0 to Pred(FRequest.Count) do
begin
if AnsiStartsText('Connection: Keep-Alive', FRequest[n]) then
begin
FkeepAlive:= true;
Break;
end;
end;
FFile := CreateFile(PAnsiChar(FObjName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
if FFile = INVALID_HANDLE_VALUE then
begin
Server.LogMsg(1, 1, ClientID, 'ProcessRequest - failed CreateFile !!!!! '+IntToHex(GetLastError, 8));
Server.LogMsg(1, 1, ClientID, 'Failed CreateFile '+FObjName);
FFile := 0;
if GetLastError = ERROR_FILE_NOT_FOUND then SendError(404) else SendError(500);
Exit;
end;
Size.LowPart:= GetFileSize(FFile, @Size.HighPart);
if FHttpMethod = METHOD_HEAD then CloseFile;
if KeepAlive then
FResponse:= Format(sSend_OK_KeepAlive, [Size.QuadPart])
else
FResponse:= Format(sSend_OK, [Size.QuadPart]);
Server.LogMsg(1, 1, ClientID, 'ProcessRequest - Complete');
SendResponse(@FResponse[1], Length(FResponse));
finally
FObjName:= '';
FParams:= '';
FVersion:= '';
FRequest.Clear;
end;
end;
procedure THttpSrvClient.SendError(ErrCode: integer; const sAuthInfo: AnsiString);
var
sErr: AnsiString;
const
sTemplate = 'HTTP/1.0 %s'#$0D#$0A'Server: HttpDemo 1.0'#$0D#$0A#$0D#$0A;
begin
case ErrCode of
400: sErr:= '400 Bad Request';
401:
if FAuthType = atBasic then
sErr:= '401 Unauthorized'#$0D#$0A +
'WWW-Authenticate: Basic realm="localhost zone"'
else
sErr:= '401 Unauthorized'#$0D#$0A + sAuthInfo;
404: sErr:= '404 Not Found';
405: sErr:= '405 Method Not Allowed';
413: sErr:= '413 Request Entity Too Large';
414: sErr:= '414 Request-URI Too Long';
500: sErr:= '500 Internal Server Error';
501: sErr:= '501 Not Implemented';
999: begin
//sErr:= '999 Fuck u';
FResponse := 'HTTP/1.0 200 OK'#$0D#$0A+
'Server: HttpDemo 1.0'#$0D#$0A+
'Connection: Keep-Alive'#$0D#$0A+
'Content-length: 6'#$0D#$0A+
#$0D#$0A+
'Hello 888';
SendResponse2(@FResponse[1], Length(FResponse));
Exit;
end;
else sErr:= IntToStr(ErrCode);
end;
FResponse := Format(sTemplate, [sErr]);
//KeepAlive:= false;
SendResponse(@FResponse[1], Length(FResponse));
end;
procedure THttpSrvClient.SendResponse(PData: Pointer; DataLen: integer);
var
Buf: TTransmitFileBuffers;
begin
Buf.Head := PData;
Buf.HeadLength := DataLen;
Buf.Tail := nil;
Buf.TailLength := 0;
if (Transmit(FFile, 0, 0, @Buf, 0, false) <> 0) //or not KeepAlive
then Disconnect;
end;
procedure THttpSrvClient.SendResponse2(PData: Pointer; DataLen: integer);
var
wsabuf: TWsaBuf;
res: Integer;
begin
wsabuf.pBuffer := PData;
wsabuf.cLength := Cardinal(DataLen);
res := Self.WriteBuffer(wsabuf, 1, 0);
if res <> 0 then begin
Server.LogMsg(1, 0, ClientID, '[ERROR] WSASend Error: '+IntToStr(res));
Disconnect;
end else begin
Server.LogMsg(1, 1, ClientID, 'WSASend OK.'); // DO-
end;
end;
end.