FBB::CGI(3bobcat)

CGI interface
(libbobcat-dev_6.02.02)

2005-2022

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 usually resulting in faster processing and in construction time benefits from the type safety offered by C++. The class CGI automatically handles data submitted using the GET method as well as data submitted using the POST method.

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::Exception 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

Copy and move constructors (and assignment operators) are available.

OERLOADED OPERATORS

Note: the following three insertion operators, defining sets of characters that should be escaped, can only be used before calling any of the param, begin or end members. As soon as one of these latter three members has been called the set of characters to be escaped is fixed and attempts to modify that set is silently ignored.

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::Exception exception will be thrown.

STATIC MEMBERS

EXAMPLE

#include "main.ih"

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

    for (auto &str: mapValue.second)
        cout << "    " << CGI::dos2unix(str) << "\n"
            "    ";

    cout << '\n';
}

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

    // usage and version are in the source archive in .../cgi/driver
    // 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;
                            // set environment vars simulating
                            // a GET form
        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() << '\n';
        }
        else
            cout << "FAILED: setenv " << line << '\n';
    }

    CGI cgi(false);             // chars are not escaped

    cgi << arg[1];

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

    cout << "Max upload size (b): " << cgi.maxUploadSize() << '\n';

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

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

    cout << "Query string: " << cgi.query() << '\n';

    cout << "Submit string: `" << cgi.param1("submit") << "'\n";

    for (auto &mapElement: cgi)
        showParam(mapElement);

    cout << "END OF PROGRAM\n";
}
catch (exception const &err)
{
    cout << err.what() << '\n';
    return 1;
}
catch (...)
{
    return 1;
}

To test the program's get form processing, call it as driver get '[:cgi:]', with the file get containing:

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

To test the program's post form processing, call it as driver post1 '[:cgi:]', using post1 and post1.cin found in Bobcat's source archive under ../cgi/driver.

FILES

bobcat/cgi - defines the class interface

SEE ALSO

bobcat(7)

BUGS

None Reported.

BOBCAT PROJECT 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).