Skip to content

Commit

Permalink
Comprimir string #11
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciussanchez committed Feb 11, 2022
1 parent f4d2f32 commit 64dce70
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 117 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ __history/
__recovery/
*.~*

# Lazarus
backup/
lib/
*.lps

# Castalia statistics file (since XE7 Castalia is distributed with Delphi)
*.stat

Expand Down
16 changes: 9 additions & 7 deletions samples/delphi/Samples.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ program Samples;

uses
Horse,
Horse.Jhonson,
Horse.Compression, // It's necessary to use the unit
System.JSON;

begin
THorse
.Use(Compression()) // Must come before Jhonson middleware
.Use(Jhonson);
.Use(Compression()); // Must come before Jhonson middleware

// You can set compression threshold:
// THorse.Use(Compression(1024));
Expand All @@ -24,10 +22,14 @@ begin
LPong: TJSONArray;
begin
LPong := TJSONArray.Create;
for I := 0 to 1000 do
LPong.Add(TJSONObject.Create(TJSONPair.Create('ping', 'pong')));
Res.Send(LPong);
try
for I := 0 to 1000 do
LPong.Add(TJSONObject.Create(TJSONPair.Create('ping', 'pong')));
Res.Send(LPong.ToJSON);
finally
LPong.Free;
end;
end);

THorse.Listen(9000);
THorse.Listen;
end.
9 changes: 2 additions & 7 deletions samples/delphi/Samples.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<DCC_K>false</DCC_K>
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace)</DCC_Namespace>
<SanitizedProjectName>Samples</SanitizedProjectName>
<DCC_UnitSearchPath>..\..\src;modules\.dcp;modules\.dcu;modules;modules\horse\src;modules\jhonson\src;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win32)'!=''">
<DCC_UsePackage>DBXSqliteDriver;IndyIPCommon;RESTComponents;bindcompdbx;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;svnui;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;FireDAC;vcltouch;vcldb;bindcompfmx;svn;FireDACSqliteDriver;FireDACPgDriver;inetdb;soaprtl;DbxCommonDriver;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage)</DCC_UsePackage>
Expand Down Expand Up @@ -77,8 +76,8 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1_Win32)'!=''">
<DCC_RemoteDebug>false</DCC_RemoteDebug>
<DCC_UnitSearchPath>..\src;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
<VerInfo_Locale>1033</VerInfo_Locale>
<DCC_UnitSearchPath>..\..\modules\horse\src;..\..\src;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
<Manifest_File>(None)</Manifest_File>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''">
Expand Down Expand Up @@ -111,11 +110,7 @@
<Source>
<Source Name="MainSource">Samples.dpr</Source>
</Source>
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclDataSnapNativeServer260.bpl">Embarcadero DBExpress DataSnap Native Server Components</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k260.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclofficexp260.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
</Excluded_Packages>
<Excluded_Packages/>
</Delphi.Personality>
<Deployment Version="3"/>
<Platforms>
Expand Down
22 changes: 0 additions & 22 deletions samples/delphi/boss-lock.json

This file was deleted.

11 changes: 0 additions & 11 deletions samples/delphi/boss.json

This file was deleted.

12 changes: 8 additions & 4 deletions samples/lazarus/Console.lpi
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="Console"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
Expand All @@ -24,7 +24,6 @@
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<Units Count="1">
<Unit0>
Expand All @@ -41,9 +40,14 @@
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="..\..\src;modules\.dcp;modules\.dcu;modules;modules\horse\src;modules\jhonson\src"/>
<OtherUnitFiles Value="..\..\modules\horse\src;..\..\src"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf2Set"/>
</Debugging>
</Linking>
<Other>
<CustomOptions Value="-dUseCThreads"/>
</Other>
Expand Down
30 changes: 13 additions & 17 deletions samples/lazarus/Console.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
cthreads,
{$ENDIF}{$ENDIF}
Horse,
Horse.Jhonson,
Horse.Compression, // It's necessary to use the unit
fpjson,
SysUtils;
fpjson;

procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
var
Expand All @@ -19,29 +17,27 @@ procedure GetPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
LJson: TJSONObject;
begin
LPong := TJSONArray.Create;
for I := 0 to 1000 do
begin
LJson := TJSONObject.Create;
LJson.Add('ping', 'pong');
LPong.Add(LJson);
try
for I := 0 to 1000 do
begin
LJson := TJSONObject.Create;
LJson.Add('ping', 'pong');
LPong.Add(LJson);
end;
Res.Send(LPong.AsJSON);
finally
LPong.Free;
end;
Res.Send<TJSONArray>(LPong);
end;

