2018-10-23 16:18:34 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2021-08-15 18:39:55 +02:00
|
|
|
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
|
2018-10-23 16:18:34 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2017-10-09 10:41:38 +02:00
|
|
|
unit MVCFramework.Middleware.Compression;
|
|
|
|
|
2019-08-05 12:55:44 +02:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2017-10-09 10:41:38 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2018-10-23 16:18:34 +02:00
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Logger;
|
2017-10-09 10:41:38 +02:00
|
|
|
|
|
|
|
type
|
2018-10-23 16:18:34 +02:00
|
|
|
TMVCCompressionMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
2017-12-12 18:32:41 +01:00
|
|
|
private
|
|
|
|
fCompressionThreshold: Integer;
|
2017-10-09 10:41:38 +02:00
|
|
|
protected
|
2021-09-17 10:46:34 +02:00
|
|
|
procedure OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
const AActionNAme: string; const AHandled: Boolean);
|
2020-04-30 22:53:53 +02:00
|
|
|
procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
|
|
|
procedure OnBeforeControllerAction(AContext: TWebContext;
|
2021-09-17 10:46:34 +02:00
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string;
|
|
|
|
var AHandled: Boolean);
|
2020-04-29 01:57:29 +02:00
|
|
|
procedure OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
|
2017-12-12 18:32:41 +01:00
|
|
|
public
|
|
|
|
constructor Create(aCompressionThreshold: Integer = 1024); virtual;
|
2017-10-09 10:41:38 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-10-23 16:18:34 +02:00
|
|
|
System.SysUtils,
|
|
|
|
System.ZLib,
|
|
|
|
System.Classes,
|
|
|
|
MVCFramework.Commons;
|
2017-10-09 10:41:38 +02:00
|
|
|
|
|
|
|
{ TMVCSalutationMiddleware }
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
constructor TMVCCompressionMiddleware.Create(aCompressionThreshold: Integer);
|
2017-12-12 18:32:41 +01:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fCompressionThreshold := aCompressionThreshold;
|
|
|
|
end;
|
|
|
|
|
2021-09-17 10:46:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnAfterControllerAction
|
|
|
|
(AContext: TWebContext; const AActionNAme: string; const AHandled: Boolean);
|
2020-04-30 22:53:53 +02:00
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2021-09-17 10:46:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnAfterRouting(AContext: TWebContext;
|
|
|
|
const AHandled: Boolean);
|
2017-10-09 10:41:38 +02:00
|
|
|
var
|
|
|
|
lMemStream: TMemoryStream;
|
|
|
|
lContentStream: TStream;
|
2017-10-16 22:57:27 +02:00
|
|
|
lAcceptEncoding: string;
|
|
|
|
lEncodings: TArray<string>;
|
|
|
|
lItem: string;
|
2018-10-23 16:18:34 +02:00
|
|
|
lRespCompressionType: TMVCCompressionType;
|
|
|
|
lTmpItem: string;
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$IF not Defined(ALEXANDRIAORBETTER)}
|
2021-09-17 10:46:34 +02:00
|
|
|
lZStream: TZCompressionStream;
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$ENDIF}
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
2020-06-29 18:07:25 +02:00
|
|
|
if IsLibrary then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
2020-04-30 22:53:53 +02:00
|
|
|
lContentStream := AContext.Response.RawWebResponse.ContentStream;
|
2021-09-17 10:46:34 +02:00
|
|
|
if (lContentStream = nil) or (lContentStream.Size <= fCompressionThreshold)
|
|
|
|
then
|
2017-12-12 18:32:41 +01:00
|
|
|
Exit;
|
|
|
|
|
2020-04-30 22:53:53 +02:00
|
|
|
lAcceptEncoding := AContext.Request.Headers['Accept-Encoding'];
|
2017-10-16 22:57:27 +02:00
|
|
|
if lAcceptEncoding.IsEmpty then
|
2018-10-23 16:18:34 +02:00
|
|
|
begin
|
2017-10-16 22:57:27 +02:00
|
|
|
Exit;
|
2018-10-23 16:18:34 +02:00
|
|
|
end;
|
|
|
|
lAcceptEncoding := lAcceptEncoding.Trim.ToLower;
|
2017-10-16 22:57:27 +02:00
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
lRespCompressionType := TMVCCompressionType.ctNone;
|
2017-10-16 22:57:27 +02:00
|
|
|
lEncodings := lAcceptEncoding.Split([',']);
|
|
|
|
for lItem in lEncodings do
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
lTmpItem := lItem.Trim;
|
|
|
|
if lTmpItem = 'gzip' then
|
2017-10-16 22:57:27 +02:00
|
|
|
begin
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$IF Defined(ALEXANDRIAORBETTER)}
|
2021-09-17 10:46:34 +02:00
|
|
|
{
|
|
|
|
There is a bug in 11 Alexandria with TZCompressionStream, so the gzip compression
|
|
|
|
is not available until the fix.
|
|
|
|
The issue has been created on https://quality.embarcadero.com/browse/RSP-35516
|
|
|
|
}
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$ELSE}
|
2018-10-23 16:18:34 +02:00
|
|
|
lRespCompressionType := TMVCCompressionType.ctGZIP;
|
|
|
|
break;
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$ENDIF}
|
2018-10-23 16:18:34 +02:00
|
|
|
end
|
|
|
|
else if lTmpItem = 'deflate' then
|
|
|
|
begin
|
|
|
|
lRespCompressionType := TMVCCompressionType.ctDeflate;
|
|
|
|
break;
|
2017-10-16 22:57:27 +02:00
|
|
|
end;
|
|
|
|
end;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
|
|
|
if lRespCompressionType = TMVCCompressionType.ctNone then
|
2017-10-09 10:41:38 +02:00
|
|
|
Exit;
|
|
|
|
|
2020-04-30 22:53:53 +02:00
|
|
|
{ When it is a TFileStream copy it to a TMemoryStream, as TFileStream is read only }
|
2021-09-17 10:46:34 +02:00
|
|
|
// if lContentStream is TFileStream then
|
|
|
|
// begin
|
2017-10-09 10:41:38 +02:00
|
|
|
lMemStream := TMemoryStream.Create;
|
|
|
|
try
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$IF Defined(ALEXANDRIAORBETTER)}
|
|
|
|
//ZCompressStream(lContentStream, lMemStream);
|
2021-09-17 10:46:34 +02:00
|
|
|
// use it only for deflate, ZCompressStream (Delphi 11.0) cannot
|
|
|
|
// create gzip compliant streams
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$ELSE}
|
2021-09-17 10:46:34 +02:00
|
|
|
lZStream := TZCompressionStream.Create(lMemStream,
|
2021-10-20 11:12:57 +02:00
|
|
|
TZCompressionLevel.zcMax, MVC_COMPRESSION_ZLIB_WINDOW_BITS
|
2021-09-17 10:46:34 +02:00
|
|
|
[lRespCompressionType]);
|
2018-10-23 16:18:34 +02:00
|
|
|
try
|
2021-09-17 10:46:34 +02:00
|
|
|
lContentStream.Position := 0;
|
2021-10-07 10:10:25 +02:00
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
// There is a bug in 11 Alexandria with TZCompressionStream, this bug
|
|
|
|
// raises an exception here and the debugger catches it, but it its "normal" behavior.
|
|
|
|
// The gzip stream is correctly created. Please, vote for its resolution.
|
|
|
|
// https://quality.embarcadero.com/browse/RSP-35516
|
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
|
|
|
|
lZStream.CopyFrom(lContentStream, 0);
|
2018-10-23 16:18:34 +02:00
|
|
|
finally
|
|
|
|
lZStream.Free;
|
|
|
|
end;
|
2021-10-07 10:10:25 +02:00
|
|
|
{.$ENDIF}
|
2021-09-17 10:46:34 +02:00
|
|
|
except
|
2018-11-21 22:11:58 +01:00
|
|
|
lMemStream.Free;
|
2021-09-17 10:46:34 +02:00
|
|
|
raise;
|
2020-04-30 22:53:53 +02:00
|
|
|
end;
|
2021-09-17 10:46:34 +02:00
|
|
|
lMemStream.Position := 0;
|
|
|
|
AContext.Response.RawWebResponse.ContentStream := lMemStream;
|
|
|
|
{$IF Defined(SeattleOrBetter)}
|
|
|
|
AContext.Response.RawWebResponse.ContentEncoding :=
|
|
|
|
MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType];
|
|
|
|
{$ELSE}
|
|
|
|
AContext.Response.RawWebResponse.ContentEncoding :=
|
|
|
|
AnsiString(MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType]);
|
|
|
|
{$ENDIF}
|
|
|
|
// end;
|
|
|
|
//
|
|
|
|
// lContentStream.Position := 0;
|
|
|
|
// lMemStream := TMemoryStream.Create;
|
|
|
|
// try
|
|
|
|
// ZCompressStream(lContentStream, lMemStream, TZCompressionLevel.zcDefault);
|
|
|
|
// except
|
|
|
|
// lMemStream.Free;
|
|
|
|
// raise;
|
|
|
|
// end;
|
|
|
|
//
|
|
|
|
// { TODO -oDanieleT -cGeneral : Use directly lContentStreamHelper? }
|
|
|
|
// // lZStream := TZCompressionStream.Create(lMemStream, TZCompressionLevel.zcMax,
|
|
|
|
// // MVC_COMPRESSION_ZLIB_WINDOW_BITS[lRespCompressionType]);
|
|
|
|
// // try
|
|
|
|
// // lZStream.CopyFrom(lContentStreamHelper, 0);
|
|
|
|
// // finally
|
|
|
|
// // lZStream.Free;
|
|
|
|
// // end;
|
|
|
|
// lMemStream.Position := 0;
|
|
|
|
//
|
|
|
|
// // AContext.Response.Content := '';
|
|
|
|
// // lContentStreamHelper.Size := 0;
|
|
|
|
// // lContentStreamHelper.CopyFrom(lMemStream, 0);
|
|
|
|
// AContext.Response.RawWebResponse.ContentStream := lMemStream;
|
|
|
|
// {$IF Defined(SeattleOrBetter)}
|
|
|
|
// AContext.Response.RawWebResponse.ContentEncoding :=
|
|
|
|
// MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType];
|
|
|
|
// {$ELSE}
|
|
|
|
// AContext.Response.RawWebResponse.ContentEncoding :=
|
|
|
|
// AnsiString(MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType]);
|
|
|
|
// {$ENDIF}
|
|
|
|
// // finally
|
|
|
|
// // lMemStream.Free;
|
|
|
|
// // end;
|
2020-04-29 01:57:29 +02:00
|
|
|
end;
|
|
|
|
|
2021-09-17 10:46:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnBeforeControllerAction
|
|
|
|
(AContext: TWebContext; const AControllerQualifiedClassName,
|
|
|
|
AActionNAme: string; var AHandled: Boolean);
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2021-09-17 10:46:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnBeforeRouting(AContext: TWebContext;
|
|
|
|
var AHandled: Boolean);
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|