~ubuntu-branches/debian/squeeze/nginx/squeeze

« back to all changes in this revision

Viewing changes to src/http/ngx_http_request.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2009-05-31 18:38:56 UTC
  • mfrom: (1.1.10 upstream) (4.1.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20090531183856-3xhvf6wd0bw5556i
Tags: 0.7.59-1
* New upstream release, first in Debian for the 0.7 branch. Among other
  issues, it also fixes the problem with wildcard dns names used with SSL.
  (Closes: #515904)
* debian/watch: updated.
* debian/postinst: fixed a bashism. (Closes: #507913)
* debian/conf/nginx.conf: removed default_type. (Closes: #509390)
* debian/control: updated Standards-Version to 3.8.1, no changes needed.
* debian/NEWS.Debian: documented the issues with
  server_names_hash_bucket_size. (Closes: #524785)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
#include <ngx_config.h>
8
8
#include <ngx_core.h>
9
 
#include <ngx_event.h>
10
9
#include <ngx_http.h>
11
10
 
12
11
 
39
38
static void ngx_http_request_handler(ngx_event_t *ev);
40
39
static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
41
40
static void ngx_http_writer(ngx_http_request_t *r);
 
41
static void ngx_http_request_finalizer(ngx_http_request_t *r);
42
42
 
43
 
static void ngx_http_test_reading(ngx_http_request_t *r);
44
43
static void ngx_http_set_keepalive(ngx_http_request_t *r);
45
44
static void ngx_http_keepalive_handler(ngx_event_t *ev);
46
45
static void ngx_http_set_lingering_close(ngx_http_request_t *r);
48
47
static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
49
48
static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
50
49
static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
 
50
static void ngx_http_log_request(ngx_http_request_t *r);
51
51
static void ngx_http_close_connection(ngx_connection_t *c);
52
52
 
53
53
static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
215
215
 
216
216
    ngx_add_timer(rev, c->listening->post_accept_timeout);
217
217
 
218
 
    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
218
    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
219
219
#if (NGX_STAT_STUB)
220
220
        ngx_atomic_fetch_add(ngx_stat_reading, -1);
221
221
#endif
232
232
    ngx_uint_t                  i;
233
233
    ngx_connection_t           *c;
234
234
    ngx_http_request_t         *r;
235
 
    ngx_http_in_port_t         *hip;
236
 
    ngx_http_in_addr_t         *hia;
 
235
    struct sockaddr_in         *sin;
 
236
    ngx_http_port_t            *port;
 
237
    ngx_http_in_addr_t         *addr;
237
238
    ngx_http_log_ctx_t         *ctx;
 
239
    ngx_http_addr_conf_t       *addr_conf;
238
240
    ngx_http_connection_t      *hc;
239
241
    ngx_http_core_srv_conf_t   *cscf;
240
242
    ngx_http_core_loc_conf_t   *clcf;
241
243
    ngx_http_core_main_conf_t  *cmcf;
 
244
#if (NGX_HAVE_INET6)
 
245
    struct sockaddr_in6        *sin6;
 
246
    ngx_http_in6_addr_t        *addr6;
 
247
#endif
242
248
 
243
249
#if (NGX_STAT_STUB)
244
250
    ngx_atomic_fetch_add(ngx_stat_reading, -1);
292
298
 
293
299
    /* find the server configuration for the address:port */
294
300
 
295
 
    /* AF_INET only */
296
 
 
297
 
    hip = c->listening->servers;
298
 
    hia = hip->addrs;
299
 
 
300
 
    r->port = hip->port;
301
 
    r->port_text = &hip->port_text;
302
 
 
303
 
    i = 0;
 
301
    port = c->listening->servers;
304
302
 
305
303
    r->connection = c;
306
304
 
307
 
    if (hip->naddrs > 1) {
 
305
    if (port->naddrs > 1) {
308
306
 
309
307
        /*
310
 
         * There are several addresses on this port and one of them
311
 
         * is the "*:port" wildcard so getsockname() is needed to determine
312
 
         * the server address.
313
 
         *
314
 
         * AcceptEx() already has given this address.
 
308
         * there are several addresses on this port and one of them
 
309
         * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
 
310
         * is required to determine a server address
315
311
         */
316
312
 
317
 
#if (NGX_WIN32)
318
 
        if (c->local_sockaddr) {
319
 
            r->in_addr =
320
 
                   ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
321
 
 
322
 
        } else
 
313
        if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
 
314
            ngx_http_close_connection(c);
 
315
            return;
 
316
        }
 
317
 
 
318
        switch (c->local_sockaddr->sa_family) {
 
319
 
 
320
#if (NGX_HAVE_INET6)
 
321
        case AF_INET6:
 
322
            sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
 
323
 
 
324
            addr6 = port->addrs;
 
325
 
 
326
            /* the last address is "*" */
 
327
 
 
328
            for (i = 0; i < port->naddrs - 1; i++) {
 
329
                if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
 
330
                    break;
 
331
                }
 
332
            }
 
333
 
 
334
            addr_conf = &addr6[i].conf;
 
335
 
 
336
            break;
323
337
#endif
324
 
        {
325
 
            if (ngx_http_server_addr(r, NULL) != NGX_OK) {
326
 
                ngx_http_close_connection(c);
327
 
                return;
328
 
            }
329
 
        }
330
 
 
331
 
        /* the last address is "*" */
332
 
 
333
 
        for ( /* void */ ; i < hip->naddrs - 1; i++) {
334
 
            if (hia[i].addr == r->in_addr) {
335
 
                break;
336
 
            }
 
338
 
 
339
        default: /* AF_INET */
 
340
            sin = (struct sockaddr_in *) c->local_sockaddr;
 
341
 
 
342
            addr = port->addrs;
 
343
 
 
344
            /* the last address is "*" */
 
345
 
 
346
            for (i = 0; i < port->naddrs - 1; i++) {
 
347
                if (addr[i].addr == sin->sin_addr.s_addr) {
 
348
                    break;
 
349
                }
 
350
            }
 
351
 
 
352
            addr_conf = &addr[i].conf;
 
353
 
 
354
            break;
337
355
        }
338
356
 
339
357
    } else {
340
 
        r->in_addr = hia[0].addr;
 
358
 
 
359
        switch (c->local_sockaddr->sa_family) {
 
360
 
 
361
#if (NGX_HAVE_INET6)
 
362
        case AF_INET6:
 
363
            addr6 = port->addrs;
 
364
            addr_conf = &addr6[0].conf;
 
365
            break;
 
366
#endif
 
367
 
 
368
        default: /* AF_INET */
 
369
            addr = port->addrs;
 
370
            addr_conf = &addr[0].conf;
 
371
            break;
 
372
        }
341
373
    }
342
374
 
343
 
    r->virtual_names = hia[i].virtual_names;
 
375
    r->virtual_names = addr_conf->virtual_names;
344
376
 
345
377
    /* the default server configuration for the address:port */
346
 
    cscf = hia[i].core_srv_conf;
 
378
    cscf = addr_conf->core_srv_conf;
347
379
 
348
380
    r->main_conf = cscf->ctx->main_conf;
349
381
    r->srv_conf = cscf->ctx->srv_conf;
357
389
    ngx_http_ssl_srv_conf_t  *sscf;
358
390
 
359
391
    sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
360
 
    if (sscf->enable) {
 
392
    if (sscf->enable || addr_conf->ssl) {
361
393
 
362
394
        if (c->ssl == NULL) {
 
395
 
 
396
            c->log->action = "SSL handshaking";
 
397
 
 
398
            if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
 
399
                ngx_log_error(NGX_LOG_ERR, c->log, 0,
 
400
                              "no \"ssl_certificate\" is defined "
 
401
                              "in server listening on SSL port");
 
402
                ngx_http_close_connection(c);
 
403
                return;
 
404
            }
 
405
 
363
406
            if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
364
 
                == NGX_ERROR)
 
407
                != NGX_OK)
365
408
            {
366
409
                ngx_http_close_connection(c);
367
410
                return;
377
420
#endif
378
421
 
379
422
    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
380
 
    c->log->file = clcf->err_log->file;
 
423
    c->log->file = clcf->error_log->file;
381
424
    if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
382
 
        c->log->log_level = clcf->err_log->log_level;
 
425
        c->log->log_level = clcf->error_log->log_level;
383
426
    }
384
427
 
385
428
    if (c->buffer == NULL) {
404
447
 
405
448
    if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
406
449
                      sizeof(ngx_table_elt_t))
407
 
        == NGX_ERROR)
 
450
        != NGX_OK)
408
451
    {
409
452
        ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
410
453
        return;
493
536
            ngx_add_timer(rev, c->listening->post_accept_timeout);
494
537
        }
495
538
 
496
 
        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
539
        if (ngx_handle_read_event(rev, 0) != NGX_OK) {
497
540
            ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
498
541
        }
499
542
 
529
572
        }
530
573
    }
531
574
 
 
575
    c->log->action = "reading client request line";
 
576
 
532
577
    rev->handler = ngx_http_process_request_line;
533
578
    ngx_http_process_request_line(rev);
534
579
}
571
616
int
572
617
ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
573
618
{
 
619
    size_t                    len;
574
620
    const char               *servername;
575
621
    ngx_connection_t         *c;
576
622
    ngx_http_request_t       *r;
587
633
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
588
634
                   "SSL server name: \"%s\"", servername);
