URLRouter_frMain.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 URLRouter_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. BrookHTTPRequest,
  40. BrookHTTPResponse,
  41. BrookHTTPServer,
  42. BrookURLRouter;
  43. type
  44. TfrMain = class(TForm)
  45. acStart: TAction;
  46. acStop: TAction;
  47. alMain: TActionList;
  48. BrookURLRouter1: TBrookURLRouter;
  49. BrookHTTPServer1: TBrookHTTPServer;
  50. btStart: TButton;
  51. btStop: TButton;
  52. edPort: TSpinEdit;
  53. lbLink: TLabel;
  54. lbPort: TLabel;
  55. procedure acStartExecute(Sender: TObject);
  56. procedure acStopExecute(Sender: TObject);
  57. procedure BrookURLRouter1NotFound(ASender: TObject; const ARoute: string;
  58. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  59. procedure BrookURLRouter1Routes0Request(ASender: TObject;
  60. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  61. AResponse: TBrookHTTPResponse);
  62. procedure BrookURLRouter1Routes1Request(ASender: TObject;
  63. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  64. AResponse: TBrookHTTPResponse);
  65. procedure BrookURLRouter1Routes2Request(ASender: TObject;
  66. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  67. AResponse: TBrookHTTPResponse);
  68. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  69. procedure BrookHTTPServer1Request(ASender: TObject;
  70. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  71. procedure BrookHTTPServer1RequestError(ASender: TObject;
  72. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  73. AException: Exception);
  74. procedure BrookHTTPServer1Start(Sender: TObject);
  75. procedure BrookHTTPServer1Stop(Sender: TObject);
  76. procedure edPortChange(Sender: TObject);
  77. procedure lbLinkClick(Sender: TObject);
  78. procedure lbLinkMouseEnter(Sender: TObject);
  79. procedure lbLinkMouseLeave(Sender: TObject);
  80. protected
  81. procedure DoError(AData: PtrInt);
  82. public
  83. procedure UpdateControls; inline;
  84. end;
  85. var
  86. frMain: TfrMain;
  87. implementation
  88. {$R *.lfm}
  89. procedure TfrMain.DoError(AData: PtrInt);
  90. var
  91. S: PString absolute AData;
  92. begin
  93. try
  94. MessageDlg(S^, mtError, [mbOK], 0);
  95. finally
  96. DisposeStr(S);
  97. end;
  98. end;
  99. procedure TfrMain.UpdateControls;
  100. begin
  101. if BrookHTTPServer1.Active then
  102. edPort.Value := BrookHTTPServer1.Port
  103. else
  104. BrookHTTPServer1.Port := edPort.Value;
  105. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  106. acStart.Enabled := not BrookHTTPServer1.Active;
  107. acStop.Enabled := not acStart.Enabled;
  108. edPort.Enabled := acStart.Enabled;
  109. lbLink.Enabled := not acStart.Enabled;
  110. end;
  111. procedure TfrMain.acStartExecute(Sender: TObject);
  112. begin
  113. BrookURLRouter1.Open;
  114. BrookHTTPServer1.Open;
  115. end;
  116. procedure TfrMain.acStopExecute(Sender: TObject);
  117. begin
  118. BrookHTTPServer1.Close;
  119. end;
  120. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  121. begin
  122. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  123. end;
  124. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  125. begin
  126. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  127. end;
  128. procedure TfrMain.lbLinkClick(Sender: TObject);
  129. begin
  130. OpenURL(lbLink.Caption);
  131. end;
  132. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  133. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  134. begin
  135. BrookURLRouter1.Route(ASender, ARequest, AResponse);
  136. end;
  137. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  138. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  139. AException: Exception);
  140. begin
  141. AResponse.SendFmt(
  142. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  143. [AException.Message], 'text/html; charset=utf-8', 500);
  144. end;
  145. procedure TfrMain.BrookURLRouter1NotFound(ASender: TObject;
  146. const ARoute: string; ARequest: TBrookHTTPRequest;
  147. AResponse: TBrookHTTPResponse);
  148. begin
  149. AResponse.SendFmt(
  150. '<html><head><title>Not found</title></head><body>Page not found: %s</body></html>',
  151. [ARequest.Path], 'text/html; charset=utf-8', 404);
  152. end;
  153. procedure TfrMain.BrookURLRouter1Routes0Request(ASender: TObject;
  154. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  155. AResponse: TBrookHTTPResponse);
  156. begin
  157. AResponse.Send(
  158. '<html><head><title>Home page</title></head><body>Home page</body></html>',
  159. 'text/html; charset=utf-8', 200);
  160. end;
  161. procedure TfrMain.BrookURLRouter1Routes1Request(ASender: TObject;
  162. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  163. AResponse: TBrookHTTPResponse);
  164. begin
  165. AResponse.SendFmt(
  166. '<html><head><title>Downloads</title></head><body>Downloaded file: %s</body></html>',
  167. [ARoute.Variables['file']], 'text/html; charset=utf-8', 200);
  168. end;
  169. procedure TfrMain.BrookURLRouter1Routes2Request(ASender: TObject;
  170. ARoute: TBrookURLRoute; ARequest: TBrookHTTPRequest;
  171. AResponse: TBrookHTTPResponse);
  172. begin
  173. AResponse.SendFmt(
  174. '<html><head><title>Page</title></head><body>Page number: %d</body></html>',
  175. [ARoute.Segments[0].ToInteger], 'text/html; charset=utf-8', 200);
  176. end;
  177. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  178. begin
  179. UpdateControls;
  180. end;
  181. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  182. begin
  183. UpdateControls;
  184. end;
  185. procedure TfrMain.edPortChange(Sender: TObject);
  186. begin
  187. UpdateControls;
  188. end;
  189. {$PUSH}{$WARN 4055 OFF}
  190. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  191. AException: Exception);
  192. begin
  193. Application.QueueAsyncCall(DoError, PtrInt(NewStr(AException.Message)));
  194. end;
  195. {$POP}
  196. {$POP}
  197. end.