diff --git a/README.md b/README.md index 2e2dff56..14d04316 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,8 @@ end; - Fixed! [issue232](https://github.com/danieleteti/delphimvcframework/issues/232) (Thanks to [João Antônio Duarte](https://github.com/joaoduarte19)) +- Fixed! [issue312](https://github.com/danieleteti/delphimvcframework/issues/312) + - New Installation procedure! - Open the project group (select the correct one from the following table) diff --git a/delphimvcframework_handbook_cover.png b/delphimvcframework_handbook_cover.png new file mode 100644 index 00000000..e4d9652b Binary files /dev/null and b/delphimvcframework_handbook_cover.png differ diff --git a/sources/MVCFramework.Middleware.Trace.pas b/sources/MVCFramework.Middleware.Trace.pas new file mode 100644 index 00000000..70a35a30 --- /dev/null +++ b/sources/MVCFramework.Middleware.Trace.pas @@ -0,0 +1,102 @@ +// *************************************************************************** +// +// Delphi MVC Framework +// +// Copyright (c) 2010-2019 Daniele Teti and the DMVCFramework Team +// +// 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, + MVCFramework.Logger; + +type + TMVCTraceMiddleware = class(TInterfacedObject, IMVCMiddleware) + 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); + end; + +implementation + +uses + System.SysUtils, + System.ZLib, + System.Classes, + MVCFramework.Commons, Web.HTTPApp; + +{ TMVCSalutationMiddleware } + +procedure TMVCTraceMiddleware.OnAfterControllerAction(Context: TWebContext; + const AActionNAme: string; const Handled: Boolean); +var + lContentStream: TStringStream; +begin + lContentStream := TStringStream.Create; + try + Log.Debug('[RESPONSE][HEADERS] ' + string.Join(' | ', Context.Response.CustomHeaders.ToStringArray), 'trace'); + if Assigned(Context.Response.RawWebResponse.ContentStream) then + begin + lContentStream.CopyFrom(Context.Response.RawWebResponse.ContentStream, 0); + Context.Response.RawWebResponse.ContentStream.Position := 0; + end + else + begin + lContentStream.WriteString(Context.Response.RawWebResponse.Content); + end; + Log.Debug('[RESPONSE][BODY] ' + lContentStream.DataString, 'trace'); + finally + lContentStream.Free; + end; +end; + +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; +begin + lContentStream := TStringStream.Create; + try + Context.Request.RawWebRequest.ReadTotalContent; + Log.Debug('[REQUEST][URL] ' + Context.Request.RawWebRequest.PathInfo, 'trace'); + Log.Debug('[REQUEST][QUERYSTRING] ' + Context.Request.RawWebRequest.QueryFields.DelimitedText, 'trace'); + lContentStream.WriteString(EncodingGetString(Context.Request.Headers['content-type'], + Context.Request.RawWebRequest.RawContent)); + Log.Debug('[REQUEST][BODY] ' + lContentStream.DataString, 'trace'); + finally + lContentStream.Free; + end; +end; + +end.