2018-10-23 16:18:34 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2019-01-08 12:48:27 +01:00
|
|
|
// Copyright (c) 2010-2019 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
|
|
|
|
procedure OnAfterControllerAction(Context: TWebContext; const AActionNAme: string;
|
|
|
|
const Handled: Boolean);
|
|
|
|
procedure OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
|
|
|
procedure OnBeforeControllerAction(Context: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string; var Handled: 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;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnAfterControllerAction(Context: TWebContext;
|
2017-10-09 10:41:38 +02:00
|
|
|
const AActionNAme: string; const Handled: Boolean);
|
|
|
|
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
|
|
|
lZStream: TZCompressionStream;
|
|
|
|
lRespCompressionType: TMVCCompressionType;
|
|
|
|
lTmpItem: string;
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
2017-12-12 18:32:41 +01:00
|
|
|
lContentStream := Context.Response.RawWebResponse.ContentStream;
|
2019-04-01 23:03:55 +02:00
|
|
|
if (lContentStream = nil) or (lContentStream is TFileStream) or (lContentStream.Size <= fCompressionThreshold) then
|
2017-12-12 18:32:41 +01:00
|
|
|
Exit;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
lAcceptEncoding := Context.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
|
2018-10-23 16:18:34 +02:00
|
|
|
lRespCompressionType := TMVCCompressionType.ctGZIP;
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
|
|
|
|
lContentStream.Position := 0;
|
|
|
|
lMemStream := TMemoryStream.Create;
|
|
|
|
try
|
2019-07-19 17:22:33 +02:00
|
|
|
{TODO -oDanieleT -cGeneral : Use directly lContentStream?}
|
2018-10-23 16:18:34 +02:00
|
|
|
lZStream := TZCompressionStream.Create(lMemStream, TZCompressionLevel.zcMax, MVC_COMPRESSION_ZLIB_WINDOW_BITS[lRespCompressionType]);
|
|
|
|
try
|
|
|
|
lZStream.CopyFrom(lContentStream, 0);
|
|
|
|
finally
|
|
|
|
lZStream.Free;
|
|
|
|
end;
|
2017-10-09 10:41:38 +02:00
|
|
|
lMemStream.Position := 0;
|
|
|
|
|
2018-11-21 22:11:58 +01:00
|
|
|
Context.Response.Content := '';
|
|
|
|
lContentStream.Size := 0;
|
|
|
|
lContentStream.CopyFrom(lMemStream, 0);
|
2019-08-05 12:55:44 +02:00
|
|
|
{$IF Defined(SeattleOrBetter)}
|
2018-11-21 22:11:58 +01:00
|
|
|
Context.Response.RawWebResponse.ContentEncoding := MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType];
|
2019-08-05 12:55:44 +02:00
|
|
|
{$ELSE}
|
|
|
|
Context.Response.RawWebResponse.ContentEncoding := AnsiString(MVC_COMPRESSION_TYPE_AS_STRING[lRespCompressionType]);
|
|
|
|
{$ENDIF}
|
2018-11-21 22:11:58 +01:00
|
|
|
finally
|
|
|
|
lMemStream.Free;
|
|
|
|
end;
|
2017-10-09 10:41:38 +02:00
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnBeforeControllerAction(Context: TWebContext;
|
2017-10-09 10:41:38 +02:00
|
|
|
const AControllerQualifiedClassName, AActionNAme: string; var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
procedure TMVCCompressionMiddleware.OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
2017-10-09 10:41:38 +02:00
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|