FBB::CGI

FBB::CGI

libbobcat1-dev_2.02.03-x.tar.gz

2005-2009


FBB::CGI(3bobcat)

FBB::CGI(3bobcat)

libbobcat1-dev_2.02.03-x.tar.gz CGI interface

2005-2009

NAME

FBB::CGI - handles GET and POST submitted form data

SYNOPSIS

#include <bobcat/cgi>
Linking option: -lbobcat

DESCRIPTION

The class CGI offers an interface to data submitted by web-forms. The data is sent to a script handling the data using a <form action="/path/to/form/script"> stanza. Very often this is indeed a script, like a Perl script, but there is no need to use a scripting language. The class CGI allows C++ programmers to process the form by an executable which usually results in faster processing and at construction time benefits from the type safety offered by C++. The class CGI handles data submitted using the GET method as well as data submitted using the POST method automatically.

By default the class's constructor writes the customary Content-type header lines to the standard output stream. Additional (html) output of a reply page must be provided by other code. Therefore, a program processing an uploaded form will have an organization comparable to the following basic setup:


    // assume includes and namespace std/FBB were defined
    int main()
    {
        CGI cgi;
        cout << "<html><body>\n";
        if (parametersOK(cgi))
        {
            process(cgi);     
            generateReplyPage();
        }
        else
            generateErrorPage();
        cout << "</body></html>\n;
    }
        

When errors in the received form-data are detected an error message is written to the standard output stream and an FBB::Errno exception is thrown.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

TYPEDEF

ENUMERATIONS

The CGI::Method enumeration specifies values indicating the way the form's data were submitted:

The CGI::Create enumeration is used to request or suppress creation of the directory to contain any file uploaded by a form:

CONSTRUCTORS

The copy constructor is available.

OERLOADED OPERATORS

Note: the following three insertion operators defining sets of characters that should be escaped can only be used until the first time one of the param(), begin() or end() members is called. Once one of these latter three members has been called the set of characters to be escaped is fixed and attempts to modify them will fail silently.

The assignment operator is available.

MEMBER FUNCTIONS

The first time one of the param(), begin() or end() members is called these members may detect errors in the the received form data. If so, an error message is written to the standard output stream and an FBB::Errno exception will be thrown.

EXAMPLE

#include "main.ih"

void outstr(string const &str)
{
    cout << CGI::dos2unix(str) << "\n"
            "    ";
}

void showParam(CGI::MapStringVector::value_type const &mapValue)
{
    cout << "Param: " << mapValue.first << "\n"
            "    ";

    FBB::for_each(mapValue.second.begin(), mapValue.second.end(), outstr);
    cout << endl;
}

int main(int argc, char **argv)
try
{
    Arg &arg = Arg::initialize("evhm:", argc, argv);

    arg.versionHelp(usage, version, 2);

    ifstream in(arg[0]);
    string line;
    while (getline(in, line))
    {
        size_t pos = line.find('=');
        if (pos == string::npos)
            continue;
        if (setenv(line.substr(0, pos).c_str(), 
               line.substr(pos + 1).c_str(), true) == 0)
        {
            if (arg.option('e'))
                cout << line.substr(0, pos).c_str() << '=' <<
                       line.substr(pos + 1).c_str() << endl;
        }
        else
            cout << "FAILED: setenv " << line << endl;
    }

    CGI cgi(false);             // by default no escapes

    cgi << arg[1];

    if (arg.option(&line, 'm'))
        cgi.setMaxUploadSize(A2x(line), *line.rbegin());

    cout << "Max upload size (b): " << cgi.maxUploadSize() << endl;

    CGI::Method method = cgi.method();

    cout << "To escape:\n" << 
            cgi << "\n"
            "Method: " << (method == CGI::GET ? "GET" : "POST") <<
            endl;

    cout << "Query string: " << cgi.query() << endl;

    cgi.param("submit");

    FBB::for_each(cgi.begin(), cgi.end(), &showParam);

    cout << "END OF PROGRAM\n";

    return 0;
}
catch (Errno const &err)
{
    cout << err.what() << endl;
    return 1;
}
catch (...)
{
    return 1;
}




To test the program provide it, e.g., with the following file at its standard input:

INFO=This is an abbreviated set of environment variables
SERVER_ADMIN=f.b.brokken@rug.nl
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
QUERY_STRING=hidden=hidval&submit=Submit+%20Query

FILES

bobcat/cgi - defines the class interface

SEE ALSO

bobcat(7)

BUGS

None Reported.

DISTRIBUTION FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).