HTTPUpload_frMain.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. BrookHTTPUploads,
  40. BrookHTTPRequest,
  41. BrookHTTPResponse,
  42. BrookHTTPServer;
  43. type
  44. TfrMain = class(TForm)
  45. acStart: TAction;
  46. acStop: TAction;
  47. alMain: TActionList;
  48. BrookHTTPServer1: TBrookHTTPServer;
  49. btStart: TButton;
  50. btStop: TButton;
  51. edPort: TSpinEdit;
  52. lbLink: TLabel;
  53. lbPort: TLabel;
  54. procedure acStartExecute(Sender: TObject);
  55. procedure acStopExecute(Sender: TObject);
  56. procedure BrookHTTPServer1Request(ASender: TObject;
  57. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  58. procedure BrookHTTPServer1RequestError(ASender: TObject;
  59. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  60. AException: Exception);
  61. procedure BrookHTTPServer1Start(Sender: TObject);
  62. procedure BrookHTTPServer1Stop(Sender: TObject);
  63. procedure edPortChange(Sender: TObject);
  64. procedure FormShow(Sender: TObject);
  65. procedure lbLinkClick(Sender: TObject);
  66. procedure lbLinkMouseEnter(Sender: TObject);
  67. procedure lbLinkMouseLeave(Sender: TObject);
  68. public
  69. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  70. end;
  71. const
  72. PAGE_FORM = Concat(
  73. '<html>',
  74. '<body>',
  75. '<form action="" method="post" enctype="multipart/form-data">',
  76. '<fieldset>',
  77. '<legend>Choose the files:</legend>',
  78. 'File 1: <input type="file" name="file1"/><br>',
  79. 'File 2: <input type="file" name="file2"/><br>',
  80. '<input type="submit"/>',
  81. '</fieldset>',
  82. '</form>',
  83. '</body>',
  84. '</html>'
  85. );
  86. PAGE_DONE = Concat(
  87. '<html>',
  88. '<head>',
  89. '<title>Uploads</title>',
  90. '</head>',
  91. '<body>',
  92. '<strong>Uploaded files:</strong><br>',
  93. '%s',
  94. '</body>',
  95. '</html>'
  96. );
  97. CONTENT_TYPE = 'text/html; charset=utf-8';
  98. var
  99. frMain: TfrMain;
  100. implementation
  101. {$R *.lfm}
  102. procedure TfrMain.FormShow(Sender: TObject);
  103. begin
  104. if BrookHTTPServer1.UploadsDir.IsEmpty then
  105. BrookHTTPServer1.UploadsDir := Sagui.TmpDir;
  106. end;
  107. procedure TfrMain.UpdateControls;
  108. begin
  109. if BrookHTTPServer1.Active then
  110. edPort.Value := BrookHTTPServer1.Port
  111. else
  112. BrookHTTPServer1.Port := edPort.Value;
  113. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  114. acStart.Enabled := not BrookHTTPServer1.Active;
  115. acStop.Enabled := not acStart.Enabled;
  116. edPort.Enabled := acStart.Enabled;
  117. lbLink.Enabled := not acStart.Enabled;
  118. end;
  119. procedure TfrMain.acStartExecute(Sender: TObject);
  120. begin
  121. BrookHTTPServer1.Open;
  122. end;
  123. procedure TfrMain.acStopExecute(Sender: TObject);
  124. begin
  125. BrookHTTPServer1.Close;
  126. end;
  127. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  128. begin
  129. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  130. end;
  131. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  132. begin
  133. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  134. end;
  135. procedure TfrMain.lbLinkClick(Sender: TObject);
  136. begin
  137. OpenURL(lbLink.Caption);
  138. end;
  139. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  140. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  141. var
  142. VUpload: TBrookHTTPUpload;
  143. VFile, VList, VError: string;
  144. begin
  145. if ARequest.IsUploading then
  146. begin
  147. VList := '<ol>';
  148. for VUpload in ARequest.Uploads do
  149. if VUpload.Save(False, VError) then
  150. VList := Concat(VList, '<li><a href="?file=', VUpload.Name, '">',
  151. VUpload.Name, '</a></li>')
  152. else
  153. VList := Concat(VList, '<li><font color="red">', VUpload.Name,
  154. ' - failed - ', VError, '</font></li>');
  155. VList := Concat(VList, '</ol>');
  156. AResponse.SendFmt(PAGE_DONE, [VList], CONTENT_TYPE, 200);
  157. end
  158. else
  159. begin
  160. if ARequest.Params.TryValue('file', VFile) then
  161. AResponse.Download(
  162. Concat(BrookHTTPServer1.UploadsDir, PathDelim, VFile))
  163. else
  164. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  165. end;
  166. end;
  167. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  168. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  169. AException: Exception);
  170. begin
  171. AResponse.SendFmt(
  172. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  173. [AException.Message], 'text/html; charset=utf-8', 500);
  174. end;
  175. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  176. begin
  177. UpdateControls;
  178. end;
  179. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  180. begin
  181. UpdateControls;
  182. end;
  183. procedure TfrMain.edPortChange(Sender: TObject);
  184. begin
  185. UpdateControls;
  186. end;
  187. end.