HTTPAuth_frMain.pas 5.4 KB

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