procedure OnListen(Horse: THorse);
begin
Writeln(Format('Server is runing on %s:%d', [Horse.Host, Horse.Port]));
end;

begin
THorse
.Use(Compression()) // Must come before Jhonson middleware
.Use(Jhonson);
.Use(Compression()); // Must come before Jhonson middleware

// You can set compression threshold:
// THorse.Use(Compression(1024));

THorse.Get('/ping', GetPing);

THorse.Listen(9000, OnListen);
THorse.Listen;
end.
22 changes: 0 additions & 22 deletions samples/lazarus/boss-lock.json

This file was deleted.

11 changes: 0 additions & 11 deletions samples/lazarus/boss.json

This file was deleted.

34 changes: 18 additions & 16 deletions src/Horse.Compression.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
LAcceptEncoding: string;
LZStream: {$IF DEFINED(FPC)}TCompressionStream{$ELSE}TZCompressionStream{$ENDIF};
LResponseCompressionType: THorseCompressionType;
LContent: TObject;
LStringStream: TStringStream;
begin
Next;
LContent := Res.Content;
if (not Assigned(LContent)) or (not LContent.InheritsFrom({$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF})) then
Exit;
if Trim(Res.RawWebResponse.Content) = EmptyStr then
if (Res.Content = nil) or (not Res.Content.InheritsFrom({$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF})) then
Exit;
if Trim(Req.Headers[ACCEPT_ENCODING]) = EmptyStr then
Exit;
LAcceptEncoding := Req.Headers[ACCEPT_ENCODING].ToLower;
Expand All @@ -56,34 +56,36 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
{$ENDIF}
else
Exit;
Res.RawWebResponse.ContentStream := TStringStream.Create({$IF DEFINED(FPC)}TJsonData(LContent).AsJSON{$ELSE}TJSONValue(LContent).ToJSON{$ENDIF});
if Res.RawWebResponse.ContentStream.Size <= CompressionThreshold then
Exit;
LMemoryStream := TMemoryStream.Create;
LStringStream := nil;
try
Res.RawWebResponse.ContentStream.Position := 0;
if Trim(Res.RawWebResponse.Content) = EmptyStr then
LStringStream := TStringStream.Create({$IF DEFINED(FPC)}TJsonData(Res.Content).AsJSON{$ELSE}TJSONValue(Res.Content).ToJSON{$ENDIF})
else
LStringStream := TStringStream.Create(Res.RawWebResponse.Content);
if LStringStream.Size <= CompressionThreshold then
Exit;
LMemoryStream := TMemoryStream.Create;
{$IF DEFINED(FPC)}
LZStream := TCompressionStream.Create(Tcompressionlevel.clmax, LMemoryStream, LResponseCompressionType.WindowsBits = -15);
LZStream := TCompressionStream.Create(Tcompressionlevel.clmax, LMemoryStream, LResponseCompressionType.WindowsBits = -15);
{$ELSE}
LZStream := TZCompressionStream.Create(LMemoryStream, TZCompressionLevel.zcMax, LResponseCompressionType.WindowsBits);
{$ENDIF}
try
Res.RawWebResponse.ContentStream.Position := 0;
LZStream.CopyFrom(Res.RawWebResponse.ContentStream, 0);
LStringStream.Position := 0;
LZStream.CopyFrom(LStringStream, 0);
finally
LZStream.Free;
end;
LMemoryStream.Position := 0;
Res.RawWebResponse.ContentStream.Size := 0;
Res.RawWebResponse.ContentStream.CopyFrom(LMemoryStream, 0);
Res.RawWebResponse.ContentStream := LMemoryStream;
{$IF DEFINED(FPC)}
Res.RawWebResponse.ContentLength := LMemoryStream.Size;
Res.RawWebResponse.ContentLength := LMemoryStream.Size;
{$ELSE}
Res.RawWebResponse.Content := EmptyStr;
{$ENDIF}
Res.RawWebResponse.ContentEncoding := LResponseCompressionType.ToString;
finally
LMemoryStream.Free;
LStringStream.Free;
end;
end;

Expand Down

0 comments on commit 64dce70

Please sign in to comment.