File: test_cgi.c

package info (click to toggle)
libapreq2 2.13-7~deb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,700 kB
  • sloc: sh: 9,378; ansic: 8,043; perl: 5,834; cpp: 380; makefile: 299
file content (126 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download | duplicates (9)
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
126
/*
**  Licensed to the Apache Software Foundation (ASF) under one or more
** contributor license agreements.  See the NOTICE file distributed with
** this work for additional information regarding copyright ownership.
** The ASF licenses this file to You under the Apache License, Version 2.0
** (the "License"); you may not use this file except in compliance with
** the License.  You may obtain a copy of the License at
**
**      http://www.apache.org/licenses/LICENSE-2.0
**
**  Unless required by applicable law or agreed to in writing, software
**  distributed under the License is distributed on an "AS IS" BASIS,
**  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
**  See the License for the specific language governing permissions and
**  limitations under the License.
*/

#include "apreq_module.h"
#include "apreq_util.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_tables.h"
#include <stdlib.h>

static int dump_table(void *count, const char *key, const char *value)
{
    int *c = (int *) count;
    int value_len = key - value - 1; /* == strlen(value) by construction */
    if (value_len)
        *c += strlen(key) + value_len;
    return 1;
}

#define cookie_bake(c) apr_file_printf(out, "Set-Cookie: %s\n", \
                                          apreq_cookie_as_string(c, pool))

#define cookie_bake2(c) apr_file_printf(out, "Set-Cookie2: %s\n", \
                                          apreq_cookie_as_string(c, pool))


int main(int argc, char const * const * argv)
{
    apr_pool_t *pool;
    apreq_handle_t *req;
    const apreq_param_t *foo, *bar, *test, *key;
    apr_file_t *out;

    atexit(apr_terminate);
    if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
        fprintf(stderr, "apr_app_initialize failed\n");
        exit(-1);
    }

    if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
        fprintf(stderr, "apr_pool_create failed\n");
        exit(-1);
    }

    if (apreq_initialize(pool) != APR_SUCCESS) {
        fprintf(stderr, "apreq_initialize failed\n");
        exit(-1);
    }

    req = apreq_handle_cgi(pool);

    apr_file_open_stdout(&out, pool);

    foo = apreq_param(req, "foo");
    bar = apreq_param(req, "bar");

    test = apreq_param(req, "test");
    key  = apreq_param(req, "key");

    if (foo || bar) {
        apr_file_printf(out, "%s", "Content-Type: text/plain\n\n");

        if (foo) {
            apr_file_printf(out, "\t%s => %s\n", "foo", foo->v.data);
        }
        if (bar) {
            apr_file_printf(out, "\t%s => %s\n", "bar", bar->v.data);
        }
    }

    else if (test && key) {
        apreq_cookie_t *cookie;
        char *dest;
        apr_size_t dlen;

        cookie = apreq_cookie(req, key->v.data);

        if (cookie == NULL) {
            exit(-1);
        }

        if (strcmp(test->v.data, "bake") == 0) {
            cookie_bake(cookie);
        }
        else if (strcmp(test->v.data, "bake2") == 0) {
            cookie_bake2(cookie);
        }

        apr_file_printf(out, "%s", "Content-Type: text/plain\n\n");
        dest = apr_pcalloc(pool, cookie->v.dlen + 1);
        if (apreq_decode(dest, &dlen, cookie->v.data,
                         cookie->v.dlen) == APR_SUCCESS)
            apr_file_printf(out, "%s", dest);
        else {
            exit(-1);
        }
    }

    else {
        const apr_table_t *params = apreq_params(req, pool);
        int count = 0;
        apr_file_printf(out, "%s", "Content-Type: text/plain\n\n");

        if (params == NULL) {
            exit(-1);
        }
        apr_table_do(dump_table, &count, params, NULL);
        apr_file_printf(out, "%d", count);
    }

    return 0;
}