aboutsummaryrefslogtreecommitdiff
path: root/examples/echo.c
blob: 6901b719762da18d717fd6bf0ab3462d21370a8a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cgi.h>
#include <cookie.h>
#include <param.h>
#include <request.h>
#include <stdio.h>
#include <stdlib.h>


void proceed_cookies(struct magi_cookie_list * cookies,
                     struct magi_response *    response)
{
    magi_response_content(response, "<h2>Cookies:</h2>");
    while (cookies) {
        magi_response_content(response, "Cookie with name [");
        magi_response_content(response, cookies->item.name);
        if (cookies->item.data) {
            magi_response_content(response, "] is [");
            magi_response_content(response, cookies->item.data);
        }
        if (cookies->item.domain) {
            magi_response_content(response, "] for domain [");
            magi_response_content(response, cookies->item.domain);
        }
        if (cookies->item.port) {
            magi_response_content(response, "] for port [");
            magi_response_content(response, cookies->item.port);
        }
        if (cookies->item.path) {
            magi_response_content(response, "] for path [");
            magi_response_content(response, cookies->item.path);
        }
        magi_response_content(response, "]<br/>");
        cookies = cookies->next;
    }
    magi_response_content(response, "<hr/>");
}

void proceed_fields(struct magi_field_list * fields,
                    struct magi_response *   response)
{
    magi_response_content(response, "<h2>Feilds:</h2>");
    while (fields) {
        magi_response_content(response, "Feild [");
        magi_response_content(response, fields->item.name);
        magi_response_content(response, "] = [");
        magi_response_content(response, fields->item.data);
        magi_response_content(response, "]<br/>");
        fields = fields->next;
    }
    magi_response_content(response, "<hr/>");
}

void proceed_params(struct magi_param_list * params,
                    struct magi_response *   response)
{
    magi_response_content(response, "<h2>HTTP Parameters:</h2>");
    while (params) {
        magi_response_content(response, "Param [");
        magi_response_content(response, params->item.name);
        magi_response_content(response, "] is [");
        magi_response_content(response, params->item.data);
        magi_response_content(response, "]<br/>");
        params = params->next;
    }
    magi_response_content(response, "<hr/>");
}

void process_meta(struct magi_request * req, struct magi_response * res)
{
    magi_response_content(res, "<h1>Echo CGI Script</h1>"
                               "I was called with method [");
    magi_response_content(res, req->method);
    if (req->uri) {
        magi_response_content(res, "] with URL [");
        magi_response_content(res, req->uri);
    }
    if (req->server_name) {
        magi_response_content(res, "] for server [");
        magi_response_content(res, req->server_name);
    }
    if (req->server_port) {
        magi_response_content(res, "] on port [");
        magi_response_content(res, req->server_port);
    }
    if (req->server_protocol) {
        magi_response_content(res, "] with protocol [");
        magi_response_content(res, req->server_protocol);
    }
    if (req->server_software) {
        magi_response_content(res, "] and I am running on software [");
        magi_response_content(res, req->server_software);
    }
    magi_response_content(res, "]<br/>");
}

void response_request(struct magi_request * req, struct magi_response * res)
{
    magi_response_content_type(res, magi_xhtml);
    magi_response_content(
        res, "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' "
             "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>"
             "<html xmlns='http://www.w3.org/1999/xhtml'>"
             "<head><title>Echo</title></head>"
             "<body>");
    process_meta(req, res);
    proceed_cookies(req->cookies, res);
    proceed_fields(req->fields, res);
    proceed_params(req->http_params, res);
    magi_response_content(res, "</body></html>");
}

int main(int argc, char const * argv[])
{
    struct magi_request request;
    if (magi_cgi(&request, 0, 0)) {
        struct magi_response response;
        response_request(&request, &response);
        magi_cgi_response(&response);
        magi_response_destroy();
    } else {
        magi_cgi_error(request.error);
    }
    magi_request_destroy(&request);
    return 0;
}