summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_source.c
blob: 889dcd088eba63483a04e2a41eb5f5b704705ad4 (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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

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

#include <nxt_main.h>


typedef struct {
    nxt_http_chunk_parse_t  parse;
    nxt_source_hook_t       next;
} nxt_http_source_chunk_t;


static nxt_buf_t *nxt_http_source_request_create(nxt_http_source_t *hs);

static void nxt_http_source_status_filter(nxt_task_t *task, void *obj,
    void *data);
static void nxt_http_source_header_filter(nxt_task_t *task, void *obj,
    void *data);

static nxt_int_t nxt_http_source_header_line_process(nxt_http_source_t *hs);
static nxt_int_t nxt_http_source_content_length(nxt_upstream_source_t *us,
    nxt_name_value_t *nv);
static nxt_int_t nxt_http_source_transfer_encoding(nxt_upstream_source_t *us,
    nxt_name_value_t *nv);

static void nxt_http_source_header_ready(nxt_task_t *task,
    nxt_http_source_t *hs, nxt_buf_t *rest);
static void nxt_http_source_chunk_filter(nxt_task_t *task, void *obj,
    void *data);
static void nxt_http_source_chunk_error(nxt_task_t *task, void *obj,
    void *data);
static void nxt_http_source_body_filter(nxt_task_t *task, void *obj,
    void *data);

static void nxt_http_source_sync_buffer(nxt_task_t *task, nxt_http_source_t *hs,
    nxt_buf_t *b);
static void nxt_http_source_error(nxt_task_t *task,
    nxt_stream_source_t *stream);
static void nxt_http_source_fail(nxt_task_t *task, nxt_http_source_t *hs);
static void nxt_http_source_message(const char *msg, size_t len, u_char *p);


void
nxt_http_source_handler(nxt_task_t *task, nxt_upstream_source_t *us,
    nxt_http_source_request_create_t request_create)
{
    nxt_http_source_t    *hs;
    nxt_stream_source_t  *stream;

    hs = nxt_mp_zget(us->buffers.mem_pool, sizeof(nxt_http_source_t));
    if (nxt_slow_path(hs == NULL)) {
        goto fail;
    }

    us->protocol_source = hs;

    hs->header_in.list = nxt_list_create(us->buffers.mem_pool, 8,
                                         sizeof(nxt_name_value_t));
    if (nxt_slow_path(hs->header_in.list == NULL)) {
        goto fail;
    }

    hs->header_in.hash = us->header_hash;
    hs->upstream = us;
    hs->request_create = request_create;

    stream = us->stream;

    if (stream == NULL) {
        stream = nxt_mp_zget(us->buffers.mem_pool, sizeof(nxt_stream_source_t));
        if (nxt_slow_path(stream == NULL)) {
            goto fail;
        }

        us->stream = stream;
        stream->upstream = us;

    } else {
        nxt_memzero(stream, sizeof(nxt_stream_source_t));
    }

    /*
     * Create the HTTP source filter chain:
     *   stream source | HTTP status line filter
     */
    stream->next = &hs->query;
    stream->error_handler = nxt_http_source_error;

    hs->query.context = hs;
    hs->query.filter = nxt_http_source_status_filter;

    hs->header_in.content_length = -1;

    stream->out = nxt_http_source_request_create(hs);

    if (nxt_fast_path(stream->out != NULL)) {
        nxt_memzero(&hs->u.status_parse, sizeof(nxt_http_status_parse_t));

        nxt_stream_source_connect(task, stream);
        return;
    }

fail:

    nxt_http_source_fail(task, hs);
}


nxt_inline u_char *
nxt_http_source_copy(u_char *p, nxt_str_t *src, size_t len)
{
    u_char  *s;

    if (nxt_fast_path(len >= src->len)) {
        len = src->len;
        src->len = 0;

    } else {
        src->len -= len;
    }

    s = src->data;
    src->data += len;

    return nxt_cpymem(p, s, len);
}


static nxt_buf_t *
nxt_http_source_request_create(nxt_http_source_t *hs)
{
    nxt_int_t  ret;
    nxt_buf_t  *b, *req, **prev;

    nxt_thread_log_debug("http source create request");

    prev = &req;

new_buffer:

    ret = nxt_buf_pool_mem_alloc(&hs->upstream->buffers, 0);
    if (nxt_slow_path(ret != NXT_OK)) {
        return NULL;
    }

    b = hs->upstream->buffers.current;
    hs->upstream->buffers.current = NULL;

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

    for ( ;; ) {
        ret = hs->request_create(hs);

        if (nxt_fast_path(ret == NXT_OK)) {
            b->mem.free = nxt_http_source_copy(b->mem.free, &hs->u.request.copy,
                                               b->mem.end - b->mem.free);

            if (nxt_fast_path(hs->u.request.copy.len == 0)) {
                continue;
            }

            nxt_thread_log_debug("\"%*s\"", b->mem.free - b->mem.pos,
                                 b->mem.pos);

            goto new_buffer;
        }

        if (nxt_slow_path(ret == NXT_ERROR)) {
            return NULL;
        }

        /* ret == NXT_DONE */
        break;
    }

    nxt_thread_log_debug("\"%*s\"", b->mem.free - b->mem.pos, b->mem.pos);

    return req;
}


static void
nxt_http_source_status_filter(nxt_task_t *task, void *obj, void *data)
{
    nxt_int_t          ret;
    nxt_buf_t          *b;
    nxt_http_source_t  *hs;

    hs = obj;
    b = data;

    /*
     * No cycle over buffer chain is required since at
     * start the stream source passes buffers one at a time.
     */

    nxt_debug(task, "http source status filter");

    if (nxt_slow_path(nxt_buf_is_sync(b))) {
        nxt_http_source_sync_buffer(task, hs, b);
        return;
    }

    ret = nxt_http_status_parse(&hs->u.status_parse, &b->mem);

    if (nxt_fast_path(ret == NXT_OK)) {
        /*
         * Change the HTTP source filter chain:
         *    stream source | HTTP header filter
         */
        hs->query.filter = nxt_http_source_header_filter;

        nxt_debug(task, "upstream status: \"%*s\"",
                  hs->u.status_parse.end - b->mem.start, b->mem.start);

        hs->header_in.status = hs->u.status_parse.code;

        nxt_debug(task, "upstream version:%d status:%uD \"%*s\"",
                  hs->u.status_parse.http_version,
                  hs->u.status_parse.code,
                  hs->u.status_parse.end - hs->u.status_parse.start,
                  hs->u.status_parse.start);

        nxt_memzero(&hs->u.header, sizeof(nxt_http_split_header_parse_t));
        hs->u.header.mem_pool = hs->upstream->buffers.mem_pool;

        nxt_http_source_header_filter(task, hs, b);
        return;
    }

    if (nxt_slow_path(ret == NXT_ERROR)) {
        /* HTTP/0.9 response. */
        hs->header_in.status = 200;
        nxt_http_source_header_ready(task, hs, b);
        return;
    }

    /* ret == NXT_AGAIN */

    /*
     * b->mem.pos is always equal to b->mem.end because b is a buffer
     * which points to a response part read by the stream source.
     * However, since the stream source is an immediate source of the
     * status filter, b->parent is a buffer the stream source reads in.
     */
    if (b->parent->mem.pos == b->parent->mem.end) {
        nxt_http_source_message("upstream sent too long status line: \"%*s\"",
                                b->mem.pos - b->mem.start, b->mem.start);

        nxt_http_source_fail(task, hs);
    }
}


static void
nxt_http_source_header_filter(nxt_task_t *task, void *obj, void *data)
{
    nxt_int_t          ret;
    nxt_buf_t          *b;
    nxt_http_source_t  *hs;

    hs = obj;
    b = data;

    /*
     * No cycle over buffer chain is required since at
     * start the stream source passes buffers one at a time.
     */

    nxt_debug(task, "http source header filter");

    if (nxt_slow_path(nxt_buf_is_sync(b))) {
        nxt_http_source_sync_buffer(task, hs, b);
        return;
    }

    for ( ;; ) {
        ret = nxt_http_split_header_parse(&hs->u.header, &b->mem);

        if (nxt_slow_path(ret != NXT_OK)) {
            break;
        }

        ret = nxt_http_source_header_line_process(hs);

        if (nxt_slow_path(ret != NXT_OK)) {
            break;
        }
    }

    if (nxt_fast_path(ret == NXT_DONE)) {
        nxt_debug(task, "http source header done");
        nxt_http_source_header_ready(task, hs, b);
        return;
    }

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

    if (ret != NXT_ERROR) {
        /* ret == NXT_DECLINED: "\r" is not followed by "\n" */
        nxt_log(task, NXT_LOG_ERR,
                "upstream sent invalid header line: \"%*s\\r...\"",
                hs->u.header.parse.header_end
                    - hs->u.header.parse.header_name_start,
                hs->u.header.parse.header_name_start);
    }

    /* ret == NXT_ERROR */

    nxt_http_source_fail(task, hs);
}


static nxt_int_t
nxt_http_source_header_line_process(nxt_http_source_t *hs)
{
    size_t                     name_len;
    nxt_name_value_t           *nv;
    nxt_lvlhsh_query_t         lhq;
    nxt_http_header_parse_t    *hp;
    nxt_upstream_name_value_t  *unv;

    hp = &hs->u.header.parse;

    name_len = hp->header_name_end - hp->header_name_start;

    if (name_len > 255) {
        nxt_http_source_message("upstream sent too long header field name: "
                                "\"%*s\"", name_len, hp->header_name_start);
        return NXT_ERROR;
    }

    nv = nxt_list_add(hs->header_in.list);
    if (nxt_slow_path(nv == NULL)) {
        return NXT_ERROR;
    }

    nv->hash = hp->header_hash;
    nv->skip = 0;
    nv->name_len = name_len;
    nv->name_start = hp->header_name_start;
    nv->value_len = hp->header_end - hp->header_start;
    nv->value_start = hp->header_start;

    nxt_thread_log_debug("upstream header: \"%*s: %*s\"",
                         nv->name_len, nv->name_start,
                         nv->value_len, nv->value_start);

    lhq.key_hash = nv->hash;
    lhq.key.len = nv->name_len;
    lhq.key.data = nv->name_start;
    lhq.proto = &nxt_upstream_header_hash_proto;

    if (nxt_lvlhsh_find(&hs->header_in.hash, &lhq) == NXT_OK) {
        unv = lhq.value;

        if (unv->handler(hs->upstream, nv) != NXT_OK) {
            return NXT_ERROR;
        }
    }

    return NXT_OK;
}


static const nxt_upstream_name_value_t  nxt_http_source_headers[]
    nxt_aligned(32) =
{
    { nxt_http_source_content_length,
      nxt_upstream_name_value("content-length") },

    { nxt_http_source_transfer_encoding,
      nxt_upstream_name_value("transfer-encoding") },
};


nxt_int_t
nxt_http_source_hash_create(nxt_mp_t *mp, nxt_lvlhsh_t *lh)
{
    return nxt_upstream_header_hash_add(mp, lh, nxt_http_source_headers,
                                        nxt_nitems(nxt_http_source_headers));
}


static nxt_int_t
nxt_http_source_content_length(nxt_upstream_source_t *us, nxt_name_value_t *nv)
{
    nxt_off_t          length;
    nxt_http_source_t  *hs;

    length = nxt_off_t_parse(nv->value_start, nv->value_len);

    if (nxt_fast_path(length > 0)) {
        hs = us->protocol_source;
        hs->header_in.content_length = length;
        return NXT_OK;
    }

    return NXT_ERROR;
}


static nxt_int_t
nxt_http_source_transfer_encoding(nxt_upstream_source_t *us,
    nxt_name_value_t *nv)
{
    u_char             *end;
    nxt_http_source_t  *hs;

    end = nv->value_start + nv->value_len;

    if (nxt_memcasestrn(nv->value_start, end, "chunked", 7) != NULL) {
        hs = us->protocol_source;
        hs->chunked = 1;
    }

    return NXT_OK;
}


static void
nxt_http_source_header_ready(nxt_task_t *task, nxt_http_source_t *hs,
    nxt_buf_t *rest)
{
    nxt_buf_t                *b;
    nxt_upstream_source_t    *us;
    nxt_http_source_chunk_t  *hsc;

    us = hs->upstream;

    /* Free buffers used for request header. */

    for (b = us->stream->out; b != NULL; b = b->next) {
        nxt_buf_pool_free(&us->buffers, b);
    }

    if (nxt_fast_path(nxt_buf_pool_available(&us->buffers))) {

        if (hs->chunked) {
            hsc = nxt_mp_zalloc(hs->upstream->buffers.mem_pool,
                                sizeof(nxt_http_source_chunk_t));
            if (nxt_slow_path(hsc == NULL)) {
                goto fail;
            }

            /*
             * Change the HTTP source filter chain:
             *    stream source | chunk filter | HTTP body filter
             */
            hs->query.context = hsc;
            hs->query.filter = nxt_http_source_chunk_filter;

            hsc->next.context = hs;
            hsc->next.filter = nxt_http_source_body_filter;

            hsc->parse.mem_pool = hs->upstream->buffers.mem_pool;

            if (nxt_buf_mem_used_size(&rest->mem) != 0) {
                hs->rest = nxt_http_chunk_parse(task, &hsc->parse, rest);

                if (nxt_slow_path(hs->rest == NULL)) {
                    goto fail;
                }
            }

        } else {
            /*
             * Change the HTTP source filter chain:
             *    stream source | HTTP body filter
             */
            hs->query.filter = nxt_http_source_body_filter;

            if (nxt_buf_mem_used_size(&rest->mem) != 0) {
                hs->rest = rest;
            }
        }

        hs->upstream->state->ready_handler(hs);
        return;
    }

    nxt_thread_log_error(NXT_LOG_ERR, "%d buffers %uDK each "
                         "are not enough to read upstream response",
                         us->buffers.max, us->buffers.size / 1024);
fail:

    nxt_http_source_fail(task, hs);
}


static void
nxt_http_source_chunk_filter(nxt_task_t *task, void *obj, void *data)
{
    nxt_buf_t                *b;
    nxt_http_source_t        *hs;
    nxt_http_source_chunk_t  *hsc;

    hsc = obj;
    b = data;

    nxt_debug(task, "http source chunk filter");

    b = nxt_http_chunk_parse(task, &hsc->parse, b);

    hs = hsc->next.context;

    if (hsc->parse.error) {
        nxt_http_source_fail(task, hs);
        return;
    }

    if (hsc->parse.chunk_error) {
        /* Output all parsed before a chunk error and close upstream. */
        nxt_thread_current_work_queue_add(task->thread,
                                          nxt_http_source_chunk_error,
                                          task, hs, NULL);
    }

    if (b != NULL) {
        nxt_source_filter(task->thread, hs->upstream->work_queue, task,
                          &hsc->next, b);
    }
}


static void
nxt_http_source_chunk_error(nxt_task_t *task, void *obj, void *data)
{
    nxt_http_source_t  *hs;

    hs = obj;

    nxt_http_source_fail(task, hs);
}


/*
 * The HTTP source body filter accumulates first body buffers before the next
 * filter will be established and sets completion handler for the last buffer.
 */

static void
nxt_http_source_body_filter(nxt_task_t *task, void *obj, void *data)
{
    nxt_buf_t          *b, *in;
    nxt_http_source_t  *hs;

    hs = obj;
    in = data;

    nxt_debug(task, "http source body filter");

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

        if (nxt_buf_is_last(b)) {
            b->data = hs->upstream->data;
            b->completion_handler = hs->upstream->state->completion_handler;
        }
    }

    if (hs->next != NULL) {
        nxt_source_filter(task->thread, hs->upstream->work_queue, task,
                          hs->next, in);
        return;
    }

    nxt_buf_chain_add(&hs->rest, in);
}


