HTTPUpload_frMain.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit HTTPUpload_frMain;
  26. {$MODE DELPHI}
  27. {$PUSH}{$WARN 5024 OFF}
  28. interface
  29. uses
  30. SysUtils,
  31. Classes,
  32. StdCtrls,
  33. ActnList,
  34. Graphics,
  35. Spin,
  36. Dialogs,
  37. Forms,
  38. LCLIntf,
  39. BrookUtility,
  40. BrookHTTPUploads,
  41. BrookHTTPRequest,
  42. BrookHTTPResponse,
  43. BrookHTTPServer;
  44. type
  45. TfrMain = class(TForm)
  46. acStart: TAction;
  47. acStop: TAction;
  48. alMain: TActionList;
  49. BrookHTTPServer1: TBrookHTTPServer;
  50. btStart: TButton;
  51. btStop: TButton;
  52. edPort: TSpinEdit;
  53. lbLink: TLabel;
  54. lbPort: TLabel;
  55. procedure acStartExecute(Sender: TObject);
  56. procedure acStopExecute(Sender: TObject);
  57. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  58. procedure BrookHTTPServer1Request(ASender: TObject;
  59. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  60. procedure BrookHTTPServer1RequestError(ASender: TObject;
  61. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  62. AException: Exception);
  63. procedure BrookHTTPServer1Start(Sender: TObject);
  64. procedure BrookHTTPServer1Stop(Sender: TObject);
  65. procedure edPortChange(Sender: TObject);
  66. procedure FormShow(Sender: TObject);
  67. procedure lbLinkClick(Sender: TObject);
  68. procedure lbLinkMouseEnter(Sender: TObject);
  69. procedure lbLinkMouseLeave(Sender: TObject);
  70. protected
  71. procedure DoError(AData: PtrInt);
  72. public
  73. procedure UpdateControls; inline;
  74. end;
  75. const
  76. PAGE_FORM = Concat(
  77. '<html>',
  78. '<body>',
  79. '<form action="" method="post" enctype="multipart/form-data">',
  80. '<fieldset>',
  81. '<legend>Choose the files:</legend>',
  82. 'File 1: <input type="file" name="file1"/><br>',
  83. 'File 2: <input type="file" name="file2"/><br>',
  84. '<input type="submit"/>',
  85. '</fieldset>',
  86. '</form>',
  87. '</body>',
  88. '</html>'
  89. );
  90. PAGE_DONE = Concat(
  91. '<html>',
  92. '<head>',
  93. '<title>Uploads</title>',
  94. '</head>',
  95. '<body>',
  96. '<strong>Uploaded files:</strong><br>',
  97. '%s',
  98. '</body>',
  99. '</html>'
  100. );
  101. CONTENT_TYPE = 'text/html; charset=utf-8';
  102. var
  103. frMain: TfrMain;
  104. implementation
  105. {$R *.lfm}
  106. procedure TfrMain.FormShow(Sender: TObject);
  107. begin
  108. if BrookHTTPServer1.UploadsDir.IsEmpty then
  109. BrookHTTPServer1.UploadsDir := Sagui.TmpDir;
  110. end;
  111. procedure TfrMain.DoError(AData: PtrInt);
  112. var
  113. S: PString absolute AData;
  114. begin
  115. try
  116. MessageDlg(S^, mtError, [mbOK], 0);
  117. finally
  118. DisposeStr(S);
  119. end;
  120. end;
  121. procedure TfrMain.UpdateControls;
  122. begin
  123. if BrookHTTPServer1.Active then
  124. edPort.Value := BrookHTTPServer1.Port
  125. else
  126. BrookHTTPServer1.Port := edPort.Value;
  127. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  128. acStart.Enabled := not BrookHTTPServer1.Active;
  129. acStop.Enabled := not acStart.Enabled;
  130. edPort.Enabled := acStart.Enabled;
  131. lbLink.Enabled := not acStart.Enabled;
  132. end;
  133. procedure TfrMain.acStartExecute(Sender: TObject);
  134. begin
  135. BrookHTTPServer1.Open;
  136. end;
  137. procedure TfrMain.acStopExecute(Sender: TObject);
  138. begin
  139. BrookHTTPServer1.Close;
  140. end;
  141. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  142. begin
  143. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  144. end;
  145. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  146. begin
  147. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  148. end;
  149. procedure TfrMain.lbLinkClick(Sender: TObject);
  150. begin
  151. OpenURL(lbLink.Caption);
  152. end;
  153. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  154. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  155. var
  156. VUpload: TBrookHTTPUpload;
  157. VFile, VList, VError: string;
  158. begin
  159. if ARequest.IsUploading then
  160. begin
  161. VList := '<ol>';
  162. for VUpload in ARequest.Uploads do
  163. if VUpload.Save(False, VError) then
  164. VList := Concat(VList, '<li><a href="?file=', VUpload.Name, '">',
  165. VUpload.Name, '</a></li>')
  166. else
  167. VList := Concat(VList, '<li><font color="red">', VUpload.Name,
  168. ' - failed - ', VError, '</font></li>');
  169. VList := Concat(VList, '</ol>');
  170. AResponse.SendFmt(PAGE_DONE, [VList], CONTENT_TYPE, 200);
  171. end
  172. else
  173. begin
  174. if ARequest.Params.TryValue('file', VFile) then
  175. AResponse.Download(
  176. Concat(BrookHTTPServer1.UploadsDir, PathDelim, VFile))
  177. else
  178. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  179. end;
  180. end;
  181. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  182. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  183. AException: Exception);
  184. begin
  185. AResponse.SendFmt(
  186. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  187. [AException.Message], 'text/html; charset=utf-8', 500);
  188. end;
  189. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  190. begin
  191. UpdateControls;
  192. end;
  193. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  194. begin
  195. UpdateControls;
  196. end;
  197. procedure TfrMain.edPortChange(Sender: TObject);
  198. begin
  199. UpdateControls;
  200. end;
  201. {$PUSH}{$WARN 4055 OFF}
  202. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  203. AException: Exception);
  204. begin
  205. Application.QueueAsyncCall(DoError, PtrInt(NewStr(AException.Message)));
  206. end;
  207. {$POP}
  208. {$POP}
  209. end.