589
635
 
 
636
    len = ngx_strlen(servername);
 
637
 
 
638
    if (len == 0) {
 
639
        return SSL_TLSEXT_ERR_NOACK;
 
640
    }
 
641
 
590
642
    r = c->data;
591
643
 
592
 
    if (ngx_http_find_virtual_server(r, (u_char *) servername,
593
 
                                     ngx_strlen(servername))
594
 
        != NGX_OK)
595
 
    {
 
644
    if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) {
596
645
        return SSL_TLSEXT_ERR_NOACK;
597
646
    }
598
647
 
661
710
 
662
711
            if (r->complex_uri || r->quoted_uri) {
663
712
 
664
 
                r->uri.data = ngx_palloc(r->pool, r->uri.len + 1);
 
713
                r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
665
714
                if (r->uri.data == NULL) {
666
715
                    ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
667
716
                    return;
757
806
 
758
807
            if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
759
808
                              sizeof(ngx_table_elt_t))
760
 
                == NGX_ERROR)
 
809
                != NGX_OK)
761
810
            {
762
811
                ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
763
812
                return;
766
815
 
767
816
            if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
768
817
                               sizeof(ngx_table_elt_t *))
769
 
                == NGX_ERROR)
 
818
                != NGX_OK)
770
819
            {
771
820
                ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
772
821
                return;
821
870
    ssize_t                     n;
822
871
    ngx_int_t                   rc, rv;
823
872
    ngx_str_t                   header;
824
 
    ngx_uint_t                  i;
825
873
    ngx_table_elt_t            *h;
826
874
    ngx_connection_t           *c;
827
875
    ngx_http_header_t          *hh;
886
934
            }
