From 3c6b2b36a919c33303c543b4ed4f8f6be2f3402f Mon Sep 17 00:00:00 2001 From: Danilo Lucas Date: Wed, 5 Jan 2022 12:03:10 -0300 Subject: [PATCH 1/2] Valid JSON --- src/Horse.Jhonson.pas | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Horse.Jhonson.pas b/src/Horse.Jhonson.pas index 61432ee..d7d0751 100644 --- a/src/Horse.Jhonson.pas +++ b/src/Horse.Jhonson.pas @@ -38,13 +38,28 @@ function Jhonson(const ACharset: string): THorseCallback; overload; procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF}); var LJSON: {$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF}; + LMethod: TMethodType; begin - if ({$IF DEFINED(FPC)} StringCommandToMethodType(Req.RawWebRequest.Method) - {$ELSE} Req.RawWebRequest.MethodType{$ENDIF} in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then + LMethod := {$IF DEFINED(FPC)} StringCommandToMethodType(Req.RawWebRequest.Method); {$ELSE} Req.RawWebRequest.MethodType; {$ENDIF} + + if (LMethod in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then begin - LJSON := {$IF DEFINED(FPC)} GetJSON(Req.Body) {$ELSE}TJSONObject.ParseJSONValue(Req.Body){$ENDIF}; + try + LJSON := {$IF DEFINED(FPC)} GetJSON(Req.Body) {$ELSE}TJSONObject.ParseJSONValue(Req.Body){$ENDIF}; + except + Res.Send('Invalid JSON').Status(THTTPStatus.BadRequest); + raise EHorseCallbackInterrupted.Create; + end; + + if not Assigned(LJSON) then + begin + Res.Send('Invalid JSON').Status(THTTPStatus.BadRequest); + raise EHorseCallbackInterrupted.Create; + end; + Req.Body(LJSON); end; + try Next; finally From d66b79b706c8c2ece4886155f0c74d10ccf12ef2 Mon Sep 17 00:00:00 2001 From: Danilo Lucas Date: Wed, 5 Jan 2022 13:58:33 -0300 Subject: [PATCH 2/2] Update Horse.Jhonson.pas --- src/Horse.Jhonson.pas | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Horse.Jhonson.pas b/src/Horse.Jhonson.pas index d7d0751..4fe8de4 100644 --- a/src/Horse.Jhonson.pas +++ b/src/Horse.Jhonson.pas @@ -38,11 +38,8 @@ function Jhonson(const ACharset: string): THorseCallback; overload; procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF}); var LJSON: {$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF}; - LMethod: TMethodType; begin - LMethod := {$IF DEFINED(FPC)} StringCommandToMethodType(Req.RawWebRequest.Method); {$ELSE} Req.RawWebRequest.MethodType; {$ENDIF} - - if (LMethod in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then + if (Req.MethodType in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType = 'application/json') then begin try LJSON := {$IF DEFINED(FPC)} GetJSON(Req.Body) {$ELSE}TJSONObject.ParseJSONValue(Req.Body){$ENDIF};