forked from RDP1974/Delphi64RTL
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRDPWebBroker64.pas
More file actions
78 lines (68 loc) · 2.32 KB
/
RDPWebBroker64.pas
File metadata and controls
78 lines (68 loc) · 2.32 KB
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
unit RDPWebBroker64;
// 28 Febr 2019 Roberto Della Pasqua www.dellapasqua.com
// for better performances put RDPMM64 as first unit clause in project source
// define SEAZLIB for compression realtime speed up
// 26 Febr 2020 buffer len check
// 20 Febr 2023 updated zlibdeflate for correct mime
// be careful to set the correct content-type with utf-8 charset
{$DEFINE SEAZLIB}
interface
uses Windows, Sysutils, System.Classes, Web.HTTPApp, Web.WebReq;
type
TWBHelper = class helper for TWebResponse
public
procedure ZlibDeflate; overload;
// procedure ZlibDeflate(const Src: TMemoryStream); overload;
end;
implementation
uses RDPZlib64;
procedure TWBHelper.ZlibDeflate; // compress utf-8 text and json
var
ZBuff: TMemoryStream;
Src, Dst: TBytes;
Cl: string;
begin
Cl := Lowercase(ContentType); // example ContentType := 'application/json; charset="UTF-8"';
if ((Pos('text', Cl) > 0) or (Pos('json', Cl) > 0) and (Pos('utf-8', Cl) > 0)) then
begin
if ContentStream = nil then // use content string
begin
if (Length(Content) > 1500) and (Length(Content) < 10000000) then // 10MB
begin
Src := TEncoding.UTF8.GetBytes(Content);
SeaZlib.Compress(Src, Dst);
ZBuff := TMemoryStream.Create;
ZBuff.Write(Dst, Length(Dst));
ContentStream := ZBuff;
ContentStream.Seek(0,0);
ContentEncoding := 'deflate';
ContentLength := ContentStream.Size;
end;
end
else if (ContentStream.Size > 1500) and (ContentStream.Size < 10000000) then // 10MB
begin
ZBuff := TMemoryStream.Create;
SeaZlib.CompressStream(ContentStream, ZBuff, Z_BEST_SPEED_AC);
ContentStream.Size := ZBuff.Size;
ContentStream.Write(ZBuff, ZBuff.Size);
ContentEncoding := 'deflate';
ContentLength := ContentStream.Size;
ZBuff.Free;
end;
end;
end;
//procedure TWBHelper.ZlibDeflate(const Src: TMemoryStream);
//var
// Helper: TMemoryStream;
//begin
// if (Src.Size > 1500) and (Src.Size < 10000000) then // 10MB
// begin
// Src.Seek(0, 0);
// Helper := TMemoryStream.Create;
// SeaZlib.CompressStream(Src, Helper, Z_BEST_SPEED_AC);
// ContentStream := Helper;
// ContentEncoding := 'deflate';
// ContentLength := Helper.Size;
// end;
//end;
end.