Skip to content

terrakuh/fast_cgi

Repository files navigation

fast_cgi

Installation

Requirements:

  • CMake >= 3.1
  • Git
  • C++11 compiler
git clone https://github.com/terrakuh/fast_cgi.git
cd fast_cgi
git submodule init
git submodule update

mkdir build
cd build

cmake ..

# cmake -DFAST_CGI_BUILD_EXAMPLES=OFF
# cmake -DFAST_CGI_ENABLE_LOGGING=OFF

cmake --build .
cmake --build . --target install

Cheatsheet

Roles

Every role is provided an output stream by output(), an error stream by error() and request parameters by params(). For every incoming request from the web server a new role instance is created. Only one user specific role can be integrated for each type. Integration can look like this:

protocol.set_role<my_responder>();

A detailed definition of the following roles can be found here.

Responder (fast_cgi::responder)

A responder additionally receives optinal input by input(). This role has the same purpose as simple CGI/1.1 programs (this is probably what you want). A simple Hello, World might look like this:

#include <fast_cgi/fast_cgi.hpp>

class hello_world : public fast_cgi::responder
{
public:
    status_code_type run() override
    {
        using namespace fast_cgi::manipulator;

        output() << "Content-type: text/html" << feed << feed;
        output() << "<html><body>"
                 << "<h1>" << "Hello, World!" << "</h1><br/><br/>"
                 << "<span>Here are all parameters:</span><br/>";

        // Print all parameters
        for (auto& i : params()) {
            output() << i.first << "=" << i.second << "<br/>";
        }
        
        output() << "<span>Your payload:</span><br/>";
        output() << input().rdbuf();
        output() << "</body></html>"

        return 0;
    }
};

More information can be found here.

Filter (fast_cgi::filter)

See here.

Authorizer (fast_cgi::authorizer)

See here.

Parameters

Parameters can be iterated like:

for (auto& parameter : params()) {
    std::cout << parameter.first << "=" << parameter.second << "\n";
}

Getting a parameter (this example may throw an std::out_of_range exception if the key does not exist):

auto& value = params()["REQUEST_URI"];
// or
auto& value = params("REQUEST_URI");

Checking if a parameter is available:

auto has_uri = params().has("REQUEST_URI");

License

MIT License

Releases

No releases published

Packages

No packages published