Skip to content

Commit 675bdfe

Browse files
committed
new Quick.HttpServer
1 parent a037fef commit 675bdfe

4 files changed

+1381
-0
lines changed

Quick.HttpServer.Request.pas

+311
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
{ ***************************************************************************
2+
3+
Copyright (c) 2016-2019 Kike Pérez
4+
5+
Unit : Quick.HttpServer.Request
6+
Description : Http Server Request
7+
Author : Kike Pérez
8+
Version : 1.8
9+
Created : 30/08/2019
10+
Modified : 31/08/2019
11+
12+
This file is part of QuickLib: https://github.com/exilon/QuickLib
13+
14+
***************************************************************************
15+
16+
Licensed under the Apache License, Version 2.0 (the "License");
17+
you may not use this file except in compliance with the License.
18+
You may obtain a copy of the License at
19+
20+
http://www.apache.org/licenses/LICENSE-2.0
21+
22+
Unless required by applicable law or agreed to in writing, software
23+
distributed under the License is distributed on an "AS IS" BASIS,
24+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
See the License for the specific language governing permissions and
26+
limitations under the License.
27+
28+
*************************************************************************** }
29+
30+
unit Quick.HttpServer.Request;
31+
32+
{$i QuickLib.inc}
33+
34+
interface
35+
36+
uses
37+
Classes,
38+
SysUtils,
39+
Quick.Commons,
40+
Quick.Arrays,
41+
Quick.Value,
42+
Quick.HttpServer.Types;
43+
44+
type
45+
EHttpRequestError = class(Exception);
46+
47+
type
48+
IHttpRequest = interface
49+
['{D6B236A5-9D04-4380-8A89-5BD4CC60A1A6}']
50+
function GetPathSegment(aIndex : Integer) : string;
51+
function GetQuery(const aName : string) : TFlexValue;
52+
function GetURL: string;
53+
function GetMethod: TMethodVerb;
54+
function GetCacheControl: string;
55+
function GetClientIP: string;
56+
function GetContent: TStream;
57+
function GetHeaders: TPairList;
58+
function GetHost: string;
59+
function GetPort: Integer;
60+
function GetReferer: string;
61+
function GetUnparsedParams: string;
62+
function GetUserAgent: string;
63+
property URL : string read GetURL;
64+
property Method : TMethodVerb read GetMethod;
65+
property Host : string read GetHost;
66+
property Port : Integer read GetPort;
67+
property Referer : string read GetReferer;
68+
property UserAgent : string read GetUserAgent;
69+
property CacheControl : string read GetCacheControl;
70+
property PathSegment[aIndex : Integer] : string read GetPathSegment;
71+
property UnparsedParams : string read GetUnparsedParams;
72+
property Query[const aName : string] : TFlexValue read GetQuery;
73+
property ClientIP : string read GetClientIP;
74+
property Headers : TPairList read GetHeaders;
75+
property Content : TStream read GetContent;
76+
function ContentAsString : string;
77+
function GetMethodAsString: string;
78+
end;
79+
80+
THttpRequest = class(TInterfacedObject,IHttpRequest)
81+
private
82+
fURL : string;
83+
fMethod : TMethodVerb;
84+
fHost : string;
85+
fPort : Integer;
86+
fReferer : string;
87+
fUserAgent : string;
88+
fCacheControl : string;
89+
fUnparsedParams : string;
90+
fParsedQuery : TFlexPairArray;
91+
fClientIP : string;
92+
fHeaders : TPairList;
93+
fContent : TStream;
94+
fContentType : string;
95+
fContentEncoding : string;
96+
fContentLength : Int64;
97+
function GetPathSegment(aIndex : Integer) : string;
98+
function GetQuery(const aName : string) : TFlexValue;
99+
procedure ParseQuery;
100+
function GetURL: string;
101+
function GetMethod: TMethodVerb;
102+
function GetCacheControl: string;
103+
function GetClientIP: string;
104+
function GetContent: TStream;
105+
function GetHeaders: TPairList;
106+
function GetHost: string;
107+
function GetPort: Integer;
108+
function GetReferer: string;
109+
function GetUnparsedParams: string;
110+
function GetUserAgent: string;
111+
procedure SetURL(const Value: string);
112+
function GetContentEncoding: string;
113+
function GetContentLength: Int64;
114+
function GetContentType: string;
115+
procedure SetContentEncoding(const Value: string);
116+
procedure SetContentLength(const Value: Int64);
117+
procedure SetContentType(const Value: string);
118+
public
119+
constructor Create;
120+
destructor Destroy; override;
121+
property URL : string read GetURL write SetURL;
122+
property Method : TMethodVerb read GetMethod write fMethod;
123+
property Host : string read GetHost write fHost;
124+
property Port : Integer read GetPort write fPort;
125+
property Referer : string read GetReferer write fReferer;
126+
property UserAgent : string read GetUserAgent write fUserAgent;
127+
property CacheControl : string read GetCacheControl write fCacheControl;
128+
property PathSegment[aIndex : Integer] : string read GetPathSegment;
129+
property UnparsedParams : string read GetUnparsedParams write fUnparsedParams;
130+
property Query[const aName : string] : TFlexValue read GetQuery;
131+
property ClientIP : string read GetClientIP write fClientIP;
132+
property Headers : TPairList read GetHeaders write fHeaders;
133+
property Content : TStream read GetContent write fContent;
134+
property ContentType : string read GetContentType write SetContentType;
135+
property ContentEncoding : string read GetContentEncoding write SetContentEncoding;
136+
property ContentLength : Int64 read GetContentLength write SetContentLength;
137+
procedure SetMethodFromString(const aVerbMethod : string);
138+
function GetMethodAsString: string;
139+
function ContentAsString : string;
140+
end;
141+
142+
implementation
143+
144+
function THttpRequest.ContentAsString: string;
145+
begin
146+
if fContent <> nil then Result := StreamToString2(fContent,TEncoding.UTF8);
147+
end;
148+
149+
constructor THttpRequest.Create;
150+
begin
151+
fHeaders := TPairList.Create;
152+
end;
153+
154+
destructor THttpRequest.Destroy;
155+
begin
156+
fHeaders.Free;
157+
inherited;
158+
end;
159+
160+
function THttpRequest.GetCacheControl: string;
161+
begin
162+
Result := fCacheControl;
163+
end;
164+
165+
function THttpRequest.GetClientIP: string;
166+
begin
167+
Result := fClientIP;
168+
end;
169+
170+
function THttpRequest.GetContent: TStream;
171+
begin
172+
Result := fContent;
173+
end;
174+
175+
function THttpRequest.GetContentEncoding: string;
176+
begin
177+
Result := fContentEncoding;
178+
end;
179+
180+
function THttpRequest.GetContentLength: Int64;
181+
begin
182+
Result := fContentLength;
183+
end;
184+
185+
function THttpRequest.GetContentType: string;
186+
begin
187+
Result := fContentType;
188+
end;
189+
190+
function THttpRequest.GetHeaders: TPairList;
191+
begin
192+
Result := fHeaders;
193+
end;
194+
195+
function THttpRequest.GetHost: string;
196+
begin
197+
Result := fHost;
198+
end;
199+
200+
function THttpRequest.GetMethod: TMethodVerb;
201+
begin
202+
Result := fMethod;
203+
end;
204+
205+
function THttpRequest.GetMethodAsString: string;
206+
begin
207+
Result := MethodVerbStr[Integer(fMethod)];
208+
end;
209+
210+
function THttpRequest.GetPathSegment(aIndex: Integer): string;
211+
var
212+
upath : string;
213+
segment : TArray<string>;
214+
begin
215+
try
216+
if fURL.StartsWith('/') then upath := furl.Substring(1)
217+
else upath := fURL;
218+
segment := upath.Split(['/']);
219+
if (High(segment) < aIndex) or (aIndex < 0) then raise EHttpRequestError.CreateFmt('param out of bounds (%d)',[aIndex]);
220+
Result := segment[aIndex];
221+
except
222+
on E : Exception do raise EHttpRequestError.CreateFmt('Error getting url path param : %s',[e.message]);
223+
end;
224+
end;
225+
226+
function THttpRequest.GetPort: Integer;
227+
begin
228+
Result := fPort;
229+
end;
230+
231+
function THttpRequest.GetQuery(const aName: string): TFlexValue;
232+
begin
233+
if fParsedQuery.Count = 0 then ParseQuery;
234+
Result := fParsedQuery.GetValue(aName);
235+
end;
236+
237+
function THttpRequest.GetReferer: string;
238+
begin
239+
Result := fReferer;
240+
end;
241+
242+
function THttpRequest.GetUnparsedParams: string;
243+
begin
244+
Result := fUnparsedParams;
245+
end;
246+
247+
function THttpRequest.GetURL: string;
248+
begin
249+
Result := fURL;
250+
end;
251+
252+
function THttpRequest.GetUserAgent: string;
253+
begin
254+
Result := fUserAgent;
255+
end;
256+
257+
procedure THttpRequest.ParseQuery;
258+
var
259+
param : string;
260+
pair : TFlexPair;
261+
posi : Integer;
262+
begin
263+
for param in fUnparsedParams.Split(['&']) do
264+
begin
265+
posi := Pos('=',param);
266+
pair.Name := Copy(param,1,posi - 1);
267+
pair.Value := param.Substring(posi);
268+
fParsedQuery.Add(pair);
269+
end;
270+
end;
271+
272+
procedure THttpRequest.SetContentEncoding(const Value: string);
273+
begin
274+
fContentEncoding := Value;
275+
end;
276+
277+
procedure THttpRequest.SetContentLength(const Value: Int64);
278+
begin
279+
fContentLength := Value;
280+
end;
281+
282+
procedure THttpRequest.SetContentType(const Value: string);
283+
begin
284+
fContentType := Value;
285+
end;
286+
287+
procedure THttpRequest.SetMethodFromString(const aVerbMethod: string);
288+
var
289+
i : Integer;
290+
begin
291+
fMethod := TMethodVerb.mUNKNOWN;
292+
for i := 0 to Ord(High(TMethodVerb)) do
293+
begin
294+
if CompareText(aVerbMethod,MethodVerbStr[i]) = 0 then
295+
begin
296+
fMethod := TMethodVerb(i);
297+
Exit;
298+
end;
299+
end;
300+
end;
301+
302+
procedure THttpRequest.SetURL(const Value: string);
303+
begin
304+
//remove first slash
305+
if Value.StartsWith('/') then fURL := Value.Substring(1)
306+
else fURL := Value;
307+
//remove last slash
308+
if fURL.EndsWith('/') then fURL := Copy(fURL,0,fURL.Length -1);
309+
end;
310+
311+
end.

0 commit comments

Comments
 (0)