Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Dv::Cgi::SessionServer Class Reference

A class that packages the link to the hidden server. More...

#include <sessionserver.h>

Collaboration diagram for Dv::Cgi::SessionServer:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SessionServer (const std::string &program_name, time_t delay, const std::string &timeouturl, const std::string &errorurl, const std::string &logdir="/tmp", const std::string &sockdir="/tmp")
 Constructor.
virtual ~SessionServer ()
 Destructor.
const std::string & name () const
 Name of server application.
const Util::Propsprops () const
 Get props object containing formdata from client.
Dv::Util::Propsprops ()
 Get props object containing formdata from client.
const std::string * operator() (const std::string &key) const
 Easy check for presence of a formdata item.
const std::string & props (const std::string &name) const throw (std::runtime_error)
std::string & props (const std::string &name) throw (std::runtime_error)
const Util::Propsenv () const
 Get Props object containing shell environment from client.
const std::string & env (const std::string &name) const throw (std::runtime_error)
const Util::Propscookies () const
 Get Props object containing cookies from client.
const std::string & cookies (const std::string &name) const throw (std::exception)
std::string here () const throw (std::runtime_error)
 Return url of current request.
std::string here_base () const throw (std::runtime_error)
 Return base url of current request.
const HttpHeaderheader () const
 Get HttpHeader object containing output headers.
HttpHeaderheader ()
 Get HttpHeader object containing output headers.
HttpHeaderoperator-> ()
 Get HttpHeader object containing output headers.
 operator bool () const
 Get status of server.
void disconnect (const std::string &url)
 Remove cookie, relocate to url, stop network server and connection.
void disconnect ()
 Stop server and network connection.
void refresh ()
 Refresh: send header (if not already sent) and get input from client.
std::ostream & log () const
 Log stream.

Private Member Functions

 SessionServer (const SessionServer &s)
SessionServeroperator= (const SessionServer &s)
void send_header ()
void get_input ()

Private Attributes

std::string name_
time_t delay_
std::string timeouturl_
std::string errorurl_
std::string server_filename_
Dv::Net::userversocketserver_socket_
Dv::Net::usocketclient_socket_
bool status_
Dv::Util::Props props_
Dv::Util::Props env_
Dv::Util::Props cookies_
Dv::Cgi::HttpHeader header_
std::ostream * log_
Filter filter_

Friends

class Filter

Classes

class  Filter
 Filter class that ensures that header_ is output before anything else. More...

Detailed Description

A class that packages the link to the hidden server.

Definition at line 19 of file sessionserver.h.


Constructor & Destructor Documentation

Dv::Cgi::SessionServer::SessionServer const std::string &  program_name,
time_t  delay,
const std::string &  timeouturl,
const std::string &  errorurl,
const std::string &  logdir = "/tmp",
const std::string &  sockdir = "/tmp"
 

Constructor.

A Dv::Cgi::SessionServer object must be used in a cgi program.

 Dv::Cgi::SessionServer client("myprog",5*1000, "index.html", "http://tinf2.vub.ac.be/index.html");
  • If there is a cookie with the name/key "myprog", its value will be interpreted as the name of a file that corresponds to a unix socket on which a server is listening.
  • If a server is listening on that socket, this constructor will simply transfer the cgi formdata, cookies and environment to the server, then copy any input from the server to cout and exit the program.
  • If no server is running, it will start the server by forking a daemon and then behave as above. The constructor will only return in de server daemon process.

The server communicates with its (cgi) clients using a unix socket attached to a filename /tmp/program_name.xxx where xxx is generated to make the filename unique.

Application code is rather simple: the program communicates with the client (a Dv::Cgi::SessionServer object) using interactions of the form

 client << .. html .. << Dv::Cgi::eof;
 // now client input is available, e.g. in client.props()
 if ( client("number") ) {
   int number;
   client.props() >> "number" >> number;   
   ..
   }

