HTTPCookie_frMain.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 HTTPCookie_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. const
  69. EMPTY_FAVICON = '<link rel="icon" href="data:,">';
  70. CONTENT_TYPE = 'text/html; charset=utf-8';
  71. INITIAL_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Use F5 to refresh this page ...</body></html>');
  72. COUNT_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Refresh count: %d</body></html>');
  73. COOKIE_NAME = 'refresh_count';
  74. var
  75. frMain: TfrMain;
  76. implementation
  77. {$R *.lfm}
  78. procedure TfrMain.UpdateControls;
  79. begin
  80. if BrookHTTPServer1.Active then
  81. edPort.Value := BrookHTTPServer1.Port
  82. else
  83. BrookHTTPServer1.Port := edPort.Value;
  84. lbLink.Caption := Concat('http://localhost:', edPort.Value.ToString);
  85. acStart.Enabled := not BrookHTTPServer1.Active;
  86. acStop.Enabled := not acStart.Enabled;
  87. edPort.Enabled := acStart.Enabled;
  88. lbLink.Enabled := not acStart.Enabled;
  89. end;
  90. procedure TfrMain.acStartExecute(Sender: TObject);
  91. begin
  92. BrookHTTPServer1.Open;
  93. end;
  94. procedure TfrMain.acStopExecute(Sender: TObject);
  95. begin
  96. BrookHTTPServer1.Close;
  97. end;
  98. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  99. begin
  100. lbLink.Font.Style := lbLink.Font.Style + [fsUnderline];
  101. end;
  102. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  103. begin
  104. lbLink.Font.Style := lbLink.Font.Style - [fsUnderline];
  105. end;
  106. procedure TfrMain.lbLinkClick(Sender: TObject);
  107. begin
  108. OpenURL(lbLink.Caption);
  109. end;
  110. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  111. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  112. var
  113. VCount: Integer;
  114. begin
  115. if ARequest.Cookies.IsEmpty then
  116. VCount := 0
  117. else
  118. VCount := StrToIntDef(ARequest.Cookies.Get(COOKIE_NAME), 0);
  119. if VCount = 0 then
  120. begin
  121. AResponse.Send(INITIAL_PAGE, CONTENT_TYPE, 200);
  122. VCount := 1;
  123. end
  124. else
  125. begin
  126. AResponse.SendFmt(COUNT_PAGE, [VCount], CONTENT_TYPE, 200);
  127. Inc(VCount);
  128. end;
  129. AResponse.SetCookie(COOKIE_NAME, VCount.ToString);
  130. end;
  131. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  132. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  133. AException: Exception);
  134. begin
  135. AResponse.SendFmt(
  136. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  137. [AException.Message], 'text/html; charset=utf-8', 500);
  138. end;
  139. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  140. begin
  141. UpdateControls;
  142. end;
  143. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  144. begin
  145. UpdateControls;
  146. end;
  147. procedure TfrMain.edPortChange(Sender: TObject);
  148. begin
  149. UpdateControls;
  150. end;
  151. end.