887
935
        }
888
936
 
889
 
        rc = ngx_http_parse_header_line(r, r->header_in);
 
937
        rc = ngx_http_parse_header_line(r, r->header_in,
 
938
                                        cscf->underscores_in_headers);
890
939
 
891
940
        if (rc == NGX_OK) {
892
941
 
921
970
            h->value.data = r->header_start;
922
971
            h->value.data[h->value.len] = '\0';
923
972
 
924
 
            h->lowcase_key = ngx_palloc(r->pool, h->key.len);
 
973
            h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
925
974
            if (h->lowcase_key == NULL) {
926
975
                ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
927
976
                return;
931
980
                ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
932
981
 
933
982
            } else {
934
 
                for (i = 0; i < h->key.len; i++) {
935
 
                    h->lowcase_key[i] = ngx_tolower(h->key.data[i]);
936
 
                }
 
983
                ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
937
984
            }
938
985
 
939
986
            hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
1022
1069
            ngx_add_timer(rev, cscf->client_header_timeout);
1023
1070
        }
1024
1071
 
1025
 
        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
1072
        if (ngx_handle_read_event(rev, 0) != NGX_OK) {
1026
1073
            ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1027
1074
            return NGX_ERROR;
1028
1075
        }
1288
1335
ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
1289
1336
    ngx_uint_t offset)
