2
0

HTTPAuth_frMain.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 HTTPAuth_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. BrookHTTPAuthentication,
  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. function BrookHTTPServer1Authenticate(ASender: TObject;
  57. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  58. AResponse: TBrookHTTPResponse): Boolean;
  59. procedure BrookHTTPServer1AuthenticateError(ASender: TObject;
  60. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  61. AResponse: TBrookHTTPResponse; AException: Exception);
  62. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  63. procedure BrookHTTPServer1Request(ASender: TObject;
  64. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  65. procedure BrookHTTPServer1RequestError(ASender: TObject;
  66. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  67. AException: Exception);
  68. procedure BrookHTTPServer1Start(Sender: TObject);
  69. procedure BrookHTTPServer1Stop(Sender: TObject);
  70. procedure edPortChange(Sender: TObject);
  71. procedure lbLinkClick(Sender: TObject);
  72. procedure lbLinkMouseEnter(Sender: TObject);
  73. procedure lbLinkMouseLeave(Sender: TObject);
  74. protected
  75. procedure DoError(AData: PtrInt);
  76. public
  77. procedure UpdateControls; inline;
  78. end;
  79. var
  80. frMain: TfrMain;
  81. implementation
  82. {$R *.lfm}
  83. procedure TfrMain.DoError(AData: PtrInt);
  84. var
  85. S: PString absolute AData;
  86. begin
  87. try
  88. MessageDlg(S^, mtError, [mbOK], 0);
  89. finally
  90. DisposeStr(S);
  91. end;
  92. end;
  93. procedure TfrMain.UpdateControls;
  94. begin
  95. if BrookHTTPServer1.Active then
  96. edPort.Value := BrookHTTPServer1.Port
  97. else
  98. BrookHTTPServer1.Port := edPort.Value;
  99. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  100. acStart.Enabled := not BrookHTTPServer1.Active;
  101. acStop.Enabled := not acStart.Enabled;
  102. edPort.Enabled := acStart.Enabled;
  103. lbLink.Enabled := not acStart.Enabled;
  104. end;
  105. procedure TfrMain.acStartExecute(Sender: TObject);
  106. begin
  107. BrookHTTPServer1.Open;
  108. end;
  109. procedure TfrMain.acStopExecute(Sender: TObject);
  110. begin
  111. BrookHTTPServer1.Close;
  112. end;
  113. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  114. begin
  115. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  116. end;
  117. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  118. begin
  119. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  120. end;
  121. procedure TfrMain.lbLinkClick(Sender: TObject);
  122. begin
  123. OpenURL(lbLink.Caption);
  124. end;
  125. function TfrMain.BrookHTTPServer1Authenticate(ASender: TObject;
  126. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  127. AResponse: TBrookHTTPResponse): Boolean;
  128. begin
  129. AAuthentication.Credentials.Realm := 'My realm';
  130. Result := AAuthentication.Credentials.UserName.Equals('abc') and
  131. AAuthentication.Credentials.Password.Equals('123');
  132. if not Result then
  133. AAuthentication.Deny(
  134. '<html><head><title>Denied</title></head><body><font color="red">Go away</font></body></html>',
  135. 'text/html; charset=utf-8');
  136. end;
  137. procedure TfrMain.BrookHTTPServer1AuthenticateError(ASender: TObject;
  138. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  139. AResponse: TBrookHTTPResponse; AException: Exception);
  140. begin
  141. AAuthentication.Deny(
  142. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  143. [AException.Message], 'text/html; charset=utf-8');
  144. AAuthentication.Cancel;
  145. end;
  146. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  147. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  148. begin
  149. AResponse.Send(
  150. '<html><head><title>Secret</title></head><body><font color="green">Secret page</font></body></html>',
  151. 'text/html; charset=utf-8', 200);
  152. end;
  153. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  154. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  155. AException: Exception);
  156. begin
  157. AResponse.SendFmt(
  158. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  159. [AException.Message], 'text/html; charset=utf-8', 500);
  160. end;
  161. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  162. begin
  163. UpdateControls;
  164. end;
  165. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  166. begin
  167. UpdateControls;
  168. end;
  169. procedure TfrMain.edPortChange(Sender: TObject);
  170. begin
  171. UpdateControls;
  172. end;
  173. {$PUSH}{$WARN 4055 OFF}
  174. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  175. AException: Exception);
  176. begin
  177. Application.QueueAsyncCall(DoError, PtrInt(NewStr(AException.Message)));
  178. end;
  179. {$POP}
  180. {$POP}
  181. end.