forked from zamronypj/fano
/
JsonContentTypeMiddlewareImpl.pas
74 lines (61 loc) · 1.87 KB
/
JsonContentTypeMiddlewareImpl.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{*!
* Fano Web Framework (https://fanoframework.github.io)
*
* @link https://github.com/fanoframework/fano
* @copyright Copyright (c) 2018 - 2022 Zamrony P. Juhara
* @license https://github.com/fanoframework/fano/blob/master/LICENSE (MIT)
*}
unit JsonContentTypeMiddlewareImpl;
interface
{$MODE OBJFPC}
uses
RequestIntf,
ResponseIntf,
MiddlewareIntf,
RouteArgsReaderIntf,
RequestHandlerIntf,
InjectableObjectImpl;
type
(*!------------------------------------------------
* middleware class that make POST PUT request with
* body of content type json available as request
* parsed body
*
* @author Zamrony P. Juhara <zamronypj@yahoo.com>
*-------------------------------------------------*)
TJsonContentTypeMiddleware = class(TInjectableObject, IMiddleware)
public
function handleRequest(
const request : IRequest;
const response : IResponse;
const args : IRouteArgsReader;
const nextMdlwr : IRequestHandler
) : IResponse;
end;
implementation
uses
JsonRequestImpl;
function TJsonContentTypeMiddleware.handleRequest(
const request : IRequest;
const response : IResponse;
const args : IRouteArgsReader;
const nextMdlwr : IRequestHandler
) : IResponse;
var req : IRequest;
isJsonRequest : boolean;
method : string;
begin
method := request.method;
isJsonRequest := ((method = 'POST') or (method = 'PUT') or
(method = 'PATCH') or (method = 'DELETE')) and
(request.env.contentType = 'application/json');
if isJsonRequest then
begin
req := TJsonRequest.create(request);
end else
begin
req := request;
end;
result := nextMdlwr.handleRequest(req, response, args);
end;
end.