-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuWebModuleMethod.pas
102 lines (90 loc) · 2.81 KB
/
uWebModuleMethod.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
unit uWebModuleMethod;
Interface
uses
System.SysUtils, System.Classes, Web.HTTPApp, Datasnap.DSHTTPCommon,
Datasnap.DSHTTPWebBroker, Datasnap.DSServer, Datasnap.DSHTTP,
Datasnap.DSAuth, IPPeerServer, Datasnap.DSCommonServer,
Datasnap.DSSession, Web.HTTPProd, URestPoolerDBMethod,
System.ZLib;
Type
TOnAfterDispatch = Procedure (Sender : TObject;
Request : TWebRequest;
Response : TWebResponse;
Var Handled : Boolean) Of Object;
Type
TDWModule = Class(TWebModule)
Private
vCompress : Boolean;
Function GetCompressVar : Boolean;
Procedure SetCompressVar(Value : Boolean);
Procedure OnAfterDispatch(Sender : TObject;
Request : TWebRequest;
Response : TWebResponse;
Var Handled : Boolean);
Public
Constructor Create(AOwner : TComponent);Override;
Published
Property Compression : Boolean Read GetCompressVar Write SetCompressVar;
End;
implementation
Constructor TDWModule.Create(AOwner : TComponent);
Begin
Inherited Create(AOwner);
AfterDispatch := OnAfterDispatch;
End;
Function TDWModule.GetCompressVar: Boolean;
Begin
Result := vCompress;
End;
Procedure TDWModule.SetCompressVar(Value: Boolean);
Begin
vCompress := Value;
End;
Procedure TDWModule.OnAfterDispatch(Sender : TObject;
Request : TWebRequest;
Response : TWebResponse;
Var Handled : Boolean);
{
Var
Original,
gZIPStream : TMemoryStream;
oString : String;
Len : Integer;
Procedure doGZIP(Input, gZipped: TMemoryStream);//helper function
Const
GZIP = 31;//very important because gzip is a linux zip format
Var
CompactadorGZip : TZCompressionStream;
Begin
Input.Position := 0;
CompactadorGZip := TZCompressionStream.Create(gZipped, zcMax, GZIP);
CompactadorGZip.CopyFrom(Input, Input.Size);
CompactadorGZip.Free;
gZipped.Position := 0;
End;
}
Begin
{
If vCompress Then
Begin
Original := TMemoryStream.Create;
gZIPStream := TMemoryStream.Create;
Try
oString := UTF8String(Response.Content);
Len := Length(oString);
Original.WriteBuffer(oString[1], len);
//make it gzip
doGZIP(Original, gZIPStream);
//prepare responsestream and set content encoding and type
Response.Content := '';
Response.ContentStream := gZIPStream;
Response.ContentEncoding := 'gzip, deflate';
Response.ContentType := 'application/json';
Finally
Original.DisposeOf;
gZIPStream.DisposeOf;
End;
End;
}
End;
end.