routingsessiondemo.lpr 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. program routingsessiondemo;
  2. {$mode objfpc}{$H+}
  3. uses
  4. SysUtils,
  5. fphttpapp, // Standaline
  6. fphttp, // Session manager
  7. iniwebsession, // Ini based sessions
  8. httpdefs, // TRequest,TResponse
  9. httproute;
  10. {$IFDEF VER3_0}
  11. Type
  12. { TSessionState }
  13. TSessionState = Class(TObject)
  14. IsNew : Boolean;
  15. IsExpired : Boolean;
  16. Procedure OnNew(Sender : TObject);
  17. Procedure OnExpired(Sender : TObject);
  18. end;
  19. procedure TSessionState.OnNew(Sender: TObject);
  20. begin
  21. IsNew:=True;
  22. end;
  23. procedure TSessionState.OnExpired(Sender: TObject);
  24. begin
  25. IsExpired:=True;
  26. end;
  27. {$ENDIF}
  28. Procedure DisplayForm(aResponse : TResponse);
  29. begin
  30. With AResponse.Contents do
  31. begin
  32. Add('<H1>Set variable value:</H1>');
  33. Add('The value of a variable can be stored in the session.<br> Here you can set the value of the variable.<p>');
  34. Add('<form action="insession" method="get">');
  35. Add('<input type="text" name="var"></input><p>');
  36. Add('<input type="submit" Value="Set new value"/>');
  37. Add('</form><p>');
  38. Add('<a href="newsession">Start new session</a><p>');
  39. Add('<a href="endsession">End the session</a>');
  40. end;
  41. end;
  42. Procedure DisplayNewSession(aResponse : TResponse; aSession : TCustomSession);
  43. Var
  44. C : TCookie;
  45. begin
  46. With AResponse.Contents do
  47. begin
  48. Add('<HTML><TITLE>Demo session was started</TITLE><BODY>');
  49. Add('<H1>New session started</H1>');
  50. {$IFNDEF VER3_0}
  51. if (ssExpired in aSession.SessionState) then
  52. Add('The session associated with the cookie the browser sent, has expired.<P>');
  53. {$ENDIF}
  54. Add('A new session was started.<P>');
  55. C:=AResponse.Cookies.FindCookie(aSession.SessionCookie);
  56. If Assigned(C) then
  57. begin
  58. Add('The issued session cookie is called <B>'+C.Name+'</B>.<BR> ');
  59. Add('The issued session cookie has value <B>'+C.Value+'</B>.<BR>');
  60. end
  61. else
  62. Add('No session cookie was found.');
  63. DisplayForm(AResponse);
  64. Add('</BODY></HTML>');
  65. end;
  66. end;
  67. Procedure NewSession(aRequest : TRequest; aResponse : TResponse);
  68. Var
  69. Session : TCustomSession;
  70. begin
  71. Session:=SessionFactory.CreateSession(aRequest);
  72. try
  73. Session.InitSession(aRequest,Nil,Nil);
  74. Session.InitResponse(aResponse);
  75. DisplayNewSession(aResponse,Session);
  76. Finally
  77. Session.Free;
  78. end;
  79. end;
  80. Procedure EndSession(aRequest : TRequest; aResponse : TResponse);
  81. Var
  82. Session : TCustomSession;
  83. begin
  84. Session:=SessionFactory.CreateSession(aRequest);
  85. try
  86. Session.InitSession(aRequest,Nil,Nil);
  87. // Stop the session
  88. Session.Terminate;
  89. Session.InitResponse(aResponse);
  90. With AResponse.Contents do
  91. begin
  92. Add('<HTML><TITLE>Demo Session Is Terminated</TITLE><BODY>');
  93. Add('<H1>Demo session Terminated</H1>');
  94. Add('The session was terminated, the cookie is cleared and the');
  95. Add('stored value is lost</p>');
  96. Add('<a href="newsession">Start new session</a>');
  97. Add('</BODY></HTML>');
  98. end;
  99. finally
  100. Session.Free;
  101. AResponse.SendContent;
  102. end;
  103. end;
  104. Procedure InSession(aRequest : TRequest; aResponse : TResponse);
  105. Var
  106. V : string;
  107. C : TCookie;
  108. Session : TCustomSession;
  109. NewSession : Boolean;
  110. {$IFDEF VER3_0}
  111. State : TSessionState;
  112. {$ENDIF VER3_0}
  113. begin
  114. {$IFDEF VER3_0}
  115. State:=nil;
  116. {$ENDIF VER3_0}
  117. Session:=SessionFactory.CreateSession(aRequest);
  118. try
  119. {$IFDEF VER3_0}
  120. State:=TSessionState.Create;
  121. Session.InitSession(aRequest,@State.OnNew,@State.OnExpired);
  122. NewSession:=State.IsNew;
  123. {$ELSE}
  124. Session.InitSession(aRequest,Nil,Nil);
  125. NewSession:=ssNew in Session.SessionState;
  126. {$ENDIF}
  127. Session.InitResponse(aResponse);
  128. // The url was called, but there was no session yet or it expired...
  129. if NewSession then
  130. DisplayNewSession(AResponse,Session)
  131. else
  132. With AResponse.Contents do
  133. begin
  134. Add('<HTML><TITLE>Demo session active</TITLE><BODY>');
  135. Add('<H1>Demo session active</H1>');
  136. Add('The demo session is still active<P>');
  137. // If Session is TFPWebSession then
  138. begin
  139. C:=AResponse.Cookies.FindCookie(Session.SessionCookie);
  140. If Assigned(C) then
  141. begin
  142. Add('Current session Cookie is called <B>'+C.Name+'</B><BR>');
  143. Add('and has value <B>'+C.Value+'</B>.');
  144. end;
  145. V:=Session.Variables['Var'];
  146. If (V<>'') then
  147. Add('<P>Stored session value: <B>'+V+'</B>.')
  148. else
  149. Add('<P>No values stored in session.');
  150. V:=ARequest.QueryFields.Values['Var'];
  151. If V<>'' then
  152. begin
  153. Add('<P>Storing new session value: <B>'+V+'</B>.');
  154. Session.Variables['Var']:=V;
  155. end;
  156. end;
  157. DisplayForm(AResponse);
  158. Add('</BODY></HTML>');
  159. AResponse.SendContent; // Handles the response.
  160. end;
  161. finally
  162. {$IFDEF VER3_0}
  163. State.Free;
  164. {$ENDIF}
  165. Session.Free;
  166. end;
  167. end;
  168. begin
  169. HTTPRouter.RegisterRoute('/insession',@InSession);
  170. HTTPRouter.RegisterRoute('/endsession',@EndSession);
  171. HTTPRouter.RegisterRoute('/newsession',@NewSession,True);
  172. if ParamCount=1 then
  173. Application.Port:=StrToIntDef(ParamStr(1),8080)
  174. else
  175. Application.Port:=8080;
  176. Writeln('Server listens on port : ',Application.Port);
  177. Application.Initialize;
  178. Application.Run;
  179. end.