HTTPCookie_frMain.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. interface
  27. uses
  28. System.SysUtils,
  29. System.UITypes,
  30. System.Classes,
  31. System.Actions,
  32. FMX.Types,
  33. FMX.ActnList,
  34. FMX.Graphics,
  35. FMX.Controls,
  36. FMX.StdCtrls,
  37. FMX.Edit,
  38. FMX.EditBox,
  39. FMX.NumberBox,
  40. FMX.DialogService,
  41. FMX.Forms,
  42. FMX.Controls.Presentation,
  43. BrookHandledClasses,
  44. BrookHTTPRequest,
  45. BrookHTTPResponse,
  46. BrookHTTPServer,
  47. Utility;
  48. type
  49. TfrMain = class(TForm)
  50. lbPort: TLabel;
  51. edPort: TNumberBox;
  52. btStart: TButton;
  53. btStop: TButton;
  54. lbLink: TLabel;
  55. alMain: TActionList;
  56. acStart: TAction;
  57. acStop: TAction;
  58. BrookHTTPServer1: TBrookHTTPServer;
  59. procedure acStartExecute(Sender: TObject);
  60. procedure acStopExecute(Sender: TObject);
  61. procedure lbLinkMouseEnter(Sender: TObject);
  62. procedure lbLinkMouseLeave(Sender: TObject);
  63. procedure lbLinkClick(Sender: TObject);
  64. procedure BrookHTTPServer1Request(ASender: TObject;
  65. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  66. procedure BrookHTTPServer1RequestError(ASender: TObject;
  67. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  68. AException: Exception);
  69. procedure BrookHTTPServer1Start(Sender: TObject);
  70. procedure BrookHTTPServer1Stop(Sender: TObject);
  71. procedure edPortChange(Sender: TObject);
  72. procedure edPortChangeTracking(Sender: TObject);
  73. public
  74. procedure UpdateControls; {$IFNDEF DEBUG}inline;{$ENDIF}
  75. end;
  76. const
  77. EMPTY_FAVICON = '<link rel="icon" href="data:,">';
  78. CONTENT_TYPE = 'text/html; charset=utf-8';
  79. INITIAL_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Use F5 to refresh this page ...</body></html>');
  80. COUNT_PAGE = Concat('<html><head>', EMPTY_FAVICON, '<title>Cookies</title></head><body>Refresh count: %d</body></html>');
  81. COOKIE_NAME = 'refresh_count';
  82. var
  83. frMain: TfrMain;
  84. implementation
  85. {$R *.fmx}
  86. procedure TfrMain.UpdateControls;
  87. begin
  88. if Application.Terminated then
  89. Exit;
  90. if BrookHTTPServer1.Active then
  91. edPort.Value := BrookHTTPServer1.Port
  92. else
  93. BrookHTTPServer1.Port := edPort.Text.ToInteger;
  94. lbLink.Text := Concat('http://localhost:', edPort.Value.ToString);
  95. acStart.Enabled := not BrookHTTPServer1.Active;
  96. acStop.Enabled := not acStart.Enabled;
  97. edPort.Enabled := acStart.Enabled;
  98. lbLink.Enabled := not acStart.Enabled;
  99. end;
  100. procedure TfrMain.acStartExecute(Sender: TObject);
  101. begin
  102. BrookHTTPServer1.Open;
  103. end;
  104. procedure TfrMain.acStopExecute(Sender: TObject);
  105. begin
  106. BrookHTTPServer1.Close;
  107. end;
  108. procedure TfrMain.lbLinkMouseEnter(Sender: TObject);
  109. begin
  110. lbLink.Font.Style := lbLink.Font.Style + [TFontStyle.fsUnderline];
  111. end;
  112. procedure TfrMain.lbLinkMouseLeave(Sender: TObject);
  113. begin
  114. lbLink.Font.Style := lbLink.Font.Style - [TFontStyle.fsUnderline];
  115. end;
  116. procedure TfrMain.lbLinkClick(Sender: TObject);
  117. begin
  118. OpenURL(lbLink.Text);
  119. end;
  120. procedure TfrMain.BrookHTTPServer1Request(ASender: TObject;
  121. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse);
  122. var
  123. VCount: Integer;
  124. begin
  125. if ARequest.Cookies.IsEmpty then
  126. VCount := 0
  127. else
  128. VCount := StrToIntDef(ARequest.Cookies.Get(COOKIE_NAME), 0);
  129. if VCount = 0 then
  130. begin
  131. AResponse.Send(INITIAL_PAGE, CONTENT_TYPE, 200);
  132. VCount := 1;
  133. end
  134. else
  135. begin
  136. AResponse.SendFmt(COUNT_PAGE, [VCount], CONTENT_TYPE, 200);
  137. Inc(VCount);
  138. end;
  139. AResponse.SetCookie(COOKIE_NAME, VCount.ToString);
  140. end;
  141. procedure TfrMain.BrookHTTPServer1RequestError(ASender: TObject;
  142. ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse;
  143. AException: Exception);
  144. begin
  145. AResponse.SendFmt(
  146. '<html><head><title>Error</title></head><body><font color="red">%s</font></body></html>',
  147. [AException.Message], 'text/html; charset=utf-8', 500);
  148. end;
  149. procedure TfrMain.BrookHTTPServer1Start(Sender: TObject);
  150. begin
  151. UpdateControls;
  152. end;
  153. procedure TfrMain.BrookHTTPServer1Stop(Sender: TObject);
  154. begin
  155. UpdateControls;
  156. end;
  157. procedure TfrMain.edPortChange(Sender: TObject);
  158. begin
  159. UpdateControls;
  160. end;
  161. procedure TfrMain.edPortChangeTracking(Sender: TObject);
  162. begin
  163. UpdateControls;
  164. end;
  165. end.