httpuploads.dpr 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. program httpuploads;
  26. {$IFDEF MSWINDOWS}
  27. {$APPTYPE CONSOLE}
  28. {$ENDIF}
  29. uses
  30. SysUtils,
  31. IOUtils,
  32. BrookUtility,
  33. BrookStringMap,
  34. BrookHTTPUploads,
  35. BrookHTTPRequest,
  36. BrookHTTPResponse,
  37. BrookHTTPServer;
  38. const
  39. PAGE_FORM = Concat(
  40. '<html>',
  41. '<body>',
  42. '<form action="" method="post" enctype="multipart/form-data">',
  43. '<fieldset>',
  44. '<legend>Choose the files:</legend>',
  45. 'File 1: <input type="file" name="file1"/><br>',
  46. 'File 2: <input type="file" name="file2"/><br>',
  47. 'User 1: <input type="text" name="user1"/><br>',
  48. 'User 2: <input type="text" name="user2"/><br>',
  49. '<input type="submit"/>',
  50. '</fieldset>',
  51. '</form>',
  52. '</body>',
  53. '</html>'
  54. );
  55. PAGE_DONE = Concat(
  56. '<html>',
  57. '<head>',
  58. '<title>Uploads</title>',
  59. '</head>',
  60. '<body>',
  61. '<strong>Uploaded files:</strong><br>',
  62. '%s',
  63. '<strong>Users:</strong><br>',
  64. '%s',
  65. '</body>',
  66. '</html>'
  67. );
  68. CONTENT_TYPE = 'text/html; charset=utf-8';
  69. type
  70. THTTPServer = class(TBrookHTTPServer)
  71. protected
  72. procedure DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  73. AResponse: TBrookHTTPResponse); override;
  74. end;
  75. procedure THTTPServer.DoRequest(ASender: TObject; ARequest: TBrookHTTPRequest;
  76. AResponse: TBrookHTTPResponse);
  77. var
  78. VUpload: TBrookHTTPUpload;
  79. VField: TBrookStringPair;
  80. VFile, VFileList, VUsersList, VError: string;
  81. begin
  82. if ARequest.IsUploading then
  83. begin
  84. VFileList := '<ol>';
  85. for VUpload in ARequest.Uploads do
  86. if VUpload.Save(False, VError) then
  87. VFileList := Concat(VFileList, '<li><a href="?file=', VUpload.Name, '">',
  88. VUpload.Name, '</a></li>')
  89. else
  90. VFileList := Concat(VFileList, '<li><font color="red">', VUpload.Name,
  91. ' - failed - ', VError, '</font></li>');
  92. VFileList := Concat(VFileList, '</ol>');
  93. VUsersList := '<ol>';
  94. for VField in ARequest.Fields do
  95. VUsersList := Concat(VUsersList, '<li>', VField.Value, '</li>');
  96. VUsersList := Concat(VUsersList, '</ol>');
  97. AResponse.SendFmt(PAGE_DONE, [VFileList, VUsersList], CONTENT_TYPE, 200);
  98. end
  99. else
  100. begin
  101. if ARequest.Params.TryValue('file', VFile) then
  102. AResponse.Download(Concat(UploadsDir, PathDelim, VFile))
  103. else
  104. AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
  105. end;
  106. end;
  107. begin
  108. with THTTPServer.Create(nil) do
  109. try
  110. UploadsDir := TPath.Combine(Sagui.TmpDir, 'uploads');
  111. NoFavicon := True;
  112. Open;
  113. if not Active then
  114. Exit;
  115. ForceDirectories(UploadsDir);
  116. WriteLn('Server running at http://localhost:', Port);
  117. ReadLn;
  118. finally
  119. Free;
  120. end;
  121. end.