summaryrefslogblamecommitdiffhomepage
path: root/src/nxt_http_parse.c
blob: fbc2f73ffb7ad5bceda5c78718e98f8823638fc6 (plain) (tree)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595


















































































































































































































































































































































































































































































































































































































                                                                                

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

#include <nxt_main.h>


static nxt_int_t nxt_http_split_header_part(nxt_http_split_header_parse_t *shp,
    u_char *start, u_char *end);
static nxt_int_t nxt_http_split_header_join(nxt_http_split_header_parse_t *shp);


nxt_int_t
nxt_http_status_parse(nxt_http_status_parse_t *sp, nxt_buf_mem_t *b)
{
    u_char  ch, *p;
    enum {
        sw_start = 0,
        sw_H,
        sw_HT,
        sw_HTT,
        sw_HTTP,
        sw_major_digit,
        sw_dot,
        sw_minor_digit,
        sw_space_after_version,
        sw_status_start,
        sw_status_code,
        sw_status_text,
        sw_end,
    } state;

    state = sp->state;

    for (p = b->pos; p < b->free; p++) {

        ch = *p;

        switch (state) {

        /* "HTTP/" */
        case sw_start:
            if (nxt_fast_path(ch == 'H')) {
                state = sw_H;
                continue;
            }

            return NXT_ERROR;

        case sw_H:
            if (nxt_fast_path(ch == 'T')) {
                state = sw_HT;
                continue;
            }

            return NXT_ERROR;

        case sw_HT:
            if (nxt_fast_path(ch == 'T')) {
                state = sw_HTT;
                continue;
            }

            return NXT_ERROR;

        case sw_HTT:
            if (nxt_fast_path(ch == 'P')) {
                state = sw_HTTP;
                continue;
            }

            return NXT_ERROR;

        case sw_HTTP:
            if (nxt_fast_path(ch == '/')) {
                state = sw_major_digit;
                continue;
            }

            return NXT_ERROR;

        /*
         * Only HTTP/x.x format is tested because it
         * is unlikely that other formats will appear.
         */
        case sw_major_digit:
            if (nxt_fast_path(ch >= '1' && ch <= '9')) {
                sp->http_version = 10 * (ch - '0');
                state = sw_dot;
                continue;
            }

            return NXT_ERROR;

        case sw_dot:
            if (nxt_fast_path(ch == '.')) {
                state = sw_minor_digit;
                continue;
            }

            return NXT_ERROR;

        case sw_minor_digit:
            if (nxt_fast_path(ch >= '0' && ch <= '9')) {
                sp->http_version += ch - '0';
                state = sw_space_after_version;
                continue;
            }

            return NXT_ERROR;

        case sw_space_after_version:
            if (nxt_fast_path(ch == ' ')) {
                state = sw_status_start;
                continue;
            }

            return NXT_ERROR;

        case sw_status_start:
            if (nxt_slow_path(ch == ' ')) {
                continue;
            }

            sp->start = p;
            state = sw_status_code;

            /* Fall through. */

        /* HTTP status code. */
        case sw_status_code:
            if (nxt_fast_path(ch >= '0' && ch <= '9')) {
                sp->code = sp->code * 10 + (ch - '0');
                continue;
            }

            switch (ch) {
            case ' ':
                state = sw_status_text;
                continue;
            case '.':                    /* IIS may send 403.1, 403.2, etc. */
                state = sw_status_text;
                continue;
            case NXT_CR:
                sp->end = p;
                state = sw_end;
                continue;
            case NXT_LF:
                sp->end = p;
                goto done;
            default:
                return NXT_ERROR;
            }

        /* Any text until end of line. */
        case sw_status_text:
            switch (ch) {
            case NXT_CR:
                sp->end = p;
                state = sw_end;
                continue;
            case NXT_LF:
                sp->end = p;
                goto done;
            }
            continue;

        /* End of status line. */
        case sw_end:
            if (nxt_fast_path(ch == NXT_LF)) {
                goto done;
            }

            return NXT_ERROR;
        }
    }

    b->pos = p;
    sp->state = state;

    return NXT_AGAIN;

done:

    b->pos = p + 1;

    return NXT_OK;
}


