summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_fastcgi_record_parse.c
blob: 7d2ce32ee61c6b47d686266281e1b8ce07ab3158 (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


#define NXT_FASTCGI_DATA_MIDDLE         0
#define NXT_FASTCGI_DATA_END_ON_BORDER  1
#define NXT_FASTCGI_DATA_END            2


static nxt_int_t nxt_fastcgi_buffer(nxt_fastcgi_parse_t *fp, nxt_buf_t ***tail,
    nxt_buf_t *in);


void
nxt_fastcgi_record_parse(nxt_task_t *task, nxt_fastcgi_parse_t *fp,
    nxt_buf_t *in)
{
    u_char        ch;
    nxt_int_t     ret, stream;
    nxt_buf_t     *b, *nb, **tail[2];
    const char    *msg;
    enum {
        sw_fastcgi_version = 0,
        sw_fastcgi_type,
        sw_fastcgi_request_id_high,
        sw_fastcgi_request_id_low,
        sw_fastcgi_content_length_high,
        sw_fastcgi_content_length_low,
        sw_fastcgi_padding_length,
        sw_fastcgi_reserved,
        sw_fastcgi_data,
        sw_fastcgi_padding,
        sw_fastcgi_end_request,
    } state;

    fp->out[0] = NULL;
    fp->out[1] = NULL;

    tail[0] = &fp->out[0];
    tail[1] = &fp->out[1];

    state = fp->state;

    for (b = in; b != NULL; b = b->next) {

        if (nxt_buf_is_sync(b)) {
            **tail = b;
            *tail = &b->next;
            continue;
        }

        fp->pos = b->mem.pos;

        while (fp->pos < b->mem.free) {
            /*
             * The sw_fastcgi_data state is tested outside the
             * switch to preserve fp->pos and to not touch memory.
             */
            if (state == sw_fastcgi_data) {

                /*
                 * fp->type here can be only NXT_FASTCGI_STDOUT
                 * or NXT_FASTCGI_STDERR.  NXT_FASTCGI_END_REQUEST
                 * is tested in sw_fastcgi_reserved.
                 */
                stream = fp->type - NXT_FASTCGI_STDOUT;

                ret = nxt_fastcgi_buffer(fp, &tail[stream], b);

                if (ret == NXT_FASTCGI_DATA_MIDDLE) {
                    goto next;
                }

                if (nxt_slow_path(ret == NXT_ERROR)) {
                    fp->error = 1;
                    goto done;
                }

                if (fp->padding == 0) {
                    state = sw_fastcgi_version;

                } else {
                    state = sw_fastcgi_padding;
                }

                if (ret == NXT_FASTCGI_DATA_END_ON_BORDER) {
                    goto next;
                }

                /* ret == NXT_FASTCGI_DATA_END */
            }

            ch = *fp->pos++;

            nxt_thread_log_debug("fastcgi record byte: %02Xd", ch);

            switch (state) {

            case sw_fastcgi_version:
                if (nxt_fast_path(ch == 1)) {
                    state = sw_fastcgi_type;
                    continue;
                }

                msg = "unsupported FastCGI protocol version";
                goto fastcgi_error;

            case sw_fastcgi_type:
                switch (ch) {
                case NXT_FASTCGI_STDOUT:
                case NXT_FASTCGI_STDERR:
                case NXT_FASTCGI_END_REQUEST:
                    fp->type = ch;
                    state = sw_fastcgi_request_id_high;
                    continue;
                default:
                    msg = "invalid FastCGI record type";
                    goto fastcgi_error;
                }

            case sw_fastcgi_request_id_high:
                /* FastCGI multiplexing is not supported. */
                if (nxt_fast_path(ch == 0)) {
                    state = sw_fastcgi_request_id_low;
                    continue;
                }

                msg = "unexpected FastCGI request ID high byte";
                goto fastcgi_error;

            case sw_fastcgi_request_id_low:
                if (nxt_fast_path(ch == 1)) {
                    state = sw_fastcgi_content_length_high;
                    continue;
                }

                msg = "unexpected FastCGI request ID low byte";
                goto fastcgi_error;

            case sw_fastcgi_content_length_high:
                fp->length = ch << 8;
                state = sw_fastcgi_content_length_low;
                continue;

            case sw_fastcgi_content_length_low:
                fp->length |= ch;
                state = sw_fastcgi_padding_length;
                continue;

            case sw_fastcgi_padding_length:
                fp->padding = ch;
                state = sw_fastcgi_reserved;
                continue;

            case sw_fastcgi_reserved:
                nxt_thread_log_debug("fastcgi record type:%d "
                                     "length:%uz padding:%d",
                                     fp->type, fp->length, fp->padding);

                if (nxt_fast_path(fp->type != NXT_FASTCGI_END_REQUEST)) {
                    state = sw_fastcgi_data;
                    continue;
                }

                state = sw_fastcgi_end_request;
                continue;

            case sw_fastcgi_data:
                /*
                 * This state is processed before the switch.
                 * It added here just to suppress a warning.
                 */
                continue;

            case sw_fastcgi_padding:
                /*
                 * No special fast processing of padding
                 * because it usually takes just 1-7 bytes.
                 */
                fp->padding--;

                if (fp->padding == 0) {
                    nxt_thread_log_debug("fastcgi record end");
                    state = sw_fastcgi_version;
                }
                continue;

            case sw_fastcgi_end_request:
                /* Just skip 8 bytes of END_REQUEST. */
                fp->length--;

                if (fp->length != 0) {
                    continue;
                }

                fp->done = 1;

                nxt_thread_log_debug("fastcgi end request");

                goto done;
            }
        }

        if (b->retain == 0) {
            /* No record data was found in a buffer. */
            nxt_thread_current_work_queue_add(task->thread,
                                              b->completion_handler,
                                              task, b, b->parent);
        }

    next:

        continue;
    }

    fp->state = state;

    return;

fastcgi_error:

    nxt_thread_log_error(NXT_LOG_ERR, "upstream sent %s: %d", msg, ch);

    fp->fastcgi_error = 1;

done:

    nb = fp->last_buf(fp);

    if (nxt_fast_path(nb != NULL)) {
        *tail[0] = nb;

    } else {
        fp->error = 1;
    }

    // STUB: fp->fastcgi_error = 1;
    // STUB: fp->error = 1;

    return;
}


static nxt_int_t
nxt_fastcgi_buffer(nxt_fastcgi_parse_t *fp, nxt_buf_t ***tail, nxt_buf_t *in)
{
    u_char     *p;
    size_t     size;
    nxt_buf_t  *b;

    if (fp->length == 0) {
        return NXT_FASTCGI_DATA_END;
    }

    p = fp->pos;
    size = in->mem.free - p;

    if (fp->length >= size && in->retain == 0) {
        /*
         * Use original buffer if the buffer is lesser than or equal to
         * FastCGI record size and this is the first record in the buffer.
         */
        in->mem.pos = p;
        **tail = in;
        *tail = &in->next;

    } else {
        b = nxt_buf_mem_alloc(fp->mem_pool, 0, 0);
        if (nxt_slow_path(b == NULL)) {
            return NXT_ERROR;
        }

        **tail = b;
        *tail = &b->next;

        b->parent = in;
        in->retain++;
        b->mem.pos = p;
        b->mem.start = p;

        if (fp->length < size) {
            p += fp->length;
            fp->pos = p;

            b->mem.free = p;
            b->mem.end = p;

            return NXT_FASTCGI_DATA_END;
        }

        b->mem.free = in->mem.free;
        b->mem.end = in->mem.free;
    }

    fp->length -= size;

    if (fp->length == 0) {
        return NXT_FASTCGI_DATA_END_ON_BORDER;
    }

    return NXT_FASTCGI_DATA_MIDDLE;
}