HTTPCookie_frMain.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 HTTPCookie_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. 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. procedure BrookHTTPServer1Error(ASender: TObject; AException: Exception);
  56. procedure BrookHTTPServer1Request(ASender: TObject;
  57. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  58. procedure BrookHTTPServer1RequestError(ASender: TObject;
  59. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  60. AException: Exception);
  61. procedure BrookHTTPServer1Start(Sender: TObject);
  62. procedure BrookHTTPServer1Stop(Sender: TObject);
  63. procedure edPortChange(Sender: TObject);
  64. procedure lbLinkClick(Sender: TObject);
  65. procedure lbLinkMouseEnter(Sender: TObject);
  66. procedure lbLinkMouseLeave(Sender: TObject);
  67. protected
  68. procedure DoError(AData: PtrInt);
  69. public
  70. procedure UpdateControls; inline;
  71. end;
  72. const
  73. EMPTY_FAVICON = '<link rel="icon" href="data:,">';
  74. CONTENT_TYPE = 'text/html; charset=utf-8';
  75. INITIAL_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Use F5 to refresh this page ...</body></html>');
  76. COUNT_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Refresh count: %d</body></html>');
  77. COOKIE_NAME = 'refresh_count';
  78. var
  79. frMain: TfrMain;
  80. implementation
  81. {$R *.lfm}
  82. procedure TfrMain.DoError(AData: PtrInt);
  83. var
  84. S: PString absolute AData;
  85. begin
  86. try
  87. MessageDlg(S^, mtError, [mbOK], 0);
  88. finally
  89. DisposeStr(S);
  90. end;
  91. end;
  92. procedure TfrMain.UpdateControls;
  93. begin
  94. if BrookHTTPServer1.Active then
  95. edPort.Value := BrookHTTPServer1.Port
  96. else
  97. BrookHTTPServer1.Port := edPort.Value;
  98. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  99. acStart.Enabled := not BrookHTTPServer1.Active;
  100. acStop.Enabled := not acStart.Enabled;
  101. edPort.Enabled := acStart.Enabled;
  102. lbLink.Enabled := not acStart.Enabled;
  103. end;
  104. procedure TfrMain.acStartExecute(Sender: TObject);
  105. begin
  106. BrookHTTPServer1.Open;
  107. end;
  108. procedure TfrMain.acStopExecute(Sender: TObject);
  109. begin
  110. BrookHTTPServer1.Close;
  111. end;
  112. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  113. begin
  114. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  115. end;
  116. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  117. begin
  118. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  119. end;
  120. procedure TfrMain.lbLinkClick(Sender: TObject);
  121. begin
  122. OpenURL(lbLink.Caption);
  123. end;
  124. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  125. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  126. var
  127. VCount: Integer;
  128. begin
  129. if ARequest.Cookies.IsEmpty then
  130. VCount := 0
  131. else
  132. VCount := StrToIntDef(ARequest.Cookies.Get(COOKIE_NAME), 0);
  133. if VCount = 0 then
  134. begin
  135. AResponse.Send(INITIAL_PAGE, CONTENT_TYPE, 200);
  136. VCount := 1;
  137. end
  138. else
  139. begin
  140. AResponse.SendFmt(COUNT_PAGE, [VCount], CONTENT_TYPE, 200);
  141. Inc(VCount);
  142. end;
  143. AResponse.SetCookie(COOKIE_NAME, VCount.ToString);
  144. end;
  145. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  146. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  147. AException: Exception);
  148. begin
  149. AResponse.SendFmt(
  150. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  151. [AException.Message], 'text/html; charset=utf-8', 500);
  152. end;
  153. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  154. begin
  155. UpdateControls;
  156. end;
  157. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  158. begin
  159. UpdateControls;
  160. end;
  161. procedure TfrMain.edPortChange(Sender: TObject);
  162. begin
  163. UpdateControls;
  164. end;
  165. {$PUSH}{$WARN 4055 OFF}
  166. procedure TfrMain.BrookHTTPServer1Error(ASender: TObject;
  167. AException: Exception);
  168. begin
  169. Application.QueueAsyncCall(DoError, PtrInt(NewStr(AException.Message)));
  170. end;
  171. {$POP}
  172. {$POP}
  173. end.