Note that all C++ variables are persistent (since they reside in the server process). E.g. it is easy to write a login function like so:

 std::string
 login(Dv::Cgi::SessionServer& client) {
 while (client) {
   client << login_form << Dv::Cgi::eof;
   if ( client("name") ) {
     std::string uname = client.props("name");
     std::string uname = client.props("password");
     if (check_password_valid)
       return uname;
     }
   }
 throw std::runtime_error("login timed out");
 }

To exit the application use Dv::Cgi::SessionServer::disconnect .

Silly example applications are available in the distribution: see test-sessionserver.C and example-session.C .

Parameters:
program_name used to generate the server unix server socket filename, also used as the name of the cookie whose value is the actual server filename.
delay (in milliseconds) is used by the client and the server to determine the allowable wait for response.
timeouturl is used to relocate any request that suffers from a timeout
errorurl is used to relocate any request that suffers from an unrecoverable error (except for timeout)
logdir directory where logfile will be created
sockdir directory where sockets will be created
Warning:
do not set a cookie with key/name program_name

virtual Dv::Cgi::SessionServer::~SessionServer  )  [virtual]
 

Destructor.

Dv::Cgi::SessionServer::SessionServer const SessionServer s  )  [private]
 


Member Function Documentation

const std::string& Dv::Cgi::SessionServer::name  )  const [inline]
 

Name of server application.

Definition at line 103 of file sessionserver.h.

References name_.

Referenced by cookies(), env(), and props().

const Util::Props& Dv::Cgi::SessionServer::props  )  const [inline]
 

Get props object containing formdata from client.

Definition at line 106 of file sessionserver.h.

References props_.

Referenced by operator()(), and props().

Dv::Util::Props& Dv::Cgi::SessionServer::props  )  [inline]
 

Get props object containing formdata from client.

Definition at line 108 of file sessionserver.h.

References props_.

const std::string* Dv::Cgi::SessionServer::operator() const std::string &  key  )  const [inline]
 

Easy check for presence of a formdata item.

Definition at line 111 of file sessionserver.h.

References Dv::Util::Props::find(), and props().

const std::string& Dv::Cgi::SessionServer::props const std::string &  name  )  const throw (std::runtime_error) [inline]
 

Returns:
value of form data variable
Exceptions:
std::runtime_error if variable is not defined

Definition at line 115 of file sessionserver.h.

References name(), and props().

std::string& Dv::Cgi::SessionServer::props const std::string &  name  )  throw (std::runtime_error) [inline]
 

Returns:
value of form data variable
Exceptions:
std::runtime_error if variable is not defined

Definition at line 120 of file sessionserver.h.

References name(), and props().

const Util::Props& Dv::Cgi::SessionServer::env  )  const [inline]
 

Get Props object containing shell environment from client.

Definition at line 123 of file sessionserver.h.

References env_.

Referenced by env().

const std::string& Dv::Cgi::SessionServer::env const std::string &  name  )  const throw (std::runtime_error) [inline]
 

Returns:
value of environment variable.
Exceptions:
std::runtime_error if variable is not defined

Definition at line 127 of file sessionserver.h.

References env(), and name().

const Util::Props& Dv::Cgi::SessionServer::cookies  )  const [inline]
 

Get Props object containing cookies from client.

Definition at line 131 of file sessionserver.h.

References cookies_.

Referenced by cookies().

const std::string& Dv::Cgi::SessionServer::cookies const std::string &  name  )  const throw (std::exception) [inline]
 

Returns:
value of cookie variable
Exceptions:
std::runtime_error if variable is not defined

Definition at line 135 of file sessionserver.h.

References cookies(), and name().

std::string Dv::Cgi::SessionServer::here  )  const throw (std::runtime_error)
 

Return url of current request.

Returns:
value of REQUEST_URI in env()
Exceptions:
std::runtime_error if not available
See also:
Dv::Cgi::SessionServer::env

std::string Dv::Cgi::SessionServer::here_base  )  const throw (std::runtime_error)
 

Return base url of current request.

Returns:
value of here() without parameters, i.e. after removing the first '?' and anything following it.
Exceptions:
std::runtime_error if not available
See also:
Dv::Cgi::SessionServer::here

