Skip to content

Commit

Permalink
horse update
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeice committed Feb 14, 2020
1 parent f14c20b commit 85ec94a
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 38 deletions.
1 change: 1 addition & 0 deletions Demo/Demo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
144 changes: 138 additions & 6 deletions Demo/Demo.dproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Demo/boss-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Loggastic.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace)</DCC_Namespace>
<DCC_CBuilderOutput>All</DCC_CBuilderOutput>
<SanitizedProjectName>Loggastic</SanitizedProjectName>
<DCC_UnitSearchPath>modules\.dcp;modules\.dcu;modules;modules\horse\src;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
<DCC_UnitSearchPath>$(DCC_UnitSearchPath);modules\.dcp;modules\.dcu;modules;modules\horse\src</DCC_UnitSearchPath>
<RuntimeOnlyPackage>true</RuntimeOnlyPackage>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Android)'!=''">
Expand Down
4 changes: 2 additions & 2 deletions boss-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/Horse.Loggastic.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
80 changes: 56 additions & 24 deletions src/Providers/Providers.Log.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TProviderLogRequest = class

function ToJSON: TJSONObject;

constructor Create(const ARequest: THorseRequest);
constructor Create(const ARequest: THorseRequest; AStartDate: TDateTime);
end;

TProviderLogGeneral = class
Expand Down Expand Up @@ -89,36 +89,45 @@ 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
LLog.Free;
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
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.

0 comments on commit 85ec94a

Please sign in to comment.