HTTPUpload_frMain.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 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. interface
  28. uses
  29. SysUtils,
  30. Classes,
  31. StdCtrls,
  32. ActnList,
  33. Graphics,
  34. Spin,
  35. Dialogs,
  36. Forms,
  37. LCLIntf,
  38. BrookUtility,
  39. BrookStringMap,
  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 BrookHTTPServer1Request(ASender: TObject;
  58. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  59. procedure BrookHTTPServer1RequestError(ASender: TObject;
  60. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  61. AException: Exception);
  62. procedure BrookHTTPServer1Start(Sender: TObject);
  63. procedure BrookHTTPServer1Stop(Sender: TObject);
  64. procedure edPortChange(Sender: TObject);
  65. procedure FormShow(Sender: TObject);
  66. procedure lbLinkClick(Sender: TObject);
  67. procedure lbLinkMouseEnter(Sender: TObject);
  68. procedure lbLinkMouseLeave(Sender: TObject);
  69. public
  70. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  71. end;
  72. const
  73. PAGE_FORM = Concat(
  74. '<html>',
  75. '<body>',
  76. '<form action="" method="post" enctype="multipart/form-data">',
  77. '<fieldset>',
  78. '<legend>Choose the files:</legend>',
  79. 'File 1: <input type="file" name="file1"/><br>',
  80. 'File 2: <input type="file" name="file2"/><br>',
  81. 'User 1: <input type="text" name="user1"/><br>',
  82. 'User 2: <input type="text" name="user2"/><br>',
  83. '<input type="submit"/>',
  84. '</fieldset>',
  85. '</form>',
  86. '</body>',
  87. '</html>'
  88. );
  89. PAGE_DONE = Concat(
  90. '<html>',
  91. '<head>',
  92. '<title>Uploads</title>',
  93. '</head>',
  94. '<body>',
  95. '<strong>Uploaded files:</strong><br>',
  96. '%s',
  97. '<strong>Users:</strong><br>',
  98. '%s',
  99. '</body>',
  100. '</html>'
  101. );
  102. CONTENT_TYPE = 'text/html; charset=utf-8';
  103. var
  104. frMain: TfrMain;
  105. implementation
  106. {$R *.lfm}
  107. procedure TfrMain.FormShow(Sender: TObject);
  108. begin
  109. if BrookHTTPServer1.UploadsDir.IsEmpty then
  110. BrookHTTPServer1.UploadsDir := Sagui.TmpDir;
  111. end;
  112. procedure TfrMain.UpdateControls;
  113. begin
  114. if BrookHTTPServer1.Active then
  115. edPort.Value := BrookHTTPServer1.Port
  116. else
  117. BrookHTTPServer1.Port := edPort.Value;
  118. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  119. acStart.Enabled := not BrookHTTPServer1.Active;
  120. acStop.Enabled := not acStart.Enabled;
  121. edPort.Enabled := acStart.Enabled;
  122. lbLink.Enabled := not acStart.Enabled;
  123. end;
  124. procedure TfrMain.acStartExecute(Sender: TObject);
  125. begin
  126. BrookHTTPServer1.Open;
  127. end;
  128. procedure TfrMain.acStopExecute(Sender: TObject);
  129. begin
  130. BrookHTTPServer1.Close;
  131. end;
  132. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  133. begin
  134. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  135. end;
  136. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  137. begin
  138. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  139. end;
  140. procedure TfrMain.lbLinkClick(Sender: TObject);
  141. begin
  142. OpenURL(lbLink.Caption);
  143. end;
  144. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  145. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  146. var
  147. VUpload: TBrookHTTPUpload;
  148. VField: TBrookStringPair;
  149. VFile, VFileList, VUsersList, VError: string;
  150. begin
  151. if ARequest.IsUploading then
  152. begin
  153. VFileList := '<ol>';
  154. for VUpload in ARequest.Uploads do
  155. if VUpload.Save(False, VError) then
  156. VFileList := Concat(VFileList, '<li><a href="?file=', VUpload.Name, '">',
  157. VUpload.Name, '</a></li>')
  158. else
  159. VFileList := Concat(VFileList, '<li><font color="red">', VUpload.Name,
  160. ' - failed - ', VError, '</font></li>');
  161. VFileList := Concat(VFileList, '</ol>');
  162. VUsersList := '<ol>';
  163. for VField in ARequest.Fields do
  164. VUsersList := Concat(VUsersList, '<li>', VField.Value, '</li>');
  165. VUsersList := Concat(VUsersList, '</ol>');
  166. AResponse.SendFmt(PAGE_DONE, [VFileList, VUsersList], CONTENT_TYPE, 200);
  167. end
  168. else
  169. begin
  170. if ARequest.Params.TryValue('file', VFile) then
  171. AResponse.Download(
  172. Concat(BrookHTTPServer1.UploadsDir, PathDelim, VFile))
  173. else
  174. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  175. end;
  176. end;
  177. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  178. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  179. AException: Exception);
  180. begin
  181. AResponse.SendFmt(
  182. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  183. [AException.Message], 'text/html; charset=utf-8', 500);
  184. end;
  185. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  186. begin
  187. UpdateControls;
  188. end;
  189. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  190. begin
  191. UpdateControls;
  192. end;
  193. procedure TfrMain.edPortChange(Sender: TObject);
  194. begin
  195. UpdateControls;
  196. end;
  197. end.