summaryrefslogtreecommitdiffstats
path: root/src/libfcgi-test.cpp
blob: eea6b9dd6de3b4cba842294c84ebec8513298280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * This file is part of MXE. See LICENSE.md for licensing information.
 */

#include <sstream>

#include <fcgi_stdio.h>

using std::string;
using std::stringstream;

void show_html(const string &);
bool ends_with(const string &, const string &);
string get_env_var(const string &);

int main(void)
{
    unsigned long counter = 0;
    while (FCGI_Accept() >= 0) {
        ++counter;

        const string full_path = get_env_var("SCRIPT_NAME");
        if (ends_with(full_path, "") || ends_with(full_path, "/")) {
            show_html("<b>Hello, stranger!</b></br>\n"
                      "</br>\n"
                      "What are you looking for?</br>\n"
                      "</br>\n"
                      "Counter of visits may be found <a href='/counter'>here</a></br>\n");
        }
        else if (ends_with(full_path, "/counter") || ends_with(full_path, "/counter/")) {
            stringstream counter_str;
            counter_str << counter;
            show_html("Counter: " + counter_str.str());
        }
        else {
            show_html("<center><h2>This is not the page you are looking for!</h2></center>\n");
        }
    }

    return 0;
}

void show_html(const string &str)
{
    printf("Content-type: text/html\n\n");
    printf("%s", str.c_str());
}

bool ends_with(const string &str, const string &sfx)
{
    if (sfx.size() > str.size())
        return false;

    return equal(str.begin() + str.size() - sfx.size(), str.end(), sfx.begin());
}

string get_env_var(const string &var)
{
    const char *ptr = getenv(var.c_str());
    return (ptr ? string(ptr) : "");
}