From 85ec94a1d6a2a677b8ac32380d0b175a79e03221 Mon Sep 17 00:00:00 2001 From: Rodrigo Bernardi Date: Fri, 14 Feb 2020 16:17:37 -0300 Subject: [PATCH] horse update --- Demo/Demo.dpr | 1 + Demo/Demo.dproj | 144 ++++++++++++++++++++++++++++++-- Demo/boss-lock.json | 4 +- Loggastic.dproj | 2 +- boss-lock.json | 4 +- src/Horse.Loggastic.pas | 8 +- src/Providers/Providers.Log.pas | 80 ++++++++++++------ 7 files changed, 205 insertions(+), 38 deletions(-) diff --git a/Demo/Demo.dpr b/Demo/Demo.dpr index 343194d..8f3a643 100644 --- a/Demo/Demo.dpr +++ b/Demo/Demo.dpr @@ -15,6 +15,7 @@ var begin App := THorse.Create; try + //See output on https://ptsv2.com/t/39fiw-1573504844 App.Use(Loggastic('https://ptsv2.com/t/39fiw-1573504844/post')); App.Get('/', procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc) diff --git a/Demo/Demo.dproj b/Demo/Demo.dproj index 3d66c86..0f8ebbe 100644 --- a/Demo/Demo.dproj +++ b/Demo/Demo.dproj @@ -1,7 +1,7 @@  {40B1F2E9-8D1B-4E15-B0B4-DA2D797F8465} - 18.7 + 18.8 None Demo.dpr True @@ -13,8 +13,8 @@ true - - true + + true Base true @@ -61,8 +61,11 @@ Demo modules\.dcp;modules\.dcu;modules;modules\horse\src;$(DCC_UnitSearchPath) - - DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;tethering;DataSnapFireDAC;bindcompfmx;FmxTeeUI;fmx;FireDACIBDriver;FireDACDBXDriver;dbexpress;IndyCore;dsnap;emsclient;DataSnapCommon;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;IndyIPCommon;bindcompdbx;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;FMXTee;soaprtl;DbxCommonDriver;xmlrtl;soapmidas;DataSnapNativeClient;FireDACDSDriver;rtl;DbxClientDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;$(DCC_UsePackage) + + true + Base + true + DBXSqliteDriver;RESTComponents;DBXInterBaseDriver;emsclientfiredac;tethering;DataSnapFireDAC;bindcompfmx;FmxTeeUI;fmx;FireDACIBDriver;FireDACDBXDriver;dbexpress;IndyCore;dsnap;emsclient;DataSnapCommon;FireDACCommon;RESTBackendComponents;soapserver;bindengine;CloudService;FireDACCommonDriver;DataSnapClient;inet;IndyIPCommon;bindcompdbx;IndyIPServer;IndySystem;fmxFireDAC;FireDAC;FireDACSqliteDriver;FMXTee;soaprtl;DbxCommonDriver;xmlrtl;soapmidas;DataSnapNativeClient;FireDACDSDriver;rtl;DbxClientDriver;CustomIPTransport;bindcomp;IndyIPClient;dbxcds;dsnapxml;DataSnapProviderClient;dbrtl;IndyProtocols;$(DCC_UsePackage);$(DCC_UsePackage) $(BDS)\bin\Artwork\Android\FM_LauncherIcon_36x36.png $(BDS)\bin\Artwork\Android\FM_LauncherIcon_48x48.png $(BDS)\bin\Artwork\Android\FM_LauncherIcon_72x72.png @@ -181,12 +184,20 @@ classes 1 + + classes + 1 + res\xml 1 + + res\xml + 1 + @@ -199,138 +210,242 @@ library\lib\armeabi 1 + + library\lib\armeabi + 1 + + + + + library\lib\armeabi-v7a + 1 + library\lib\mips 1 + + library\lib\mips + 1 + library\lib\armeabi-v7a 1 + + library\lib\arm64-v8a + 1 + + + + + library\lib\armeabi-v7a + 1 + res\drawable 1 + + res\drawable + 1 + res\values 1 + + res\values + 1 + res\values-v21 1 + + res\values-v21 + 1 + res\values 1 + + res\values + 1 + res\drawable 1 + + res\drawable + 1 + res\drawable-xxhdpi 1 + + res\drawable-xxhdpi + 1 + res\drawable-ldpi 1 + + res\drawable-ldpi + 1 + res\drawable-mdpi 1 + + res\drawable-mdpi + 1 + res\drawable-hdpi 1 + + res\drawable-hdpi + 1 + res\drawable-xhdpi 1 + + res\drawable-xhdpi + 1 + res\drawable-mdpi 1 + + res\drawable-mdpi + 1 + res\drawable-hdpi 1 + + res\drawable-hdpi + 1 + res\drawable-xhdpi 1 + + res\drawable-xhdpi + 1 + res\drawable-xxhdpi 1 + + res\drawable-xxhdpi + 1 + res\drawable-xxxhdpi 1 + + res\drawable-xxxhdpi + 1 + res\drawable-small 1 + + res\drawable-small + 1 + res\drawable-normal 1 + + res\drawable-normal + 1 + res\drawable-large 1 + + res\drawable-large + 1 + res\drawable-xlarge 1 + + res\drawable-xlarge + 1 + res\values 1 + + res\values + 1 + @@ -419,6 +534,9 @@ 0 + + 0 + 0 @@ -708,6 +826,9 @@ 1 + + 1 + @@ -800,6 +921,10 @@ library\lib\armeabi-v7a 1 + + library\lib\arm64-v8a + 1 + 1 @@ -824,6 +949,12 @@ 0 + + + library\lib\armeabi-v7a + 1 + + 1 @@ -861,9 +992,10 @@ + - False + False False True False diff --git a/Demo/boss-lock.json b/Demo/boss-lock.json index ae9f5b5..7a627e6 100644 --- a/Demo/boss-lock.json +++ b/Demo/boss-lock.json @@ -4,8 +4,8 @@ "installedModules": { "github.com/hashload/horse": { "name": "horse", - "version": "1.6.8", - "hash": "7a0b2394ac49dbd1a62f6d1021cac5b9", + "version": "1.7.0", + "hash": "7c3b15d6289c0ab49f483626c61da28b", "artifacts": {}, "failed": false, "changed": false diff --git a/Loggastic.dproj b/Loggastic.dproj index 4b26996..f5a973a 100644 --- a/Loggastic.dproj +++ b/Loggastic.dproj @@ -52,7 +52,7 @@ System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) All Loggastic - modules\.dcp;modules\.dcu;modules;modules\horse\src;$(DCC_UnitSearchPath) + $(DCC_UnitSearchPath);modules\.dcp;modules\.dcu;modules;modules\horse\src true diff --git a/boss-lock.json b/boss-lock.json index fbd9039..85a5325 100644 --- a/boss-lock.json +++ b/boss-lock.json @@ -4,8 +4,8 @@ "installedModules": { "github.com/hashload/horse": { "name": "horse", - "version": "1.6.8", - "hash": "7a0b2394ac49dbd1a62f6d1021cac5b9", + "version": "1.7.0", + "hash": "7c3b15d6289c0ab49f483626c61da28b", "artifacts": {}, "failed": false, "changed": false diff --git a/src/Horse.Loggastic.pas b/src/Horse.Loggastic.pas index 16045d7..853edd2 100644 --- a/src/Horse.Loggastic.pas +++ b/src/Horse.Loggastic.pas @@ -17,16 +17,18 @@ function Loggastic(AElasticSearchUrl: string): THorseCallback; TProviderLog.ElasticSearchUrl := AElasticSearchUrl; Result := procedure(AReq: THorseRequest; ARes: THorseResponse; ANext: TProc) + var + LStartDate: TDateTime; begin + LStartDate := Now; try - AReq.Headers.AddOrSetValue(DATE_HEADER, DateTimeToStr(Now)); ANext(); - Log(AReq, ARes); + Log(AReq, ARes, LStartDate); except on E: Exception do begin if not E.InheritsFrom(EHorseCallbackInterrupted) then - Log(AReq, ARes, E.Message); + Log(AReq, ARes, E.Message, LStartDate); raise; end; end; diff --git a/src/Providers/Providers.Log.pas b/src/Providers/Providers.Log.pas index 78170d1..ed725aa 100644 --- a/src/Providers/Providers.Log.pas +++ b/src/Providers/Providers.Log.pas @@ -51,7 +51,7 @@ TProviderLogRequest = class function ToJSON: TJSONObject; - constructor Create(const ARequest: THorseRequest); + constructor Create(const ARequest: THorseRequest; AStartDate: TDateTime); end; TProviderLogGeneral = class @@ -89,24 +89,33 @@ TProviderLog = class procedure SendLog; function ToJSON: TJSONObject; - constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse); overload; - constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string); overload; + constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); overload; + constructor Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); overload; destructor Destroy; override; end; -procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse); overload; +procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); overload; -procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string); overload; +procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); overload; implementation -uses System.NetEncoding, System.Classes; +uses System.NetEncoding, System.Classes, IdHTTPWebBrokerBridge, + IdHTTPHeaderInfo; + +type + TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest + public + function GetRequestInfo: TIdEntityHeaderInfo; + function GetHeadersJSON: TJSONObject; + end; + -procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse); +procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); var LLog: TProviderLog; begin - LLog := TProviderLog.Create(ARequest, AResponse); + LLog := TProviderLog.Create(ARequest, AResponse, AStartDate); try LLog.SendLog; finally @@ -114,11 +123,11 @@ procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse); end; end; -procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string); overload; +procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); overload; var LLog: TProviderLog; begin - LLog := TProviderLog.Create(ARequest, AResponse, AError); + LLog := TProviderLog.Create(ARequest, AResponse, AError, AStartDate); try LLog.SendLog; finally @@ -128,17 +137,17 @@ procedure Log(const ARequest: THorseRequest; const AResponse: THorseResponse; AE { TProviderLog } -constructor TProviderLog.Create(const ARequest: THorseRequest; const AResponse: THorseResponse); +constructor TProviderLog.Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AStartDate: TDateTime); begin FGeneral := TProviderLogGeneral.Create(ARequest); - FRequest := TProviderLogRequest.Create(ARequest); + FRequest := TProviderLogRequest.Create(ARequest, AStartDate); FResponse := TProviderLogResponse.Create(AResponse); end; -constructor TProviderLog.Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string); +constructor TProviderLog.Create(const ARequest: THorseRequest; const AResponse: THorseResponse; AError: string; AStartDate: TDateTime); begin FGeneral := TProviderLogGeneral.Create(ARequest); - FRequest := TProviderLogRequest.Create(ARequest); + FRequest := TProviderLogRequest.Create(ARequest, AStartDate); FResponse := TProviderLogResponse.Create(AResponse.Status, AError, THorseHackResponse(AResponse) .GetWebResponse.Content); end; @@ -211,7 +220,9 @@ procedure TProviderLogGeneral.SetSession(const ARequest: THorseRequest); const JWT_PAYLOAD = 1; begin - if ARequest.Headers.TryGetValue('X-Authorization', LToken) then + LToken := ARequest.Headers['X-Authorization']; + + if not LToken.IsEmpty then begin LToken := LToken.Replace('bearer ', '', [rfIgnoreCase]); LPayloadEncoded := LToken.Split(['.'])[JWT_PAYLOAD]; @@ -246,21 +257,22 @@ constructor TProviderLogGeneral.Create(const ARequest: THorseRequest); { TProviderLogRequest } -constructor TProviderLogRequest.Create(const ARequest: THorseRequest); +constructor TProviderLogRequest.Create(const ARequest: THorseRequest; AStartDate: TDateTime); var - LCount: integer; - LBody: TJSONObject; + LHackedRequest: THorseHackRequest; begin - FDate := StrToDateTime(ARequest.Headers[DATE_HEADER]); + FDate := AStartDate; FMethod := THorseHackRequest(ARequest).GetWebRequest.Method; FContentType := THorseHackRequest(ARequest).GetWebRequest.ContentType; - + LHackedRequest := THorseHackRequest(ARequest); FParams := TJSONObject.Create; FParams.AddPair('querys', DictionaryToJsonObject(ARequest.Query)); FParams.AddPair('params', DictionaryToJsonObject(ARequest.Params)); - FParams.AddPair('headers', DictionaryToJsonObject(ARequest.Headers)); - + if LHackedRequest.GetWebRequest.inheritsfrom(TIdHTTPAppRequest) then + begin + FParams.AddPair('headers', TIdHTTPAppRequest(LHackedRequest.GetWebRequest).GetHeadersJSON); + end; if FContentType = 'application/json' then FBody := THorseHackRequest(ARequest).Body; @@ -294,8 +306,6 @@ function TProviderLogRequest.ToJSON: TJSONObject; constructor TProviderLogResponse.Create(const AResponse: THorseResponse); var LHostResponse: THorseHackResponse; - LBody: string; - LContentStream: TMemoryStream; begin LHostResponse := THorseHackResponse(AResponse); @@ -341,4 +351,26 @@ function TProviderLogResponse.ToJSON: TJSONObject; Result.AddPair('body', FBody); end; +{ TIdHTTPAppRequestHelper } + +function TIdHTTPAppRequestHelper.GetHeadersJSON: TJSONObject; +var + LKey, LValue, LHeader: string; + LPosSeparator: Integer; +begin + Result := TJSONObject.Create; + for LHeader in GetRequestInfo.RawHeaders do + begin + LPosSeparator := Pos(':', LHeader); + LKey := Copy(LHeader, 0, LPosSeparator - 1); + LValue := Copy(LHeader, LPosSeparator + 1, LHeader.Length - LPosSeparator); + Result.AddPair(LowerCase(LKey), Trim(LValue)); + end; +end; + +function TIdHTTPAppRequestHelper.GetRequestInfo: TIdEntityHeaderInfo; +begin + Result := FRequestInfo; +end; + end.