HTTPAuth_frMain.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 HTTPAuth_frMain;
  26. interface
  27. uses
  28. System.SysUtils,
  29. System.UITypes,
  30. System.Classes,
  31. System.Actions,
  32. FMX.Types,
  33. FMX.ActnList,
  34. FMX.Graphics,
  35. FMX.Controls,
  36. FMX.StdCtrls,
  37. FMX.Edit,
  38. FMX.EditBox,
  39. FMX.NumberBox,
  40. FMX.DialogService,
  41. FMX.Forms,
  42. FMX.Controls.Presentation,
  43. BrookHandledClasses,
  44. BrookHTTPAuthentication,
  45. BrookHTTPRequest,
  46. BrookHTTPResponse,
  47. BrookHTTPServer,
  48. Utility;
  49. type
  50. TfrMain = class(TForm)
  51. lbPort: TLabel;
  52. edPort: TNumberBox;
  53. btStart: TButton;
  54. btStop: TButton;
  55. lbLink: TLabel;
  56. alMain: TActionList;
  57. acStart: TAction;
  58. acStop: TAction;
  59. BrookHTTPServer1: TBrookHTTPServer;
  60. procedure acStartExecute(Sender: TObject);
  61. procedure acStopExecute(Sender: TObject);
  62. procedure lbLinkMouseEnter(Sender: TObject);
  63. procedure lbLinkMouseLeave(Sender: TObject);
  64. procedure lbLinkClick(Sender: TObject);
  65. procedure BrookHTTPServer1Request(ASender: TObject;
  66. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  67. procedure BrookHTTPServer1RequestError(ASender: TObject;
  68. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  69. AException: Exception);
  70. function BrookHTTPServer1Authenticate(ASender: TObject;
  71. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  72. AResponse: TBrookHTTPResponse): Boolean;
  73. procedure BrookHTTPServer1AuthenticateError(ASender: TObject;
  74. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  75. AResponse: TBrookHTTPResponse; AException: Exception);
  76. procedure BrookHTTPServer1Start(Sender: TObject);
  77. procedure BrookHTTPServer1Stop(Sender: TObject);
  78. procedure edPortChange(Sender: TObject);
  79. procedure edPortChangeTracking(Sender: TObject);
  80. public
  81. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  82. end;
  83. var
  84. frMain: TfrMain;
  85. implementation
  86. {$R *.fmx}
  87. procedure TfrMain.UpdateControls;
  88. begin
  89. if Application.Terminated then
  90. Exit;
  91. if BrookHTTPServer1.Active then
  92. edPort.Value := BrookHTTPServer1.Port
  93. else
  94. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  95. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  96. acStart.Enabled := not BrookHTTPServer1.Active;
  97. acStop.Enabled := not acStart.Enabled;
  98. edPort.Enabled := acStart.Enabled;
  99. lbLink.Enabled := not acStart.Enabled;
  100. end;
  101. procedure TfrMain.acStartExecute(Sender: TObject);
  102. begin
  103. BrookHTTPServer1.Open;
  104. end;
  105. procedure TfrMain.acStopExecute(Sender: TObject);
  106. begin
  107. BrookHTTPServer1.Close;
  108. end;
  109. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  110. begin
  111. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  112. end;
  113. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  114. begin
  115. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  116. end;
  117. procedure TfrMain.lbLinkClick(Sender: TObject);
  118. begin
  119. OpenURL(lbLink.Text);
  120. end;
  121. function TfrMain.BrookHTTPServer1Authenticate(ASender: TObject;
  122. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  123. AResponse: TBrookHTTPResponse): Boolean;
  124. begin
  125. AAuthentication.Credentials.Realm := 'My realm';
  126. Result := AAuthentication.Credentials.UserName.Equals('abc') and
  127. AAuthentication.Credentials.Password.Equals('123');
  128. if not Result then
  129. AAuthentication.Deny(
  130. '<html><head><title>Denied</title></head><body><font color="red">Go away</font></body></html>',
  131. 'text/html; charset=utf-8');
  132. end;
  133. procedure TfrMain.BrookHTTPServer1AuthenticateError(ASender: TObject;
  134. AAuthentication: TBrookHTTPAuthentication; ARequest: TBrookHTTPRequest;
  135. AResponse: TBrookHTTPResponse; AException: Exception);
  136. begin
  137. AAuthentication.Deny(
  138. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  139. [AException.Message], 'text/html; charset=utf-8');
  140. AAuthentication.Cancel;
  141. end;
  142. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  143. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  144. begin
  145. AResponse.Send(
  146. '<html><head><title>Secret</title></head><body><font color="green">Secret page</font></body></html>',
  147. 'text/html; charset=utf-8', 200);
  148. end;
  149. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  150. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  151. AException: Exception);
  152. begin
  153. AResponse.SendFmt(
  154. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  155. [AException.Message], 'text/html; charset=utf-8', 500);
  156. end;
  157. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  158. begin
  159. UpdateControls;
  160. end;
  161. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  162. begin
  163. UpdateControls;
  164. end;
  165. procedure TfrMain.edPortChange(Sender: TObject);
  166. begin
  167. UpdateControls;
  168. end;
  169. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  170. begin
  171. UpdateControls;
  172. end;
  173. end.