1290
1337
{
1291
 
    u_char  *ua, *user_agent;
 
1338
    u_char  *user_agent, *msie;
1292
1339
 
1293
1340
    if (r->headers_in.user_agent) {
1294
1341
        return NGX_OK;
1300
1347
 
1301
1348
    user_agent = h->value.data;
1302
1349
 
1303
 
    ua = ngx_strstrn(user_agent, "MSIE", 4 - 1);
 
1350
    msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
1304
1351
 
1305
 
    if (ua && ua + 8 < user_agent + h->value.len) {
 
1352
    if (msie && msie + 7 < user_agent + h->value.len) {
1306
1353
 
1307
1354
        r->headers_in.msie = 1;
1308
1355
 
1309
 
        if (ua[4] == ' ' && ua[5] == '4' && ua[6] == '.') {
1310
 
            r->headers_in.msie4 = 1;
 
1356
        if (msie[6] == '.') {
 
1357
 
 
1358
            switch (msie[5]) {
 
1359
            case '4':
 
1360
                r->headers_in.msie4 = 1;
 
1361
                /* fall through */
 
1362
            case '5':
 
1363
            case '6':
 
1364
                r->headers_in.msie6 = 1;
 
1365
            }
1311
1366
        }
1312
1367
 
1313
1368
#if 0
1322
1377
        r->headers_in.opera = 1;
1323
1378
        r->headers_in.msie = 0;
1324
1379
        r->headers_in.msie4 = 0;
 
1380
        r->headers_in.msie6 = 0;
1325
1381
    }
1326
1382
 
1327
1383
    if (!r->headers_in.msie && !r->headers_in.opera) {
1387
1443
        }
1388
1444
    }
1389
1445
 
1390
 
    if (r->method & (NGX_HTTP_POST|NGX_HTTP_PUT)
1391
 
        && r->headers_in.content_length_n == -1)
1392
 
    {
 
1446
    if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
1393
1447
        ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
1394
1448
                  "client sent %V method without \"Content-Length\" header",
1395
1449
                  &r->method_name);
1449
1503
 
1450
1504
        sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
1451
1505
 
1452
 
        if (sscf->verify) {
 
1506
        if (sscf->verify == 1) {
1453
1507
            rc = SSL_get_verify_result(c->ssl->connection);
1454
1508
 
1455
1509
            if (rc != X509_V_OK) {
1500
1554
 
1501
1555
    ngx_http_handler(r);
1502
1556
 
1503
 
    return;
 
1557
    ngx_http_run_posted_requests(c);
1504
1558
}
1505
1559
 
1506
1560
 
1533
1587
            continue;
1534
1588
        }
1535
1589
 
1536
 
        if (ch == '/' || ch == '\0') {
1537
 
            return -1;
1538
 
        }
1539
 
 
1540
 
#if (NGX_WIN32)
1541
 
        if (ch == '\\') {
1542
 
            return -1;
1543
 
        }
1544
 
#endif
 
1590
        if (ngx_path_separator(ch) || ch == '\0') {
 
1591
            return -1;
 
1592
        }
1545
1593
    }
1546
1594
 
1547
1595
    if (dot) {
1555
1603
static ngx_int_t
1556
1604
ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
1557
1605
{
1558
 
    u_char                    *server, ch;
1559
 
    ngx_uint_t                 i, hash;
 
1606
    u_char                    *server;
 
1607
    ngx_uint_t                 hash;
1560
1608
    ngx_http_core_loc_conf_t  *clcf;
1561
1609
    ngx_http_core_srv_conf_t  *cscf;
1562
1610
    u_char                     buf[32];
1563
1611
 
1564
 
    if (len == 0 || r->virtual_names == NULL) {
 
1612
    if (r->virtual_names == NULL) {
1565
1613
        return NGX_DECLINED;
1566
1614
    }
1567
1615
 
1569
1617
        server = buf;
1570
1618
 
1571
1619
    } else {
1572
 
        server = ngx_palloc(r->pool, len);
 
1620
        server = ngx_pnalloc(r->pool, len);
1573
1621
        if (server == NULL) {
1574
1622
            return NGX_ERROR;
1575
1623
        }
1576
1624
    }
1577
1625
 
1578
 
    hash = 0;
1579
 
 
1580
 
    for (i = 0; i < len; i++) {
1581
 
        ch = host[i];
1582
 
 
1583
 
        ch = ngx_tolower(ch);
1584
 
        server[i] = ch;
1585
 
 
1586
 
        hash = ngx_hash(hash, ch);
1587
 
    }
 
1626
    hash = ngx_hash_strlow(server, host, len);
1588
1627
 
1589
1628
    cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len);
1590
1629
 
1595
1634
#if (NGX_PCRE)
1596
1635
 
1597
1636
    if (r->virtual_names->nregex) {
 
1637
        size_t                   ncaptures;
1598
1638
        ngx_int_t                n;
1599
1639
        ngx_uint_t               i;
1600
1640
        ngx_str_t                name;
1603
1643
        name.len = len;
1604
1644
        name.data = server;
1605
1645
 
 
1646
        ncaptures = 0;
 
1647
 
1606
1648
        sn = r->virtual_names->regex;
1607
1649
 
1608
1650
        for (i = 0; i < r->virtual_names->nregex; i++) {
1609
1651
 
1610
 
            n = ngx_regex_exec(sn[i].regex, &name, NULL, 0);
 
1652
            if (sn[i].captures && r->captures == NULL) {
 
1653
 
 
1654
                ncaptures = (NGX_HTTP_MAX_CAPTURES + 1) * 3;
 
1655
 
 
1656
                r->captures = ngx_palloc(r->pool, ncaptures * sizeof(int));
 
1657
                if (r->captures == NULL) {
 
1658
                    return NGX_ERROR;
 
1659
                }
 
1660
 
 
1661
                if (server == buf) {
 
1662
                    server = ngx_pnalloc(r->pool, len);
 
1663
                    if (server == NULL) {
 
1664
                        return NGX_ERROR;
 
1665
                    }
 
1666
 
 
1667
                    ngx_memcpy(server, buf, len);
 
1668
                    name.data = server;
 
1669
                }
 
1670
            }
 
1671
 
 
1672
            n = ngx_regex_exec(sn[i].regex, &name, r->captures, ncaptures);
1611
1673
 
1612
1674
            if (n == NGX_REGEX_NO_MATCHED) {
1613
1675
                continue;
1625
1687
 
1626
1688
            cscf = sn[i].core_srv_conf;
1627
1689
 
 
1690
            r->ncaptures = ncaptures;
 
1691
            r->captures_data = server;
 
1692
 
1628
1693
            goto found;
1629
1694
        }
1630
1695
    }
1639
1704
    r->loc_conf = cscf->ctx->loc_conf;
1640
1705
 
1641
1706
    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1642
 
    r->connection->log->file = clcf->err_log->file;
 
1707
    r->connection->log->file = clcf->error_log->file;
1643
1708
 
1644
1709
    if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
1645
 
        r->connection->log->log_level = clcf->err_log->log_level;
 
1710
        r->connection->log->log_level = clcf->error_log->log_level;
1646
1711
    }
1647
1712
 
1648
1713
    return NGX_OK;
1662
1727
    ctx = c->log->data;
1663
1728
    ctx->current_request = r;
1664
1729
 
 
1730
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
 
1731
                   "http run request: \"%V?%V\"", &r->uri, &r->args);
 
1732
 
1665
1733
    if (ev->write) {
1666
1734
        r->write_event_handler(r);
1667
1735
 
1668
1736
    } else {
1669
1737
        r->read_event_handler(r);
1670
1738
    }
 
1739
 
 
1740
    ngx_http_run_posted_requests(c);
 
1741
}
 
1742
 
 
1743
 
 
1744
void
 
1745
ngx_http_run_posted_requests(ngx_connection_t *c)
 
1746
{
 
1747
    ngx_http_request_t         *r;
 
1748
    ngx_http_log_ctx_t         *ctx;
 
1749
    ngx_http_posted_request_t  *pr;
 
1750
 
 
1751
    for ( ;; ) {
 
1752
 
 
1753
        if (c->destroyed) {
 
1754
            return;
 
1755
        }
 
1756
 
 
1757
        r = c->data;
 
1758
        pr = r->main->posted_requests;
 
1759
 
 
1760
        if (pr == NULL) {
 
1761
            return;
 
1762
        }
 
1763
 
 
1764
        r->main->posted_requests = pr->next;
 
1765
 
 
1766
        r = pr->request;
 
1767
 
 
1768
        ctx = c->log->data;
 
1769
        ctx->current_request = r;
 
1770
 
 
1771
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
 
1772
                       "http posted request: \"%V?%V\"", &r->uri, &r->args);
 
1773
 
 
1774
        r->write_event_handler(r);
 
1775
    }
 
1776
}
 
1777
 
 
1778
 
 
1779
ngx_int_t
 
1780
ngx_http_post_request(ngx_http_request_t *r)
 
1781
{
 
1782
    ngx_http_posted_request_t  *pr, **p;
 
1783
 
 
1784
    pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
 
1785
    if (pr == NULL) {
 
1786
        return NGX_ERROR;
 
1787
    }
 
1788
 
 
1789
    pr->request = r;
 
1790
    pr->next = NULL;
 
1791
 
 
1792
    for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
 
1793
 
 
1794
    *p = pr;
 
1795
 
 
1796
    return NGX_OK;
1671
1797
}
1672
1798
 
1673
1799
 
1676
1802
{
1677
1803
    ngx_connection_t          *c;
1678
1804
    ngx_http_request_t        *pr;
1679
 
    ngx_http_log_ctx_t        *ctx;
1680
1805
    ngx_http_core_loc_conf_t  *clcf;
1681
1806
 
1682
1807
    if (rc == NGX_DONE) {
1686
1811
 
1687
1812
    c = r->connection;
1688
1813
 
1689
 
    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
1690
 
                   "http finalize request: %d, \"%V?%V\"",
1691
 
                   rc, &r->uri, &r->args);
 
1814
    ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
 
1815
                   "http finalize request: %d, \"%V?%V\" %d",
 
1816
                   rc, &r->uri, &r->args, r == c->data);
 
1817
 
 
1818
    if (rc == NGX_OK && r->filter_finalize) {
 
1819
        c->error = 1;
 
1820
        return;
 
1821
    }
1692
1822
 
1693
1823
    if (rc == NGX_DECLINED) {
1694
1824
        r->content_handler = NULL;
1737
1867
            }
1738
1868
        }