nxt_int_t
nxt_http_header_parse(nxt_http_header_parse_t *hp, nxt_buf_mem_t *b)
{
    u_char    c, ch, *p;
    uint32_t  hash;
    enum {
        sw_start = 0,
        sw_name,
        sw_space_before_value,
        sw_value,
        sw_space_after_value,
        sw_ignore_line,
        sw_almost_done,
        sw_header_almost_done,
    } state;

    static const u_char  normal[256]  nxt_aligned(64) =
        "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
        "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"

        /* These 64 bytes should reside in one cache line */
        "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"
        "\0abcdefghijklmnopqrstuvwxyz\0\0\0\0\0"

        "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
        "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
        "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
        "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

    nxt_prefetch(&normal[0]);
    nxt_prefetch(&normal[64]);

    state = hp->state;
    hash = hp->header_hash;

    for (p = b->pos; p < b->free; p++) {
        ch = *p;

        switch (state) {

        /* first char */
        case sw_start:
            hp->header_name_start = p;
            hp->invalid_header = 0;

            switch (ch) {
            case NXT_CR:
                hp->header_end = p;
                state = sw_header_almost_done;
                break;
            case NXT_LF:
                hp->header_end = p;
                goto header_done;
            default:
                state = sw_name;

                c = normal[ch];

                if (c) {
                    hash = nxt_djb_hash_add(NXT_DJB_HASH_INIT, c);
                    break;
                }

                if (ch == '_') {
                    hash = nxt_djb_hash_add(NXT_DJB_HASH_INIT, ch);
                    hp->underscore = 1;
                    break;
                }

                hp->invalid_header = 1;
                break;
            }
            break;

        /* header name */
        case sw_name:
            c = normal[ch];

            if (c) {
                hash = nxt_djb_hash_add(hash, c);
                break;
            }

            if (ch == ':') {
                hp->header_name_end = p;
                state = sw_space_before_value;
                break;
            }

            if (ch == NXT_CR) {
                hp->header_name_end = p;
                hp->header_start = p;
                hp->header_end = p;
                state = sw_almost_done;
                break;
            }

            if (ch == NXT_LF) {
                hp->header_name_end = p;
                hp->header_start = p;
                hp->header_end = p;
                goto done;
            }

            if (ch == '_') {
                hash = nxt_djb_hash_add(hash, ch);
                hp->underscore = 1;
                break;
            }

            /* IIS may send the duplicate "HTTP/1.1 ..." lines */
            if (ch == '/'
                && hp->upstream
                && p - hp->header_name_start == 4
                && nxt_memcmp(hp->header_name_start, "HTTP", 4) == 0)
            {
                state = sw_ignore_line;
                break;
            }

            hp->invalid_header = 1;
            break;

        /* space* before header value */
        case sw_space_before_value:
            switch (ch) {
            case ' ':
                break;
            case NXT_CR:
                hp->header_start = p;
                hp->header_end = p;
                state = sw_almost_done;
                break;
            case NXT_LF:
                hp->header_start = p;
                hp->header_end = p;
                goto done;
            case '\0':
                hp->invalid_header = 1;
                /* Fall through. */
            default:
                hp->header_start = p;
                state = sw_value;
                break;
            }
            break;

        /* header value */
        case sw_value:
            switch (ch) {
            case ' ':
                hp->header_end = p;
                state = sw_space_after_value;
                break;
            case NXT_CR:
                hp->header_end = p;
                state = sw_almost_done;
                break;
            case NXT_LF:
                hp->header_end = p;
                goto done;
            case '\0':
                hp->invalid_header = 1;
                break;
            }
            break;

        /* space* before end of header line */
        case sw_space_after_value:
            switch (ch) {
            case ' ':
                break;
            case NXT_CR:
                state = sw_almost_done;
                break;
            case NXT_LF:
                goto done;
            case '\0':
                hp->invalid_header = 1;
                /* Fall through. */
            default:
                state = sw_value;
                break;
            }
            break;

        /* ignore header line */
        case sw_ignore_line:
            switch (ch) {
            case NXT_LF:
                state = sw_start;
                break;
            default:
                break;
            }
            break;

        /* end of header line */
        case sw_almost_done:
            switch (ch) {
            case NXT_LF:
                goto done;
            case NXT_CR:
                break;
            default:
                return NXT_DECLINED;
            }
            break;

        /* end of header */
        case sw_header_almost_done:
            switch (ch) {
            case NXT_LF:
                goto header_done;
            default:
                return NXT_DECLINED;
            }
        }
    }

    b->pos = p;
    hp->state = state;
    hp->header_hash = hash;

    return NXT_AGAIN;

done:

    b->pos = p + 1;
    hp->state = sw_start;
    hp->header_hash = hash;

    return NXT_OK;

header_done:

    b->pos = p + 1;
    hp->state = sw_start;

    return NXT_DONE;
}


