PageRenderTime 23ms CodeModel.GetById 8ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/components/fpweb/demo/session/wmsession.pp

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