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

Dv::Cgi::Cgi Class Reference

A wrapper class that represents the http interface to a cgi program. More...

#include <cgi.h>

Collaboration diagram for Dv::Cgi::Cgi:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Cgi (const std::string &name, const std::string &reply_format="text/html", bool debug=false) throw ()
 Constructor takes encoding type of response as parameter.
virtual ~Cgi ()
 Destructor.
const Dv::Util::Propsprops () const
const std::string & props (const std::string &name) const throw (std::runtime_error)
Dv::Util::Propsprops ()
std::string & props (const std::string &name) throw (std::runtime_error)
const Dv::Util::Propscookies () const
const std::string & cookies (const std::string &name) const throw (std::runtime_error)
const Util::Propsenv () const
const std::string & env (const std::string &name) const throw (std::runtime_error)
HttpHeaderoperator-> ()
 Easy access to HttpHeader.
const std::string * operator() (const std::string &key) const
 Easy check for formdata.
const std::string & data () const
HttpHeaderheader ()
const HttpHeaderheader () const
const std::string & name () const
std::ostream & log ()
const std::string & here () const
const std::string & here_base () const
const std::string & client_host () const
const std::string & client_host_name () const
const std::string & this_host () const
const std::string & this_host_name () const
std::string url (const Dv::Util::Props &props) const
 Extend the base url of this Cgi object with formdata from props.
void debug (std::ostream &os) const
 Write debug info on output stream.

Static Public Member Functions

static std::string props_as_url_args (const Dv::Util::Props &props)
 Print props as url-arguments.
static std::string url (const std::string &url, const Dv::Util::Props &props)
 Extend a url with arguments from props.

Private Attributes

const std::string name_
Filter filter_
Dv::Util::Props cookies_
Dv::Util::Props env_
Dv::Util::Props formdata_
std::string data_
Dv::Util::logstream log_
std::string here_
 url of this program
std::string here_base_
 base of url of this program: anything before '?'
std::string client_host_
 dot address of client host
std::string client_host_name_
 name of client host
std::string this_host_
 dot address of this host
std::string this_host_name_
 name of this host
Dv::Cgi::HttpHeader header_
 header to be sent before reply

Classes

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

Detailed Description

A wrapper class that represents the http interface to a cgi program.

The constructor stores any form data and cookies in a property list. Dv::Cgi::operator<<(ostream&,const Dv::Cgi::Cgi& ) outputs the header lines set in Cgi::header().

A typical usage could be:

  try {
    Cgi cgi("myapplication.cgi");
    cgi.header().content_type("text/plain");
    cgi << cgi.props() << endl;
    }
  catch (std::exception& e) {
    cerr << e.what() << endl;
    }

Definition at line 41 of file cgi.h.


Constructor & Destructor Documentation

Dv::Cgi::Cgi::Cgi const std::string &  name,
const std::string &  reply_format = "text/html",
bool  debug = false
throw ()
 

Constructor takes encoding type of response as parameter.

Parameters:
name of application, will be used as a prefix in output to cgi.log() [i.e. the http server log file]
reply_format default is "text/html"
debug flag to be passed to Dv::Cgi::HttpHeader
The constructor does not throw any exception. This supports the following template code.
   Cgi cgi("myapplication.cgi", "text/html", true);
   try {
     }
   catch (exception& e) {
     cgi.header().content_type("text/plain");
     cgi << e.what();
     }
  @encode

  which ensure that there is always proper output to cout.

  Even the following will work because the destructor will
  output something.

  @code
  {
  Cgi cgi("myapplication.cgi");
  try {
    }
  catch (std::exception& e) {
    }
  } // Cgi::~Cgi() will output something.

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

Destructor.

The destructor will output a header and some standard error message, if nothing else was output yet.


Member Function Documentation

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

Returns:
form data in Dv::Util::Props form

Definition at line 84 of file cgi.h.

References formdata_.

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

const std::string& Dv::Cgi::Cgi::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 88 of file cgi.h.

References props().

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

Returns:
form data in Dv::Util::Props form

Definition at line 92 of file cgi.h.

References formdata_.

std::string& Dv::Cgi::Cgi::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 96 of file cgi.h.

References props().

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

Returns:
cookies in Dv::Util::Props form

Definition at line 98 of file cgi.h.

References cookies_.

Referenced by cookies().

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

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

Definition at line 102 of file cgi.h.

References cookies().

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

Returns:
environment (REQUEST_URI etc.) in Dv::Util::Props form

Definition at line 105 of file cgi.h.

References env_.

Referenced by env().

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

Returns:
value of environment variable (REQUEST_URI etc.)
Exceptions:
std::runtime_error if variable is not defined

Definition at line 109 of file cgi.h.

References env().

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

Easy access to HttpHeader.

Returns:
pointer to HTTP header object.
Deprecated:
Use cgi.header().f().g().
See also:
Dv::Cgi::Cgi::header

Definition at line 117 of file cgi.h.

References header_.

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

Easy check for formdata.

Parameters:
key of value in formdata.
Returns:
value associated with key or 0 if none found.

Definition at line 122 of file cgi.h.

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

const std::string& Dv::Cgi::Cgi::data  )  const [inline]
 

Returns:
raw data as received from HTTP server

Definition at line 125 of file cgi.h.

References data_.

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

Returns:
HTTP header.

