summaryrefslogtreecommitdiff
path: root/echo-nginx-module-0.63/src/ngx_http_echo_foreach.c
blob: a4a2b5467a38d7acc9e0a8c398d7aad192c2b180 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include "ngx_http_echo_foreach.h"
#include "ngx_http_echo_util.h"

#include <nginx.h>


ngx_int_t
ngx_http_echo_it_variable(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    ngx_http_echo_ctx_t         *ctx;
    ngx_uint_t                   i;
    ngx_array_t                 *choices;
    ngx_str_t                   *choice_elts, *choice;

    ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);

    if (ctx && ctx->foreach != NULL) {

        choices = ctx->foreach->choices;
        i = ctx->foreach->next_choice;

        if (i < choices->nelts) {
            choice_elts = choices->elts;
            choice = &choice_elts[i];

            v->len = choice->len;
            v->data = choice->data;
            v->valid = 1;
            v->no_cacheable = 1;
            v->not_found = 0;

            return NGX_OK;
        }
    }

    v->not_found = 1;

    return NGX_OK;
}


ngx_int_t
ngx_http_echo_exec_echo_foreach_split(ngx_http_request_t *r,
    ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
{
    ngx_http_echo_loc_conf_t    *elcf;
    ngx_str_t                   *delimiter, *compound;
    u_char                      *pos, *last, *end;
    ngx_str_t                   *choice;
    ngx_str_t                   *computed_arg_elts;
    ngx_array_t                 *cmds;
    ngx_http_echo_cmd_t         *cmd;
    ngx_http_echo_cmd_t         *cmd_elts;

    if (ctx->foreach != NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "Nested echo_foreach not supported yet.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (computed_args->nelts < 2) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "echo_foreach should take at least two arguments. "
                      "(if your delimiter starts with \"-\", preceding it "
                      "with a \"--\".)");

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    computed_arg_elts = computed_args->elts;

    compound  = &computed_arg_elts[1];

    dd("HEY coumpound len: %u", (int) compound->len);

    ctx->foreach = ngx_palloc(r->pool, sizeof(ngx_http_echo_foreach_ctx_t));

    if (ctx->foreach == NULL) {
        return NGX_ERROR;
    }

    ctx->foreach->cmd_index = ctx->next_handler_cmd;

    ctx->foreach->next_choice = 0;

    ctx->foreach->choices = ngx_array_create(r->pool, 10, sizeof(ngx_str_t));
    if (ctx->foreach->choices == NULL) {
        return NGX_ERROR;
    }

    delimiter = &computed_arg_elts[0];

    pos = compound->data;
    end = compound->data + compound->len;

    while ((last = ngx_http_echo_strlstrn(pos, end, delimiter->data,
                                          delimiter->len - 1)) != NULL)
    {
        dd("entered the loop");

        if (last == pos) {
            dd("!!! len == 0");
            pos = last + delimiter->len;
            continue;
        }

        choice = ngx_array_push(ctx->foreach->choices);
        if (choice == NULL) {
            return NGX_ERROR;
        }

        choice->data = pos;
        choice->len  = last - pos;
        pos = last + delimiter->len;
    }

    if (pos < end) {
        choice = ngx_array_push(ctx->foreach->choices);
        if (choice == NULL) {
            return NGX_ERROR;
        }

        choice->data = pos;
        choice->len  = end - pos;
    }

    if (ctx->foreach->choices->nelts == 0) {
        /* skip the foreach body entirely */
        elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
        cmds = elcf->handler_cmds;
        cmd_elts = cmds->elts;
        for (/* void */; ctx->next_handler_cmd < cmds->nelts;
             ctx->next_handler_cmd++)
        {
            cmd = &cmd_elts[ctx->next_handler_cmd + 1];
            if (cmd->opcode == echo_opcode_echo_end) {
                return NGX_OK;
            }
        }

    }

    return NGX_OK;
}


ngx_int_t
ngx_http_echo_exec_echo_end(ngx_http_request_t *r,
    ngx_http_echo_ctx_t *ctx)
{
    if (ctx->foreach == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "Found a echo_end that has no corresponding echo_foreach "
                      "before it.");
        return NGX_ERROR;
    }

    ctx->foreach->next_choice++;

    if (ctx->foreach->next_choice >= ctx->foreach->choices->nelts) {
        /* TODO We need to explicitly free the foreach ctx from
         * the pool */
        ctx->foreach = NULL;

        return NGX_OK;
    }

    dd("echo_end: ++ next_choice (total: %u): %u",
       (unsigned) ctx->foreach->choices->nelts,
       (unsigned) ctx->foreach->next_choice);

    /* the main handler dispatcher loop will increment
     *   ctx->next_handler_cmd for us anyway. */
    ctx->next_handler_cmd = ctx->foreach->cmd_index;

    return NGX_OK;
}