-
Notifications
You must be signed in to change notification settings - Fork 188
/
Quick.HttpServer.Request.pas
317 lines (274 loc) · 8.58 KB
/
Quick.HttpServer.Request.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
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
{ ***************************************************************************
Copyright (c) 2016-2021 Kike Pérez
Unit : Quick.HttpServer.Request
Description : Http Server Request
Author : Kike Pérez
Version : 1.8
Created : 30/08/2019
Modified : 07/02/2021
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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.HttpServer.Request;
{$i QuickLib.inc}
interface
uses
{$IFDEF DEBUG_HTTPSERVER}
Quick.Debug.Utils,
{$ENDIF}
Classes,
SysUtils,
Quick.Commons,
Quick.Arrays,
Quick.Value,
Quick.HttpServer.Types;
type
EHttpRequestError = class(Exception);
type
IHttpRequest = interface
['{D6B236A5-9D04-4380-8A89-5BD4CC60A1A6}']
function GetPathSegment(aIndex : Integer) : string;
function GetQuery(const aName : string) : TFlexValue;
function GetURL: string;
function GetMethod: TMethodVerb;
function GetCacheControl: string;
function GetClientIP: string;
function GetContent: TStream;
function GetHeaders: TPairList;
function GetHost: string;
function GetPort: Integer;
function GetReferer: string;
function GetUnparsedParams: string;
function GetUserAgent: string;
property URL : string read GetURL;
property Method : TMethodVerb read GetMethod;
property Host : string read GetHost;
property Port : Integer read GetPort;
property Referer : string read GetReferer;
property UserAgent : string read GetUserAgent;
property CacheControl : string read GetCacheControl;
property PathSegment[aIndex : Integer] : string read GetPathSegment;
property UnparsedParams : string read GetUnparsedParams;
property Query[const aName : string] : TFlexValue read GetQuery;
property ClientIP : string read GetClientIP;
property Headers : TPairList read GetHeaders;
property Content : TStream read GetContent;
function ContentAsString : string;
function GetMethodAsString: string;
end;
THttpRequest = class(TInterfacedObject,IHttpRequest)
private
fURL : string;
fMethod : TMethodVerb;
fHost : string;
fPort : Integer;
fReferer : string;
fUserAgent : string;
fCacheControl : string;
fUnparsedParams : string;
fParsedQuery : TFlexPairArray;
fClientIP : string;
fHeaders : TPairList;
fContent : TStream;
fContentType : string;
fContentEncoding : string;
fContentLength : Int64;
function GetPathSegment(aIndex : Integer) : string;
function GetQuery(const aName : string) : TFlexValue;
procedure ParseQuery;
function GetURL: string;
function GetMethod: TMethodVerb;
function GetCacheControl: string;
function GetClientIP: string;
function GetContent: TStream;
function GetHeaders: TPairList;
function GetHost: string;
function GetPort: Integer;
function GetReferer: string;
function GetUnparsedParams: string;
function GetUserAgent: string;
procedure SetURL(const Value: string);
function GetContentEncoding: string;
function GetContentLength: Int64;
function GetContentType: string;
procedure SetContentEncoding(const Value: string);
procedure SetContentLength(const Value: Int64);
procedure SetContentType(const Value: string);
public
constructor Create;
destructor Destroy; override;
property URL : string read GetURL write SetURL;
property Method : TMethodVerb read GetMethod write fMethod;
property Host : string read GetHost write fHost;
property Port : Integer read GetPort write fPort;
property Referer : string read GetReferer write fReferer;
property UserAgent : string read GetUserAgent write fUserAgent;
property CacheControl : string read GetCacheControl write fCacheControl;
property PathSegment[aIndex : Integer] : string read GetPathSegment;
property UnparsedParams : string read GetUnparsedParams write fUnparsedParams;
property Query[const aName : string] : TFlexValue read GetQuery;
property ClientIP : string read GetClientIP write fClientIP;
property Headers : TPairList read GetHeaders write fHeaders;
property Content : TStream read GetContent write fContent;
property ContentType : string read GetContentType write SetContentType;
property ContentEncoding : string read GetContentEncoding write SetContentEncoding;
property ContentLength : Int64 read GetContentLength write SetContentLength;
procedure SetMethodFromString(const aVerbMethod : string);
function GetMethodAsString: string;
function ContentAsString : string;
end;
implementation
function THttpRequest.ContentAsString: string;
begin
{$IFDEF DEBUG_HTTPSERVER}
TDebugger.Trace(Self,'ContentAsString Encode: %s',[ContentEncoding]);
{$ENDIF}
if fContent <> nil then Result := StreamToString(fContent,TEncoding.UTF8);
end;
constructor THttpRequest.Create;
begin
fHeaders := TPairList.Create;
end;
destructor THttpRequest.Destroy;
begin
fHeaders.Free;
inherited;
end;
function THttpRequest.GetCacheControl: string;
begin
Result := fCacheControl;
end;
function THttpRequest.GetClientIP: string;
begin
Result := fClientIP;
end;
function THttpRequest.GetContent: TStream;
begin
Result := fContent;
end;
function THttpRequest.GetContentEncoding: string;
begin
Result := fContentEncoding;
end;
function THttpRequest.GetContentLength: Int64;
begin
Result := fContentLength;
end;
function THttpRequest.GetContentType: string;
begin
Result := fContentType;
end;
function THttpRequest.GetHeaders: TPairList;
begin
Result := fHeaders;
end;
function THttpRequest.GetHost: string;
begin
Result := fHost;
end;
function THttpRequest.GetMethod: TMethodVerb;
begin
Result := fMethod;
end;
function THttpRequest.GetMethodAsString: string;
begin
Result := MethodVerbStr[Integer(fMethod)];
end;
function THttpRequest.GetPathSegment(aIndex: Integer): string;
var
upath : string;
segment : TArray<string>;
begin
try
if fURL.StartsWith('/') then upath := furl.Substring(1)
else upath := fURL;
segment := upath.Split(['/']);
if (High(segment) < aIndex) or (aIndex < 0) then raise EHttpRequestError.CreateFmt('param out of bounds (%d)',[aIndex]);
Result := segment[aIndex];
except
on E : Exception do raise EHttpRequestError.CreateFmt('Error getting url path param : %s',[e.message]);
end;
end;
function THttpRequest.GetPort: Integer;
begin
Result := fPort;
end;
function THttpRequest.GetQuery(const aName: string): TFlexValue;
begin
if fParsedQuery.Count = 0 then ParseQuery;
Result := fParsedQuery.GetValue(aName);
end;
function THttpRequest.GetReferer: string;
begin
Result := fReferer;
end;
function THttpRequest.GetUnparsedParams: string;
begin
Result := fUnparsedParams;
end;
function THttpRequest.GetURL: string;
begin
Result := fURL;
end;
function THttpRequest.GetUserAgent: string;
begin
Result := fUserAgent;
end;
procedure THttpRequest.ParseQuery;
var
param : string;
pair : TFlexPair;
posi : Integer;
begin
for param in fUnparsedParams.Split(['&']) do
begin
posi := Pos('=',param);
pair.Name := Copy(param,1,posi - 1);
pair.Value := param.Substring(posi);
fParsedQuery.Add(pair);
end;
end;
procedure THttpRequest.SetContentEncoding(const Value: string);
begin
fContentEncoding := Value;
end;
procedure THttpRequest.SetContentLength(const Value: Int64);
begin
fContentLength := Value;
end;
procedure THttpRequest.SetContentType(const Value: string);
begin
fContentType := Value;
end;
procedure THttpRequest.SetMethodFromString(const aVerbMethod: string);
var
i : Integer;
begin
fMethod := TMethodVerb.mUNKNOWN;
for i := 0 to Ord(High(TMethodVerb)) do
begin
if CompareText(aVerbMethod,MethodVerbStr[i]) = 0 then
begin
fMethod := TMethodVerb(i);
Exit;
end;
end;
end;
procedure THttpRequest.SetURL(const Value: string);
begin
//remove first slash
if Value.StartsWith('/') then fURL := Value.Substring(1)
else fURL := Value;
//remove last slash
if fURL.EndsWith('/') then fURL := Copy(fURL,0,fURL.Length -1);
end;
end.