-
Notifications
You must be signed in to change notification settings - Fork 6
/
uHTTPServer.pas
734 lines (703 loc) · 30 KB
/
uHTTPServer.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
unit uHTTPServer;
{******************************************************************************}
{* Package HTTP Server Unit *}
{* Revolutionary Confederation of Anarcho Syndicalists *}
{* Written by: black.rabbit 2011-2012 *}
{******************************************************************************}
interface
{$I 'std.inc'}
uses
Windows, SysUtils, Variants, Classes,
Controls, ComCtrls, Gauges,
sListView,
{ utils }
DateUtils, Utils, Strings, VarRecs, Versions, EClasses,
DllThreads,
{ synapse }
WinSock, BlckSock, SynaUtil,
{ http }
HTTPServer,
{ kernel }
Kernel, ProtoClasses, CryptoClasses, MetaClasses, ParaClasses,
HypoClasses, HyperClasses,
{ engine }
Engine,
{ SQLite }
SQLite3, SQLite3DLL, SQLiteTable3;
{ èêîíêè }
const
pckDefault = 0;
pckEmpty = 1;
pckFull = 2;
pckTest = 3;
pckError = 4;
pckSend = 5;
pckReceive = 6;
type
{ ïàêåòíûé ñåðâåð }
{$M+}
CHTTPPackageServer = class of THTTPPackageServer;
PHTTPPackageServer = ^THTTPPackageServer;
THTTPPackageServer = class (THTTPServer)
private
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_ListView: TsListView; { ëîã }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
public
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
public
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property ListView: TsListView read f_ListView write f_ListView;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
end;
{$M-}
{ äî÷åðíèé ïîòîê ïàêåòíîãî ñåðâåðà }
{$M+}
CHTTPPackageServerThread = class of THTTPPackageServerThread;
PHTTPPackageServerThread = ^THTTPPackageServerThread;
THTTPPackageServerThread = class (THTTPServerThread)
private
f_DBFileName: String; { ôàéë áàçû äàííûõ }
f_DB: TSQLiteDatabase; { îáúåêò áàçû äàííûõ }
f_ListView: TsListView; { ëîã }
f_Gauge: TGauge; { ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel: TStatusPanel; { ñòðîêà ñîñòîÿíèÿ çàãðóçêè }
public
constructor Create (anArgs: array of const); override;
destructor Destroy; override;
public
procedure Main; override;
procedure Return; override;
function Process : WORD; override;
procedure ProcessPackage (aPackage: TPackage); virtual;
protected
procedure WriteStatus (const aMessage: String); overload;
procedure WriteStatus (const aMessage: String;
aParams: array of const); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
procedure WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = ''); overload;
public
property DBFileName: String read f_DBFileName;
property DB: TSQLiteDatabase read f_DB write f_DB;
property ListView: TsListView read f_ListView write f_ListView;
property Gauge: TGauge read f_Gauge write f_Gauge;
property StatusPanel: TStatusPanel read f_StatusPanel write f_StatusPanel;
end;
{$M-}
{ THTTPPackageServer Errors }
resourcestring
ERR_THTTPPACKAGESERVER_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_THTTPPACKAGESERVER_CREATE = 'Îøèáêà ñîçäàíèÿ ïîòîêà ïàêåòíîãî ñåðâåðà!';
ERR_THTTPPACKAGESERVER_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ ïîòîêà ïàêåòíîãî ñåðâåðà!';
ERR_THTTPPACKAGESERVER_EXECUTE = 'Îøèáêà âûïîëíåíèÿ ïîòîêà ïàêåòíîãî ñåðâåðà!';
ERR_THTTPPACKAGESERVER_MAIN = 'Îøèáêà ãëàâíîé ôóíêöèè ïîòîêà!';
ERR_THTTPPACKAGESERVER_RETURN = 'Îøèáêà ôóíêöèè âîçâðàòà ïîòîêà!';
ERR_THTTPPACKAGESERVER_INVALID_PORT = 'Íåêîððåêòíûé ïîðò!';
ERR_THTTPPACKAGESERVER_INVALID_TIMEOUT = 'Íåêîððåêòíîå âðåìÿ îæèäàíèÿ!';
ERR_THTTPPACKAGESERVER_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
{ THTTPPackageServerThread Errors }
resourcestring
ERR_THTTPPACKAGESERVERTHREAD_INCORRECT_DATABASE = 'Íå èíèöèàëèçèðîâàí îáúåêò ÁÄ!';
ERR_THTTPPACKAGESERVERTHREAD_CREATE = 'Îøèáêà ñîçäàíèÿ äî÷åðíåãî ïîòîêà ïàêåòíîãî ñåðâåðà!';
ERR_THTTPPACKAGESERVERTHREAD_DESTROY = 'Îøèáêà óíè÷òîæåíèÿ äî÷åðíåãî ïîòîêà ïàêåòíîãî ñåðâåðà!';
ERR_THTTPPACKAGESERVERTHREAD_MAIN = 'Îøèáêà ãëàâíîé ôóíêöèè ïîòîêà!';
ERR_THTTPPACKAGESERVERTHREAD_RETURN = 'Îøèáêà ôóíêöèè âîçâðàòà ïîòîêà!';
ERR_THTTPPACKAGESERVERTHREAD_PROCESS = 'Îøèáêà îáðàáîòêè çàïðîñà!';
ERR_THTTPPACKAGESERVERTHREAD_INVALID_SOCKET_ID = 'Íåêîððåêòíûé äèñêðèïòîð ñîêåòà!';
ERR_THTTPPACKAGESERVERTHREAD_INVALID_TIMEOUT = 'Íåêîððåêòíîå âðåìÿ îæèäàíèÿ!';
ERR_THTTPPACKAGESERVERTHREAD_SOCKET_ERROR = 'Îøèáêà ñîêåòà %d : %s';
ERR_THTTPPACKAGESERVERTHREAD_IVALID_REQUEST = 'Íåêîððåêòíûé çàïðîñ!';
ERR_THTTPPACKAGESERVERTHREAD_IVALID_PROTOCOL = 'Íåêîððåêòíûé ïðîòîêîë!';
ERR_THTTPPACKAGESERVERTHREAD_IVALID_METHOD = 'Íåêîððåêòíûé ìåòîä!';
ERR_THTTPPACKAGESERVERTHREAD_IVALID_URI = 'Íåêîððåêòíûé URI!';
ERR_THTTPPACKAGESERVERTHREAD_INVALID_PACKAGE = 'Ïîëó÷åí íåêîððåêòíûé ïàêåò!';
ERR_THTTPPACKAGESERVERTHREAD_LOAD_PACKAGES = 'Îøèáêà çàãðóçêè ïàêåòîâ èç ïîòîêà!';
ERR_THTTPPACKAGESERVERTHREAD_WRITE_STATUS = 'Îøèáêà îòîáðàæåíèÿ ñòàòóñà!';
ERR_THTTPPACKAGESERVERTHREAD_WRITE_LOG = 'Îøèáêà âåäåíèÿ ëîãà!';
{ THTTPPackageServerThread Hints }
resourcestring
MSG_TPACKAGESERVERTHREAD_RECEIVED_PACKAGE = 'Ïîëó÷åí ïàêåò îò %s:%d';
MSG_TPACKAGESERVERTHREAD_EXECUTING_PACKAGE = 'Îáðàáîòêà ïàêåòà ''%s''...';
MSG_TPACKAGESERVERTHREAD_TEST = 'Òåñò: «%s» Âåðñèÿ êëèåíòà: v.%s';
MSG_TPACKAGESERVERTHREAD_SAVED_PACKAGE = 'Ïîëó÷åí ïàêåò ''%s''.';
implementation
uses
DialogClasses;
{ THTTPPackageServer }
constructor THTTPPackageServer.Create (anArgs: array of const);
var
I : Integer;
args1 : array_of_const;
args2 : array_of_const;
OBJ : TObject;
begin
try
{ âòîðîé è òðåòèé ïàðàìåòðû - ïîðò è âðåìÿ îæèäàíèÿ }
args1 := _([NIL,NIL]);
if notEmpty (1,anArgs) then
args1 [0] := anArgs [1];
if notEmpty (2,anArgs) then
args1 [1] := anArgs [2];
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ ñåäüìîãî àðãóìåíòà }
if ( High (anArgs) >= 6 ) then
begin
SetLength ( args2, High (anArgs)-6 +1 );
for I := 6 to High (anArgs) do
args2 [I-6] := anArgs [I];
end
else
args2 := _([]);
inherited Create ( _(args1,args2) );
{ èìÿ ïîòîêà }
Name := ClassName;
{ ïðèîðèòåò }
Priority := tpNormal;
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_THTTPPACKAGESERVER_INCORRECT_DATABASE);
{ ÷åòâåðòûé ïàðàìåòð - ListView
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_ListView := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsListView) ) then
f_ListView := TsListView (OBJ);
end;
{ ïÿòûé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (4,anArgs) then
begin
OBJ := toObject (anArgs [4]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{ øåñòîé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (5,anArgs) then
begin
OBJ := toObject (anArgs [5]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
except on E: Exception do
_raise ([ 'Create', ERR_THTTPPACKAGESERVER_CREATE, E, Exception (FatalException) ],
['{CCE24D09-B65F-4E90-9C8C-C9F9950A7A9E}']);
end;
end;
destructor THTTPPackageServer.Destroy;
begin
try
try
FreeAndNil (f_DB);
finally
inherited Destroy;
end;
except on E: Exception do
_raise ([ 'Destroy', ERR_THTTPPACKAGESERVER_DESTROY, E, Exception (FatalException) ],
['{BB9FE7C3-A458-4B76-BBDA-4589FAC8D1AA}']);
end;
end;
procedure THTTPPackageServer.Main;
var
ClientSock : TSocket;
Thr : THTTPPackageServerThread;
begin
try
if Terminated then Exit;
with Sock do
begin
if CanRead (TimeOut) then
begin
ClientSock := Accept;
if ( LastError = 0 ) and ( ClientSock > 0 ) then
try
Thr := THTTPPackageServerThread.Create ([ DBFileName,
ClientSock,
TimeOut,
ListView,
Gauge,
StatusPanel ]);
if ( Threads.Add (Thr) < 0 ) then
FreeAndNil (Thr);
except
FreeAndNil (Thr);
end;
end;
end;
except on E: Exception do
_raise ([ 'Main', ERR_THTTPPACKAGESERVER_MAIN, E, Exception (FatalException) ],
['{05D7A6D7-0509-4E4C-8DC9-4FF3182FC334}']);
end;
end;
procedure THTTPPackageServer.WriteStatus (const aMessage: String);
begin
try
if Assigned (StatusPanel) then
begin
StatusPanel.Text := aMessage;
ProcessMessages;
end;
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGESERVER_WRITE_STATUS,E],
['{273F84AB-3B52-499B-B129-70D32BF144C3}']);
end;
end;
procedure THTTPPackageServer.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGESERVER_WRITE_STATUS,E],
['{46305BEA-C0A6-4E89-9912-9B77C84C98BB}']);
end;
end;
{ THTTPPackageServerThread }
constructor THTTPPackageServerThread.Create (anArgs: array of const);
var
I : Integer;
args1 : array_of_const;
args2 : array_of_const;
OBJ : TObject;
begin
try
{ âòîðîé è òðåòèé ïàðàìåòðû - ñîêåò è âðåìÿ îæèäàíèÿ }
args1 := _([NIL,NIL]);
if notEmpty (1,anArgs) then
args1 [0] := anArgs [1];
if notEmpty (2,anArgs) then
args1 [1] := anArgs [2];
{ ïåðåäàåì ïàðàìåòðû ñîçäàíèÿ áàçîâîãî ïîòîêà,
íà÷èíàÿ ñ ñåäüìîãî àðãóìåíòà }
if ( High (anArgs) >= 6 ) then
begin
SetLength ( args2, High (anArgs)-6 +1 );
for I := 6 to High (anArgs) do
args2 [I-6] := anArgs [I];
end
else
args2 := _([]);
inherited Create ( _(args1,args2) );
{ ïåðâûé ïàðàìåòð - ôàéë ÁÄ }
f_DBFileName := '';
f_DB := NIL;
if notEmpty (0,anArgs) then
begin
f_DBFileName := toString (anArgs [0]);
f_DB := TSQLiteDatabase.Create (f_DBFileName);
end;
if ( not Assigned (f_DB) ) then
raise Exception.Create (ERR_THTTPPACKAGESERVERTHREAD_INCORRECT_DATABASE);
{ ÷åòâåðòûé ïàðàìåòð - ListView
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_ListView := NIL;
if notEmpty (3,anArgs) then
begin
OBJ := toObject (anArgs [3]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TsListView) ) then
f_ListView := TsListView (OBJ);
end;
{ ïÿòûé ïàðàìåòð - Gauge
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_Gauge := NIL;
if notEmpty (4,anArgs) then
begin
OBJ := toObject (anArgs [4]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TControl) ) then
f_Gauge := TGauge (OBJ);
end;
{ øåñòîé ïàðàìåòð - StatusPanel
ïîêàçàòåëü ïðîãðåññà çàãðóçêè }
f_StatusPanel := NIL;
if notEmpty (5,anArgs) then
begin
OBJ := toObject (anArgs [5]);
if ( Assigned (OBJ) and OBJ.InheritsFrom (TStatusPanel) ) then
f_StatusPanel := TStatusPanel (OBJ);
end;
except on E: Exception do
_raise ([ 'Create', ERR_THTTPPACKAGESERVERTHREAD_CREATE, E, Exception (FatalException) ],
['{42006DEB-FD90-4A51-A3CB-2BFA9DA2EB6D}']);
end;
end;
destructor THTTPPackageServerThread.Destroy;
begin
try
try
FreeAndNil (f_DB);
finally
inherited Destroy;
end;
except on E: Exception do
_raise ([ 'Destroy', ERR_THTTPPACKAGESERVERTHREAD_DESTROY, E, Exception (FatalException) ],
['{53EA63BC-6A17-442C-BF9F-8504C7F05C0A}']);
end;
end;
procedure THTTPPackageServerThread.Main;
begin
try
inherited Main;
except
Terminate;
end;
end;
procedure THTTPPackageServerThread.Return;
begin
try
inherited Return;
except on E: Exception do
WriteStatus (E.Message);
end;
end;
function THTTPPackageServerThread.Process : WORD;
var
Packages : TPackages;
I : Integer;
vClient : Variant;
uriProt : String;
uriUser : String;
uriPass : String;
uriHost : String;
uriPort : String;
uriPath : String;
uriPara : String;
Params : TStringList;
ClientHash : Hex;
ClientID : TID;
Client : TUser;
SessionKey : Hex;
Sign : Hex;
TimeStamp : TDateTime;
begin
Result := HTTP_STATUS_INTERNAL_SERVER_ERROR;
try
if ( Sock.LastError <> 0 ) then
raise ESocketError.CreateFmt (ERR_THTTPSERVERTHREAD_SOCKET_ERROR,[Sock.LastError,Sock.LastErrorDesc]);
{ îáðàáîòêà ïàêåòà }
WriteStatus (MSG_TPACKAGESERVERTHREAD_RECEIVED_PACKAGE,
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]);
if ( Method = 'GET' ) then
begin
ParseURL (URI,uriProt,uriUser,uriPass,uriHost,uriPort,uriPath,uriPara);
try
Params := TStringList.Create;
Params.Delimiter := '&';
Params.DelimitedText := uriPara;
{ âåðñèÿ êëèåíòà }
vClient := v(NULL_VERSION);
vClient := Params.Values ['version'];
{ òåñòîâûé çàïðîñ }
if notEmpty (Params.Values ['test']) then
begin
WriteLog ( Format ('%s:%d',
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]),
Format ('%s:%d',
[ Sock.GetLocalSinIP,
Sock.GetLocalSinPort ]),
MSG_TPACKAGESERVERTHREAD_TEST,
[ Params.Values ['test'], vClient ],
pckTest );
Result := inherited Process;
end
{ ïåðåäàí õýø-êëþ÷ êëèåíòà }
else if notEmpty (Params.Values ['user']) and
isHex (Params.Values ['user']) then
begin
Result := HTTP_STATUS_NOT_FOUND;
ClientHash := Params.Values ['user'];
ClientID := TUsers.Find (DB,ClientHash,USER_ID);
if ( ClientID > 0 ) then
Client := TUser.Load (DB,ClientID,[objSimple]) as TUser;
if Assigned (Client) then
begin
(*{ çàïðîñ ñåññèîííîãî êëþ÷à }
if ( Params.Values ['get_session_key'] = '1' ) then
try
{ ãåíåðèðóåì ñåññèîííûé êëþ÷ }
SessionKey := User.Crypto.GenerateKey;
{ óäàëÿåì çàïèñè î ñåññèîííîì êëþ÷å äàííîãî êëèåíòà }
TSessions.Delete (DB,[ _([]),
_([ Format ('%s_session_key',[ClientHash]) ]),
_([USER_ID]) ])
{ ñîõðàíÿåì ñåññèîííûé êëþ÷ }
TimeStamp := now;
Session := TSession.Create (DB,[ 0,
Format ('%s_session_key',[ClientHash]),
USER_ID
TimeStamp,
TimeStamp + User.SessionTimeOut ]);
try
Session.Value := SessionKey;
Session.Save;
finally
FreeAndNil (Session);
end;
{ â îòâåòå ñåðâåðà ïèøåì ñåññèîííûé êëþ÷,
çàøèôðîâàííûé ïóáëè÷íûì êëþ÷îì êëèåíòà
è ïîäïèñàííûé ïðèâàòíûì êëþ÷îì ñåðâåðà }
Headers.Clear;
Headers.Add ('Content-type: Application/hex-stream');
TMemoryStream (OutputData).Clear;
WriteStrL ( OutputData,
User.Crypto.Encrypt (SessionKey,
Client.PublicKey,
User.PrivateKey) );
Result := HTTP_STATUS_OK;
finally
_FillChar ( SessionKey, Length (SessionKey), $00 );
TimeStamp := 0.0;
end
{ ïðîâåðÿåì ïîäïèñü }
else if notEmpty (Params.Values ['sign']) then
begin
Result := HTTP_STATUS_FORBIDDEN;
Sessions := TSessions.Load (DB,[ _([]),
_([ Format ('%s_session_key',[ClientHash]) ]),
_([USER_ID]),
_([NIL, now]) ]) as TSessions;
try
if ( Sessions.Count = 1 ) then
try
SessionKey := Sessions.ItemAt [0].Value;
{ ïðîâåðÿåì ïîäïèñü }
Sign := Params.Values ['sign'];
if notEmpty (SessionKey) and
notEmpty (Sign) and
( User.Crypto.Decrypt (Sign,
Client.PublicKey,
User.PrivateKey) = SessionKey ) then
begin
{ ïðîâåðêà ïîäïèñè ïðîéäåíà óñïåøíî }
end;
finally
_FillChar ( SessionKey, Length (SessionKey), $00 );
_FillChar ( Sign, Length (Sign), $00 );
end
else if ( Sessions.Count > 1 ) then
raise Exception.CreateFmt (ERR_THTTPPACKAGESERVERTHREAD_TOO_MANY_SESSION_KEYS,
[ Format ('%s_session_key',[ClientHash]) ]);
finally
FreeAndNil (Sessions);
end;
end;*)
end;
end;
finally
_FillChar ( uriProt, Length (uriProt), $00 );
_FillChar ( uriUser, Length (uriUser), $00 );
_FillChar ( uriPass, Length (uriPass), $00 );
_FillChar ( uriHost, Length (uriHost), $00 );
_FillChar ( uriPort, Length (uriPort), $00 );
_FillChar ( uriPath, Length (uriPath), $00 );
_FillChar ( uriPara, Length (uriPara), $00 );
FreeAndNil (Params);
_FillChar ( ClientHash, Length (ClientHash), $00 );
ClientID := 0;
FreeAndNil (Client);
end;
{ çàïðîñ ïàêåòîâ }
// êëèåíò äåëàåò çàïðîñ
// ñåðâåð îòñûëàåò ñåññèîííûé êëþ÷
// êëèåíò ïîäïèñûâàåò ñåññèîííûé êëþ÷
// êëèåíò ïîñûëàåò ñåðâåðó ïîäïèñàííûé êëþ÷
end
else if ( Method = 'POST' ) then
begin
Headers.Clear;
Headers.Add ('Content-type: Application/hex-stream');
TMemoryStream (OutputData).Clear;
Packages := TPackages.Create (DB,[]);
try
try
InputData.Position := 0;
Packages.LoadFromStream (InputData);
except on E: Exception do begin
WriteLog ( Format ('%s:%d',
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]),
Format ('%s:%d',
[ Sock.GetLocalSinIP,
Sock.GetLocalSinPort ]),
ERR_THTTPPACKAGESERVERTHREAD_LOAD_PACKAGES,
pckError );
end; end;
for I := 0 to Packages.Count-1 do
try
{ ïðîâåðÿåì - íîâûé ëè ïàêåò? }
if not ( TPackages.Find ( DB,
Packages.ItemAt [I].KeyHash,
USER_ID ) > 0 ) then
begin
{ ïðèøåë íîâûé ïàêåò - ïàêåò ïîëó÷åí }
Packages.ItemAt [I].IDStatus := PACKAGE_RECEIVED_STATUS_ID;
Packages.ItemAt [I].Save;
{ ïèøåì â ëîã ñòàòóñ ïàêåòà }
WriteLog ( Format ('%s:%d',
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]),
Format ('%s:%d',
[ Sock.GetLocalSinIP,
Sock.GetLocalSinPort ]),
Format (MSG_TPACKAGESERVERTHREAD_SAVED_PACKAGE,
[ Packages.ItemAt [I].KeyHash ]),
pckReceive,
GetPckTypeExternal (Packages.ItemAt [I].IDType),
Packages.ItemAt [I].MetaClass.GetClassID );
end;
{ ïèøåì îòâåò ñåðâåðà ïî êàæäîìó ïàêåòó }
WriteStrL ( OutputData, Packages.ItemAt [I].KeyHash );
WriteStrL ( OutputData, StrToHex ( IntToStr (Packages.ItemAt [I].IDStatus) ) );
except on SaveError: Exception do begin
if Assigned (Packages.ItemAt [I]) then
begin
{ ïèøåì îòâåò ñåðâåðà ïî êàæäîìó ïàêåòó }
WriteStrL ( OutputData, Packages.ItemAt [I].KeyHash );
WriteStrL ( OutputData, StrToHex ( IntToStr (PACKAGE_REJECTED_STATUS_ID) ) );
{ ïèøåì â ëîã ïðè÷èíó îøèáêè }
WriteLog ( Format ('%s:%d',
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]),
Format ('%s:%d',
[ Sock.GetLocalSinIP,
Sock.GetLocalSinPort ]),
Format ('%s : %s',
[ ERR_THTTPPACKAGESERVERTHREAD_INVALID_PACKAGE,
SaveError.Message ]),
pckError );
end
else
WriteStatus (SaveError.Message);
end; end;
Result := HTTP_STATUS_OK;
finally
FreeAndNil (Packages);
end;
end
else
begin
WriteLog ( Format ('%s:%d',
[ Sock.GetRemoteSinIP,
Sock.GetRemoteSinPort ]),
Format ('%s:%d',
[ Sock.GetLocalSinIP,
Sock.GetLocalSinPort ]),
Format ('%s : ''%s''',
[ ERR_THTTPPACKAGESERVERTHREAD_IVALID_METHOD,
Method ]),
pckError );
Result := HTTP_STATUS_BAD_REQUEST;
end;
except on E: Exception do
_raise ([ 'Process', ERR_THTTPPACKAGESERVERTHREAD_PROCESS, E, Exception (FatalException) ],
['{729EEE41-8224-4965-BFFA-C2B3BA669B54}']);
end;
end;
procedure THTTPPackageServerThread.ProcessPackage (aPackage: TPackage);
begin
if not Assigned (aPackage) then
raise Exception.Create (ERR_THTTPPACKAGESERVERTHREAD_INVALID_PACKAGE);
WriteStatus (MSG_TPACKAGESERVERTHREAD_EXECUTING_PACKAGE,
[ aPackage.KeyHash ]);
aPackage.ProcessMetaData;
end;
procedure THTTPPackageServerThread.WriteStatus (const aMessage: String);
begin
try
if Assigned (StatusPanel) then
begin
StatusPanel.Text := aMessage;
ProcessMessages;
end;
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGESERVERTHREAD_WRITE_STATUS,E],
['{201B6CBB-B248-43D5-B702-670DE570052E}']);
end;
end;
procedure THTTPPackageServerThread.WriteStatus (const aMessage: String;
aParams: array of const);
begin
try
WriteStatus ( Format (aMessage,aParams) );
except on E: Exception do
_raise (['WriteStatus',ERR_THTTPPACKAGESERVERTHREAD_WRITE_STATUS,E],
['{9976176A-96ED-4CE5-8549-D2F5F696A9BD}']);
end;
end;
procedure THTTPPackageServerThread.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
var
Itm : TListItem;
begin
try
if Assigned (ListView) then
begin
Itm := ListView.Items.Add;
Itm.Caption := _DateTimeToStr (now);
Itm.ImageIndex := anImageIndex;
Itm.SubItems.Add (aSender);
Itm.SubItems.Add (aReceiver);
if notEmpty (aMetaClassID) then
Itm.SubItems.Add ( Format ('%s : %s : %s',[aMetaClassID,aPackageType,aMessage]) )
else
Itm.SubItems.Add (aMessage);
end;
except on E: Exception do
_raise (['WriteLog',ERR_THTTPPACKAGESERVERTHREAD_WRITE_LOG,E],
['{E9F9E881-D650-405C-8AF4-50B817650EB8}']);
end;
end;
procedure THTTPPackageServerThread.WriteLog (const aSender: String;
const aReceiver: String;
const aMessage: String;
aParams: array of const;
const anImageIndex: Integer = pckDefault;
const aPackageType: String = '';
const aMetaClassID: String = '');
begin
try
WriteLog ( aSender, aReceiver, Format (aMessage,aParams), anImageIndex, aPackageType, aMetaClassID );
except on E: Exception do
_raise (['WriteLog',ERR_THTTPPACKAGESERVERTHREAD_WRITE_LOG,E],
['{781BA73B-A41E-44C0-A8EA-90AB08DFC138}']);
end;
end;
end.