1739
1869
 
 
1870
        c->read->handler = ngx_http_request_handler;
 
1871
        c->write->handler = ngx_http_request_handler;
 
1872
 
1740
1873
        ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
1741
1874
        return;
1742
1875
    }
1743
1876
 
1744
 
    if (r != r->main || rc == NGX_AGAIN) {
1745
 
        if (ngx_http_set_write_handler(r) != NGX_OK) {
1746
 
            return;
1747
 
        }
1748
 
    }
1749
 
 
1750
 
    r->done = 1;
1751
 
 
1752
 
    if (r != c->data) {
1753
 
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1754
 
                       "http finalize non-active request: \"%V?%V\"",
1755
 
                       &r->uri, &r->args);
1756
 
        return;
1757
 
    }
1758
 
 
1759
1877
    if (r != r->main) {
1760
1878
 
 
1879
        if (r->buffered || r->postponed) {
 
1880
 
 
1881
            if (ngx_http_set_write_handler(r) != NGX_OK) {
 
1882
                ngx_http_close_request(r->main, 0);
 
1883
            }
 
1884
 
 
1885
            return;
 
1886
        }
 
1887
 
 
1888
#if (NGX_DEBUG)
 
1889
        if (r != c->data) {
 
1890
            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
 
1891
                           "http finalize non-active request: \"%V?%V\"",
 
1892
                           &r->uri, &r->args);
 
1893
        }
 
