comparison src/http/ngx_http_upstream.c @ 428:21aff1b3da48 NGINX_0_7_26

nginx 0.7.26 *) Bugfix: in subrequest processing; the bug had appeared in 0.7.25.
author Igor Sysoev <http://sysoev.ru>
date Mon, 08 Dec 2008 00:00:00 +0300
parents e7dbea1ee115
children dac47e9ef0d5
comparison
equal deleted inserted replaced
427:135cffea8f75 428:21aff1b3da48
30 static void ngx_http_upstream_process_body_in_memory(ngx_event_t *rev); 30 static void ngx_http_upstream_process_body_in_memory(ngx_event_t *rev);
31 static void ngx_http_upstream_send_response(ngx_http_request_t *r, 31 static void ngx_http_upstream_send_response(ngx_http_request_t *r,
32 ngx_http_upstream_t *u); 32 ngx_http_upstream_t *u);
33 static void 33 static void
34 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r); 34 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r);
35 static void ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev); 35 static void ngx_http_upstream_process_non_buffered_upstream(ngx_event_t *ev);
36 static void
37 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
38 ngx_uint_t do_write);
36 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data); 39 static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data);
37 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data, 40 static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data,
38 ssize_t bytes); 41 ssize_t bytes);
39 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r); 42 static void ngx_http_upstream_process_downstream(ngx_http_request_t *r);
40 static void ngx_http_upstream_process_body(ngx_event_t *ev); 43 static void ngx_http_upstream_process_upstream(ngx_event_t *rev);
44 static void ngx_http_upstream_process_request(ngx_http_request_t *r);
41 static void ngx_http_upstream_store(ngx_http_request_t *r, 45 static void ngx_http_upstream_store(ngx_http_request_t *r,
42 ngx_http_upstream_t *u); 46 ngx_http_upstream_t *u);
43 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev); 47 static void ngx_http_upstream_dummy_handler(ngx_event_t *wev);
44 static void ngx_http_upstream_next(ngx_http_request_t *r, 48 static void ngx_http_upstream_next(ngx_http_request_t *r,
45 ngx_http_upstream_t *u, ngx_uint_t ft_type); 49 ngx_http_upstream_t *u, ngx_uint_t ft_type);
1649 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; 1653 u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;
1650 u->input_filter = ngx_http_upstream_non_buffered_filter; 1654 u->input_filter = ngx_http_upstream_non_buffered_filter;
1651 u->input_filter_ctx = r; 1655 u->input_filter_ctx = r;
1652 } 1656 }
1653 1657
1654 u->read_event_handler = ngx_http_upstream_process_non_buffered_body; 1658 u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream;
1655 r->write_event_handler = 1659 r->write_event_handler =
1656 ngx_http_upstream_process_non_buffered_downstream; 1660 ngx_http_upstream_process_non_buffered_downstream;
1657 1661
1658 r->limit_rate = 0; 1662 r->limit_rate = 0;
1659 1663
1687 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) { 1691 if (u->input_filter(u->input_filter_ctx, size) == NGX_ERROR) {
1688 ngx_http_upstream_finalize_request(r, u, 0); 1692 ngx_http_upstream_finalize_request(r, u, 0);
1689 return; 1693 return;
1690 } 1694 }
1691 1695
1692 ngx_http_upstream_process_non_buffered_body(c->write); 1696 ngx_http_upstream_process_non_buffered_downstream(r);
1693 1697
1694 } else { 1698 } else {
1695 u->buffer.pos = u->buffer.start; 1699 u->buffer.pos = u->buffer.start;
1696 u->buffer.last = u->buffer.start; 1700 u->buffer.last = u->buffer.start;
1697 1701
1699 ngx_http_upstream_finalize_request(r, u, 0); 1703 ngx_http_upstream_finalize_request(r, u, 0);
1700 return; 1704 return;
1701 } 1705 }
1702 1706
1703 if (u->peer.connection->read->ready) { 1707 if (u->peer.connection->read->ready) {
1704 ngx_http_upstream_process_non_buffered_body( 1708 ngx_http_upstream_process_non_buffered_upstream(
1705 u->peer.connection->read); 1709 u->peer.connection->read);
1706 } 1710 }
1707 } 1711 }
1708 1712
1709 return; 1713 return;
1830 1834
1831 p->read_timeout = u->conf->read_timeout; 1835 p->read_timeout = u->conf->read_timeout;
1832 p->send_timeout = clcf->send_timeout; 1836 p->send_timeout = clcf->send_timeout;
1833 p->send_lowat = clcf->send_lowat; 1837 p->send_lowat = clcf->send_lowat;
1834 1838
1835 u->read_event_handler = ngx_http_upstream_process_body; 1839 u->read_event_handler = ngx_http_upstream_process_upstream;
1836 r->write_event_handler = ngx_http_upstream_process_downstream; 1840 r->write_event_handler = ngx_http_upstream_process_downstream;
1837 1841
1838 ngx_http_upstream_process_body(u->peer.connection->read); 1842 ngx_http_upstream_process_upstream(u->peer.connection->read);
1839 } 1843 }
1840 1844
1841 1845
1842 static void 1846 static void
1843 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r) 1847 ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
1844 { 1848 {
1845 ngx_http_upstream_process_non_buffered_body(r->connection->write); 1849 ngx_event_t *wev;
1850 ngx_connection_t *c;
1851 ngx_http_upstream_t *u;
1852
1853 c = r->connection;
1854 u = r->upstream;
1855 wev = c->write;
1856
1857 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1858 "http upstream process non buffered downstream");
1859
1860 c->log->action = "sending to client";
1861
1862 if (wev->timedout) {
1863 c->timedout = 1;
1864 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1865 ngx_http_upstream_finalize_request(r, u, 0);
1866 return;
1867 }
1868
1869 ngx_http_upstream_process_non_buffered_request(r, 1);
1846 } 1870 }
1847 1871
1848 1872
1849 static void 1873 static void
1850 ngx_http_upstream_process_non_buffered_body(ngx_event_t *ev) 1874 ngx_http_upstream_process_non_buffered_upstream(ngx_event_t *rev)
1875 {
1876 ngx_connection_t *c;
1877 ngx_http_request_t *r;
1878 ngx_http_upstream_t *u;
1879
1880 c = rev->data;
1881 r = c->data;
1882 u = r->upstream;
1883
1884 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1885 "http upstream process non buffered upstream");
1886
1887 c->log->action = "reading upstream";
1888
1889 if (rev->timedout) {
1890 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1891 ngx_http_upstream_finalize_request(r, u, 0);
1892 return;
1893 }
1894
1895 ngx_http_upstream_process_non_buffered_request(r, 0);
1896 }
1897
1898
1899 static void
1900 ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r,
1901 ngx_uint_t do_write)
1851 { 1902 {
1852 size_t size; 1903 size_t size;
1853 ssize_t n; 1904 ssize_t n;
1854 ngx_buf_t *b; 1905 ngx_buf_t *b;
1855 ngx_int_t rc; 1906 ngx_int_t rc;
1856 ngx_uint_t do_write; 1907 ngx_connection_t *downstream, *upstream;
1857 ngx_connection_t *c, *downstream, *upstream;
1858 ngx_http_request_t *r;
1859 ngx_http_upstream_t *u; 1908 ngx_http_upstream_t *u;
1860 ngx_http_core_loc_conf_t *clcf; 1909 ngx_http_core_loc_conf_t *clcf;
1861 1910
1862 c = ev->data;
1863 r = c->data;
1864 u = r->upstream; 1911 u = r->upstream;
1865
1866 if (ev->write) {
1867 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1868 "http upstream process non buffered downstream");
1869 c->log->action = "sending to client";
1870
1871 } else {
1872 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
1873 "http upstream process non buffered upstream");
1874 c->log->action = "reading upstream";
1875 }
1876
1877 if (ev->timedout) {
1878 if (ev->write) {
1879 c->timedout = 1;
1880 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
1881
1882 } else {
1883 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
1884 }
1885
1886 ngx_http_upstream_finalize_request(r, u, 0);
1887 return;
1888 }
1889
1890 downstream = r->connection; 1912 downstream = r->connection;
1891 upstream = u->peer.connection; 1913 upstream = u->peer.connection;
1892 1914
1893 b = &u->buffer; 1915 b = &u->buffer;
1894 1916
1895 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); 1917 do_write = do_write || u->length == 0;
1896
1897 do_write = ev->write || u->length == 0;
1898 1918
1899 for ( ;; ) { 1919 for ( ;; ) {
1900 1920
1901 if (do_write) { 1921 if (do_write) {
1902 1922
1958 } 1978 }
1959 1979
1960 break; 1980 break;
1961 } 1981 }
1962 1982
1983 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1984
1963 if (downstream->data == r) { 1985 if (downstream->data == r) {
1964 if (ngx_handle_write_event(downstream->write, clcf->send_lowat) 1986 if (ngx_handle_write_event(downstream->write, clcf->send_lowat)
1965 != NGX_OK) 1987 != NGX_OK)
1966 { 1988 {
1967 ngx_http_upstream_finalize_request(r, u, 0); 1989 ngx_http_upstream_finalize_request(r, u, 0);
2040 2062
2041 2063
2042 static void 2064 static void
2043 ngx_http_upstream_process_downstream(ngx_http_request_t *r) 2065 ngx_http_upstream_process_downstream(ngx_http_request_t *r)
2044 { 2066 {
2045 ngx_http_upstream_process_body(r->connection->write); 2067 ngx_event_t *wev;
2068 ngx_connection_t *c;
2069 ngx_event_pipe_t *p;
2070 ngx_http_upstream_t *u;
2071
2072 c = r->connection;
2073 u = r->upstream;
2074 p = u->pipe;
2075 wev = c->write;
2076
2077 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2078 "http upstream process downstream");
2079
2080 c->log->action = "sending to client";
2081
2082 if (wev->timedout) {
2083
2084 if (wev->delayed) {
2085
2086 wev->timedout = 0;
2087 wev->delayed = 0;
2088
2089 if (!wev->ready) {
2090 ngx_add_timer(wev, p->send_timeout);
2091
2092 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
2093 ngx_http_upstream_finalize_request(r, u, 0);
2094 }
2095
2096 return;
2097 }
2098
2099 if (ngx_event_pipe(p, wev->write) == NGX_ABORT) {
2100
2101 if (c->destroyed) {
2102 return;
2103 }
2104
2105 ngx_http_upstream_finalize_request(r, u, 0);
2106 return;
2107 }
2108
2109 } else {
2110 p->downstream_error = 1;
2111 c->timedout = 1;
2112 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
2113 }
2114
2115 } else {
2116
2117 if (wev->delayed) {
2118
2119 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2120 "http downstream delayed");
2121
2122 if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
2123 ngx_http_upstream_finalize_request(r, u, 0);
2124 }
2125
2126 return;
2127 }
2128
2129 if (ngx_event_pipe(p, 1) == NGX_ABORT) {
2130
2131 if (c->destroyed) {
2132 return;
2133 }
2134
2135 ngx_http_upstream_finalize_request(r, u, 0);
2136 return;
2137 }
2138 }
2139
2140 ngx_http_upstream_process_request(r);
2046 } 2141 }
2047 2142
2048 2143
2049 static void 2144 static void
2050 ngx_http_upstream_process_body(ngx_event_t *ev) 2145 ngx_http_upstream_process_upstream(ngx_event_t *rev)
2146 {
2147 ngx_connection_t *c;
2148 ngx_event_pipe_t *p;
2149 ngx_http_request_t *r;
2150 ngx_http_upstream_t *u;
2151
2152 c = rev->data;
2153 r = c->data;
2154 u = r->upstream;
2155 p = u->pipe;
2156
2157 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2158 "http upstream process upstream");
2159
2160 c->log->action = "reading upstream";
2161
2162 if (rev->timedout) {
2163 p->upstream_error = 1;
2164 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
2165
2166 } else {
2167 c = r->connection;
2168
2169 if (ngx_event_pipe(p, 0) == NGX_ABORT) {
2170
2171 if (c->destroyed) {
2172 return;
2173 }
2174
2175 ngx_http_upstream_finalize_request(r, u, 0);
2176 return;
2177 }
2178 }
2179
2180 ngx_http_upstream_process_request(r);
2181 }
2182
2183
2184 static void
2185 ngx_http_upstream_process_request(ngx_http_request_t *r)
2051 { 2186 {
2052 ngx_temp_file_t *tf; 2187 ngx_temp_file_t *tf;
2053 ngx_event_pipe_t *p; 2188 ngx_event_pipe_t *p;
2054 ngx_connection_t *c, *downstream;
2055 ngx_http_request_t *r;
2056 ngx_http_upstream_t *u; 2189 ngx_http_upstream_t *u;
2057 2190
2058 c = ev->data;
2059 r = c->data;
2060 u = r->upstream; 2191 u = r->upstream;
2061 downstream = r->connection;
2062
2063 if (ev->write) {
2064 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2065 "http upstream process downstream");
2066 c->log->action = "sending to client";
2067
2068 } else {
2069 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2070 "http upstream process upstream");
2071 c->log->action = "reading upstream";
2072 }
2073
2074 p = u->pipe; 2192 p = u->pipe;
2075
2076 if (ev->timedout) {
2077 if (ev->write) {
2078 if (ev->delayed) {
2079
2080 ev->timedout = 0;
2081 ev->delayed = 0;
2082
2083 if (!ev->ready) {
2084 ngx_add_timer(ev, p->send_timeout);
2085
2086 if (ngx_handle_write_event(ev, p->send_lowat) != NGX_OK) {
2087 ngx_http_upstream_finalize_request(r, u, 0);
2088 return;
2089 }
2090
2091 return;
2092 }
2093
2094 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
2095
2096 if (downstream->destroyed) {
2097 return;
2098 }
2099
2100 ngx_http_upstream_finalize_request(r, u, 0);
2101 return;
2102 }
2103
2104 } else {
2105 p->downstream_error = 1;
2106 c->timedout = 1;
2107 ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
2108 }
2109
2110 } else {
2111 p->upstream_error = 1;
2112 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
2113 }
2114
2115 } else {
2116 if (ev->write && ev->delayed) {
2117 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
2118 "http downstream delayed");
2119
2120 if (ngx_handle_write_event(ev, p->send_lowat) != NGX_OK) {
2121 ngx_http_upstream_finalize_request(r, u, 0);
2122 return;
2123 }
2124
2125 return;
2126 }
2127
2128 if (ngx_event_pipe(p, ev->write) == NGX_ABORT) {
2129
2130 if (downstream->destroyed) {
2131 return;
2132 }
2133
2134 ngx_http_upstream_finalize_request(r, u, 0);
2135 return;
2136 }
2137 }
2138 2193
2139 if (u->peer.connection) { 2194 if (u->peer.connection) {
2140 2195
2141 if (u->store) { 2196 if (u->store) {
2142 2197
2184 } 2239 }
2185 2240
2186 #endif 2241 #endif
2187 2242
2188 if (p->upstream_done || p->upstream_eof || p->upstream_error) { 2243 if (p->upstream_done || p->upstream_eof || p->upstream_error) {
2189 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, 2244 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2190 "http upstream exit: %p", p->out); 2245 "http upstream exit: %p", p->out);
2191 #if 0 2246 #if 0
2192 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); 2247 ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
2193 #endif 2248 #endif
2194 ngx_http_upstream_finalize_request(r, u, 0); 2249 ngx_http_upstream_finalize_request(r, u, 0);
2195 return; 2250 return;
2196 } 2251 }
2197 } 2252 }
2198 2253
2199 if (p->downstream_error) { 2254 if (p->downstream_error) {
2200 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, 2255 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2201 "http upstream downstream error"); 2256 "http upstream downstream error");
2202 2257
2203 if (!u->cacheable && u->peer.connection) { 2258 if (!u->cacheable && u->peer.connection) {
2204 ngx_http_upstream_finalize_request(r, u, 0); 2259 ngx_http_upstream_finalize_request(r, u, 0);
2205 } 2260 }