delphimvcframework/sources/MVCFramework.Middleware.Trace.pas

142 lines
4.9 KiB
ObjectPascal
Raw Normal View History

2019-12-23 18:37:28 +01:00
// ***************************************************************************
//
// Delphi MVC Framework
//
2021-08-15 18:39:55 +02:00
// Copyright (c) 2010-2021 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
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);
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
procedure TMVCTraceMiddleware.OnAfterControllerAction(Context: TWebContext;
const AActionNAme: string; const Handled: Boolean);
var
lContentStream: TStringStream;
begin
2020-08-22 15:37:08 +02:00
Log.Debug('[AFTER ACTION][RESPONSE][STATUS] ' +
Format('%d: %s', [Context.Response.StatusCode, Context.Response.ReasonString]),
'trace');
Log.Debug('[AFTER ACTION][RESPONSE][CUSTOM HEADERS] ' + string.Join(' | ',
Context.Response.CustomHeaders.ToStringArray), 'trace');
Log.Debug('[AFTER ACTION][RESPONSE][CONTENT-TYPE] ' + Context.Response.ContentType, 'trace');
2019-12-23 18:37:28 +01:00
lContentStream := TStringStream.Create;
try
if Assigned(Context.Response.RawWebResponse.ContentStream) then
begin
2020-08-22 15:37:08 +02:00
lContentStream.CopyFrom(Context.Response.RawWebResponse.ContentStream,
Min(Context.Response.RawWebResponse.ContentStream.Size, fMaxBodySize));
2019-12-23 18:37:28 +01:00
Context.Response.RawWebResponse.ContentStream.Position := 0;
end
else
begin
2020-08-22 15:37:08 +02:00
lContentStream.WriteString(Context.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);
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');
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;
lContentType: string;
2019-12-23 18:37:28 +01:00
begin
lContentStream := TStringStream.Create;
try
Context.Request.RawWebRequest.ReadTotalContent;
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');
lContentType := Context.Request.Headers['content-type'].ToLower;
if lContentType.StartsWith(TMVCMediaType.APPLICATION_JSON, true) or
lContentType.StartsWith(TMVCMediaType.APPLICATION_XML, true) or
lContentType.StartsWith(TMVCMediaType.APPLICATION_FORM_URLENCODED, true) or
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>');
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.