Repository navigation
Expand file tree
/
Copy pathQuick.Core.Mvc.ActionResult.pas
More file actions
457 lines (403 loc) · 14.1 KB
/
Copy pathQuick.Core.Mvc.ActionResult.pas
File metadata and controls
457 lines (403 loc) · 14.1 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Mvc.ActionResult
Description : Core Mvc ActionResult
Author : Kike Perez
Version : 1.0
Created : 11/10/2019
Modified : 22/02/2020
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
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 Quick.Core.Mvc.ActionResult;
{$i QuickCore.inc}
interface
uses
System.Classes,
System.SysUtils,
Quick.Arrays,
Quick.Json.Serializer,
Quick.Core.Mvc.Context,
Quick.HttpServer.Types,
Quick.HttpServer.Response,
Quick.Core.Mvc.ViewFeatures;
type
IActionResult = interface
['{CF13DC00-D3F0-4B27-9D76-EC1AE863A440}']
procedure ExecuteResult(aContext : TActionContext);
end;
/// <summary>Bind a controller parameter from multipart/form-data or
/// application/x-www-form-urlencoded. Use with TUploadedFile for file parts.</summary>
FromForm = class(TCustomAttribute)
private
fName : string;
public
constructor Create(const aName : string = '');
property Name : string read fName;
end;
/// <summary>A file part received via multipart/form-data. Populated automatically
/// when a parameter is annotated with [FromForm] and its type is TUploadedFile.</summary>
TUploadedFile = class
private
fFieldName : string;
fFileName : string;
fContentType : string;
fData : TBytes;
public
property FieldName : string read fFieldName write fFieldName;
property FileName : string read fFileName write fFileName;
property ContentType : string read fContentType write fContentType;
property Data : TBytes read fData write fData;
function Size : Int64;
function AsString(aEncoding : TEncoding = nil) : string;
end;
TActionResult = class(TInterfacedObject,IActionResult)
public
procedure ExecuteResult(aContext : TActionContext); virtual; abstract;
end;
TStatusResult = class(TActionResult)
private
fStatusCode : Integer;
fStatusText : string;
public
constructor Create(aStatusCode : Integer; const aStatusText : string = '');
property StatusCode : Integer read fStatusCode write fStatusCode;
property StatusText : string read fStatusText write fStatusText;
procedure ExecuteResult(aContext : TActionContext); override;
end;
TRedirectResult = class(TActionResult)
private
fStatusCode : Integer;
fURL : string;
public
constructor Create(aStatusCode : Integer; const aURL : string);
property StatusCode : Integer read fStatusCode write fStatusCode;
property URL : string read fURL write fURL;
procedure ExecuteResult(aContext : TActionContext); override;
end;
TContentResult = class(TActionResult)
private
fContent : string;
fContentType : string;
public
constructor Create(const aContentText : string); overload;
constructor Create(const aContentText, aContentType : string); overload;
property Content : string read fContent write fContent;
property ContentType : string read fContentType write fContentType;
procedure ExecuteResult(aContext : TActionContext); override;
end;
TJsonResult = class(TActionResult)
private
fJsonText : string;
fJsonSerializer : TJsonSerializer;
public
constructor Create(aObject: TObject; aOnlyPublishedProperties : Boolean = False); overload;
constructor Create(const aJsonText : string); overload;
destructor Destroy; override;
property JsonText : string read fJsonText write fJsonText;
/// <summary>Respects the Accept header: returns JSON or minimal XML.</summary>
procedure ExecuteResult(aContext : TActionContext); override;
end;
TViewResult = class(TActionResult)
private
fViewName : string;
fViewData : TViewDataDictionary;
fViewEngine : IViewEngine;
fContentType : string;
public
constructor Create(const aViewName : string);
property ViewName : string read fViewName write fViewName;
property ViewData : TViewDataDictionary read fViewData write fViewData;
property ViewEngine : IViewEngine read fViewEngine write fViewEngine;
property ContentType : string read fContentType write fContentType;
procedure ExecuteResult(aContext : TActionContext); override;
end;
/// <summary>RFC 7807 Problem Details response.
/// Content-Type: application/problem+json
/// Body: { "type": "...", "title": "...", "status": NNN,
/// "detail": "...", "instance": "..." }
/// </summary>
TProblemDetailsResult = class(TActionResult)
private
fStatus : Integer;
fTitle : string;
fDetail : string;
fType : string;
fInstance : string;
fExtra : string; // optional extra JSON fields (raw, no outer braces)
public
constructor Create(aStatus : Integer; const aTitle : string;
const aDetail : string = ''; const aType : string = '';
const aInstance : string = ''); overload;
property Status : Integer read fStatus write fStatus;
property Title : string read fTitle write fTitle;
property Detail : string read fDetail write fDetail;
property &Type : string read fType write fType;
property Instance : string read fInstance write fInstance;
/// <summary>Append extra raw JSON properties (no braces) to the response body.</summary>
property Extra : string read fExtra write fExtra;
procedure ExecuteResult(aContext : TActionContext); override;
end;
implementation
{ FromForm }
constructor FromForm.Create(const aName : string);
begin
fName := aName;
end;
{ TUploadedFile }
function TUploadedFile.Size : Int64;
begin
Result := Length(fData);
end;
function TUploadedFile.AsString(aEncoding : TEncoding) : string;
var
enc : TEncoding;
begin
if aEncoding = nil then enc := TEncoding.UTF8
else enc := aEncoding;
Result := enc.GetString(fData);
end;
{ TStatusResult }
constructor TStatusResult.Create(aStatusCode: Integer; const aStatusText: string);
begin
fStatusCode := aStatusCode;
fStatusText := aStatusText;
end;
procedure TStatusResult.ExecuteResult(aContext: TActionContext);
begin
aContext.HttpContext.Response.StatusCode := fStatusCode;
aContext.HttpContext.Response.StatusText := fStatusText;
if fStatusCode > 399 then raise EControlledException.Create(Self,fStatusText);
end;
{ TRedirectResult }
constructor TRedirectResult.Create(aStatusCode : Integer; const aURL : string);
begin
fStatusCode := aStatusCode;
fURL := aURL;
end;
procedure TRedirectResult.ExecuteResult(aContext: TActionContext);
begin
aContext.HttpContext.Response.Headers.AddOrUpdate('Location',fURL);
end;
{ TContentResult }
constructor TContentResult.Create(const aContentText: string);
begin
fContent := aContentText;
end;
constructor TContentResult.Create(const aContentText, aContentType: string);
begin
fContent := aContentText;
fContentType := aContentType;
end;
procedure TContentResult.ExecuteResult(aContext: TActionContext);
begin
aContext.HttpContext.Response.ContentText := fContent;
aContext.HttpContext.Response.ContentType := fContentType;
end;
{ TViewResult }
constructor TViewResult.Create(const aViewName: string);
begin
if aViewName = '' then fViewName := 'Home'
else fViewName := aViewName;
end;
procedure TViewResult.ExecuteResult(aContext: TActionContext);
var
viewContext : TViewContext;
view : IView;
streamPage : TMemoryStream;
begin
if fViewEngine = nil then raise Exception.Create('Not MVC Engine defined');
view := fViewEngine.FindView(aContext,fViewName);
streamPage := TStringStream.Create;
viewContext := TViewContext.Create(aContext,fViewData,nil,streampage);
try
view.Render(viewContext);
finally
viewContext.Free;
end;
aContext.HttpContext.Response.Content := streampage;
aContext.HttpContext.Response.ContentType := 'text/html';
aContext.HttpContext.Response.StatusCode := 200;
end;
{ TJsonResult }
constructor TJsonResult.Create(const aJsonText: string);
begin
fJsonText := aJsonText;
end;
constructor TJsonResult.Create(aObject: TObject; aOnlyPublishedProperties : Boolean = False);
var
serializerlevel : TSerializeLevel;
begin
if aOnlyPublishedProperties then serializerlevel := TSerializeLevel.slPublishedProperty
else serializerlevel := TSerializeLevel.slPublicProperty;
fJsonSerializer := TJsonSerializer.Create(serializerlevel,True);
fJsonText := fJsonSerializer.ObjectToJson(aObject,False);
end;
destructor TJsonResult.Destroy;
begin
fJsonSerializer.Free;
inherited;
end;
procedure TJsonResult.ExecuteResult(aContext: TActionContext);
var
acceptHeader : string;
xmlBody : string;
sb : TStringBuilder;
i : Integer;
ch : Char;
inStr : Boolean;
key : string;
value : string;
depth : Integer;
// Minimal JSON→XML helper (flat/scalar properties only)
procedure AppendXmlEscaped(b : TStringBuilder; const s : string);
var j : Integer;
begin
for j := 1 to Length(s) do
case s[j] of
'<' : b.Append('<');
'>' : b.Append('>');
'&' : b.Append('&');
'"' : b.Append('"');
else b.Append(s[j]);
end;
end;
begin
acceptHeader := aContext.HttpContext.Request.Headers.GetValue('Accept');
if acceptHeader.Contains('application/xml') or
acceptHeader.Contains('text/xml') then
begin
// Produce a minimal XML representation from the JSON text
sb := TStringBuilder.Create;
try
sb.Append('<?xml version="1.0" encoding="utf-8"?><response>');
// Simple single-pass flat JSON→XML (top-level scalars only)
i := 1; inStr := False; depth := 0; key := ''; value := '';
while i <= Length(fJsonText) do
begin
ch := fJsonText[i];
case ch of
'"' :
begin
inStr := not inStr;
if inStr and (depth = 1) and (key = '') then
begin
Inc(i);
while (i <= Length(fJsonText)) and not
((fJsonText[i] = '"') and (fJsonText[i-1] <> '\')) do
begin
key := key + fJsonText[i];
Inc(i);
end;
end
else if inStr and (depth = 1) and (key <> '') then
begin
Inc(i);
while (i <= Length(fJsonText)) and not
((fJsonText[i] = '"') and (fJsonText[i-1] <> '\')) do
begin
value := value + fJsonText[i];
Inc(i);
end;
sb.Append('<'); sb.Append(key); sb.Append('>');
AppendXmlEscaped(sb, value);
sb.Append('</'); sb.Append(key); sb.Append('>');
key := ''; value := '';
end;
end;
'{', '[' : if not inStr then Inc(depth);
'}', ']' : if not inStr then Dec(depth);
':' :
if (not inStr) and (depth = 1) and (key <> '') then
begin
Inc(i);
while (i <= Length(fJsonText)) and (fJsonText[i] = ' ') do Inc(i);
if (i <= Length(fJsonText)) and (fJsonText[i] <> '"') and
(fJsonText[i] <> '{') and (fJsonText[i] <> '[') then
begin
value := '';
while (i <= Length(fJsonText)) and
not CharInSet(fJsonText[i], [',','}',']',#10,#13]) do
begin
value := value + fJsonText[i];
Inc(i);
end;
value := Trim(value);
if (value <> '') and (value <> 'null') then
begin
sb.Append('<'); sb.Append(key); sb.Append('>');
AppendXmlEscaped(sb, value);
sb.Append('</'); sb.Append(key); sb.Append('>');
end;
key := ''; value := '';
Continue;
end;
end;
end;
Inc(i);
end;
sb.Append('</response>');
xmlBody := sb.ToString;
finally
sb.Free;
end;
aContext.HttpContext.Response.ContentText := xmlBody;
aContext.HttpContext.Response.ContentType := 'application/xml';
end
else
begin
aContext.HttpContext.Response.ContentText := fJsonText;
aContext.HttpContext.Response.ContentType := 'application/json';
end;
end;
{ TProblemDetailsResult }
constructor TProblemDetailsResult.Create(aStatus : Integer; const aTitle : string;
const aDetail, aType, aInstance : string);
begin
fStatus := aStatus;
fTitle := aTitle;
fDetail := aDetail;
fType := aType;
fInstance := aInstance;
end;
procedure TProblemDetailsResult.ExecuteResult(aContext : TActionContext);
var
sb : TStringBuilder;
typeUri : string;
begin
typeUri := fType;
if typeUri.IsEmpty then
typeUri := 'https://tools.ietf.org/html/rfc7807';
sb := TStringBuilder.Create;
try
sb.Append('{');
sb.AppendFormat('"type":"%s"', [typeUri]);
sb.AppendFormat(',"title":"%s"', [StringReplace(fTitle,'"','\"',[rfReplaceAll])]);
sb.AppendFormat(',"status":%d', [fStatus]);
if not fDetail.IsEmpty then
sb.AppendFormat(',"detail":"%s"', [StringReplace(fDetail,'"','\"',[rfReplaceAll])]);
if not fInstance.IsEmpty then
sb.AppendFormat(',"instance":"%s"', [fInstance]);
if not fExtra.IsEmpty then
begin
sb.Append(',');
sb.Append(fExtra);
end;
sb.Append('}');
aContext.HttpContext.Response.StatusCode := fStatus;
aContext.HttpContext.Response.ContentType := 'application/problem+json';
aContext.HttpContext.Response.ContentText := sb.ToString;
finally
sb.Free;
end;
end;
end.