1894
#endif
 
1895
 
1761
1896
        pr = r->parent;
1762
1897
 
1763
 
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1764
 
                       "http parent request: \"%V?%V\"", &pr->uri, &pr->args);
1765
 
 
1766
 
        if (rc != NGX_AGAIN) {
 
1898
        if (r == c->data) {
 
1899
 
 
1900
            if (!r->logged) {
 
1901
 
 
1902
                clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
1903
 
 
1904
                if (clcf->log_subrequest) {
 
1905
                    ngx_http_log_request(r);
 
1906
                }
 
1907
 
 
1908
                r->logged = 1;
 
1909
 
 
1910
            } else {
 
1911
                ngx_log_error(NGX_LOG_ALERT, c->log, 0,
 
1912
                              "subrequest: \"%V?%V\" logged again",
 
1913
                              &r->uri, &r->args);
 
1914
            }
 
1915
 
 
1916
            r->done = 1;
 
1917
 
 
1918
            if (pr->postponed && pr->postponed->request == r) {
 
1919
                pr->postponed = pr->postponed->next;
 
1920
            }
 
1921
 
1767
1922
            c->data = pr;
1768
 
        }
1769
 
 
1770
 
        ctx = c->log->data;
1771
 
        ctx->current_request = pr;
1772
 
 
1773
 
        if (pr->postponed) {
1774
 
 
1775
 
            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1776
 
                           "http request: \"%V?%V\" has postponed",
1777
 
                           &pr->uri, &pr->args);
1778
 
 
1779
 
            if (rc != NGX_AGAIN && pr->postponed->request == r) {
1780
 
                pr->postponed = pr->postponed->next;
1781
 
            }
1782
 
 
1783
 
            if (r->fast_subrequest) {
1784
 
 
1785
 
                if (rc == NGX_AGAIN) {
1786
 
                    r->fast_subrequest = 0;
1787
 
                }
1788
 
 
1789
 
                ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1790
 
                               "http fast subrequest: \"%V?%V\" done",
1791
 
                               &r->uri, &r->args);
1792
 
                return;
1793
 
            }
1794
 
 
1795
 
            if (rc != NGX_AGAIN) {
1796
 
                ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1797
 
                               "http wake parent request: \"%V?%V\"",
1798
 
                               &pr->uri, &pr->args);
1799
 
 
1800
 
                pr->write_event_handler(pr);
1801
 
            }
1802
 
        }
1803
 
 
1804
 
        return;
1805
 
    }
1806
 
 
1807
 
    if (rc == NGX_AGAIN) {
1808
 
        return;
1809
 
    }
1810
 
 
1811
 
    if (c->buffered) {
1812
 
        (void) ngx_http_set_write_handler(r);
1813
 
        return;
1814
 
    }
 
1923
 
 
1924
        } else {
 
1925
 
 
1926
            r->write_event_handler = ngx_http_request_finalizer;
 
1927
 
 
1928
            if (r->waited) {
 
1929
                r->done = 1;
 
1930
            }
 
1931
        }
 
1932
 
 
1933
        if (ngx_http_post_request(pr) != NGX_OK) {
 
1934
            ngx_http_close_request(r->main, 0);
 
1935
            return;
 
1936
        }
 
1937
 
 
1938
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
 
1939
                       "http wake parent request: \"%V?%V\"",
 
1940
                       &pr->uri, &pr->args);
 
1941
 
 
1942
        return;
 
1943
    }
 
