comparison src/http/ngx_http_request.c @ 518:86dad910eeb6 NGINX_0_8_11

nginx 0.8.11 *) Change: directive "gzip_disable msie6" enables gzipping for MSIE 6.0 SV1. *) Feature: file AIO support on FreeBSD and Linux. *) Feature: the "directio_alignment" directive.
author Igor Sysoev <http://sysoev.ru>
date Fri, 28 Aug 2009 00:00:00 +0400
parents 24b676623d4f
children d41628eb4d0a
comparison
equal deleted inserted replaced
517:15b5cddc5190 518:86dad910eeb6
34 static ssize_t ngx_http_validate_host(u_char *host, size_t len); 34 static ssize_t ngx_http_validate_host(u_char *host, size_t len);
35 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r, 35 static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
36 u_char *host, size_t len); 36 u_char *host, size_t len);
37 37
38 static void ngx_http_request_handler(ngx_event_t *ev); 38 static void ngx_http_request_handler(ngx_event_t *ev);
39 static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
40 static void ngx_http_terminate_handler(ngx_http_request_t *r);
41 static void ngx_http_finalize_connection(ngx_http_request_t *r);
39 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r); 42 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
40 static void ngx_http_writer(ngx_http_request_t *r); 43 static void ngx_http_writer(ngx_http_request_t *r);
41 static void ngx_http_request_finalizer(ngx_http_request_t *r); 44 static void ngx_http_request_finalizer(ngx_http_request_t *r);
42 45
43 static void ngx_http_set_keepalive(ngx_http_request_t *r); 46 static void ngx_http_set_keepalive(ngx_http_request_t *r);
44 static void ngx_http_keepalive_handler(ngx_event_t *ev); 47 static void ngx_http_keepalive_handler(ngx_event_t *ev);
45 static void ngx_http_set_lingering_close(ngx_http_request_t *r); 48 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
46 static void ngx_http_lingering_close_handler(ngx_event_t *ev); 49 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
47 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r); 50 static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
48 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error); 51 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
49 static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error); 52 static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
50 static void ngx_http_log_request(ngx_http_request_t *r); 53 static void ngx_http_log_request(ngx_http_request_t *r);
51 static void ngx_http_close_connection(ngx_connection_t *c); 54 static void ngx_http_close_connection(ngx_connection_t *c);
52 55
53 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len); 56 static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
54 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r, 57 static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
476 479
477 c->single_connection = 1; 480 c->single_connection = 1;
478 c->destroyed = 0; 481 c->destroyed = 0;
479 482
480 r->main = r; 483 r->main = r;
484 r->count = 1;
481 485
482 tp = ngx_timeofday(); 486 tp = ngx_timeofday();
483 r->start_sec = tp->sec; 487 r->start_sec = tp->sec;
484 r->start_msec = tp->msec; 488 r->start_msec = tp->msec;
485 489
1380 switch (msie[5]) { 1384 switch (msie[5]) {
1381 case '4': 1385 case '4':
1382 r->headers_in.msie4 = 1; 1386 r->headers_in.msie4 = 1;
1383 /* fall through */ 1387 /* fall through */
1384 case '5': 1388 case '5':
1389 r->headers_in.msie6 = 1;
1390 break;
1385 case '6': 1391 case '6':
1386 r->headers_in.msie6 = 1; 1392 if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
1393 r->headers_in.msie6 = 1;
1394 }
1395 break;
1387 } 1396 }
1388 } 1397 }
1389 1398
1390 #if 0 1399 #if 0
1391 /* MSIE ignores the SSL "close notify" alert */ 1400 /* MSIE ignores the SSL "close notify" alert */
1826 { 1835 {
1827 ngx_connection_t *c; 1836 ngx_connection_t *c;
1828 ngx_http_request_t *pr; 1837 ngx_http_request_t *pr;
1829 ngx_http_core_loc_conf_t *clcf; 1838 ngx_http_core_loc_conf_t *clcf;
1830 1839
1840 c = r->connection;
1841
1842 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
1843 "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
1844 rc, &r->uri, &r->args, r == c->data, r->main->count);
1845
1831 if (rc == NGX_DONE) { 1846 if (rc == NGX_DONE) {
1832 /* the request pool may be already destroyed */ 1847 ngx_http_finalize_connection(r);
1833 return; 1848 return;
1834 } 1849 }
1835
1836 c = r->connection;
1837
1838 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
1839 "http finalize request: %d, \"%V?%V\" %d",
1840 rc, &r->uri, &r->args, r == c->data);
1841 1850
1842 if (rc == NGX_OK && r->filter_finalize) { 1851 if (rc == NGX_OK && r->filter_finalize) {
1843 c->error = 1; 1852 c->error = 1;
1844 return; 1853 return;
1845 } 1854 }
1858 if (rc == NGX_ERROR 1867 if (rc == NGX_ERROR
1859 || rc == NGX_HTTP_REQUEST_TIME_OUT 1868 || rc == NGX_HTTP_REQUEST_TIME_OUT
1860 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST 1869 || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
1861 || c->error) 1870 || c->error)
1862 { 1871 {
1863 if (rc > 0 && r->headers_out.status == 0) {
1864 r->headers_out.status = rc;
1865 }
1866
1867 if (ngx_http_post_action(r) == NGX_OK) { 1872 if (ngx_http_post_action(r) == NGX_OK) {
1868 return; 1873 return;
1869 } 1874 }
1870 1875
1871 ngx_http_close_request(r, 0); 1876 if (r->main->blocked) {
1877 r->write_event_handler = ngx_http_request_finalizer;
1878 }
1879
1880 ngx_http_terminate_request(r, rc);
1872 return; 1881 return;
1873 } 1882 }
1874 1883
1875 if (rc >= NGX_HTTP_SPECIAL_RESPONSE 1884 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
1876 || rc == NGX_HTTP_CREATED 1885 || rc == NGX_HTTP_CREATED
1877 || rc == NGX_HTTP_NO_CONTENT) 1886 || rc == NGX_HTTP_NO_CONTENT)
1878 { 1887 {
1879 if (rc == NGX_HTTP_CLOSE) { 1888 if (rc == NGX_HTTP_CLOSE) {
1880 ngx_http_close_request(r, rc); 1889 ngx_http_terminate_request(r, rc);
1881 return; 1890 return;
1882 } 1891 }
1883 1892
1884 if (r == r->main) { 1893 if (r == r->main) {
1885 if (c->read->timer_set) { 1894 if (c->read->timer_set) {
1901 if (r != r->main) { 1910 if (r != r->main) {
1902 1911
1903 if (r->buffered || r->postponed) { 1912 if (r->buffered || r->postponed) {
1904 1913
1905 if (ngx_http_set_write_handler(r) != NGX_OK) { 1914 if (ngx_http_set_write_handler(r) != NGX_OK) {
1906 ngx_http_close_request(r->main, 0); 1915 ngx_http_terminate_request(r, 0);
1907 } 1916 }
1908 1917
1909 return; 1918 return;
1910 } 1919 }
1911 1920
1919 1928
1920 pr = r->parent; 1929 pr = r->parent;
1921 1930
1922 if (r == c->data) { 1931 if (r == c->data) {
1923 1932
1933 r->main->count--;
1934
1924 if (!r->logged) { 1935 if (!r->logged) {
1925 1936
1926 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 1937 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1927 1938
1928 if (clcf->log_subrequest) { 1939 if (clcf->log_subrequest) {
1953 r->done = 1; 1964 r->done = 1;
1954 } 1965 }
1955 } 1966 }
1956 1967
1957 if (ngx_http_post_request(pr) != NGX_OK) { 1968 if (ngx_http_post_request(pr) != NGX_OK) {
1958 ngx_http_close_request(r->main, 0); 1969 r->main->count++;
1970 ngx_http_terminate_request(r, 0);
1959 return; 1971 return;
1960 } 1972 }
1961 1973
1962 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, 1974 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1963 "http wake parent request: \"%V?%V\"", 1975 "http wake parent request: \"%V?%V\"",
1964 &pr->uri, &pr->args); 1976 &pr->uri, &pr->args);
1965 1977
1966 return; 1978 return;
1967 } 1979 }
1968 1980
1969 if (r->buffered || c->buffered || r->postponed) { 1981 if (r->buffered || c->buffered || r->postponed || r->blocked) {
1970 1982
1971 if (ngx_http_set_write_handler(r) != NGX_OK) { 1983 if (ngx_http_set_write_handler(r) != NGX_OK) {
1972 ngx_http_close_request(r, 0); 1984 ngx_http_terminate_request(r, 0);
1973 } 1985 }
1974 1986
1975 return; 1987 return;
1976 } 1988 }
1977 1989
1999 if (c->write->timer_set) { 2011 if (c->write->timer_set) {
2000 c->write->delayed = 0; 2012 c->write->delayed = 0;
2001 ngx_del_timer(c->write); 2013 ngx_del_timer(c->write);
2002 } 2014 }
2003 2015
2004 if (c->destroyed) {
2005 return;
2006 }
2007
2008 if (c->read->eof) { 2016 if (c->read->eof) {
2017 ngx_http_close_request(r, 0);
2018 return;
2019 }
2020
2021 ngx_http_finalize_connection(r);
2022 }
2023
2024
2025 static void
2026 ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc)
2027 {
2028 ngx_http_cleanup_t *cln;
2029 ngx_http_request_t *mr;
2030
2031 mr = r->main;
2032
2033 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2034 "http terminate request count:%d", mr->count);
2035
2036 cln = mr->cleanup;
2037 mr->cleanup = NULL;
2038
2039 while (cln) {
2040 if (cln->handler) {
2041 cln->handler(cln->data);
2042 }
2043
2044 cln = cln->next;
2045 }
2046
2047 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2048 "http terminate cleanup count:%d blk:%d",
2049 mr->count, mr->blocked);
2050
2051 if (mr->write_event_handler) {
2052
2053 if (mr->blocked) {
2054 return;
2055 }
2056
2057 mr->posted_requests = NULL;
2058 mr->write_event_handler = ngx_http_terminate_handler;
2059 (void) ngx_http_post_request(mr);
2060 return;
2061 }
2062
2063 ngx_http_close_request(mr, rc);
2064 }
2065
2066
2067 static void
2068 ngx_http_terminate_handler(ngx_http_request_t *r)
2069 {
2070 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2071 "http terminate handler count:%d", r->count);
2072
2073 r->count = 1;
2074
2075 ngx_http_close_request(r, 0);
2076 }
2077
2078
2079 static void
2080 ngx_http_finalize_connection(ngx_http_request_t *r)
2081 {
2082 ngx_http_core_loc_conf_t *clcf;
2083
2084 if (r->main->count != 1) {
2009 ngx_http_close_request(r, 0); 2085 ngx_http_close_request(r, 0);
2010 return; 2086 return;
2011 } 2087 }
2012 2088
2013 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 2089 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2098 2174
2099 return; 2175 return;
2100 } 2176 }
2101 2177
2102 } else { 2178 } else {
2103 if (wev->delayed) { 2179 if (wev->delayed || r->aio) {
2104 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, 2180 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
2105 "http writer delayed"); 2181 "http writer delayed");
2106 2182
2107 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) { 2183 if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
2108 ngx_http_close_request(r, 0); 2184 ngx_http_close_request(r, 0);
2111 return; 2187 return;
2112 } 2188 }
2113 } 2189 }
2114 2190
2115 rc = ngx_http_output_filter(r, NULL); 2191 rc = ngx_http_output_filter(r, NULL);
2116
2117 if (c->destroyed) {
2118 return;
2119 }
2120 2192
2121 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, 2193 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
2122 "http writer output filter: %d, \"%V?%V\"", 2194 "http writer output filter: %d, \"%V?%V\"",
2123 rc, &r->uri, &r->args); 2195 rc, &r->uri, &r->args);
2124 2196
2317 hc->busy[0] = b; 2389 hc->busy[0] = b;
2318 hc->nbusy = 1; 2390 hc->nbusy = 1;
2319 } 2391 }
2320 } 2392 }
2321 2393
2322 ngx_http_request_done(r, 0); 2394 ngx_http_free_request(r, 0);
2323 2395
2324 c->data = hc; 2396 c->data = hc;
2325 2397
2326 ngx_add_timer(rev, clcf->keepalive_timeout); 2398 ngx_add_timer(rev, clcf->keepalive_timeout);
2327 2399
2764 return NGX_OK; 2836 return NGX_OK;
2765 } 2837 }
2766 2838
2767 2839
2768 static void 2840 static void
2769 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error) 2841 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
2770 { 2842 {
2771 ngx_connection_t *c; 2843 ngx_connection_t *c;
2772 2844
2845 r = r->main;
2773 c = r->connection; 2846 c = r->connection;
2774 2847
2775 ngx_http_request_done(r->main, error); 2848 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2849 "http request count:%d blk:%d", r->count, r->blocked);
2850
2851 if (r->count == 0) {
2852 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
2853 }
2854
2855 r->count--;
2856
2857 if (r->count || r->blocked) {
2858 return;
2859 }
2860
2861 ngx_http_free_request(r, rc);
2776 ngx_http_close_connection(c); 2862 ngx_http_close_connection(c);
2777 } 2863 }
2778 2864
2779 2865
2780 static void 2866 static void
2781 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error) 2867 ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
2782 { 2868 {
2783 ngx_log_t *log; 2869 ngx_log_t *log;
2784 struct linger linger; 2870 struct linger linger;
2785 ngx_http_cleanup_t *cln; 2871 ngx_http_cleanup_t *cln;
2786 ngx_http_log_ctx_t *ctx; 2872 ngx_http_log_ctx_t *ctx;
2811 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1); 2897 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
2812 } 2898 }
2813 2899
2814 #endif 2900 #endif
2815 2901
2816 if (error && r->headers_out.status == 0) { 2902 if (rc > 0 && (r->headers_out.status == 0 || r->connection->sent == 0)) {
2817 r->headers_out.status = error; 2903 r->headers_out.status = rc;
2818 } 2904 }
2819 2905
2820 log->action = "logging request"; 2906 log->action = "logging request";
2821 2907
2822 ngx_http_log_request(r); 2908 ngx_http_log_request(r);