2
0

Quick.HttpServer.Request.pas 8.4 KB

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