static void
nxt_http_source_sync_buffer(nxt_task_t *task, nxt_http_source_t *hs,
    nxt_buf_t *b)
{
    if (nxt_buf_is_last(b)) {
        nxt_log(task, NXT_LOG_ERR,
                "upstream closed prematurely connection");

    } else {
        nxt_log(task, NXT_LOG_ERR,"%ui buffers %uz each are not "
                "enough to process upstream response header",
                hs->upstream->buffers.max, hs->upstream->buffers.size);
    }

    /* The stream source sends only the last and the nobuf sync buffer. */

    nxt_http_source_fail(task, hs);
}


static void
nxt_http_source_error(nxt_task_t *task, nxt_stream_source_t *stream)
{
    nxt_http_source_t  *hs;

    nxt_thread_log_debug("http source error");

    hs = stream->next->context;
    nxt_http_source_fail(task, hs);
}


static void
nxt_http_source_fail(nxt_task_t *task, nxt_http_source_t *hs)
{
    nxt_debug(task, "http source fail");

    /* TODO: fail, next upstream, or bad gateway */

    hs->upstream->state->error_handler(task, hs, NULL);
}


static void
nxt_http_source_message(const char *msg, size_t len, u_char *p)
{
    if (len > NXT_MAX_ERROR_STR - 300) {
        len = NXT_MAX_ERROR_STR - 300;
        p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
    }

    nxt_thread_log_error(NXT_LOG_ERR, msg, len, p);
}