2019-12-23 18:37:28 +01:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2022-01-04 15:44:47 +01:00
|
|
|
// Copyright (c) 2010-2022 Daniele Teti and the DMVCFramework Team
|
2019-12-23 18:37:28 +01: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.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
|
|
unit MVCFramework.Middleware.Trace;
|
|
|
|
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework,
|
2020-08-22 15:37:08 +02:00
|
|
|
MVCFramework.Logger,
|
|
|
|
MVCFramework.Commons;
|
2019-12-23 18:37:28 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
TMVCTraceMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
2020-08-22 15:37:08 +02:00
|
|
|
private
|
|
|
|
fMaxBodySize: Int64;
|
2019-12-23 18:37:28 +01:00
|
|
|
protected
|
2022-08-13 00:21:00 +02:00
|
|
|
procedure OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string; const AActionName: string;
|
|
|
|
const AHandled: Boolean);
|
2019-12-23 18:37:28 +01:00
|
|
|
procedure OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
|
|
|
procedure OnBeforeControllerAction(Context: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string; const AActionNAme: string; var Handled: Boolean);
|
2020-08-22 15:37:08 +02:00
|
|
|
procedure OnAfterRouting(Context: TWebContext; const AHandled: Boolean);
|
|
|
|
public
|
|
|
|
constructor Create(const MaxBodySizeInTrace: UInt64 = 1024);
|
2019-12-23 18:37:28 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.ZLib,
|
|
|
|
System.Classes,
|
2020-08-22 15:37:08 +02:00
|
|
|
MVCFramework.Rtti.Utils,
|
|
|
|
Web.HTTPApp, System.Math;
|
2019-12-23 18:37:28 +01:00
|
|
|
|
2020-08-22 15:37:08 +02:00
|
|
|
constructor TMVCTraceMiddleware.Create(const MaxBodySizeInTrace: UInt64 = 1024);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
fMaxBodySize := MaxBodySizeInTrace;
|
|
|
|
end;
|
2019-12-23 18:37:28 +01:00
|
|
|
|
2022-08-13 00:21:00 +02:00
|
|
|
procedure TMVCTraceMiddleware.OnAfterControllerAction(AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string; const AActionName: string;
|
|
|
|
const AHandled: Boolean);
|
2019-12-23 18:37:28 +01:00
|
|
|
var
|
|
|
|
lContentStream: TStringStream;
|
|
|
|
begin
|
2020-08-22 15:37:08 +02:00
|
|
|
Log.Debug('[AFTER ACTION][RESPONSE][STATUS] ' +
|
2022-08-13 00:21:00 +02:00
|
|
|
Format('%d: %s', [AContext.Response.StatusCode, AContext.Response.ReasonString]),
|
2020-08-22 15:37:08 +02:00
|
|
|
'trace');
|
|
|
|
Log.Debug('[AFTER ACTION][RESPONSE][CUSTOM HEADERS] ' + string.Join(' | ',
|
2022-08-13 00:21:00 +02:00
|
|
|
AContext.Response.CustomHeaders.ToStringArray), 'trace');
|
|
|
|
Log.Debug('[AFTER ACTION][RESPONSE][CONTENT-TYPE] ' + AContext.Response.ContentType, 'trace');
|
2020-08-22 15:37:08 +02:00
|
|
|
|
2019-12-23 18:37:28 +01:00
|
|
|
lContentStream := TStringStream.Create;
|
|
|
|
try
|
2022-08-13 00:21:00 +02:00
|
|
|
if Assigned(AContext.Response.RawWebResponse.ContentStream) then
|
2019-12-23 18:37:28 +01:00
|
|
|
begin
|
2022-08-13 00:21:00 +02:00
|
|
|
lContentStream.CopyFrom(AContext.Response.RawWebResponse.ContentStream,
|
|
|
|
Min(AContext.Response.RawWebResponse.ContentStream.Size, fMaxBodySize));
|
|
|
|
AContext.Response.RawWebResponse.ContentStream.Position := 0;
|
2019-12-23 18:37:28 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
2022-08-13 00:21:00 +02:00
|
|
|
lContentStream.WriteString(AContext.Response.RawWebResponse.Content.Substring(0, fMaxBodySize));
|
2019-12-23 18:37:28 +01:00
|
|
|
end;
|
2020-08-22 15:37:08 +02:00
|
|
|
Log.Debug('[AFTER ACTION][RESPONSE][BODY] ' + lContentStream.DataString, 'trace');
|
2019-12-23 18:37:28 +01:00
|
|
|
finally
|
|
|
|
lContentStream.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2020-08-22 15:37:08 +02:00
|
|
|
procedure TMVCTraceMiddleware.OnAfterRouting(Context: TWebContext; const AHandled: Boolean);
|
2020-04-29 01:57:29 +02:00
|
|
|
begin
|
|
|
|
Log.Debug('[AFTER ROUTING][REQUESTED URL: %s][HANDLED: %s]',
|
2020-08-22 15:37:08 +02:00
|
|
|
[Context.Request.PathInfo, AHandled.ToString(TUseBoolStrs.True)], 'trace');
|
2020-04-29 01:57:29 +02:00
|
|
|
end;
|
|
|
|
|
2019-12-23 18:37:28 +01:00
|
|
|
procedure TMVCTraceMiddleware.OnBeforeControllerAction(Context: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionNAme: string; var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
Log.Debug('[BEFORE ACTION][CONTROLLER: %s][ACTION: %s]',
|
|
|
|
[AControllerQualifiedClassName, AActionNAme], 'trace');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCTraceMiddleware.OnBeforeRouting(Context: TWebContext; var Handled: Boolean);
|
|
|
|
var
|
|
|
|
lContentStream: TStringStream;
|
2020-03-05 18:34:00 +01:00
|
|
|
lContentType: string;
|
2019-12-23 18:37:28 +01:00
|
|
|
begin
|
|
|
|
lContentStream := TStringStream.Create;
|
|
|
|
try
|
|
|
|
Context.Request.RawWebRequest.ReadTotalContent;
|
2020-09-25 00:32:55 +02:00
|
|
|
Log.Debug('[BEFORE ROUTING][REQUEST][IP] ' + Context.Request.ClientIp, 'trace');
|
2020-08-22 15:37:08 +02:00
|
|
|
Log.Debug('[BEFORE ROUTING][REQUEST][URL] ' + Context.Request.RawWebRequest.PathInfo, 'trace');
|
|
|
|
Log.Debug('[BEFORE ROUTING][REQUEST][QUERYSTRING] ' + Context.Request.RawWebRequest.QueryFields.
|
|
|
|
DelimitedText, 'trace');
|
|
|
|
|
2020-03-05 18:34:00 +01:00
|
|
|
lContentType := Context.Request.Headers['content-type'].ToLower;
|
2020-07-02 23:19:36 +02:00
|
|
|
if lContentType.StartsWith(TMVCMediaType.APPLICATION_JSON, true) or
|
|
|
|
lContentType.StartsWith(TMVCMediaType.APPLICATION_XML, true) or
|
|
|
|
lContentType.StartsWith(TMVCMediaType.APPLICATION_FORM_URLENCODED, true) or
|
2020-03-05 18:34:00 +01:00
|
|
|
lContentType.StartsWith('text/') then
|
|
|
|
begin
|
|
|
|
lContentStream.WriteString(EncodingGetString(lContentType,
|
2020-08-22 15:37:08 +02:00
|
|
|
Context.Request.RawWebRequest.RawContent).Substring(0, fMaxBodySize));
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
lContentStream.WriteString('<hidden non text content>');
|
2020-03-05 18:34:00 +01:00
|
|
|
end;
|
2020-08-22 15:37:08 +02:00
|
|
|
Log.Debug('[BEFORE ROUTING][REQUEST][BODY] ' + lContentStream.DataString, 'trace');
|
2019-12-23 18:37:28 +01:00
|
|
|
finally
|
|
|
|
lContentStream.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|