| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- (* _ _
- * | |__ _ __ ___ ___ | | __
- * | '_ \| '__/ _ \ / _ \| |/ /
- * | |_) | | | (_) | (_) | <
- * |_.__/|_| \___/ \___/|_|\_\
- *
- * Microframework which helps to develop web Pascal applications.
- *
- * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
- *
- * Brook framework is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Brook framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Brook framework; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *)
- unit HTTPUpload_frMain;
- {$MODE DELPHI}
- interface
- uses
- SysUtils,
- Classes,
- StdCtrls,
- ActnList,
- Graphics,
- Spin,
- Dialogs,
- Forms,
- LCLIntf,
- BrookUtility,
- BrookStringMap,
- BrookHTTPUploads,
- BrookHTTPRequest,
- BrookHTTPResponse,
- BrookHTTPServer;
- type
- TfrMain = class(TForm)
- acStart: TAction;
- acStop: TAction;
- alMain: TActionList;
- BrookHTTPServer1: TBrookHTTPServer;
- btStart: TButton;
- btStop: TButton;
- edPort: TSpinEdit;
- lbLink: TLabel;
- lbPort: TLabel;
- procedure acStartExecute(Sender: TObject);
- procedure acStopExecute(Sender: TObject);
- procedure BrookHTTPServer1Request(ASender: TObject;
- ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
- procedure BrookHTTPServer1RequestError(ASender: TObject;
- ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
- AException: Exception);
- procedure BrookHTTPServer1Start(Sender: TObject);
- procedure BrookHTTPServer1Stop(Sender: TObject);
- procedure edPortChange(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure lbLinkClick(Sender: TObject);
- procedure lbLinkMouseEnter(Sender: TObject);
- procedure lbLinkMouseLeave(Sender: TObject);
- public
- procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
- end;
- const
- PAGE_FORM = Concat(
- '<html>',
- '<body>',
- '<form action="" method="post" enctype="multipart/form-data">',
- '<fieldset>',
- '<legend>Choose the files:</legend>',
- 'File 1: <input type="file" name="file1"/><br>',
- 'File 2: <input type="file" name="file2"/><br>',
- 'User 1: <input type="text" name="user1"/><br>',
- 'User 2: <input type="text" name="user2"/><br>',
- '<input type="submit"/>',
- '</fieldset>',
- '</form>',
- '</body>',
- '</html>'
- );
- PAGE_DONE = Concat(
- '<html>',
- '<head>',
- '<title>Uploads</title>',
- '</head>',
- '<body>',
- '<strong>Uploaded files:</strong><br>',
- '%s',
- '<strong>Users:</strong><br>',
- '%s',
- '</body>',
- '</html>'
- );
- CONTENT_TYPE = 'text/html; charset=utf-8';
- var
- frMain: TfrMain;
- implementation
- {$R *.lfm}
- procedure TfrMain.FormShow(Sender: TObject);
- begin
- if BrookHTTPServer1.UploadsDir.IsEmpty then
- BrookHTTPServer1.UploadsDir := Sagui.TmpDir;
- end;
- procedure TfrMain.UpdateControls;
- begin
- if BrookHTTPServer1.Active then
- edPort.Value := BrookHTTPServer1.Port
- else
- BrookHTTPServer1.Port := edPort.Value;
- lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
- acStart.Enabled := not BrookHTTPServer1.Active;
- acStop.Enabled := not acStart.Enabled;
- edPort.Enabled := acStart.Enabled;
- lbLink.Enabled := not acStart.Enabled;
- end;
- procedure TfrMain.acStartExecute(Sender: TObject);
- begin
- BrookHTTPServer1.Open;
- end;
- procedure TfrMain.acStopExecute(Sender: TObject);
- begin
- BrookHTTPServer1.Close;
- end;
- procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
- begin
- lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
- end;
- procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
- begin
- lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
- end;
- procedure TfrMain.lbLinkClick(Sender: TObject);
- begin
- OpenURL(lbLink.Caption);
- end;
- procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
- ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
- var
- VUpload: TBrookHTTPUpload;
- VField: TBrookStringPair;
- VFile, VFileList, VUsersList, VError: string;
- begin
- if ARequest.IsUploading then
- begin
- VFileList := '<ol>';
- for VUpload in ARequest.Uploads do
- if VUpload.Save(False, VError) then
- VFileList := Concat(VFileList, '<li><a href="?file=', VUpload.Name, '">',
- VUpload.Name, '</a></li>')
- else
- VFileList := Concat(VFileList, '<li><font color="red">', VUpload.Name,
- ' - failed - ', VError, '</font></li>');
- VFileList := Concat(VFileList, '</ol>');
- VUsersList := '<ol>';
- for VField in ARequest.Fields do
- VUsersList := Concat(VUsersList, '<li>', VField.Value, '</li>');
- VUsersList := Concat(VUsersList, '</ol>');
- AResponse.SendFmt(PAGE_DONE, [VFileList, VUsersList], CONTENT_TYPE, 200);
- end
- else
- begin
- if ARequest.Params.TryValue('file', VFile) then
- AResponse.Download(
- Concat(BrookHTTPServer1.UploadsDir, PathDelim, VFile))
- else
- AResponse.Send(PAGE_FORM, CONTENT_TYPE, 200);
- end;
- end;
- procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
- ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
- AException: Exception);
- begin
- AResponse.SendFmt(
- '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
- [AException.Message], 'text/html; charset=utf-8', 500);
- end;
- procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
- begin
- UpdateControls;
- end;
- procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
- begin
- UpdateControls;
- end;
- procedure TfrMain.edPortChange(Sender: TObject);
- begin
- UpdateControls;
- end;
- end.
|