HTTPServer_frMain.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 HTTPServer_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. BrookHTTPRequest,
  39. BrookHTTPResponse,
  40. BrookHTTPServer;
  41. type
  42. TfrMain = class(TForm)
  43. acStart: TAction;
  44. acStop: TAction;
  45. alMain: TActionList;
  46. BrookHTTPServer1: TBrookHTTPServer;
  47. btStart: TButton;
  48. btStop: TButton;
  49. edPort: TSpinEdit;
  50. lbLink: TLabel;
  51. lbPort: TLabel;
  52. procedure acStartExecute(Sender: TObject);
  53. procedure acStopExecute(Sender: TObject);
  54. procedure BrookHTTPServer1Request(ASender: TObject;
  55. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  56. procedure BrookHTTPServer1RequestError(ASender: TObject;
  57. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  58. AException: Exception);
  59. procedure BrookHTTPServer1Start(Sender: TObject);
  60. procedure BrookHTTPServer1Stop(Sender: TObject);
  61. procedure edPortChange(Sender: TObject);
  62. procedure lbLinkClick(Sender: TObject);
  63. procedure lbLinkMouseEnter(Sender: TObject);
  64. procedure lbLinkMouseLeave(Sender: TObject);
  65. public
  66. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  67. end;
  68. var
  69. frMain: TfrMain;
  70. implementation
  71. {$R *.lfm}
  72. procedure TfrMain.UpdateControls;
  73. begin
  74. if BrookHTTPServer1.Active then
  75. edPort.Value := BrookHTTPServer1.Port
  76. else
  77. BrookHTTPServer1.Port := edPort.Value;
  78. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  79. acStart.Enabled := not BrookHTTPServer1.Active;
  80. acStop.Enabled := not acStart.Enabled;
  81. edPort.Enabled := acStart.Enabled;
  82. lbLink.Enabled := not acStart.Enabled;
  83. end;
  84. procedure TfrMain.acStartExecute(Sender: TObject);
  85. begin
  86. BrookHTTPServer1.Open;
  87. end;
  88. procedure TfrMain.acStopExecute(Sender: TObject);
  89. begin
  90. BrookHTTPServer1.Close;
  91. end;
  92. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  93. begin
  94. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  95. end;
  96. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  97. begin
  98. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  99. end;
  100. procedure TfrMain.lbLinkClick(Sender: TObject);
  101. begin
  102. OpenURL(lbLink.Caption);
  103. end;
  104. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  105. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  106. begin
  107. AResponse.Send(
  108. '<html><head><title>Hello world</title></head><body>Hello world</body></html>',
  109. 'text/html; charset=utf-8', 200);
  110. end;
  111. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  112. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  113. AException: Exception);
  114. begin
  115. AResponse.SendFmt(
  116. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  117. [AException.Message], 'text/html; charset=utf-8', 500);
  118. end;
  119. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  120. begin
  121. UpdateControls;
  122. end;
  123. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  124. begin
  125. UpdateControls;
  126. end;
  127. procedure TfrMain.edPortChange(Sender: TObject);
  128. begin
  129. UpdateControls;
  130. end;
  131. end.