Quick.HttpServer.Response.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.HttpServer.Response
  4. Description : Http Server Response
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 30/08/2019
  8. Modified : 06/10/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.Response;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. Quick.Value,
  28. Quick.Commons;
  29. type
  30. IHttpResponse = interface
  31. ['{3E90F34D-5F4D-41E5-89C5-CA9832C7405E}']
  32. procedure SetStatusCode(const Value: Cardinal);
  33. procedure SetStatusText(const Value: string);
  34. function GetStatusCode: Cardinal;
  35. function GetStatusText: string;
  36. function GetHeaders: TPairList;
  37. procedure SetHeaders(const Value: TPairList);
  38. function GetContentStream: TStream;
  39. procedure SetContentStream(const Value: TStream);
  40. function GetContentText: string;
  41. procedure SetContentText(const Value: string);
  42. function GetContentType: string;
  43. procedure SetContentType(const Value: string);
  44. property Headers : TPairList read GetHeaders write SetHeaders;
  45. property StatusCode : Cardinal read GetStatusCode write SetStatusCode;
  46. property StatusText : string read GetStatusText write SetStatusText;
  47. property Content : TStream read GetContentStream write SetContentStream;
  48. property ContentText : string read GetContentText write SetContentText;
  49. property ContentType : string read GetContentType write SetContentType;
  50. end;
  51. {$M+}
  52. THttpResponse = class(TInterfacedObject,IHttpResponse)
  53. private
  54. fHeaders : TPairList;
  55. fStatusText: string;
  56. fStatusCode: Cardinal;
  57. fContentText : string;
  58. fContent : TStream;
  59. fContentType : string;
  60. procedure SetStatusCode(const Value: Cardinal);
  61. procedure SetStatusText(const Value: string);
  62. function GetStatusCode: Cardinal;
  63. function GetStatusText: string;
  64. function GetContentText: string;
  65. function GetContentStream: TStream;
  66. procedure SetContentText(const Value: string);
  67. procedure SetContentStream(const Value: TStream);
  68. function GetHeaders: TPairList;
  69. procedure SetHeaders(const Value: TPairList);
  70. function GetContentType: string;
  71. procedure SetContentType(const Value: string);
  72. public
  73. constructor Create;
  74. destructor Destroy; override;
  75. procedure ContentFromStream(const Value: TStream);
  76. published
  77. property Headers : TPairList read GetHeaders write SetHeaders;
  78. property StatusCode : Cardinal read GetStatusCode write SetStatusCode;
  79. property StatusText : string read GetStatusText write SetStatusText;
  80. property Content : TStream read GetContentStream write SetContentStream;
  81. property ContentText : string read GetContentText write SetContentText;
  82. property ContentType : string read GetContentType write SetContentType;
  83. end;
  84. {$M-}
  85. implementation
  86. { THttpResponse }
  87. constructor THttpResponse.Create;
  88. begin
  89. fContentText := '';
  90. fContent := nil;
  91. fStatusCode := 200;
  92. fStatusText := '';
  93. //add custom header to response
  94. fHeaders := TPairList.Create;
  95. fHeaders.Add('Server','QuickHttpServer');
  96. end;
  97. destructor THttpResponse.Destroy;
  98. begin
  99. fHeaders.Free;
  100. if Assigned(fContent) and (fContent <> nil) then fContent.Free;
  101. inherited;
  102. end;
  103. function THttpResponse.GetContentStream: TStream;
  104. begin
  105. Result := fContent;
  106. end;
  107. function THttpResponse.GetContentText: string;
  108. begin
  109. Result := fContentText;
  110. end;
  111. function THttpResponse.GetContentType: string;
  112. begin
  113. Result := fContentType;
  114. end;
  115. function THttpResponse.GetHeaders: TPairList;
  116. begin
  117. Result := fHeaders;
  118. end;
  119. function THttpResponse.GetStatusCode: Cardinal;
  120. begin
  121. Result := fStatusCode;
  122. end;
  123. function THttpResponse.GetStatusText: string;
  124. begin
  125. Result := fStatusText;
  126. end;
  127. procedure THttpResponse.SetStatusCode(const Value: Cardinal);
  128. begin
  129. fStatusCode := Value;
  130. end;
  131. procedure THttpResponse.SetStatusText(const Value: string);
  132. begin
  133. fStatusText := Value;
  134. end;
  135. procedure THttpResponse.SetContentStream(const Value: TStream);
  136. begin
  137. fContent := Value;
  138. end;
  139. procedure THttpResponse.SetContentText(const Value: string);
  140. begin
  141. fContentText := Value;
  142. end;
  143. procedure THttpResponse.SetContentType(const Value: string);
  144. begin
  145. fContentType := Value;
  146. end;
  147. procedure THttpResponse.SetHeaders(const Value: TPairList);
  148. begin
  149. fHeaders := Value;
  150. end;
  151. procedure THttpResponse.ContentFromStream(const Value: TStream);
  152. begin
  153. if Value <> nil then
  154. begin
  155. if fContent = nil then fContent := TMemoryStream.Create;
  156. fContent.CopyFrom(Value,Value.Size);
  157. end
  158. else fContent := nil;
  159. end;
  160. end.