PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/fcl-web/examples/session/wmsession.pp

https://github.com/slibre/freepascal
Puppet | 165 lines | 136 code | 29 blank | 0 comment | 1 complexity | 6b9373618b97cec07e337b2fb8494fd5 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1unit wmsession;
  2
  3{$mode objfpc}{$H+}
  4
  5interface
  6
  7uses
  8  Classes, SysUtils, HTTPDefs, websession, fpHTTP, fpWeb; 
  9
 10type
 11
 12  { TSessionModule }
 13
 14  TSessionModule = class(TFPWebModule)
 15    procedure EndSessionRequest(Sender: TObject; ARequest: TRequest;
 16      AResponse: TResponse; var Handled: Boolean);
 17    procedure InSessionRequest(Sender: TObject; ARequest: TRequest;
 18      AResponse: TResponse; var Handled: Boolean);
 19    procedure NewSessionRequest(Sender: TObject; ARequest: TRequest;
 20      AResponse: TResponse; var Handled: Boolean);
 21    procedure SessionModuleNewSession(Sender: TObject);
 22  private
 23    { private declarations }
 24  public
 25    { public declarations }
 26  end; 
 27
 28var
 29  SessionModule: TSessionModule;
 30
 31implementation
 32
 33{$R *.lfm}
 34
 35{ TSessionModule }
 36
 37{
 38  The default action is the 'InSession' action. When a new session
 39  is started, the newsession event handler is called, and
 40  we set the 'NewSession' action as default.
 41}
 42
 43procedure TSessionModule.SessionModuleNewSession(Sender: TObject);
 44begin
 45  Actions.ActionByName('NewSession').Default:=True;
 46  Actions.ActionByName('InSession').Default:=False;
 47end;
 48
 49{
 50  When a new session is detected
 51
 52  - either because there was no session,in which case NewSession is the default
 53
 54  - The URL contained the newsession action in the 'DemoSession' action variable,
 55    something like:
 56
 57  http://localhost/cgi-bin/sessiondemo.cgi?DemoSession=NewSession
 58  
 59  in either case, the NewSession action is called, and this event is triggered:
 60}
 61
 62procedure TSessionModule.NewSessionRequest(Sender: TObject; ARequest: TRequest;
 63  AResponse: TResponse; var Handled: Boolean);
 64  
 65Var
 66  C : TCookie;
 67  
 68begin
 69  With AResponse.Contents do
 70    begin
 71    Add('<HTML><TITLE>Demo session was started</TITLE><BODY>');
 72    Add('<H1>New session started</H1>');
 73    Add('A new session was started<P>.');
 74    If Session is TFPWebSession then
 75      begin
 76      C:=AResponse.Cookies.FindCookie((Session as TFPWebSession).SessionCookie);
 77      If Assigned(C) then
 78        begin
 79        Add('The issued session cookie is called <B>'+C.Name+'</B><BR> ');
 80        Add('The issued session cookie has value <B>'+C.Value+'</B><BR>.');
 81        end
 82      else
 83        Add('No session cookie was found.');
 84      end;
 85    Add('</BODY></HTML>');
 86    end;
 87  Handled:=True; // Content will be sent.
 88end;
 89
 90{
 91  The default action is the 'InSession' action.
 92  We display the session cookie, and the value (name 'Var')
 93  that is currently stored in the session object.
 94
 95  If the user supplied a new value for 'var', we store it in the session.
 96  to supply the value, append
 97  ?var=value
 98  to the URL.
 99}
100
101procedure TSessionModule.InSessionRequest(Sender: TObject; ARequest: TRequest;
102  AResponse: TResponse; var Handled: Boolean);
103
104Var
105  V : string;
106  C : TCookie;
107
108begin
109  With AResponse.Contents do
110    begin
111    Add('<HTML><TITLE>Demo session active</TITLE><BODY>');
112    Add('<H1>Demo session active</H1>');
113    Add('The demo session is still active<P>');
114    If Session is TFPWebSession then
115      begin
116      C:=AResponse.Cookies.FindCookie((Session as TFPWebSession).SessionCookie);
117      If Assigned(C) then
118        begin
119        Add('Current session Cookie is called <B>'+C.Name+'</B><BR>');
120        Add('and has value <B>'+C.Value+'</B>.');
121        end;
122      V:=Session.Variables['Var'];
123      If (V<>'') then
124        Add('<P>Stored session value: <B>'+V+'</B>.')
125      else
126        Add('<P>No values stored in session.');
127      V:=ARequest.QueryFields.Values['Var'];
128      If V<>'' then
129        begin
130        Add('<P>Storing new session value: <B>'+V+'</B>.');
131        Session.Variables['Var']:=V;
132        end;
133      end;
134    Add('</BODY></HTML>');
135    AResponse.SendContent; // Handles the response.
136    end;
137end;
138
139
140{
141  When the 'EndSession' action is called, the session is ended. The
142  endsession action can be called by providing the 'EndSession' value for
143  the 'DemoSession' action variable, something like:
144
145  http://localhost/cgi-bin/sessiondemo.cgi?DemoSession=EndSession
146}
147procedure TSessionModule.EndSessionRequest(Sender: TObject; ARequest: TRequest;
148  AResponse: TResponse; var Handled: Boolean);
149begin
150  // Stop the session
151  Session.Terminate;
152  With AResponse.Contents do
153    begin
154    Add('<HTML><TITLE>Demo Session Is Terminated</TITLE><BODY>');
155    Add('<H1>Demo session Terminated</H1>');
156    Add('The session was terminated, the cookie is cleared and the');
157    Add('stored value is lost');
158    Add('</BODY></HTML>');
159    end;
160  AResponse.SendContent;
161end;
162
163initialization
164  RegisterHTTPModule('session', TSessionModule);
165end.