const HttpHeader& Dv::Cgi::SessionServer::header  )  const [inline]
 

Get HttpHeader object containing output headers.

Definition at line 153 of file sessionserver.h.

References header_.

HttpHeader& Dv::Cgi::SessionServer::header  )  [inline]
 

Get HttpHeader object containing output headers.

Definition at line 155 of file sessionserver.h.

References header_.

HttpHeader* Dv::Cgi::SessionServer::operator->  )  [inline]
 

Get HttpHeader object containing output headers.

Deprecated:
Use session.header().f() instead of session->f().

Definition at line 160 of file sessionserver.h.

References header_.

Dv::Cgi::SessionServer::operator bool  )  const [inline]
 

Get status of server.

Return values:
true iff server is working
false otherwise

Definition at line 166 of file sessionserver.h.

References status_.

void Dv::Cgi::SessionServer::disconnect const std::string &  url  ) 
 

Remove cookie, relocate to url, stop network server and connection.

Parameters:
url to relocate to

void Dv::Cgi::SessionServer::disconnect  ) 
 

Stop server and network connection.

void Dv::Cgi::SessionServer::refresh  ) 
 

Refresh: send header (if not already sent) and get input from client.

std::ostream& Dv::Cgi::SessionServer::log  )  const [inline]
 

Log stream.

This is a Dv::Util::logstream: each line is automatically prefixed by the date and the identification of the process.

See also:
Dv::Cgi::SessionServer::name

Definition at line 182 of file sessionserver.h.

References log_.

SessionServer& Dv::Cgi::SessionServer::operator= const SessionServer s  )  [private]
 

void Dv::Cgi::SessionServer::send_header  )  [private]
 

void Dv::Cgi::SessionServer::get_input  )  [private]
 


Friends And Related Function Documentation

friend class Filter [friend]
 

Definition at line 203 of file sessionserver.h.


Member Data Documentation

std::string Dv::Cgi::SessionServer::name_ [private]
 

Definition at line 208 of file sessionserver.h.

Referenced by name().

time_t Dv::Cgi::SessionServer::delay_ [private]
 

Definition at line 209 of file sessionserver.h.

std::string Dv::Cgi::SessionServer::timeouturl_ [private]
 

Definition at line 210 of file sessionserver.h.

std::string Dv::Cgi::SessionServer::errorurl_ [private]
 

Definition at line 211 of file sessionserver.h.

std::string Dv::Cgi::SessionServer::server_filename_ [private]
 

Definition at line 212 of file sessionserver.h.

Dv::Net::userversocket* Dv::Cgi::SessionServer::server_socket_ [private]
 

Definition at line 214 of file sessionserver.h.

Dv::Net::usocket* Dv::Cgi::SessionServer::client_socket_ [private]
 

Definition at line 215 of file sessionserver.h.

bool Dv::Cgi::SessionServer::status_ [private]
 

Definition at line 216 of file sessionserver.h.

Referenced by operator bool().

Dv::Util::Props Dv::Cgi::SessionServer::props_ [private]
 

Definition at line 218 of file sessionserver.h.

Referenced by props().

Dv::Util::Props Dv::Cgi::SessionServer::env_ [private]
 

Definition at line 219 of file sessionserver.h.

Referenced by env().

Dv::Util::Props Dv::Cgi::SessionServer::cookies_ [private]
 

Definition at line 220 of file sessionserver.h.

Referenced by cookies().

Dv::Cgi::HttpHeader Dv::Cgi::SessionServer::header_ [private]
 

Definition at line 222 of file sessionserver.h.

Referenced by header(), and operator->().

std::ostream* Dv::Cgi::SessionServer::log_ [private]
 

Definition at line 224 of file sessionserver.h.

Referenced by log().

Filter Dv::Cgi::SessionServer::filter_ [private]
 

Definition at line 225 of file sessionserver.h.


The documentation for this class was generated from the following file:
dvcgi-0.5.14 [22 January, 2006]