nxt_int_t
nxt_http_split_header_parse(nxt_http_split_header_parse_t *shp,
    nxt_buf_mem_t *b)
{
    u_char     *end;
    nxt_int_t  ret;

    if (shp->parts == NULL || nxt_array_is_empty(shp->parts)) {

        ret = nxt_http_header_parse(&shp->parse, b);

        if (nxt_fast_path(ret == NXT_OK)) {
            return ret;
        }

        if (nxt_fast_path(ret == NXT_AGAIN)) {
            /* A buffer is over. */

            if (shp->parse.state == 0) {
                /*
                 * A previous parsed header line is
                 * over right on the end of the buffer.
                 */
                return ret;
            }
            /*
             * Add the first header line part and return NXT_AGAIN on success.
             */
            return nxt_http_split_header_part(shp, shp->parse.header_name_start,
                                              b->pos);
        }

        return ret;
    }

    /* A header line is split in buffers. */

    end = nxt_memchr(b->pos, NXT_LF, b->free - b->pos);

    if (end != NULL) {

        /* The last header line part found. */
        end++;

        ret = nxt_http_split_header_part(shp, b->pos, end);

        if (nxt_fast_path(ret != NXT_ERROR)) {
            /* ret == NXT_AGAIN: success, mark the part if it were parsed. */
            b->pos = end;

            return nxt_http_split_header_join(shp);
        }

        return ret;
    }

    /* Add another header line part and return NXT_AGAIN on success. */

    return nxt_http_split_header_part(shp, b->pos, b->free);
}


static nxt_int_t
nxt_http_split_header_part(nxt_http_split_header_parse_t *shp, u_char *start,
    u_char *end)
{
    nxt_http_header_part_t  *part;

    nxt_thread_log_debug("http source header part: \"%*s\"",
                         end - start, start);

    if (shp->parts == NULL) {
        shp->parts = nxt_array_create(shp->mem_pool, 2,
                                     sizeof(nxt_http_header_part_t));
        if (nxt_slow_path(shp->parts == NULL)) {
            return NXT_ERROR;
        }
    }

    if (!nxt_array_is_empty(shp->parts)) {

        part = nxt_array_last(shp->parts);

        if (part->end == end) {
            part->end = end;
            return NXT_AGAIN;
        }
    }

    part = nxt_array_add(shp->parts);

    if (nxt_fast_path(part != NULL)) {
        part->start = start;
        part->end = end;
        return NXT_AGAIN;
    }

    return NXT_ERROR;
}


static nxt_int_t
nxt_http_split_header_join(nxt_http_split_header_parse_t *shp)
{
    u_char                  *p;
    size_t                  size;
    nxt_uint_t              n;
    nxt_buf_mem_t           b;
    nxt_http_header_part_t  *part;

    part = shp->parts->elts;
    n = shp->parts->nelts;

    if (n == 1) {
        /*
         * A header line was read by parts, but resides continuously in a
         * stream source buffer, so use disposition in the original buffer.
         */
        b.pos = part->start;
        b.free = part->end;

    } else {
        /* Join header line parts to store the header line and ot parse it. */

        size = 0;

        do {
            size += part->end - part->start;
            part++;
            n--;
        } while (n != 0);

        p = nxt_mem_alloc(shp->mem_pool, size);
        if (nxt_slow_path(p == NULL)) {
            return NXT_ERROR;
        }

        b.pos = p;

        part = shp->parts->elts;
        n = shp->parts->nelts;

        do {
            p = nxt_cpymem(p, part->start, part->end - part->start);
            part++;
            n--;
        } while (n != 0);

        b.free = p;
    }

    /* b.start and b.end are not required for parsing. */

    nxt_array_reset(shp->parts);

    /* Reset a header parse state to the sw_start. */
    shp->parse.state = 0;

    return nxt_http_header_parse(&shp->parse, &b);
}