aboutsummaryrefslogtreecommitdiff
path: root/examples/echo.c
blob: 7c840e2373df2a6c64c35e2c6e36c56e217c4666 (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
#include <magi.h>


void list_cookies(magi_request *r)
{
    magi_cookies *current = r->cookies;
    for (current = r->cookies; current; current = current->next) {
        magi_cookie *c = &current->item;
        magi_response(r, "Cookie with name [");
        magi_response(r, c->name);
        if (c->data) {
            magi_response(r, "] is [");
            magi_response(r, c->data);
        }
        if (c->domain) {
            magi_response(r, "] for domain [");
            magi_response(r, c->domain);
        }
        if (c->path) {
            magi_response(r, "] for path [");
            magi_response(r, c->path);
        }
        magi_response(r, "]<br/>");
    }
}

void list_params(magi_request *r, magi_params *current)
{
    for (; current; current = current->next) {
        magi_param *p = &current->item;
        magi_response_format(r, "[%s] is [%s]<br/>", p->name, p->data);
    }
}

void list_files(magi_request *r)
{
    magi_files *current;
    for (current = r->files; current; current = current->next) {
        magi_file *f = &current->item;
        magi_response_format(r, "[%s] was [%s] on clientside<br/>",
                             f->field, f->filename);
    }
}

void show_meta(magi_request *r)
{
    magi_response(r, "I was called ");
    if (r->is_secure) {
        magi_response(r, "securely ");
    }
    magi_response(r, "with method [");
    magi_response(r, r->method);
    if (r->host) {
        magi_response(r, "] on server [");
        magi_response(r, r->host);
    }
    if (r->script) {
        magi_response(r, "] being script on [");
        magi_response(r, r->script);
    }
    if (r->path) {
        magi_response(r, "] with requested path [");
        magi_response(r, r->path);
    }
    magi_response(r, "]<br/>");
}

void response(magi_request *r)
{
    magi_response(r,
        "<!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>");

    magi_response(r, "<h1>Echo CGI Script</h1>");
    show_meta(r);

    magi_response(r, "<h2>Cookies:</h2>");
    list_cookies(r);

    magi_response(r, "<h2>Parameters:</h2>");
    list_params(r, r->meta);

    magi_response(r, "<h2>URL Parameters:</h2>");
    list_params(r, r->head);

    magi_response(r, "<h2>Body Parameters:</h2>");
    list_params(r, r->body);

    magi_response(r, "<h2>Files:</h2>");
    list_files(r);

    magi_response(r, "</body></html>");
}

int main()
{
    magi_request request;
    magi_request_init(&request);
    if (magi_cgi(&request)) {
        response(&request);
    } else {
        magi_response_error(&request);
    }
    magi_request_free(&request);
    return 0;
}