1944
 
 
1945
    if (r->buffered || c->buffered || r->postponed) {
 
1946
 
 
1947
        if (ngx_http_set_write_handler(r) != NGX_OK) {
 
1948
            ngx_http_close_request(r, 0);
 
1949
        }
 
1950
 
 
1951
        return;
 
1952
    }
 
1953
 
 
1954
    if (r != c->data) {
 
1955
        ngx_log_error(NGX_LOG_ALERT, c->log, 0,
 
1956
                      "http finalize non-active request: \"%V?%V\"",
 
1957
                      &r->uri, &r->args);
 
1958
        return;
 
1959
    }
 
1960
 
 
1961
    r->done = 1;
1815
1962
 
1816
1963
    if (!r->post_action) {
1817
1964
        r->request_complete = 1;
1843
1990
 
1844
1991
    if (!ngx_terminate
1845
1992
         && !ngx_exiting
1846
 
         && r->keepalive != 0
 
1993
         && r->keepalive
1847
1994
         && clcf->keepalive_timeout > 0)
1848
1995
    {
1849
1996
        ngx_http_set_keepalive(r);
1880
2027
        ngx_add_timer(wev, clcf->send_timeout);
1881
2028
    }
1882
2029
 
1883
 
    if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
 
2030
    if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
1884
2031
        ngx_http_close_request(r, 0);
1885
2032
        return NGX_ERROR;
1886
2033
    }
1903
2050
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1904
2051
                   "http writer handler: \"%V?%V\"", &r->uri, &r->args);
1905
2052
 
 
2053
    clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
 
2054
 
1906
2055
    if (wev->timedout) {
1907
2056
        if (!wev->delayed) {
1908
2057
            ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
1917
2066
        wev->delayed = 0;
1918
2067
 
1919
2068
        if (!wev->ready) {
1920
 
            clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
1921
2069
            ngx_add_timer(wev, clcf->send_timeout);
1922
2070
 
1923
 
            if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
 
2071
            if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
1924
2072
                ngx_http_close_request(r, 0);
1925
2073
            }
1926
2074
 
1932
2080
            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1933
2081
                           "http writer delayed");
1934
2082
 
1935
 
            clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
1936
 
 
1937
 
            if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
 
2083
            if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
1938
2084
                ngx_http_close_request(r, 0);
1939
2085
            }
1940
2086
 
1952
2098
                   "http writer output filter: %d, \"%V?%V\"",
1953
2099
                   rc, &r->uri, &r->args);
1954
2100
 
1955
 
    if (rc == NGX_AGAIN) {
1956
 
        clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
 
2101
    if (rc == NGX_ERROR) {
 
2102
        ngx_http_finalize_request(r, rc);
 
2103
        return;
 
2104
    }
 
2105
 
 
2106
    if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
 
2107
 
1957
2108
        if (!wev->ready && !wev->delayed) {
1958
2109
            ngx_add_timer(wev, clcf->send_timeout);
1959
2110
        }
1960
2111
 
1961
 
        if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
 
2112
        if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
1962
2113
            ngx_http_close_request(r, 0);
1963
2114
        }
1964
2115
 
1965
 
        if (r == r->main || r->buffered) {
1966
 
            return;
1967
 
        }
1968
 
 
1969
 
        rc = NGX_OK;
 
2116
        return;
1970
2117
    }
1971
2118
 
1972
2119
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
1976
2123
}
1977
2124
 
1978
2125
 
 
2126
static void
 
2127
ngx_http_request_finalizer(ngx_http_request_t *r)
 
2128
{
 
2129
    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
2130
                   "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
 
2131
 
 
2132
    ngx_http_finalize_request(r, 0);
 
2133
}
 
2134
 
 
2135
 
1979
2136
void
1980
2137
ngx_http_block_reading(ngx_http_request_t *r)
1981
2138
{
1987
2144
    if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
1988
2145
        && r->connection->read->active)
1989
2146
    {
1990
 
        if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0)
1991
 
            == NGX_ERROR)
1992
 
        {
 
2147
        if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
1993
2148
            ngx_http_close_request(r, 0);
1994
2149
        }
1995
2150
    }
1996
2151
}
1997
2152
 
1998
2153
 
1999
 
static void
 
2154
void
2000
2155
ngx_http_test_reading(ngx_http_request_t *r)
2001
2156
{
2002
2157
    int                n;
2051
2206
 
2052
2207
    if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
2053
2208
 
2054
 
        if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
 
2209
        if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
2055
2210
            ngx_http_close_request(r, 0);
2056
2211
        }
2057
2212
    }
2091
2246
    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
2092
2247
 