Definition at line 128 of file cgi.h.

References header_.

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

Returns:
HTTP header (const version).

Definition at line 130 of file cgi.h.

References header_.

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

Returns:
name (constructor parameter) of the program.
See also:
Dv::Cgi::Cgi::Cgi

Definition at line 134 of file cgi.h.

References name_.

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

Returns:
log stream This logstream is linked to cerr. The prefix is the name of the program.
See also:
Dv::Cgi::Cgi::Cgi

Definition at line 139 of file cgi.h.

References log_.

const std::string& Dv::Cgi::Cgi::here  )  const [inline]
 

Returns:
this url. The return value is the string obtained from the environment variable REQUEST_URI.

Definition at line 145 of file cgi.h.

References here_.

const std::string& Dv::Cgi::Cgi::here_base  )  const [inline]
 

Returns:
this url without arguments. The return value is obtained from the result of Dv::Cgi::Cgi::here by removing everything after the first occurrence of '?'.
See also:
Dv::Cgi::Cgi::here

Definition at line 152 of file cgi.h.

References here_base_.

const std::string& Dv::Cgi::Cgi::client_host  )  const [inline]
 

Returns:
client host dot address. The return value is the value of the REMOTE_ADDR environment variable. If the client host is the localhost, the real "dot address" of this host is returned.

Definition at line 158 of file cgi.h.

References client_host_.

const std::string& Dv::Cgi::Cgi::client_host_name  )  const [inline]
 

Returns:
client host name. The return value is obtained from the result of Dv::Cgi::Cgi::client_host by resolving the result.
See also:
Dv::Cgi::Cgi::client_host

Definition at line 164 of file cgi.h.

References client_host_name_.

const std::string& Dv::Cgi::Cgi::this_host  )  const [inline]
 

Returns:
dot address of this host. The return value is the value of the SERVER_ADDR environment variable.

Definition at line 169 of file cgi.h.

References this_host_.

const std::string& Dv::Cgi::Cgi::this_host_name  )  const [inline]
 

Returns:
name of this host. The return value is obtained from the SERVER_NAME environment variable or, if this variable is not available, resolving the result of Dv::Cgi::Cgi::this_host.
See also:
Dv::Cgi::Cgi::this_host

Definition at line 176 of file cgi.h.

References this_host_name_.

static std::string Dv::Cgi::Cgi::props_as_url_args const Dv::Util::Props props  )  [static]
 

Print props as url-arguments.

Convert a property list of a string suitable for inclusion in a url. E.g. abc=123&key=blabla. Both keys and values will be encoded using Dv::Cgi::www_encode.

Parameters:
props property list to be printed as url arguments
Returns:
string suitable for appending to a url as arguments
See also:
Dv::Util::Props Dv::Cgi::www_encode

std::string Dv::Cgi::Cgi::url const Dv::Util::Props props  )  const
 

Extend the base url of this Cgi object with formdata from props.

Parameters:
props property list to be printed as url arguments of here_base().
Returns:
here_base() + "?" + props_as_url_args(props)
See also:
Dv::Cgi::Cgi::props_as_url_args Dv::Cgi::Cgi::here_base

static std::string Dv::Cgi::Cgi::url const std::string &  url,
const Dv::Util::Props props
[static]
 

Extend a url with arguments from props.

Parameters:
url url to be extended
props property list to be added to this url
Returns:
url extended with arguments from props.
Warning:
the input url should not contain a fragment ('#') and probably should not end with '/'.
See also:
Dv::Cgi::Cgi::props_as_url_args

void Dv::Cgi::Cgi::debug std::ostream &  os  )  const
 

Write debug info on output stream.


Member Data Documentation

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

Definition at line 223 of file cgi.h.

Referenced by name().

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

Definition at line 224 of file cgi.h.

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

Definition at line 225 of file cgi.h.

Referenced by cookies().

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

Definition at line 226 of file cgi.h.

Referenced by env().

Dv::Util::Props Dv::Cgi::Cgi::formdata_ [private]
 

Definition at line 227 of file cgi.h.

Referenced by props().

std::string Dv::Cgi::Cgi::data_ [private]
 

Definition at line 228 of file cgi.h.

Referenced by data().

Dv::Util::logstream Dv::Cgi::Cgi::log_ [private]
 

Definition at line 229 of file cgi.h.

Referenced by log().

std::string Dv::Cgi::Cgi::here_ [private]
 

url of this program

Definition at line 231 of file cgi.h.

Referenced by here().

std::string Dv::Cgi::Cgi::here_base_ [private]
 

base of url of this program: anything before '?'

Definition at line 233 of file cgi.h.

Referenced by here_base().

std::string Dv::Cgi::Cgi::client_host_ [private]
 

dot address of client host

Definition at line 235 of file cgi.h.

Referenced by client_host().

std::string Dv::Cgi::Cgi::client_host_name_ [private]
 

name of client host

Definition at line 237 of file cgi.h.

Referenced by client_host_name().

std::string Dv::Cgi::Cgi::this_host_ [private]
 

dot address of this host

Definition at line 239 of file cgi.h.

Referenced by this_host().

std::string Dv::Cgi::Cgi::this_host_name_ [private]
 

name of this host

Definition at line 241 of file cgi.h.

Referenced by this_host_name().

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

header to be sent before reply

Definition at line 243 of file cgi.h.

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


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