2093
2248
    if (r->discard_body) {
 
2249
        r->write_event_handler = ngx_http_request_empty_handler;
2094
2250
        r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2095
2251
        ngx_add_timer(rev, clcf->lingering_timeout);
2096
2252
        return;
2145
2301
 
2146
2302
    ngx_add_timer(rev, clcf->keepalive_timeout);
2147
2303
 
2148
 
    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
2304
    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2149
2305
        ngx_http_close_connection(c);
2150
2306
        return;
2151
2307
    }
2232
2388
    rev->handler = ngx_http_keepalive_handler;
2233
2389
 
2234
2390
    if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2235
 
        if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
 
2391
        if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2236
2392
            ngx_http_close_connection(c);
2237
2393
            return;
2238
2394
        }
2264
2420
                       (const void *) &tcp_nodelay, sizeof(int))
2265
2421
            == -1)
2266
2422
        {
 
2423
#if (NGX_SOLARIS)
 
2424
            /* Solaris returns EINVAL if a socket has been shut down */
 
2425
            c->log_error = NGX_ERROR_IGNORE_EINVAL;
 
2426
#endif
 
2427
 
2267
2428
            ngx_connection_error(c, ngx_socket_errno,
2268
2429
                                 "setsockopt(TCP_NODELAY) failed");
 
2430
 
 
2431
            c->log_error = NGX_ERROR_INFO;
2269
2432
            ngx_http_close_connection(c);
2270
2433
            return;
2271
2434
        }
2357
2520
    c->log_error = NGX_ERROR_INFO;
2358
2521
 
2359
2522
    if (n == NGX_AGAIN) {
2360
 
        if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
2523
        if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2361
2524
            ngx_http_close_connection(c);
2362
2525
        }
2363
2526
 
2410
2573
    r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
2411
2574
    ngx_add_timer(rev, clcf->lingering_timeout);
2412
2575
 
2413
 
    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
2576
    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2414
2577
        ngx_http_close_request(r, 0);
2415
2578
        return;
2416
2579
    }
2419
2582
    wev->handler = ngx_http_empty_handler;
2420
2583
 
2421
2584
    if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
2422
 
        if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
 
2585
        if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
2423
2586
            ngx_http_close_request(r, 0);
2424
2587
            return;
2425
2588
        }
2478
2641
 
2479
2642
    } while (rev->ready);
2480
2643
 
2481
 
    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
2644
    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
2482
2645
        ngx_http_close_request(r, 0);
2483
2646
        return;
2484
2647
    }
2586
2749
static void
2587
2750
ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
2588
2751
{
2589
 
    ngx_log_t                  *log;
2590
 
    ngx_uint_t                  i, n;
2591
 
    struct linger               linger;
2592
 
    ngx_http_cleanup_t         *cln;
2593
 
    ngx_http_log_ctx_t         *ctx;
2594
 
    ngx_http_handler_pt        *log_handler;
2595
 
    ngx_http_core_loc_conf_t   *clcf;
2596
 
    ngx_http_core_main_conf_t  *cmcf;
 
2752
    ngx_log_t                 *log;
 
2753
    struct linger              linger;
 
2754
    ngx_http_cleanup_t        *cln;
 
2755
    ngx_http_log_ctx_t        *ctx;
 
2756
    ngx_http_core_loc_conf_t  *clcf;
2597
2757
 
2598
2758
    log = r->connection->log;
2599
2759
 
2626
2786
        r->headers_out.status = error;
2627
2787
    }
2628
2788
 
2629
 
    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2630
 
 
2631
 
    log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
2632
 
    n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
2633
 
    for (i = 0; i < n; i++) {
2634
 
        log_handler[i](r);
2635
 
    }
 
2789
    log->action = "logging request";
 
2790
 
 
2791
    ngx_http_log_request(r);
 
2792
 
 
2793
    log->action = "closing request";
2636
2794
 
2637
2795
    if (r->connection->timedout) {
2638
2796
        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2663
2821
 
2664
2822
 
2665
2823
static void
 
2824
ngx_http_log_request(ngx_http_request_t *r)
 
2825
{
 
2826
    ngx_uint_t                  i, n;
 
2827
    ngx_http_handler_pt        *log_handler;
 
2828
    ngx_http_core_main_conf_t  *cmcf;
 
2829
 
 
2830
    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
 
2831
 
 
2832
    log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
 
2833
    n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
 
2834
 
 
2835
    for (i = 0; i < n; i++) {
 
2836
        log_handler[i](r);
 
2837
    }
 
2838
}
 
2839
 
 
2840
 
 
2841
static void
2666
2842
ngx_http_close_connection(ngx_connection_t *c)
2667
2843
{
2668
2844
    ngx_pool_t  *pool;