Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) Igor Sysoev |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 9 | #include <ngx_http.h> |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 10 | |
| 11 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 12 | static void ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 13 | static void ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r); |
| 14 | static void ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r); |
| 15 | static void ngx_http_upstream_check_broken_connection(ngx_http_request_t *r, |
| 16 | ngx_event_t *ev); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 17 | static void ngx_http_upstream_connect(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 18 | ngx_http_upstream_t *u); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 19 | static ngx_int_t ngx_http_upstream_reinit(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 20 | ngx_http_upstream_t *u); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 21 | static void ngx_http_upstream_send_request(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 22 | ngx_http_upstream_t *u); |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 23 | static void ngx_http_upstream_send_request_handler(ngx_http_request_t *r, |
| 24 | ngx_http_upstream_t *u); |
| 25 | static void ngx_http_upstream_process_header(ngx_http_request_t *r, |
| 26 | ngx_http_upstream_t *u); |
Igor Sysoev | 797c6ef | 2008-09-30 15:39:02 +0000 | [diff] [blame] | 27 | static ngx_int_t ngx_http_upstream_test_next(ngx_http_request_t *r, |
| 28 | ngx_http_upstream_t *u); |
Igor Sysoev | d01eea1 | 2008-09-30 14:57:09 +0000 | [diff] [blame] | 29 | static ngx_int_t ngx_http_upstream_intercept_errors(ngx_http_request_t *r, |
| 30 | ngx_http_upstream_t *u); |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 31 | static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c); |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 32 | static void ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r, |
| 33 | ngx_http_upstream_t *u); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 34 | static void ngx_http_upstream_send_response(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 35 | ngx_http_upstream_t *u); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 36 | static void |
| 37 | ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r); |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 38 | static void |
| 39 | ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r, |
| 40 | ngx_http_upstream_t *u); |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 41 | static void |
| 42 | ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r, |
| 43 | ngx_uint_t do_write); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 44 | static ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data); |
| 45 | static ngx_int_t ngx_http_upstream_non_buffered_filter(void *data, |
| 46 | ssize_t bytes); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 47 | static void ngx_http_upstream_process_downstream(ngx_http_request_t *r); |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 48 | static void ngx_http_upstream_process_upstream(ngx_http_request_t *r, |
| 49 | ngx_http_upstream_t *u); |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 50 | static void ngx_http_upstream_process_request(ngx_http_request_t *r); |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 51 | static void ngx_http_upstream_store(ngx_http_request_t *r, |
| 52 | ngx_http_upstream_t *u); |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 53 | static void ngx_http_upstream_dummy_handler(ngx_http_request_t *r, |
| 54 | ngx_http_upstream_t *u); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 55 | static void ngx_http_upstream_next(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 56 | ngx_http_upstream_t *u, ngx_uint_t ft_type); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 57 | static void ngx_http_upstream_cleanup(void *data); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 58 | static void ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 59 | ngx_http_upstream_t *u, ngx_int_t rc); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 60 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 61 | static ngx_int_t ngx_http_upstream_process_header_line(ngx_http_request_t *r, |
| 62 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 63 | static ngx_int_t |
| 64 | ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, |
| 65 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 66 | static ngx_int_t ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, |
| 67 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 68 | static ngx_int_t ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, |
| 69 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 70 | static ngx_int_t ngx_http_upstream_process_buffering(ngx_http_request_t *r, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 71 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 72 | static ngx_int_t ngx_http_upstream_process_charset(ngx_http_request_t *r, |
| 73 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 74 | static ngx_int_t ngx_http_upstream_copy_header_line(ngx_http_request_t *r, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 75 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 76 | static ngx_int_t |
| 77 | ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, |
| 78 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 79 | static ngx_int_t ngx_http_upstream_copy_content_type(ngx_http_request_t *r, |
| 80 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 81 | static ngx_int_t ngx_http_upstream_copy_content_length(ngx_http_request_t *r, |
| 82 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 83 | static ngx_int_t ngx_http_upstream_rewrite_location(ngx_http_request_t *r, |
| 84 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 85 | static ngx_int_t ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, |
| 86 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 87 | #if (NGX_HTTP_GZIP) |
| 88 | static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, |
| 89 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 90 | #endif |
| 91 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 92 | static ngx_int_t ngx_http_upstream_add_variables(ngx_conf_t *cf); |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 93 | static ngx_int_t ngx_http_upstream_addr_variable(ngx_http_request_t *r, |
| 94 | ngx_http_variable_value_t *v, uintptr_t data); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 95 | static ngx_int_t ngx_http_upstream_status_variable(ngx_http_request_t *r, |
| 96 | ngx_http_variable_value_t *v, uintptr_t data); |
| 97 | static ngx_int_t ngx_http_upstream_response_time_variable(ngx_http_request_t *r, |
| 98 | ngx_http_variable_value_t *v, uintptr_t data); |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 99 | static ngx_int_t ngx_http_upstream_response_length_variable( |
| 100 | ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 101 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 102 | static char *ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy); |
| 103 | static char *ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, |
| 104 | void *conf); |
| 105 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 106 | static void *ngx_http_upstream_create_main_conf(ngx_conf_t *cf); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 107 | static char *ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 108 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 109 | #if (NGX_HTTP_SSL) |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 110 | static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *, |
| 111 | ngx_http_upstream_t *u, ngx_connection_t *c); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 112 | static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 115 | |
| 116 | ngx_http_upstream_header_t ngx_http_upstream_headers_in[] = { |
| 117 | |
| 118 | { ngx_string("Status"), |
| 119 | ngx_http_upstream_process_header_line, |
| 120 | offsetof(ngx_http_upstream_headers_in_t, status), |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 121 | ngx_http_upstream_copy_header_line, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 122 | |
| 123 | { ngx_string("Content-Type"), |
| 124 | ngx_http_upstream_process_header_line, |
| 125 | offsetof(ngx_http_upstream_headers_in_t, content_type), |
Igor Sysoev | 08e63d4 | 2006-08-14 15:09:38 +0000 | [diff] [blame] | 126 | ngx_http_upstream_copy_content_type, 0, 1 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 127 | |
| 128 | { ngx_string("Content-Length"), |
| 129 | ngx_http_upstream_process_header_line, |
| 130 | offsetof(ngx_http_upstream_headers_in_t, content_length), |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 131 | ngx_http_upstream_copy_content_length, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 132 | |
| 133 | { ngx_string("Date"), |
| 134 | ngx_http_upstream_process_header_line, |
| 135 | offsetof(ngx_http_upstream_headers_in_t, date), |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 136 | ngx_http_upstream_copy_header_line, |
| 137 | offsetof(ngx_http_headers_out_t, date), 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 138 | |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 139 | { ngx_string("Last-Modified"), |
| 140 | ngx_http_upstream_process_header_line, |
| 141 | offsetof(ngx_http_upstream_headers_in_t, last_modified), |
| 142 | ngx_http_upstream_copy_header_line, |
| 143 | offsetof(ngx_http_headers_out_t, last_modified), 0 }, |
| 144 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 145 | { ngx_string("Server"), |
| 146 | ngx_http_upstream_process_header_line, |
| 147 | offsetof(ngx_http_upstream_headers_in_t, server), |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 148 | ngx_http_upstream_copy_header_line, |
| 149 | offsetof(ngx_http_headers_out_t, server), 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 150 | |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 151 | { ngx_string("WWW-Authenticate"), |
| 152 | ngx_http_upstream_process_header_line, |
| 153 | offsetof(ngx_http_upstream_headers_in_t, www_authenticate), |
| 154 | ngx_http_upstream_copy_header_line, 0, 0 }, |
| 155 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 156 | { ngx_string("Location"), |
Igor Sysoev | ac721d0 | 2008-08-04 14:53:16 +0000 | [diff] [blame] | 157 | ngx_http_upstream_process_header_line, |
| 158 | offsetof(ngx_http_upstream_headers_in_t, location), |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 159 | ngx_http_upstream_rewrite_location, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 160 | |
| 161 | { ngx_string("Refresh"), |
| 162 | ngx_http_upstream_ignore_header_line, 0, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 163 | ngx_http_upstream_rewrite_refresh, 0, 0 }, |
| 164 | |
| 165 | { ngx_string("Set-Cookie"), |
| 166 | ngx_http_upstream_ignore_header_line, 0, |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 167 | ngx_http_upstream_copy_header_line, 0, 1 }, |
| 168 | |
| 169 | { ngx_string("Content-Disposition"), |
| 170 | ngx_http_upstream_ignore_header_line, 0, |
| 171 | ngx_http_upstream_copy_header_line, 0, 1 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 172 | |
| 173 | { ngx_string("Cache-Control"), |
| 174 | ngx_http_upstream_process_multi_header_lines, |
| 175 | offsetof(ngx_http_upstream_headers_in_t, cache_control), |
| 176 | ngx_http_upstream_copy_multi_header_lines, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 177 | offsetof(ngx_http_headers_out_t, cache_control), 1 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 178 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 179 | { ngx_string("Expires"), |
| 180 | ngx_http_upstream_process_header_line, |
| 181 | offsetof(ngx_http_upstream_headers_in_t, expires), |
| 182 | ngx_http_upstream_copy_header_line, |
| 183 | offsetof(ngx_http_headers_out_t, expires), 1 }, |
| 184 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 185 | { ngx_string("Accept-Ranges"), |
| 186 | ngx_http_upstream_process_header_line, |
| 187 | offsetof(ngx_http_upstream_headers_in_t, accept_ranges), |
| 188 | ngx_http_upstream_copy_header_line, |
| 189 | offsetof(ngx_http_headers_out_t, accept_ranges), 1 }, |
| 190 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 191 | { ngx_string("Connection"), |
| 192 | ngx_http_upstream_ignore_header_line, 0, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 193 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 194 | |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 195 | { ngx_string("Keep-Alive"), |
| 196 | ngx_http_upstream_ignore_header_line, 0, |
| 197 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
| 198 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 199 | { ngx_string("X-Powered-By"), |
| 200 | ngx_http_upstream_ignore_header_line, 0, |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 201 | ngx_http_upstream_copy_header_line, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 202 | |
| 203 | { ngx_string("X-Accel-Expires"), |
| 204 | ngx_http_upstream_process_header_line, |
| 205 | offsetof(ngx_http_upstream_headers_in_t, x_accel_expires), |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 206 | ngx_http_upstream_copy_header_line, 0, 0 }, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 207 | |
| 208 | { ngx_string("X-Accel-Redirect"), |
| 209 | ngx_http_upstream_process_header_line, |
| 210 | offsetof(ngx_http_upstream_headers_in_t, x_accel_redirect), |
| 211 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 212 | |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 213 | { ngx_string("X-Accel-Limit-Rate"), |
| 214 | ngx_http_upstream_process_limit_rate, 0, |
| 215 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
| 216 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 217 | { ngx_string("X-Accel-Buffering"), |
| 218 | ngx_http_upstream_process_buffering, 0, |
| 219 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
| 220 | |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 221 | { ngx_string("X-Accel-Charset"), |
| 222 | ngx_http_upstream_process_charset, 0, |
| 223 | ngx_http_upstream_ignore_header_line, 0, 0 }, |
| 224 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 225 | #if (NGX_HTTP_GZIP) |
| 226 | { ngx_string("Content-Encoding"), |
| 227 | ngx_http_upstream_process_header_line, |
| 228 | offsetof(ngx_http_upstream_headers_in_t, content_encoding), |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 229 | ngx_http_upstream_copy_content_encoding, 0, 0 }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 230 | #endif |
| 231 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 232 | { ngx_null_string, NULL, 0, NULL, 0, 0 } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 233 | }; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 234 | |
| 235 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 236 | static ngx_command_t ngx_http_upstream_commands[] = { |
| 237 | |
| 238 | { ngx_string("upstream"), |
| 239 | NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, |
| 240 | ngx_http_upstream, |
| 241 | 0, |
| 242 | 0, |
| 243 | NULL }, |
| 244 | |
| 245 | { ngx_string("server"), |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 246 | NGX_HTTP_UPS_CONF|NGX_CONF_1MORE, |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 247 | ngx_http_upstream_server, |
| 248 | NGX_HTTP_SRV_CONF_OFFSET, |
| 249 | 0, |
| 250 | NULL }, |
| 251 | |
| 252 | ngx_null_command |
| 253 | }; |
| 254 | |
| 255 | |
| 256 | static ngx_http_module_t ngx_http_upstream_module_ctx = { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 257 | ngx_http_upstream_add_variables, /* preconfiguration */ |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 258 | NULL, /* postconfiguration */ |
| 259 | |
| 260 | ngx_http_upstream_create_main_conf, /* create main configuration */ |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 261 | ngx_http_upstream_init_main_conf, /* init main configuration */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 262 | |
| 263 | NULL, /* create server configuration */ |
| 264 | NULL, /* merge server configuration */ |
| 265 | |
| 266 | NULL, /* create location configuration */ |
| 267 | NULL /* merge location configuration */ |
| 268 | }; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 269 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 270 | |
| 271 | ngx_module_t ngx_http_upstream_module = { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 272 | NGX_MODULE_V1, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 273 | &ngx_http_upstream_module_ctx, /* module context */ |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 274 | ngx_http_upstream_commands, /* module directives */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 275 | NGX_HTTP_MODULE, /* module type */ |
Igor Sysoev | e573380 | 2005-09-08 14:36:09 +0000 | [diff] [blame] | 276 | NULL, /* init master */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 277 | NULL, /* init module */ |
Igor Sysoev | e573380 | 2005-09-08 14:36:09 +0000 | [diff] [blame] | 278 | NULL, /* init process */ |
| 279 | NULL, /* init thread */ |
| 280 | NULL, /* exit thread */ |
| 281 | NULL, /* exit process */ |
| 282 | NULL, /* exit master */ |
| 283 | NGX_MODULE_V1_PADDING |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 287 | static ngx_http_variable_t ngx_http_upstream_vars[] = { |
| 288 | |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 289 | { ngx_string("upstream_addr"), NULL, |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 290 | ngx_http_upstream_addr_variable, 0, |
| 291 | NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 292 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 293 | { ngx_string("upstream_status"), NULL, |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 294 | ngx_http_upstream_status_variable, 0, |
| 295 | NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 296 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 297 | { ngx_string("upstream_response_time"), NULL, |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 298 | ngx_http_upstream_response_time_variable, 0, |
| 299 | NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 300 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 301 | { ngx_string("upstream_response_length"), NULL, |
| 302 | ngx_http_upstream_response_length_variable, 0, |
| 303 | NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
| 304 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 305 | { ngx_null_string, NULL, NULL, 0, 0, 0 } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | |
Igor Sysoev | 797c6ef | 2008-09-30 15:39:02 +0000 | [diff] [blame] | 309 | static ngx_http_upstream_next_t ngx_http_upstream_next_errors[] = { |
| 310 | { 500, NGX_HTTP_UPSTREAM_FT_HTTP_500 }, |
| 311 | { 502, NGX_HTTP_UPSTREAM_FT_HTTP_502 }, |
| 312 | { 503, NGX_HTTP_UPSTREAM_FT_HTTP_503 }, |
| 313 | { 504, NGX_HTTP_UPSTREAM_FT_HTTP_504 }, |
| 314 | { 404, NGX_HTTP_UPSTREAM_FT_HTTP_404 }, |
| 315 | { 0, 0 } |
| 316 | }; |
| 317 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 318 | void |
| 319 | ngx_http_upstream_init(ngx_http_request_t *r) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 320 | { |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 321 | ngx_str_t *host; |
| 322 | ngx_uint_t i; |
| 323 | ngx_connection_t *c; |
| 324 | ngx_resolver_ctx_t *ctx, temp; |
| 325 | ngx_http_cleanup_t *cln; |
| 326 | ngx_http_upstream_t *u; |
| 327 | ngx_http_core_loc_conf_t *clcf; |
| 328 | ngx_http_upstream_srv_conf_t *uscf, **uscfp; |
| 329 | ngx_http_upstream_main_conf_t *umcf; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 330 | |
| 331 | c = r->connection; |
| 332 | |
| 333 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 334 | "http init upstream, client timer: %d", c->read->timer_set); |
| 335 | |
| 336 | if (c->read->timer_set) { |
| 337 | ngx_del_timer(c->read); |
| 338 | } |
| 339 | |
Igor Sysoev | 6d16e1e | 2006-04-05 13:40:54 +0000 | [diff] [blame] | 340 | u = r->upstream; |
Igor Sysoev | 69d73da | 2006-02-20 16:48:17 +0000 | [diff] [blame] | 341 | |
Igor Sysoev | 6d16e1e | 2006-04-05 13:40:54 +0000 | [diff] [blame] | 342 | if (!r->post_action && !u->conf->ignore_client_abort) { |
Igor Sysoev | 69d73da | 2006-02-20 16:48:17 +0000 | [diff] [blame] | 343 | r->read_event_handler = ngx_http_upstream_rd_check_broken_connection; |
| 344 | r->write_event_handler = ngx_http_upstream_wr_check_broken_connection; |
| 345 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 346 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 347 | if (ngx_event_flags & NGX_USE_CLEAR_EVENT) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 348 | |
| 349 | if (!c->write->active) { |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 350 | if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) |
| 351 | == NGX_ERROR) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 352 | { |
| 353 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 354 | return; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Igor Sysoev | e503539 | 2005-08-30 10:55:07 +0000 | [diff] [blame] | 359 | if (r->request_body) { |
| 360 | u->request_bufs = r->request_body->bufs; |
| 361 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 362 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 363 | if (u->create_request(r) != NGX_OK) { |
| 364 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 365 | return; |
| 366 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 367 | |
| 368 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 369 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 370 | u->output.pool = r->pool; |
| 371 | u->output.bufs.num = 1; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 372 | u->output.bufs.size = clcf->client_body_buffer_size; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 373 | u->output.output_filter = ngx_chain_writer; |
| 374 | u->output.filter_ctx = &u->writer; |
| 375 | |
| 376 | u->writer.pool = r->pool; |
| 377 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 378 | if (r->upstream_states == NULL) { |
| 379 | |
| 380 | r->upstream_states = ngx_array_create(r->pool, 1, |
| 381 | sizeof(ngx_http_upstream_state_t)); |
| 382 | if (r->upstream_states == NULL) { |
| 383 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | } else { |
| 388 | |
| 389 | u->state = ngx_array_push(r->upstream_states); |
| 390 | if (u->state == NULL) { |
| 391 | ngx_http_upstream_finalize_request(r, u, |
| 392 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t)); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 399 | cln = ngx_http_cleanup_add(r, 0); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 400 | if (cln == NULL) { |
| 401 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | cln->handler = ngx_http_upstream_cleanup; |
| 406 | cln->data = r; |
| 407 | u->cleanup = &cln->handler; |
| 408 | |
Igor Sysoev | fbd9b43 | 2007-07-13 08:30:34 +0000 | [diff] [blame] | 409 | u->store = (u->conf->store || u->conf->store_lengths); |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 410 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 411 | if (u->resolved == NULL) { |
| 412 | |
| 413 | uscf = u->conf->upstream; |
| 414 | |
| 415 | } else { |
| 416 | |
Igor Sysoev | 302cedc | 2008-12-23 19:35:12 +0000 | [diff] [blame] | 417 | if (u->resolved->sockaddr) { |
| 418 | |
| 419 | if (ngx_http_upstream_create_round_robin_peer(r, u->resolved) |
| 420 | != NGX_OK) |
| 421 | { |
| 422 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | ngx_http_upstream_connect(r, u); |
| 427 | |
| 428 | return; |
| 429 | } |
| 430 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 431 | host = &u->resolved->host; |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 432 | |
| 433 | umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); |
| 434 | |
| 435 | uscfp = umcf->upstreams.elts; |
| 436 | |
| 437 | for (i = 0; i < umcf->upstreams.nelts; i++) { |
| 438 | |
| 439 | uscf = uscfp[i]; |
| 440 | |
| 441 | if (uscf->host.len == host->len |
Igor Sysoev | 694c849 | 2008-10-30 15:59:10 +0000 | [diff] [blame] | 442 | && ((uscf->port == 0 && u->resolved->no_port) |
Igor Sysoev | 8efe926 | 2008-03-03 20:04:06 +0000 | [diff] [blame] | 443 | || uscf->port == u->resolved->port) |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 444 | && ngx_memcmp(uscf->host.data, host->data, host->len) == 0) |
| 445 | { |
| 446 | goto found; |
| 447 | } |
| 448 | } |
| 449 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 450 | temp.name = *host; |
| 451 | |
| 452 | ctx = ngx_resolve_start(clcf->resolver, &temp); |
| 453 | if (ctx == NULL) { |
| 454 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 455 | return; |
| 456 | } |
| 457 | |
Igor Sysoev | 6b2fce4 | 2007-12-03 10:05:19 +0000 | [diff] [blame] | 458 | if (ctx == NGX_NO_RESOLVER) { |
| 459 | ngx_log_error(NGX_LOG_ERR, c->log, 0, |
| 460 | "no resolver defined to resolve %V", host); |
| 461 | |
| 462 | ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); |
| 463 | return; |
| 464 | } |
| 465 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 466 | ctx->name = *host; |
| 467 | ctx->type = NGX_RESOLVE_A; |
| 468 | ctx->handler = ngx_http_upstream_resolve_handler; |
| 469 | ctx->data = r; |
| 470 | ctx->timeout = clcf->resolver_timeout; |
| 471 | |
Igor Sysoev | f576f0d | 2008-04-09 14:20:17 +0000 | [diff] [blame] | 472 | u->resolved->ctx = ctx; |
| 473 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 474 | if (ngx_resolve_name(ctx) != NGX_OK) { |
Igor Sysoev | f576f0d | 2008-04-09 14:20:17 +0000 | [diff] [blame] | 475 | u->resolved->ctx = NULL; |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 476 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | found: |
| 484 | |
| 485 | if (uscf->peer.init(r, uscf) != NGX_OK) { |
| 486 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 487 | return; |
| 488 | } |
| 489 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 490 | ngx_http_upstream_connect(r, u); |
| 491 | } |
| 492 | |
| 493 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 494 | static void |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 495 | ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx) |
| 496 | { |
| 497 | ngx_http_request_t *r; |
| 498 | ngx_http_upstream_resolved_t *ur; |
| 499 | |
| 500 | r = ctx->data; |
| 501 | |
| 502 | r->upstream->resolved->ctx = NULL; |
| 503 | |
| 504 | if (ctx->state) { |
| 505 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 506 | "%V could not be resolved (%i: %s)", |
| 507 | &ctx->name, ctx->state, |
| 508 | ngx_resolver_strerror(ctx->state)); |
| 509 | |
| 510 | ngx_resolve_name_done(ctx); |
| 511 | ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | ur = r->upstream->resolved; |
| 516 | ur->naddrs = ctx->naddrs; |
| 517 | ur->addrs = ctx->addrs; |
| 518 | |
| 519 | #if (NGX_DEBUG) |
| 520 | { |
| 521 | in_addr_t addr; |
| 522 | ngx_uint_t i; |
| 523 | |
| 524 | for (i = 0; i < ctx->naddrs; i++) { |
| 525 | addr = ntohl(ur->addrs[i]); |
| 526 | |
| 527 | ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 528 | "name was resolved to %ud.%ud.%ud.%ud", |
| 529 | (addr >> 24) & 0xff, (addr >> 16) & 0xff, |
| 530 | (addr >> 8) & 0xff, addr & 0xff); |
| 531 | } |
| 532 | } |
| 533 | #endif |
| 534 | |
| 535 | if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) { |
| 536 | ngx_resolve_name_done(ctx); |
| 537 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | ngx_resolve_name_done(ctx); |
| 542 | |
| 543 | ngx_http_upstream_connect(r, r->upstream); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | static void |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 548 | ngx_http_upstream_handler(ngx_event_t *ev) |
| 549 | { |
| 550 | ngx_connection_t *c; |
| 551 | ngx_http_request_t *r; |
| 552 | ngx_http_log_ctx_t *ctx; |
| 553 | ngx_http_upstream_t *u; |
| 554 | |
| 555 | c = ev->data; |
| 556 | r = c->data; |
| 557 | |
| 558 | u = r->upstream; |
| 559 | c = r->connection; |
| 560 | |
| 561 | ctx = c->log->data; |
| 562 | ctx->current_request = r; |
| 563 | |
| 564 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 565 | "http upstream request: \"%V?%V\"", &r->uri, &r->args); |
| 566 | |
| 567 | if (ev->write) { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 568 | u->write_event_handler(r, u); |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 569 | |
| 570 | } else { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 571 | u->read_event_handler(r, u); |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | ngx_http_run_posted_requests(c); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | static void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 579 | ngx_http_upstream_rd_check_broken_connection(ngx_http_request_t *r) |
| 580 | { |
| 581 | ngx_http_upstream_check_broken_connection(r, r->connection->read); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | static void |
| 586 | ngx_http_upstream_wr_check_broken_connection(ngx_http_request_t *r) |
| 587 | { |
| 588 | ngx_http_upstream_check_broken_connection(r, r->connection->write); |
| 589 | } |
| 590 | |
| 591 | |
| 592 | static void |
| 593 | ngx_http_upstream_check_broken_connection(ngx_http_request_t *r, |
| 594 | ngx_event_t *ev) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 595 | { |
| 596 | int n; |
| 597 | char buf[1]; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 598 | ngx_err_t err; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 599 | ngx_connection_t *c; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 600 | ngx_http_upstream_t *u; |
| 601 | |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 602 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0, |
| 603 | "http upstream check client, write event:%d, \"%V\"", |
| 604 | ev->write, &r->uri); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 605 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 606 | c = r->connection; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 607 | u = r->upstream; |
| 608 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 609 | if (c->error) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 610 | ngx_http_upstream_finalize_request(r, u, |
| 611 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 612 | return; |
| 613 | } |
| 614 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 615 | if (u->peer.connection == NULL) { |
| 616 | return; |
| 617 | } |
| 618 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 619 | #if (NGX_HAVE_KQUEUE) |
| 620 | |
| 621 | if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
| 622 | |
| 623 | if (!ev->pending_eof) { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | ev->eof = 1; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 628 | c->error = 1; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 629 | |
| 630 | if (ev->kq_errno) { |
| 631 | ev->error = 1; |
| 632 | } |
| 633 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 634 | if (!u->cacheable && !u->store && u->peer.connection) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 635 | ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 636 | "kevent() reported that client closed prematurely " |
| 637 | "connection, so upstream connection is closed too"); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 638 | ngx_http_upstream_finalize_request(r, u, |
| 639 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, |
| 644 | "kevent() reported that client closed " |
| 645 | "prematurely connection"); |
| 646 | |
| 647 | if (u->peer.connection == NULL) { |
| 648 | ngx_http_upstream_finalize_request(r, u, |
| 649 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | #endif |
| 657 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 658 | n = recv(c->fd, buf, 1, MSG_PEEK); |
| 659 | |
| 660 | err = ngx_socket_errno; |
| 661 | |
Igor Sysoev | 7b2d87f | 2007-02-15 10:00:59 +0000 | [diff] [blame] | 662 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, err, |
| 663 | "http upstream recv(): %d", n); |
| 664 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 665 | /* |
| 666 | * we do not need to disable the write event because |
| 667 | * that event has NGX_USE_CLEAR_EVENT type |
| 668 | */ |
| 669 | |
| 670 | if (ev->write && (n >= 0 || err == NGX_EAGAIN)) { |
| 671 | return; |
| 672 | } |
| 673 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 674 | if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) { |
| 675 | if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) { |
| 676 | ngx_http_upstream_finalize_request(r, u, |
| 677 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 678 | return; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (n > 0) { |
| 683 | return; |
| 684 | } |
| 685 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 686 | if (n == -1) { |
| 687 | if (err == NGX_EAGAIN) { |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | ev->error = 1; |
| 692 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 693 | } else { /* n == 0 */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 694 | err = 0; |
| 695 | } |
| 696 | |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 697 | ev->eof = 1; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 698 | c->error = 1; |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 699 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 700 | if (!u->cacheable && !u->store && u->peer.connection) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 701 | ngx_log_error(NGX_LOG_INFO, ev->log, err, |
| 702 | "client closed prematurely connection, " |
| 703 | "so upstream connection is closed too"); |
| 704 | ngx_http_upstream_finalize_request(r, u, |
| 705 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | ngx_log_error(NGX_LOG_INFO, ev->log, err, |
| 710 | "client closed prematurely connection"); |
| 711 | |
| 712 | if (u->peer.connection == NULL) { |
| 713 | ngx_http_upstream_finalize_request(r, u, |
| 714 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 715 | return; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 720 | static void |
| 721 | ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 722 | { |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 723 | ngx_int_t rc; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 724 | ngx_time_t *tp; |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 725 | ngx_connection_t *c; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 726 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 727 | r->connection->log->action = "connecting to upstream"; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 728 | |
| 729 | r->connection->single_connection = 0; |
| 730 | |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 731 | if (u->state && u->state->response_sec) { |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 732 | tp = ngx_timeofday(); |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 733 | u->state->response_sec = tp->sec - u->state->response_sec; |
| 734 | u->state->response_msec = tp->msec - u->state->response_msec; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 737 | u->state = ngx_array_push(r->upstream_states); |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 738 | if (u->state == NULL) { |
| 739 | ngx_http_upstream_finalize_request(r, u, |
| 740 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t)); |
| 745 | |
| 746 | tp = ngx_timeofday(); |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 747 | u->state->response_sec = tp->sec; |
| 748 | u->state->response_msec = tp->msec; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 749 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 750 | rc = ngx_event_connect_peer(&u->peer); |
| 751 | |
| 752 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 753 | "http upstream connect: %i", rc); |
| 754 | |
| 755 | if (rc == NGX_ERROR) { |
| 756 | ngx_http_upstream_finalize_request(r, u, |
| 757 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 758 | return; |
| 759 | } |
| 760 | |
Igor Sysoev | 6d4b5a1 | 2007-12-11 16:26:56 +0000 | [diff] [blame] | 761 | u->state->peer = u->peer.name; |
| 762 | |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 763 | if (rc == NGX_BUSY) { |
| 764 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams"); |
Igor Sysoev | 6876bcd | 2007-08-09 13:54:33 +0000 | [diff] [blame] | 765 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE); |
| 766 | return; |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Igor Sysoev | 6876bcd | 2007-08-09 13:54:33 +0000 | [diff] [blame] | 769 | if (rc == NGX_DECLINED) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 770 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
| 771 | return; |
| 772 | } |
| 773 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 774 | /* rc == NGX_OK || rc == NGX_AGAIN */ |
| 775 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 776 | c = u->peer.connection; |
| 777 | |
| 778 | c->data = r; |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 779 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 780 | c->write->handler = ngx_http_upstream_handler; |
| 781 | c->read->handler = ngx_http_upstream_handler; |
| 782 | |
| 783 | u->write_event_handler = ngx_http_upstream_send_request_handler; |
| 784 | u->read_event_handler = ngx_http_upstream_process_header; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 785 | |
Igor Sysoev | e101338 | 2007-08-14 20:44:09 +0000 | [diff] [blame] | 786 | c->sendfile &= r->connection->sendfile; |
Igor Sysoev | a4ec686 | 2008-04-04 12:07:13 +0000 | [diff] [blame] | 787 | u->output.sendfile = c->sendfile; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 788 | |
| 789 | c->pool = r->pool; |
| 790 | c->read->log = c->write->log = c->log = r->connection->log; |
| 791 | |
| 792 | /* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */ |
| 793 | |
| 794 | u->writer.out = NULL; |
| 795 | u->writer.last = &u->writer.out; |
| 796 | u->writer.connection = c; |
| 797 | u->writer.limit = 0; |
| 798 | |
| 799 | if (u->request_sent) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 800 | if (ngx_http_upstream_reinit(r, u) != NGX_OK) { |
| 801 | ngx_http_upstream_finalize_request(r, u, |
| 802 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 803 | return; |
| 804 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Igor Sysoev | 8ff1e32 | 2007-01-19 12:57:02 +0000 | [diff] [blame] | 807 | if (r->request_body |
| 808 | && r->request_body->buf |
| 809 | && r->request_body->temp_file |
| 810 | && r == r->main) |
| 811 | { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 812 | /* |
| 813 | * the r->request_body->buf can be reused for one request only, |
| 814 | * the subrequests should allocate their own temporay bufs |
| 815 | */ |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 816 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 817 | u->output.free = ngx_alloc_chain_link(r->pool); |
| 818 | if (u->output.free == NULL) { |
| 819 | ngx_http_upstream_finalize_request(r, u, |
| 820 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 821 | return; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 822 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 823 | |
| 824 | u->output.free->buf = r->request_body->buf; |
| 825 | u->output.free->next = NULL; |
| 826 | u->output.allocated = 1; |
| 827 | |
| 828 | r->request_body->buf->pos = r->request_body->buf->start; |
| 829 | r->request_body->buf->last = r->request_body->buf->start; |
| 830 | r->request_body->buf->tag = u->output.tag; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | u->request_sent = 0; |
| 834 | |
| 835 | if (rc == NGX_AGAIN) { |
| 836 | ngx_add_timer(c->write, u->conf->connect_timeout); |
| 837 | return; |
| 838 | } |
| 839 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 840 | #if (NGX_HTTP_SSL) |
| 841 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 842 | if (u->ssl && c->ssl == NULL) { |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 843 | ngx_http_upstream_ssl_init_connection(r, u, c); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | |
| 847 | #endif |
| 848 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 849 | ngx_http_upstream_send_request(r, u); |
| 850 | } |
| 851 | |
| 852 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 853 | #if (NGX_HTTP_SSL) |
| 854 | |
| 855 | static void |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 856 | ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r, |
| 857 | ngx_http_upstream_t *u, ngx_connection_t *c) |
| 858 | { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 859 | ngx_int_t rc; |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 860 | |
| 861 | if (ngx_ssl_create_connection(u->conf->ssl, c, |
| 862 | NGX_SSL_BUFFER|NGX_SSL_CLIENT) |
| 863 | == NGX_ERROR) |
| 864 | { |
| 865 | ngx_http_upstream_finalize_request(r, u, |
| 866 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | c->sendfile = 0; |
Igor Sysoev | 13c6874 | 2006-03-10 12:51:52 +0000 | [diff] [blame] | 871 | u->output.sendfile = 0; |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 872 | |
Igor Sysoev | 916ee8e | 2008-08-17 17:47:52 +0000 | [diff] [blame] | 873 | if (u->conf->ssl_session_reuse) { |
| 874 | if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) { |
| 875 | ngx_http_upstream_finalize_request(r, u, |
| 876 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 877 | return; |
| 878 | } |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 881 | r->connection->log->action = "SSL handshaking to upstream"; |
| 882 | |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 883 | rc = ngx_ssl_handshake(c); |
| 884 | |
| 885 | if (rc == NGX_AGAIN) { |
| 886 | c->ssl->handler = ngx_http_upstream_ssl_handshake; |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | ngx_http_upstream_ssl_handshake(c); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | static void |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 895 | ngx_http_upstream_ssl_handshake(ngx_connection_t *c) |
| 896 | { |
| 897 | ngx_http_request_t *r; |
| 898 | ngx_http_upstream_t *u; |
| 899 | |
| 900 | r = c->data; |
| 901 | u = r->upstream; |
| 902 | |
| 903 | if (c->ssl->handshaked) { |
| 904 | |
Igor Sysoev | 916ee8e | 2008-08-17 17:47:52 +0000 | [diff] [blame] | 905 | if (u->conf->ssl_session_reuse) { |
| 906 | u->peer.save_session(&u->peer, u->peer.data); |
| 907 | } |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 908 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 909 | c->write->handler = ngx_http_upstream_handler; |
| 910 | c->read->handler = ngx_http_upstream_handler; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 911 | |
| 912 | ngx_http_upstream_send_request(r, u); |
| 913 | |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
| 918 | |
| 919 | } |
| 920 | |
| 921 | #endif |
| 922 | |
| 923 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 924 | static ngx_int_t |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 925 | ngx_http_upstream_reinit(ngx_http_request_t *r, ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 926 | { |
| 927 | ngx_chain_t *cl; |
| 928 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 929 | if (u->reinit_request(r) != NGX_OK) { |
| 930 | return NGX_ERROR; |
| 931 | } |
| 932 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 933 | ngx_memzero(&u->headers_in, sizeof(ngx_http_upstream_headers_in_t)); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 934 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 935 | if (ngx_list_init(&u->headers_in.headers, r->pool, 8, |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 936 | sizeof(ngx_table_elt_t)) |
| 937 | != NGX_OK) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 938 | { |
| 939 | return NGX_ERROR; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* reinit the request chain */ |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 943 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 944 | for (cl = u->request_bufs; cl; cl = cl->next) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 945 | cl->buf->pos = cl->buf->start; |
| 946 | cl->buf->file_pos = 0; |
| 947 | } |
| 948 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 949 | /* reinit the subrequest's ngx_output_chain() context */ |
| 950 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 951 | if (r->request_body && r->request_body->temp_file |
Igor Sysoev | df3254a | 2006-01-11 15:26:57 +0000 | [diff] [blame] | 952 | && r != r->main && u->output.buf) |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 953 | { |
| 954 | u->output.free = ngx_alloc_chain_link(r->pool); |
| 955 | if (u->output.free == NULL) { |
| 956 | return NGX_ERROR; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 957 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 958 | |
| 959 | u->output.free->buf = u->output.buf; |
| 960 | u->output.free->next = NULL; |
| 961 | |
| 962 | u->output.buf->pos = u->output.buf->start; |
| 963 | u->output.buf->last = u->output.buf->start; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 964 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 965 | |
| 966 | u->output.buf = NULL; |
| 967 | u->output.in = NULL; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 968 | u->output.busy = NULL; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 969 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 970 | /* reinit u->buffer */ |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 971 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 972 | #if 0 |
| 973 | if (u->cache) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 974 | u->buffer.pos = u->buffer.start + u->cache->ctx.header_size; |
| 975 | u->buffer.last = u->buffer.pos; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 976 | |
| 977 | } else { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 978 | u->buffer.pos = u->buffer.start; |
| 979 | u->buffer.last = u->buffer.start; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 980 | } |
| 981 | #else |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 982 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 983 | u->buffer.pos = u->buffer.start; |
| 984 | u->buffer.last = u->buffer.start; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 985 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 986 | #endif |
| 987 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 988 | return NGX_OK; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 992 | static void |
| 993 | ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 994 | { |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 995 | ngx_int_t rc; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 996 | ngx_connection_t *c; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 997 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 998 | c = u->peer.connection; |
| 999 | |
| 1000 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1001 | "http upstream send request"); |
| 1002 | |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1003 | if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) { |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1004 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1005 | return; |
Igor Sysoev | c55a104 | 2006-08-09 19:59:45 +0000 | [diff] [blame] | 1006 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1007 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 1008 | c->log->action = "sending request to upstream"; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1009 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1010 | rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1011 | |
| 1012 | u->request_sent = 1; |
| 1013 | |
| 1014 | if (rc == NGX_ERROR) { |
| 1015 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (c->write->timer_set) { |
| 1020 | ngx_del_timer(c->write); |
| 1021 | } |
| 1022 | |
| 1023 | if (rc == NGX_AGAIN) { |
| 1024 | ngx_add_timer(c->write, u->conf->send_timeout); |
| 1025 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1026 | if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1027 | ngx_http_upstream_finalize_request(r, u, |
| 1028 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | /* rc == NGX_OK */ |
| 1036 | |
| 1037 | if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) { |
| 1038 | if (ngx_tcp_push(c->fd) == NGX_ERROR) { |
| 1039 | ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno, |
| 1040 | ngx_tcp_push_n " failed"); |
| 1041 | ngx_http_upstream_finalize_request(r, u, |
| 1042 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1043 | return; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | c->tcp_nopush = NGX_TCP_NOPUSH_UNSET; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1047 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1048 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1049 | ngx_add_timer(c->read, u->conf->read_timeout); |
| 1050 | |
| 1051 | #if 1 |
| 1052 | if (c->read->ready) { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1053 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1054 | /* post aio operation */ |
| 1055 | |
| 1056 | /* |
| 1057 | * TODO comment |
| 1058 | * although we can post aio operation just in the end |
| 1059 | * of ngx_http_upstream_connect() CHECK IT !!! |
| 1060 | * it's better to do here because we postpone header buffer allocation |
| 1061 | */ |
| 1062 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1063 | ngx_http_upstream_process_header(r, u); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1064 | return; |
| 1065 | } |
| 1066 | #endif |
| 1067 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1068 | u->write_event_handler = ngx_http_upstream_dummy_handler; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1069 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1070 | if (ngx_handle_write_event(c->write, 0) != NGX_OK) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1071 | ngx_http_upstream_finalize_request(r, u, |
| 1072 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1073 | return; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 1078 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1079 | ngx_http_upstream_send_request_handler(ngx_http_request_t *r, |
| 1080 | ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1081 | { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1082 | ngx_connection_t *c; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1083 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1084 | c = u->peer.connection; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1085 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1086 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1087 | "http upstream send request handler"); |
| 1088 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1089 | if (c->write->timedout) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1090 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 1094 | #if (NGX_HTTP_SSL) |
| 1095 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 1096 | if (u->ssl && c->ssl == NULL) { |
Igor Sysoev | 43f279d | 2005-12-18 16:02:44 +0000 | [diff] [blame] | 1097 | ngx_http_upstream_ssl_init_connection(r, u, c); |
| 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | #endif |
| 1102 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 1103 | if (u->header_sent) { |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1104 | u->write_event_handler = ngx_http_upstream_dummy_handler; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 1105 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1106 | (void) ngx_handle_write_event(c->write, 0); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 1107 | |
| 1108 | return; |
| 1109 | } |
| 1110 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1111 | ngx_http_upstream_send_request(r, u); |
| 1112 | } |
| 1113 | |
| 1114 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 1115 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1116 | ngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1117 | { |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1118 | ssize_t n; |
| 1119 | ngx_int_t rc; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1120 | ngx_str_t *uri, args; |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1121 | ngx_uint_t i, flags; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1122 | ngx_list_part_t *part; |
| 1123 | ngx_table_elt_t *h; |
| 1124 | ngx_connection_t *c; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1125 | ngx_http_upstream_header_t *hh; |
| 1126 | ngx_http_upstream_main_conf_t *umcf; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1127 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1128 | c = u->peer.connection; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1129 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1130 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 1131 | "http upstream process header"); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1132 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 1133 | c->log->action = "reading response header from upstream"; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1134 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1135 | if (c->read->timedout) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1136 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT); |
| 1137 | return; |
| 1138 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1139 | |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1140 | if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) { |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1141 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1145 | if (u->buffer.start == NULL) { |
| 1146 | u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size); |
| 1147 | if (u->buffer.start == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1148 | ngx_http_upstream_finalize_request(r, u, |
| 1149 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1153 | u->buffer.pos = u->buffer.start; |
| 1154 | u->buffer.last = u->buffer.start; |
| 1155 | u->buffer.end = u->buffer.start + u->conf->buffer_size; |
| 1156 | u->buffer.temporary = 1; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1157 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1158 | u->buffer.tag = u->output.tag; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1159 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 1160 | if (ngx_list_init(&u->headers_in.headers, r->pool, 8, |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1161 | sizeof(ngx_table_elt_t)) |
| 1162 | != NGX_OK) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1163 | { |
| 1164 | ngx_http_upstream_finalize_request(r, u, |
| 1165 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1166 | return; |
| 1167 | } |
| 1168 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1169 | #if 0 |
| 1170 | if (u->cache) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1171 | u->buffer.pos += u->cache->ctx.header_size; |
| 1172 | u->buffer.last = u->buffer.pos; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1173 | } |
| 1174 | #endif |
| 1175 | } |
| 1176 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1177 | for ( ;; ) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1178 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1179 | n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last); |
| 1180 | |
| 1181 | if (n == NGX_AGAIN) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1182 | #if 0 |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1183 | ngx_add_timer(rev, u->read_timeout); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1184 | #endif |
| 1185 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1186 | if (ngx_handle_read_event(c->read, 0) != NGX_OK) { |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1187 | ngx_http_upstream_finalize_request(r, u, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1188 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1189 | return; |
| 1190 | } |
| 1191 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
| 1194 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1195 | if (n == 0) { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1196 | ngx_log_error(NGX_LOG_ERR, c->log, 0, |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1197 | "upstream prematurely closed connection"); |
| 1198 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1199 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1200 | if (n == NGX_ERROR || n == 0) { |
| 1201 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1202 | return; |
| 1203 | } |
| 1204 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1205 | u->buffer.last += n; |
| 1206 | |
| 1207 | #if 0 |
| 1208 | u->valid_header_in = 0; |
| 1209 | |
| 1210 | u->peer.cached = 0; |
| 1211 | #endif |
| 1212 | |
| 1213 | rc = u->process_header(r); |
| 1214 | |
| 1215 | if (rc == NGX_AGAIN) { |
| 1216 | |
| 1217 | if (u->buffer.pos == u->buffer.end) { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1218 | ngx_log_error(NGX_LOG_ERR, c->log, 0, |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1219 | "upstream sent too big header"); |
| 1220 | |
| 1221 | ngx_http_upstream_next(r, u, |
| 1222 | NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | continue; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Igor Sysoev | 15fbaa6 | 2008-12-01 14:08:00 +0000 | [diff] [blame] | 1229 | break; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) { |
| 1233 | ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER); |
| 1234 | return; |
| 1235 | } |
| 1236 | |
Igor Sysoev | 5a55d71 | 2007-12-09 08:22:35 +0000 | [diff] [blame] | 1237 | if (rc == NGX_ERROR) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1238 | ngx_http_upstream_finalize_request(r, u, |
| 1239 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1240 | return; |
| 1241 | } |
| 1242 | |
| 1243 | /* rc == NGX_OK */ |
| 1244 | |
Igor Sysoev | d01eea1 | 2008-09-30 14:57:09 +0000 | [diff] [blame] | 1245 | if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST) { |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 1246 | |
Igor Sysoev | 797c6ef | 2008-09-30 15:39:02 +0000 | [diff] [blame] | 1247 | if (r->subrequest_in_memory) { |
| 1248 | u->buffer.last = u->buffer.pos; |
| 1249 | } |
| 1250 | |
| 1251 | if (ngx_http_upstream_test_next(r, u) == NGX_OK) { |
| 1252 | return; |
| 1253 | } |
| 1254 | |
Igor Sysoev | d01eea1 | 2008-09-30 14:57:09 +0000 | [diff] [blame] | 1255 | if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) { |
| 1256 | return; |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1260 | umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); |
| 1261 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 1262 | if (u->headers_in.x_accel_redirect) { |
Igor Sysoev | cea316d | 2006-10-10 13:17:58 +0000 | [diff] [blame] | 1263 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1264 | ngx_http_upstream_finalize_request(r, u, NGX_DECLINED); |
| 1265 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 1266 | part = &u->headers_in.headers.part; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1267 | h = part->elts; |
| 1268 | |
| 1269 | for (i = 0; /* void */; i++) { |
| 1270 | |
| 1271 | if (i >= part->nelts) { |
| 1272 | if (part->next == NULL) { |
| 1273 | break; |
| 1274 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1275 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1276 | part = part->next; |
| 1277 | h = part->elts; |
| 1278 | i = 0; |
| 1279 | } |
| 1280 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1281 | hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, |
| 1282 | h[i].lowcase_key, h[i].key.len); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1283 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1284 | if (hh && hh->redirect) { |
| 1285 | if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { |
Igor Sysoev | 3718891 | 2006-10-17 16:13:49 +0000 | [diff] [blame] | 1286 | ngx_http_finalize_request(r, |
Igor Sysoev | cea316d | 2006-10-10 13:17:58 +0000 | [diff] [blame] | 1287 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | } |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 1293 | uri = &u->headers_in.x_accel_redirect->value; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1294 | args.len = 0; |
| 1295 | args.data = NULL; |
| 1296 | flags = 0; |
| 1297 | |
| 1298 | if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) { |
Igor Sysoev | cea316d | 2006-10-10 13:17:58 +0000 | [diff] [blame] | 1299 | ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | if (flags & NGX_HTTP_ZERO_IN_URI) { |
| 1304 | r->zero_in_uri = 1; |
| 1305 | } |
| 1306 | |
Igor Sysoev | afd7ec5 | 2006-05-29 17:28:12 +0000 | [diff] [blame] | 1307 | if (r->method != NGX_HTTP_HEAD) { |
| 1308 | r->method = NGX_HTTP_GET; |
| 1309 | } |
| 1310 | |
Igor Sysoev | f60dde9 | 2008-08-17 17:44:08 +0000 | [diff] [blame] | 1311 | r->valid_unparsed_uri = 0; |
| 1312 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1313 | ngx_http_internal_redirect(r, uri, &args); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 1317 | part = &u->headers_in.headers.part; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1318 | h = part->elts; |
| 1319 | |
| 1320 | for (i = 0; /* void */; i++) { |
| 1321 | |
| 1322 | if (i >= part->nelts) { |
| 1323 | if (part->next == NULL) { |
| 1324 | break; |
| 1325 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1326 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1327 | part = part->next; |
| 1328 | h = part->elts; |
| 1329 | i = 0; |
| 1330 | } |
| 1331 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1332 | if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash, |
| 1333 | h[i].lowcase_key, h[i].key.len)) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1334 | { |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1335 | continue; |
| 1336 | } |
| 1337 | |
| 1338 | hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash, |
| 1339 | h[i].lowcase_key, h[i].key.len); |
| 1340 | |
| 1341 | if (hh) { |
| 1342 | if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1343 | ngx_http_upstream_finalize_request(r, u, |
| 1344 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | continue; |
| 1349 | } |
| 1350 | |
| 1351 | if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) { |
| 1352 | ngx_http_upstream_finalize_request(r, u, |
| 1353 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1354 | return; |
| 1355 | } |
| 1356 | } |
| 1357 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1358 | if (r->headers_out.server && r->headers_out.server->value.data == NULL) { |
| 1359 | r->headers_out.server->hash = 0; |
| 1360 | } |
| 1361 | |
| 1362 | if (r->headers_out.date && r->headers_out.date->value.data == NULL) { |
| 1363 | r->headers_out.date->hash = 0; |
| 1364 | } |
| 1365 | |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 1366 | r->headers_out.status = u->headers_in.status_n; |
| 1367 | r->headers_out.status_line = u->headers_in.status_line; |
| 1368 | |
Igor Sysoev | be0a61e | 2008-02-11 13:14:56 +0000 | [diff] [blame] | 1369 | u->headers_in.content_length_n = r->headers_out.content_length_n; |
| 1370 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1371 | if (r->headers_out.content_length_n != -1) { |
| 1372 | u->length = (size_t) r->headers_out.content_length_n; |
| 1373 | |
| 1374 | } else { |
| 1375 | u->length = NGX_MAX_SIZE_T_VALUE; |
| 1376 | } |
| 1377 | |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1378 | if (!r->subrequest_in_memory) { |
| 1379 | ngx_http_upstream_send_response(r, u); |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | /* subrequest content in memory */ |
| 1384 | |
| 1385 | if (u->input_filter == NULL) { |
| 1386 | u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; |
| 1387 | u->input_filter = ngx_http_upstream_non_buffered_filter; |
| 1388 | u->input_filter_ctx = r; |
| 1389 | } |
| 1390 | |
| 1391 | if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { |
| 1392 | ngx_http_upstream_finalize_request(r, u, |
| 1393 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
Igor Sysoev | a485909 | 2008-11-12 21:01:01 +0000 | [diff] [blame] | 1397 | n = u->buffer.last - u->buffer.pos; |
| 1398 | |
| 1399 | if (n) { |
| 1400 | u->buffer.last -= n; |
| 1401 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1402 | u->state->response_length += n; |
| 1403 | |
Igor Sysoev | a485909 | 2008-11-12 21:01:01 +0000 | [diff] [blame] | 1404 | if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1405 | ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
| 1406 | return; |
| 1407 | } |
| 1408 | |
Igor Sysoev | a485909 | 2008-11-12 21:01:01 +0000 | [diff] [blame] | 1409 | if (u->length == 0) { |
| 1410 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1411 | return; |
| 1412 | } |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1415 | u->read_event_handler = ngx_http_upstream_process_body_in_memory; |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1416 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1417 | ngx_http_upstream_process_body_in_memory(r, u); |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1421 | static ngx_int_t |
Igor Sysoev | 797c6ef | 2008-09-30 15:39:02 +0000 | [diff] [blame] | 1422 | ngx_http_upstream_test_next(ngx_http_request_t *r, ngx_http_upstream_t *u) |
| 1423 | { |
| 1424 | ngx_uint_t status; |
| 1425 | ngx_http_upstream_next_t *un; |
| 1426 | |
| 1427 | if (!(u->conf->next_upstream & NGX_HTTP_UPSTREAM_FT_STATUS)) { |
| 1428 | return NGX_DECLINED; |
| 1429 | } |
| 1430 | |
| 1431 | status = u->headers_in.status_n; |
| 1432 | |
| 1433 | for (un = ngx_http_upstream_next_errors; un->status; un++) { |
| 1434 | |
| 1435 | if (status != un->status) { |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | if (u->peer.tries > 1 && (u->conf->next_upstream & un->mask)) { |
| 1440 | ngx_http_upstream_next(r, u, un->mask); |
| 1441 | return NGX_OK; |
| 1442 | } |
| 1443 | |
| 1444 | if (status == NGX_HTTP_NOT_FOUND && u->conf->intercept_404) { |
| 1445 | ngx_http_upstream_finalize_request(r, u, NGX_HTTP_NOT_FOUND); |
| 1446 | return NGX_OK; |
| 1447 | } |
| 1448 | |
| 1449 | #if (NGX_HTTP_CACHE) |
| 1450 | |
| 1451 | if (u->peer.tries == 0 && u->stale && (u->conf->use_stale & un->mask)) { |
| 1452 | ngx_http_upstream_finalize_request(r, u, |
| 1453 | ngx_http_send_cached_response(r)); |
| 1454 | return NGX_OK; |
| 1455 | } |
| 1456 | |
| 1457 | #endif |
| 1458 | } |
| 1459 | |
| 1460 | return NGX_DECLINED; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | static ngx_int_t |
Igor Sysoev | d01eea1 | 2008-09-30 14:57:09 +0000 | [diff] [blame] | 1465 | ngx_http_upstream_intercept_errors(ngx_http_request_t *r, |
| 1466 | ngx_http_upstream_t *u) |
| 1467 | { |
| 1468 | ngx_int_t status; |
| 1469 | ngx_uint_t i; |
| 1470 | ngx_table_elt_t *h; |
| 1471 | ngx_http_err_page_t *err_page; |
| 1472 | ngx_http_core_loc_conf_t *clcf; |
| 1473 | |
| 1474 | if (!u->conf->intercept_errors) { |
| 1475 | return NGX_DECLINED; |
| 1476 | } |
| 1477 | |
| 1478 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 1479 | |
| 1480 | if (clcf->error_pages == NULL) { |
| 1481 | return NGX_DECLINED; |
| 1482 | } |
| 1483 | |
| 1484 | status = u->headers_in.status_n; |
| 1485 | |
| 1486 | err_page = clcf->error_pages->elts; |
| 1487 | for (i = 0; i < clcf->error_pages->nelts; i++) { |
| 1488 | |
| 1489 | if (err_page[i].status == status) { |
| 1490 | |
| 1491 | if (status == NGX_HTTP_UNAUTHORIZED) { |
| 1492 | |
| 1493 | h = ngx_list_push(&r->headers_out.headers); |
| 1494 | |
| 1495 | if (h == NULL) { |
| 1496 | ngx_http_upstream_finalize_request(r, u, |
| 1497 | NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1498 | return NGX_OK; |
| 1499 | } |
| 1500 | |
| 1501 | *h = *u->headers_in.www_authenticate; |
| 1502 | |
| 1503 | r->headers_out.www_authenticate = h; |
| 1504 | } |
| 1505 | |
| 1506 | ngx_http_upstream_finalize_request(r, u, status); |
| 1507 | |
| 1508 | return NGX_OK; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | return NGX_DECLINED; |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | static ngx_int_t |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1517 | ngx_http_upstream_test_connect(ngx_connection_t *c) |
| 1518 | { |
| 1519 | int err; |
| 1520 | socklen_t len; |
| 1521 | |
| 1522 | #if (NGX_HAVE_KQUEUE) |
| 1523 | |
| 1524 | if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1525 | if (c->write->pending_eof) { |
Igor Sysoev | 9e4b727 | 2007-09-09 18:43:16 +0000 | [diff] [blame] | 1526 | c->log->action = "connecting to upstream"; |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1527 | (void) ngx_connection_error(c, c->write->kq_errno, |
| 1528 | "kevent() reported that connect() failed"); |
| 1529 | return NGX_ERROR; |
| 1530 | } |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1531 | |
| 1532 | } else |
| 1533 | #endif |
| 1534 | { |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1535 | err = 0; |
| 1536 | len = sizeof(int); |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1537 | |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1538 | /* |
| 1539 | * BSDs and Linux return 0 and set a pending error in err |
| 1540 | * Solaris returns -1 and sets errno |
| 1541 | */ |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1542 | |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1543 | if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) |
| 1544 | == -1) |
| 1545 | { |
| 1546 | err = ngx_errno; |
| 1547 | } |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1548 | |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1549 | if (err) { |
Igor Sysoev | 9e4b727 | 2007-09-09 18:43:16 +0000 | [diff] [blame] | 1550 | c->log->action = "connecting to upstream"; |
Igor Sysoev | fb95894 | 2007-04-18 15:21:28 +0000 | [diff] [blame] | 1551 | (void) ngx_connection_error(c, err, "connect() failed"); |
| 1552 | return NGX_ERROR; |
| 1553 | } |
Igor Sysoev | 24a9594 | 2007-03-31 15:37:09 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | return NGX_OK; |
| 1557 | } |
| 1558 | |
| 1559 | |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1560 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1561 | ngx_http_upstream_process_body_in_memory(ngx_http_request_t *r, |
| 1562 | ngx_http_upstream_t *u) |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1563 | { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1564 | size_t size; |
| 1565 | ssize_t n; |
| 1566 | ngx_buf_t *b; |
| 1567 | ngx_event_t *rev; |
| 1568 | ngx_connection_t *c; |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1569 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1570 | c = u->peer.connection; |
| 1571 | rev = c->read; |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1572 | |
| 1573 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1574 | "http upstream process body on memory"); |
| 1575 | |
| 1576 | if (rev->timedout) { |
| 1577 | ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
| 1578 | ngx_http_upstream_finalize_request(r, u, NGX_ETIMEDOUT); |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | b = &u->buffer; |
| 1583 | |
| 1584 | for ( ;; ) { |
| 1585 | |
| 1586 | size = b->end - b->last; |
| 1587 | |
| 1588 | if (size == 0) { |
| 1589 | ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
| 1590 | "upstream buffer is too small to read repsonse"); |
| 1591 | ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
| 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | n = c->recv(c, b->last, size); |
| 1596 | |
| 1597 | if (n == NGX_AGAIN) { |
| 1598 | break; |
| 1599 | } |
| 1600 | |
| 1601 | if (n == 0 || n == NGX_ERROR) { |
| 1602 | ngx_http_upstream_finalize_request(r, u, n); |
| 1603 | return; |
| 1604 | } |
| 1605 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1606 | u->state->response_length += n; |
| 1607 | |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1608 | if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { |
| 1609 | ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | if (!rev->ready) { |
| 1614 | break; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1618 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1619 | ngx_http_upstream_finalize_request(r, u, NGX_ERROR); |
| 1620 | return; |
| 1621 | } |
| 1622 | |
| 1623 | if (rev->active) { |
| 1624 | ngx_add_timer(rev, u->conf->read_timeout); |
| 1625 | |
| 1626 | } else if (rev->timer_set) { |
| 1627 | ngx_del_timer(rev); |
| 1628 | } |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | |
| 1632 | static void |
| 1633 | ngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u) |
| 1634 | { |
| 1635 | int tcp_nodelay; |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1636 | ssize_t n; |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1637 | ngx_int_t rc; |
| 1638 | ngx_event_pipe_t *p; |
| 1639 | ngx_connection_t *c; |
| 1640 | ngx_pool_cleanup_t *cl; |
| 1641 | ngx_pool_cleanup_file_t *clf; |
| 1642 | ngx_http_core_loc_conf_t *clcf; |
| 1643 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1644 | rc = ngx_http_send_header(r); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1645 | |
Igor Sysoev | 8488a1c | 2007-01-15 17:12:43 +0000 | [diff] [blame] | 1646 | if (rc == NGX_ERROR || rc > NGX_OK || r->post_action || r->header_only) { |
Igor Sysoev | 805d9db | 2005-02-03 19:33:37 +0000 | [diff] [blame] | 1647 | ngx_http_upstream_finalize_request(r, u, rc); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | |
| 1651 | u->header_sent = 1; |
| 1652 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1653 | if (r->request_body && r->request_body->temp_file) { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 1654 | for (cl = r->pool->cleanup; cl; cl = cl->next) { |
| 1655 | if (cl->handler == ngx_pool_cleanup_file) { |
| 1656 | clf = cl->data; |
| 1657 | |
| 1658 | if (clf->fd == r->request_body->temp_file->file.fd) { |
| 1659 | cl->handler(clf); |
| 1660 | cl->handler = NULL; |
Igor Sysoev | e240762 | 2006-06-30 12:19:32 +0000 | [diff] [blame] | 1661 | r->request_body->temp_file->file.fd = NGX_INVALID_FILE; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1668 | c = r->connection; |
| 1669 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1670 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 1671 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 1672 | if (!u->buffering) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1673 | |
| 1674 | if (u->input_filter == NULL) { |
| 1675 | u->input_filter_init = ngx_http_upstream_non_buffered_filter_init; |
| 1676 | u->input_filter = ngx_http_upstream_non_buffered_filter; |
| 1677 | u->input_filter_ctx = r; |
| 1678 | } |
| 1679 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1680 | u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1681 | r->write_event_handler = |
| 1682 | ngx_http_upstream_process_non_buffered_downstream; |
| 1683 | |
| 1684 | r->limit_rate = 0; |
| 1685 | |
| 1686 | if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) { |
| 1687 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1688 | return; |
| 1689 | } |
| 1690 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1691 | if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { |
| 1692 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); |
| 1693 | |
| 1694 | tcp_nodelay = 1; |
| 1695 | |
| 1696 | if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, |
| 1697 | (const void *) &tcp_nodelay, sizeof(int)) == -1) |
| 1698 | { |
| 1699 | ngx_connection_error(c, ngx_socket_errno, |
| 1700 | "setsockopt(TCP_NODELAY) failed"); |
| 1701 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | c->tcp_nodelay = NGX_TCP_NODELAY_SET; |
| 1706 | } |
| 1707 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1708 | n = u->buffer.last - u->buffer.pos; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1709 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1710 | if (n) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1711 | u->buffer.last = u->buffer.pos; |
| 1712 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1713 | u->state->response_length += n; |
| 1714 | |
| 1715 | if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1716 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1717 | return; |
| 1718 | } |
| 1719 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1720 | ngx_http_upstream_process_non_buffered_downstream(r); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1721 | |
| 1722 | } else { |
| 1723 | u->buffer.pos = u->buffer.start; |
| 1724 | u->buffer.last = u->buffer.start; |
| 1725 | |
| 1726 | if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) { |
| 1727 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1728 | return; |
| 1729 | } |
Igor Sysoev | 3dea919 | 2007-09-09 18:32:53 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (u->peer.connection->read->ready) { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1732 | ngx_http_upstream_process_non_buffered_upstream(r, u); |
Igor Sysoev | 3dea919 | 2007-09-09 18:32:53 +0000 | [diff] [blame] | 1733 | } |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | return; |
| 1737 | } |
| 1738 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1739 | /* TODO: preallocate event_pipe bufs, look "Content-Length" */ |
| 1740 | |
| 1741 | #if 0 |
| 1742 | |
| 1743 | if (u->cache && u->cache->ctx.file.fd != NGX_INVALID_FILE) { |
| 1744 | if (ngx_close_file(u->cache->ctx.file.fd) == NGX_FILE_ERROR) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1745 | ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1746 | ngx_close_file_n " \"%s\" failed", |
| 1747 | u->cache->ctx.file.name.data); |
| 1748 | } |
| 1749 | } |
| 1750 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 1751 | if (u->cacheable) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1752 | header = (ngx_http_cache_header_t *) u->buffer->start; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1753 | |
| 1754 | header->expires = u->cache->ctx.expires; |
| 1755 | header->last_modified = u->cache->ctx.last_modified; |
| 1756 | header->date = u->cache->ctx.date; |
| 1757 | header->length = r->headers_out.content_length_n; |
| 1758 | u->cache->ctx.length = r->headers_out.content_length_n; |
| 1759 | |
| 1760 | header->key_len = u->cache->ctx.key0.len; |
| 1761 | ngx_memcpy(&header->key, u->cache->ctx.key0.data, header->key_len); |
| 1762 | header->key[header->key_len] = LF; |
| 1763 | } |
| 1764 | |
| 1765 | #endif |
| 1766 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1767 | p = u->pipe; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1768 | |
| 1769 | p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter; |
| 1770 | p->output_ctx = r; |
| 1771 | p->tag = u->output.tag; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1772 | p->bufs = u->conf->bufs; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1773 | p->busy_size = u->conf->busy_buffers_size; |
| 1774 | p->upstream = u->peer.connection; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1775 | p->downstream = c; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1776 | p->pool = r->pool; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1777 | p->log = c->log; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1778 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 1779 | p->cacheable = u->cacheable || u->store; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1780 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1781 | p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
| 1782 | if (p->temp_file == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1783 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | p->temp_file->file.fd = NGX_INVALID_FILE; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1788 | p->temp_file->file.log = c->log; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1789 | p->temp_file->path = u->conf->temp_path; |
| 1790 | p->temp_file->pool = r->pool; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1791 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 1792 | if (u->cacheable || u->store) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1793 | p->temp_file->persistent = 1; |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 1794 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1795 | } else { |
Igor Sysoev | 8f12558 | 2006-07-28 15:16:17 +0000 | [diff] [blame] | 1796 | p->temp_file->log_level = NGX_LOG_WARN; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1797 | p->temp_file->warn = "an upstream response is buffered " |
| 1798 | "to a temporary file"; |
| 1799 | } |
| 1800 | |
| 1801 | p->max_temp_file_size = u->conf->max_temp_file_size; |
| 1802 | p->temp_file_write_size = u->conf->temp_file_write_size; |
| 1803 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1804 | p->preread_bufs = ngx_alloc_chain_link(r->pool); |
| 1805 | if (p->preread_bufs == NULL) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1806 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1807 | return; |
| 1808 | } |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 1809 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1810 | p->preread_bufs->buf = &u->buffer; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1811 | p->preread_bufs->next = NULL; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1812 | u->buffer.recycled = 1; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1813 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1814 | p->preread_size = u->buffer.last - u->buffer.pos; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1815 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 1816 | if (u->cacheable) { |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 1817 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1818 | p->buf_to_file = ngx_calloc_buf(r->pool); |
| 1819 | if (p->buf_to_file == NULL) { |
| 1820 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1821 | return; |
| 1822 | } |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 1823 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1824 | p->buf_to_file->pos = u->buffer.start; |
| 1825 | p->buf_to_file->last = u->buffer.pos; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1826 | p->buf_to_file->temporary = 1; |
| 1827 | } |
| 1828 | |
| 1829 | if (ngx_event_flags & NGX_USE_AIO_EVENT) { |
| 1830 | /* the posted aio operation may currupt a shadow buffer */ |
| 1831 | p->single_buf = 1; |
| 1832 | } |
| 1833 | |
| 1834 | /* TODO: p->free_bufs = 0 if use ngx_create_chain_of_bufs() */ |
| 1835 | p->free_bufs = 1; |
| 1836 | |
| 1837 | /* |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1838 | * event_pipe would do u->buffer.last += p->preread_size |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1839 | * as though these bytes were read |
| 1840 | */ |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1841 | u->buffer.last = u->buffer.pos; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1842 | |
| 1843 | if (u->conf->cyclic_temp_file) { |
| 1844 | |
| 1845 | /* |
| 1846 | * we need to disable the use of sendfile() if we use cyclic temp file |
| 1847 | * because the writing a new data may interfere with sendfile() |
| 1848 | * that uses the same kernel file pages (at least on FreeBSD) |
| 1849 | */ |
| 1850 | |
| 1851 | p->cyclic_temp_file = 1; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1852 | c->sendfile = 0; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1853 | |
| 1854 | } else { |
| 1855 | p->cyclic_temp_file = 0; |
| 1856 | } |
| 1857 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1858 | p->read_timeout = u->conf->read_timeout; |
| 1859 | p->send_timeout = clcf->send_timeout; |
| 1860 | p->send_lowat = clcf->send_lowat; |
| 1861 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1862 | u->read_event_handler = ngx_http_upstream_process_upstream; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1863 | r->write_event_handler = ngx_http_upstream_process_downstream; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1864 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1865 | ngx_http_upstream_process_upstream(r, u); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 1869 | static void |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1870 | ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r) |
| 1871 | { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1872 | ngx_event_t *wev; |
| 1873 | ngx_connection_t *c; |
| 1874 | ngx_http_upstream_t *u; |
| 1875 | |
| 1876 | c = r->connection; |
| 1877 | u = r->upstream; |
| 1878 | wev = c->write; |
| 1879 | |
| 1880 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1881 | "http upstream process non buffered downstream"); |
| 1882 | |
| 1883 | c->log->action = "sending to client"; |
| 1884 | |
| 1885 | if (wev->timedout) { |
| 1886 | c->timedout = 1; |
| 1887 | ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
| 1888 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1889 | return; |
| 1890 | } |
| 1891 | |
| 1892 | ngx_http_upstream_process_non_buffered_request(r, 1); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1897 | ngx_http_upstream_process_non_buffered_upstream(ngx_http_request_t *r, |
| 1898 | ngx_http_upstream_t *u) |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1899 | { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1900 | ngx_connection_t *c; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1901 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1902 | c = u->peer.connection; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1903 | |
| 1904 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1905 | "http upstream process non buffered upstream"); |
| 1906 | |
| 1907 | c->log->action = "reading upstream"; |
| 1908 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 1909 | if (c->read->timedout) { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1910 | ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
| 1911 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1912 | return; |
| 1913 | } |
| 1914 | |
| 1915 | ngx_http_upstream_process_non_buffered_request(r, 0); |
| 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | static void |
| 1920 | ngx_http_upstream_process_non_buffered_request(ngx_http_request_t *r, |
| 1921 | ngx_uint_t do_write) |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1922 | { |
| 1923 | size_t size; |
| 1924 | ssize_t n; |
| 1925 | ngx_buf_t *b; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1926 | ngx_int_t rc; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1927 | ngx_connection_t *downstream, *upstream; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1928 | ngx_http_upstream_t *u; |
| 1929 | ngx_http_core_loc_conf_t *clcf; |
| 1930 | |
Igor Sysoev | e3fbaea | 2006-10-10 13:12:13 +0000 | [diff] [blame] | 1931 | u = r->upstream; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 1932 | downstream = r->connection; |
| 1933 | upstream = u->peer.connection; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1934 | |
| 1935 | b = &u->buffer; |
| 1936 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 1937 | do_write = do_write || u->length == 0; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1938 | |
| 1939 | for ( ;; ) { |
| 1940 | |
| 1941 | if (do_write) { |
| 1942 | |
| 1943 | if (u->out_bufs || u->busy_bufs) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1944 | rc = ngx_http_output_filter(r, u->out_bufs); |
| 1945 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 1946 | if (downstream->destroyed) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 1947 | return; |
| 1948 | } |
| 1949 | |
| 1950 | if (rc == NGX_ERROR) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1951 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
| 1955 | ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, |
| 1956 | &u->out_bufs, u->output.tag); |
| 1957 | } |
| 1958 | |
| 1959 | if (u->busy_bufs == NULL) { |
| 1960 | |
| 1961 | if (u->length == 0 |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 1962 | || upstream->read->eof |
| 1963 | || upstream->read->error) |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1964 | { |
| 1965 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1966 | return; |
| 1967 | } |
| 1968 | |
| 1969 | b->pos = b->start; |
| 1970 | b->last = b->start; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | size = b->end - b->last; |
| 1975 | |
| 1976 | if (size > u->length) { |
| 1977 | size = u->length; |
| 1978 | } |
| 1979 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 1980 | if (size && upstream->read->ready) { |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 1981 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 1982 | n = upstream->recv(upstream, b->last, size); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (n == NGX_AGAIN) { |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | if (n > 0) { |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 1989 | u->state->response_length += n; |
| 1990 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1991 | if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { |
| 1992 | ngx_http_upstream_finalize_request(r, u, 0); |
| 1993 | return; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | do_write = 1; |
| 1998 | |
| 1999 | continue; |
| 2000 | } |
| 2001 | |
| 2002 | break; |
| 2003 | } |
| 2004 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2005 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 2006 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2007 | if (downstream->data == r) { |
| 2008 | if (ngx_handle_write_event(downstream->write, clcf->send_lowat) |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2009 | != NGX_OK) |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2010 | { |
| 2011 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2012 | return; |
| 2013 | } |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Igor Sysoev | ae2006b | 2007-01-19 12:22:32 +0000 | [diff] [blame] | 2016 | if (downstream->write->active && !downstream->write->ready) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2017 | ngx_add_timer(downstream->write, clcf->send_timeout); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2018 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2019 | } else if (downstream->write->timer_set) { |
| 2020 | ngx_del_timer(downstream->write); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2023 | if (ngx_handle_read_event(upstream->read, 0) != NGX_OK) { |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2024 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2025 | return; |
| 2026 | } |
| 2027 | |
Igor Sysoev | ae2006b | 2007-01-19 12:22:32 +0000 | [diff] [blame] | 2028 | if (upstream->read->active && !upstream->read->ready) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2029 | ngx_add_timer(upstream->read, u->conf->read_timeout); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2030 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2031 | } else if (upstream->read->timer_set) { |
| 2032 | ngx_del_timer(upstream->read); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | static ngx_int_t |
| 2038 | ngx_http_upstream_non_buffered_filter_init(void *data) |
| 2039 | { |
| 2040 | return NGX_OK; |
| 2041 | } |
| 2042 | |
| 2043 | |
| 2044 | static ngx_int_t |
| 2045 | ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes) |
| 2046 | { |
| 2047 | ngx_http_request_t *r = data; |
| 2048 | |
| 2049 | ngx_buf_t *b; |
| 2050 | ngx_chain_t *cl, **ll; |
| 2051 | ngx_http_upstream_t *u; |
| 2052 | |
| 2053 | u = r->upstream; |
| 2054 | |
| 2055 | for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) { |
| 2056 | ll = &cl->next; |
| 2057 | } |
| 2058 | |
| 2059 | cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs); |
| 2060 | if (cl == NULL) { |
| 2061 | return NGX_ERROR; |
| 2062 | } |
| 2063 | |
| 2064 | *ll = cl; |
| 2065 | |
| 2066 | cl->buf->flush = 1; |
| 2067 | cl->buf->memory = 1; |
| 2068 | |
| 2069 | b = &u->buffer; |
| 2070 | |
| 2071 | cl->buf->pos = b->last; |
| 2072 | b->last += bytes; |
| 2073 | cl->buf->last = b->last; |
Igor Sysoev | 7c15b75 | 2008-03-03 10:42:00 +0000 | [diff] [blame] | 2074 | cl->buf->tag = u->output.tag; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2075 | |
| 2076 | if (u->length == NGX_MAX_SIZE_T_VALUE) { |
| 2077 | return NGX_OK; |
| 2078 | } |
| 2079 | |
| 2080 | u->length -= bytes; |
| 2081 | |
| 2082 | return NGX_OK; |
| 2083 | } |
| 2084 | |
| 2085 | |
| 2086 | static void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2087 | ngx_http_upstream_process_downstream(ngx_http_request_t *r) |
| 2088 | { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2089 | ngx_event_t *wev; |
| 2090 | ngx_connection_t *c; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2091 | ngx_event_pipe_t *p; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2092 | ngx_http_upstream_t *u; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2093 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2094 | c = r->connection; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2095 | u = r->upstream; |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2096 | p = u->pipe; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2097 | wev = c->write; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2098 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2099 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 2100 | "http upstream process downstream"); |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2101 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2102 | c->log->action = "sending to client"; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2103 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2104 | if (wev->timedout) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2105 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2106 | if (wev->delayed) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2107 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2108 | wev->timedout = 0; |
| 2109 | wev->delayed = 0; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2110 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2111 | if (!wev->ready) { |
| 2112 | ngx_add_timer(wev, p->send_timeout); |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2113 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2114 | if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2115 | ngx_http_upstream_finalize_request(r, u, 0); |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | return; |
| 2119 | } |
| 2120 | |
| 2121 | if (ngx_event_pipe(p, wev->write) == NGX_ABORT) { |
| 2122 | |
| 2123 | if (c->destroyed) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2127 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2128 | return; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2129 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2130 | |
| 2131 | } else { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2132 | p->downstream_error = 1; |
| 2133 | c->timedout = 1; |
| 2134 | ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out"); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | } else { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2138 | |
| 2139 | if (wev->delayed) { |
| 2140 | |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2141 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 2142 | "http downstream delayed"); |
| 2143 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2144 | if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) { |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2145 | ngx_http_upstream_finalize_request(r, u, 0); |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | return; |
| 2149 | } |
| 2150 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2151 | if (ngx_event_pipe(p, 1) == NGX_ABORT) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2152 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2153 | if (c->destroyed) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2154 | return; |
| 2155 | } |
| 2156 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2157 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2158 | return; |
| 2159 | } |
| 2160 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2161 | |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2162 | ngx_http_upstream_process_request(r); |
| 2163 | } |
| 2164 | |
| 2165 | |
| 2166 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2167 | ngx_http_upstream_process_upstream(ngx_http_request_t *r, |
| 2168 | ngx_http_upstream_t *u) |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2169 | { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2170 | ngx_connection_t *c; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2171 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2172 | c = u->peer.connection; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2173 | |
| 2174 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 2175 | "http upstream process upstream"); |
| 2176 | |
| 2177 | c->log->action = "reading upstream"; |
| 2178 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2179 | if (c->read->timedout) { |
| 2180 | u->pipe->upstream_error = 1; |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2181 | ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out"); |
| 2182 | |
| 2183 | } else { |
| 2184 | c = r->connection; |
| 2185 | |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2186 | if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2187 | |
| 2188 | if (c->destroyed) { |
| 2189 | return; |
| 2190 | } |
| 2191 | |
| 2192 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2193 | return; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | ngx_http_upstream_process_request(r); |
| 2198 | } |
| 2199 | |
| 2200 | |
| 2201 | static void |
| 2202 | ngx_http_upstream_process_request(ngx_http_request_t *r) |
| 2203 | { |
| 2204 | ngx_temp_file_t *tf; |
| 2205 | ngx_event_pipe_t *p; |
| 2206 | ngx_http_upstream_t *u; |
| 2207 | |
| 2208 | u = r->upstream; |
| 2209 | p = u->pipe; |
| 2210 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2211 | if (u->peer.connection) { |
| 2212 | |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2213 | if (u->store) { |
| 2214 | |
Igor Sysoev | be0a61e | 2008-02-11 13:14:56 +0000 | [diff] [blame] | 2215 | tf = u->pipe->temp_file; |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2216 | |
Igor Sysoev | be0a61e | 2008-02-11 13:14:56 +0000 | [diff] [blame] | 2217 | if (p->upstream_eof |
| 2218 | && u->headers_in.status_n == NGX_HTTP_OK |
| 2219 | && (u->headers_in.content_length_n == -1 |
| 2220 | || (u->headers_in.content_length_n == tf->offset))) |
| 2221 | { |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2222 | ngx_http_upstream_store(r, u); |
| 2223 | |
| 2224 | } else if ((p->upstream_error |
| 2225 | || (p->upstream_eof |
| 2226 | && u->headers_in.status_n != NGX_HTTP_OK)) |
Igor Sysoev | be0a61e | 2008-02-11 13:14:56 +0000 | [diff] [blame] | 2227 | && tf->file.fd != NGX_INVALID_FILE) |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2228 | { |
Igor Sysoev | be0a61e | 2008-02-11 13:14:56 +0000 | [diff] [blame] | 2229 | if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) { |
| 2230 | |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2231 | ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, |
| 2232 | ngx_delete_file_n " \"%s\" failed", |
| 2233 | u->pipe->temp_file->file.name.data); |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2238 | #if (NGX_HTTP_FILE_CACHE) |
| 2239 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 2240 | if (p->upstream_done && u->cacheable) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2241 | if (ngx_http_cache_update(r) == NGX_ERROR) { |
| 2242 | ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); |
| 2243 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 2247 | } else if (p->upstream_eof && u->cacheable) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2248 | |
| 2249 | /* TODO: check length & update cache */ |
| 2250 | |
| 2251 | if (ngx_http_cache_update(r) == NGX_ERROR) { |
| 2252 | ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); |
| 2253 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2254 | return; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | #endif |
| 2259 | |
| 2260 | if (p->upstream_done || p->upstream_eof || p->upstream_error) { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2261 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2262 | "http upstream exit: %p", p->out); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2263 | #if 0 |
| 2264 | ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); |
| 2265 | #endif |
| 2266 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2267 | return; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | if (p->downstream_error) { |
Igor Sysoev | 3e15a97 | 2008-12-08 18:28:06 +0000 | [diff] [blame] | 2272 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2273 | "http upstream downstream error"); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2274 | |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 2275 | if (!u->cacheable && u->peer.connection) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2276 | ngx_http_upstream_finalize_request(r, u, 0); |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 2282 | static void |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2283 | ngx_http_upstream_store(ngx_http_request_t *r, ngx_http_upstream_t *u) |
| 2284 | { |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2285 | size_t root; |
| 2286 | time_t lm; |
| 2287 | ngx_str_t path; |
| 2288 | ngx_temp_file_t *tf; |
| 2289 | ngx_ext_rename_file_t ext; |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2290 | |
Igor Sysoev | 0123ee3 | 2007-12-26 13:24:57 +0000 | [diff] [blame] | 2291 | tf = u->pipe->temp_file; |
| 2292 | |
| 2293 | if (tf->file.fd == NGX_INVALID_FILE) { |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2294 | |
| 2295 | /* create file for empty 200 response */ |
| 2296 | |
| 2297 | tf = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)); |
| 2298 | if (tf == NULL) { |
| 2299 | return; |
| 2300 | } |
| 2301 | |
| 2302 | tf->file.fd = NGX_INVALID_FILE; |
| 2303 | tf->file.log = r->connection->log; |
| 2304 | tf->path = u->conf->temp_path; |
| 2305 | tf->pool = r->pool; |
| 2306 | tf->persistent = 1; |
| 2307 | |
| 2308 | if (ngx_create_temp_file(&tf->file, tf->path, tf->pool, |
| 2309 | tf->persistent, tf->clean, tf->access) |
| 2310 | != NGX_OK) |
| 2311 | { |
| 2312 | return; |
| 2313 | } |
| 2314 | |
| 2315 | u->pipe->temp_file = tf; |
| 2316 | } |
| 2317 | |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2318 | ext.access = u->conf->store_access; |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 2319 | ext.path_access = u->conf->store_access; |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2320 | ext.time = -1; |
| 2321 | ext.create_path = 1; |
| 2322 | ext.delete_file = 1; |
Igor Sysoev | 524f54f | 2008-12-10 14:53:45 +0000 | [diff] [blame] | 2323 | ext.log_rename_error = 1; |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2324 | ext.log = r->connection->log; |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2325 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 2326 | if (u->headers_in.last_modified) { |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2327 | |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2328 | lm = ngx_http_parse_time(u->headers_in.last_modified->value.data, |
| 2329 | u->headers_in.last_modified->value.len); |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2330 | |
| 2331 | if (lm != NGX_ERROR) { |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2332 | ext.time = lm; |
| 2333 | ext.fd = tf->file.fd; |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
Igor Sysoev | fbd9b43 | 2007-07-13 08:30:34 +0000 | [diff] [blame] | 2337 | if (u->conf->store_lengths == NULL) { |
| 2338 | |
| 2339 | ngx_http_map_uri_to_path(r, &path, &root, 0); |
| 2340 | |
| 2341 | } else { |
| 2342 | if (ngx_http_script_run(r, &path, u->conf->store_lengths->elts, 0, |
| 2343 | u->conf->store_values->elts) |
| 2344 | == NULL) |
| 2345 | { |
| 2346 | return; |
| 2347 | } |
| 2348 | } |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2349 | |
| 2350 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2351 | "upstream stores \"%s\" to \"%s\"", |
| 2352 | tf->file.name.data, path.data); |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2353 | |
Igor Sysoev | 9ff26ce | 2008-03-17 13:07:35 +0000 | [diff] [blame] | 2354 | (void) ngx_ext_rename_file(&tf->file.name, &path, &ext); |
Igor Sysoev | 58feb53 | 2007-07-12 11:19:05 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | |
| 2358 | static void |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2359 | ngx_http_upstream_dummy_handler(ngx_http_request_t *r, ngx_http_upstream_t *u) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2360 | { |
Igor Sysoev | b9409a8 | 2008-12-09 17:25:03 +0000 | [diff] [blame] | 2361 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2362 | "http upstream dummy handler"); |
| 2363 | } |
| 2364 | |
| 2365 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 2366 | static void |
| 2367 | ngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u, |
| 2368 | ngx_uint_t ft_type) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2369 | { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2370 | ngx_uint_t status, state; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2371 | |
| 2372 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2373 | "http next upstream, %xi", ft_type); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2374 | |
| 2375 | #if 0 |
| 2376 | ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock); |
| 2377 | #endif |
| 2378 | |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2379 | if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2380 | state = NGX_PEER_NEXT; |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2381 | } else { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2382 | state = NGX_PEER_FAILED; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2383 | } |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2384 | |
Igor Sysoev | 6876bcd | 2007-08-09 13:54:33 +0000 | [diff] [blame] | 2385 | if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) { |
| 2386 | u->peer.free(&u->peer, u->peer.data, state); |
| 2387 | } |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2388 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2389 | if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) { |
| 2390 | ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT, |
| 2391 | "upstream timed out"); |
| 2392 | } |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2393 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2394 | if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) { |
| 2395 | status = 0; |
| 2396 | |
| 2397 | } else { |
| 2398 | switch(ft_type) { |
| 2399 | |
| 2400 | case NGX_HTTP_UPSTREAM_FT_TIMEOUT: |
| 2401 | status = NGX_HTTP_GATEWAY_TIME_OUT; |
| 2402 | break; |
| 2403 | |
| 2404 | case NGX_HTTP_UPSTREAM_FT_HTTP_500: |
| 2405 | status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 2406 | break; |
| 2407 | |
| 2408 | case NGX_HTTP_UPSTREAM_FT_HTTP_404: |
| 2409 | status = NGX_HTTP_NOT_FOUND; |
| 2410 | break; |
| 2411 | |
| 2412 | /* |
| 2413 | * NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING |
| 2414 | * never reach here |
| 2415 | */ |
| 2416 | |
| 2417 | default: |
| 2418 | status = NGX_HTTP_BAD_GATEWAY; |
| 2419 | } |
| 2420 | } |
| 2421 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2422 | if (r->connection->error) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2423 | ngx_http_upstream_finalize_request(r, u, |
| 2424 | NGX_HTTP_CLIENT_CLOSED_REQUEST); |
| 2425 | return; |
| 2426 | } |
| 2427 | |
| 2428 | if (status) { |
| 2429 | u->state->status = status; |
| 2430 | |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2431 | if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2432 | |
| 2433 | #if (NGX_HTTP_CACHE) |
| 2434 | |
| 2435 | if (u->stale && (u->conf->use_stale & ft_type)) { |
| 2436 | ngx_http_upstream_finalize_request(r, u, |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2437 | ngx_http_send_cached_response(r)); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2438 | return; |
| 2439 | } |
| 2440 | |
| 2441 | #endif |
| 2442 | |
| 2443 | ngx_http_upstream_finalize_request(r, u, status); |
| 2444 | return; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | if (u->peer.connection) { |
| 2449 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2450 | "close http upstream connection: %d", |
| 2451 | u->peer.connection->fd); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2452 | #if (NGX_HTTP_SSL) |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2453 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2454 | if (u->peer.connection->ssl) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2455 | u->peer.connection->ssl->no_wait_shutdown = 1; |
| 2456 | u->peer.connection->ssl->no_send_shutdown = 1; |
| 2457 | |
| 2458 | (void) ngx_ssl_shutdown(u->peer.connection); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2459 | } |
| 2460 | #endif |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2461 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2462 | ngx_close_connection(u->peer.connection); |
| 2463 | } |
| 2464 | |
| 2465 | #if 0 |
| 2466 | if (u->conf->busy_lock && !u->busy_locked) { |
| 2467 | ngx_http_upstream_busy_lock(p); |
| 2468 | return; |
| 2469 | } |
| 2470 | #endif |
| 2471 | |
| 2472 | ngx_http_upstream_connect(r, u); |
| 2473 | } |
| 2474 | |
| 2475 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 2476 | static void |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2477 | ngx_http_upstream_cleanup(void *data) |
| 2478 | { |
| 2479 | ngx_http_request_t *r = data; |
| 2480 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 2481 | ngx_http_upstream_t *u; |
| 2482 | |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2483 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2484 | "cleanup http upstream request: \"%V\"", &r->uri); |
| 2485 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 2486 | u = r->upstream; |
| 2487 | |
| 2488 | if (u->resolved && u->resolved->ctx) { |
| 2489 | ngx_resolve_name_done(u->resolved->ctx); |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Igor Sysoev | f2932dc | 2007-12-10 12:29:43 +0000 | [diff] [blame] | 2492 | ngx_http_upstream_finalize_request(r, u, NGX_DONE); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | static void |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 2497 | ngx_http_upstream_finalize_request(ngx_http_request_t *r, |
| 2498 | ngx_http_upstream_t *u, ngx_int_t rc) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2499 | { |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 2500 | ngx_time_t *tp; |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 2501 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2502 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2503 | "finalize http upstream request: %i", rc); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2504 | |
Igor Sysoev | cf6c582 | 2008-12-17 16:07:58 +0000 | [diff] [blame] | 2505 | if (u->cleanup) { |
| 2506 | *u->cleanup = NULL; |
| 2507 | } |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2508 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 2509 | if (u->state && u->state->response_sec) { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 2510 | tp = ngx_timeofday(); |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 2511 | u->state->response_sec = tp->sec - u->state->response_sec; |
| 2512 | u->state->response_msec = tp->msec - u->state->response_msec; |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 2513 | |
| 2514 | if (u->pipe) { |
| 2515 | u->state->response_length = u->pipe->read_length; |
| 2516 | } |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2519 | u->finalize_request(r, rc); |
| 2520 | |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 2521 | if (u->peer.free) { |
| 2522 | u->peer.free(&u->peer, u->peer.data, 0); |
| 2523 | } |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2524 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2525 | if (u->peer.connection) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2526 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2527 | #if (NGX_HTTP_SSL) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2528 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2529 | /* TODO: do not shutdown persistent connection */ |
| 2530 | |
| 2531 | if (u->peer.connection->ssl) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2532 | |
| 2533 | /* |
| 2534 | * We send the "close notify" shutdown alert to the upstream only |
| 2535 | * and do not wait its "close notify" shutdown alert. |
| 2536 | * It is acceptable according to the TLS standard. |
| 2537 | */ |
| 2538 | |
| 2539 | u->peer.connection->ssl->no_wait_shutdown = 1; |
| 2540 | |
| 2541 | (void) ngx_ssl_shutdown(u->peer.connection); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2542 | } |
| 2543 | #endif |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2544 | |
| 2545 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2546 | "close http upstream connection: %d", |
| 2547 | u->peer.connection->fd); |
| 2548 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2549 | ngx_close_connection(u->peer.connection); |
| 2550 | } |
| 2551 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2552 | u->peer.connection = NULL; |
| 2553 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 2554 | if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2555 | { |
| 2556 | rc = 0; |
| 2557 | } |
| 2558 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2559 | if (u->pipe && u->pipe->temp_file) { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2560 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2561 | "http upstream temp fd: %d", |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2562 | u->pipe->temp_file->file.fd); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | #if 0 |
| 2566 | if (u->cache) { |
| 2567 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2568 | "http upstream cache fd: %d", |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2569 | u->cache->ctx.file.fd); |
| 2570 | } |
| 2571 | #endif |
| 2572 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 2573 | if (rc == NGX_DECLINED) { |
| 2574 | return; |
| 2575 | } |
| 2576 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2577 | r->connection->log->action = "sending to client"; |
| 2578 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2579 | if (rc == 0 && r == r->main && !r->post_action) { |
| 2580 | rc = ngx_http_send_special(r, NGX_HTTP_LAST); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | ngx_http_finalize_request(r, rc); |
| 2584 | } |
| 2585 | |
| 2586 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2587 | static ngx_int_t |
| 2588 | ngx_http_upstream_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2589 | ngx_uint_t offset) |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2590 | { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2591 | ngx_table_elt_t **ph; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2592 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2593 | ph = (ngx_table_elt_t **) ((char *) &r->upstream->headers_in + offset); |
| 2594 | |
| 2595 | if (*ph == NULL) { |
| 2596 | *ph = h; |
| 2597 | } |
| 2598 | |
| 2599 | return NGX_OK; |
| 2600 | } |
| 2601 | |
| 2602 | |
| 2603 | static ngx_int_t |
| 2604 | ngx_http_upstream_process_multi_header_lines(ngx_http_request_t *r, |
| 2605 | ngx_table_elt_t *h, ngx_uint_t offset) |
| 2606 | { |
| 2607 | ngx_array_t *pa; |
| 2608 | ngx_table_elt_t **ph; |
| 2609 | |
| 2610 | pa = (ngx_array_t *) ((char *) &r->upstream->headers_in + offset); |
| 2611 | |
| 2612 | if (pa->elts == NULL) { |
| 2613 | if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) |
| 2614 | { |
| 2615 | return NGX_ERROR; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | ph = ngx_array_push(pa); |
| 2620 | if (ph == NULL) { |
| 2621 | return NGX_ERROR; |
| 2622 | } |
| 2623 | |
| 2624 | *ph = h; |
| 2625 | |
| 2626 | return NGX_OK; |
| 2627 | } |
| 2628 | |
| 2629 | |
| 2630 | static ngx_int_t |
| 2631 | ngx_http_upstream_ignore_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2632 | ngx_uint_t offset) |
| 2633 | { |
| 2634 | return NGX_OK; |
| 2635 | } |
| 2636 | |
| 2637 | |
| 2638 | static ngx_int_t |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2639 | ngx_http_upstream_process_limit_rate(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2640 | ngx_uint_t offset) |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2641 | { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 2642 | ngx_int_t n; |
| 2643 | |
| 2644 | r->upstream->headers_in.x_accel_limit_rate = h; |
| 2645 | |
| 2646 | n = ngx_atoi(h->value.data, h->value.len); |
| 2647 | |
| 2648 | if (n != NGX_ERROR) { |
| 2649 | r->limit_rate = (size_t) n; |
| 2650 | } |
| 2651 | |
| 2652 | return NGX_OK; |
| 2653 | } |
| 2654 | |
| 2655 | |
| 2656 | static ngx_int_t |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 2657 | ngx_http_upstream_process_buffering(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2658 | ngx_uint_t offset) |
| 2659 | { |
| 2660 | u_char c0, c1, c2; |
| 2661 | |
| 2662 | if (r->upstream->conf->change_buffering) { |
| 2663 | |
| 2664 | if (h->value.len == 2) { |
| 2665 | c0 = ngx_tolower(h->value.data[0]); |
| 2666 | c1 = ngx_tolower(h->value.data[1]); |
| 2667 | |
| 2668 | if (c0 == 'n' && c1 == 'o') { |
| 2669 | r->upstream->buffering = 0; |
| 2670 | } |
| 2671 | |
| 2672 | } else if (h->value.len == 3) { |
| 2673 | c0 = ngx_tolower(h->value.data[0]); |
| 2674 | c1 = ngx_tolower(h->value.data[1]); |
| 2675 | c2 = ngx_tolower(h->value.data[2]); |
| 2676 | |
| 2677 | if (c0 == 'y' && c1 == 'e' && c2 == 's') { |
| 2678 | r->upstream->buffering = 1; |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | return NGX_OK; |
| 2684 | } |
| 2685 | |
| 2686 | |
| 2687 | static ngx_int_t |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2688 | ngx_http_upstream_process_charset(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2689 | ngx_uint_t offset) |
| 2690 | { |
| 2691 | r->headers_out.override_charset = &h->value; |
| 2692 | |
| 2693 | return NGX_OK; |
| 2694 | } |
| 2695 | |
| 2696 | |
| 2697 | static ngx_int_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2698 | ngx_http_upstream_copy_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2699 | ngx_uint_t offset) |
| 2700 | { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2701 | ngx_table_elt_t *ho, **ph; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2702 | |
| 2703 | ho = ngx_list_push(&r->headers_out.headers); |
| 2704 | if (ho == NULL) { |
| 2705 | return NGX_ERROR; |
| 2706 | } |
| 2707 | |
| 2708 | *ho = *h; |
| 2709 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2710 | if (offset) { |
| 2711 | ph = (ngx_table_elt_t **) ((char *) &r->headers_out + offset); |
| 2712 | *ph = ho; |
| 2713 | } |
| 2714 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2715 | return NGX_OK; |
| 2716 | } |
| 2717 | |
| 2718 | |
| 2719 | static ngx_int_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2720 | ngx_http_upstream_copy_multi_header_lines(ngx_http_request_t *r, |
| 2721 | ngx_table_elt_t *h, ngx_uint_t offset) |
| 2722 | { |
| 2723 | ngx_array_t *pa; |
| 2724 | ngx_table_elt_t *ho, **ph; |
| 2725 | |
| 2726 | pa = (ngx_array_t *) ((char *) &r->headers_out + offset); |
| 2727 | |
| 2728 | if (pa->elts == NULL) { |
| 2729 | if (ngx_array_init(pa, r->pool, 2, sizeof(ngx_table_elt_t *)) != NGX_OK) |
| 2730 | { |
| 2731 | return NGX_ERROR; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | ph = ngx_array_push(pa); |
| 2736 | if (ph == NULL) { |
| 2737 | return NGX_ERROR; |
| 2738 | } |
| 2739 | |
| 2740 | ho = ngx_list_push(&r->headers_out.headers); |
| 2741 | if (ho == NULL) { |
| 2742 | return NGX_ERROR; |
| 2743 | } |
| 2744 | |
| 2745 | *ho = *h; |
| 2746 | *ph = ho; |
| 2747 | |
| 2748 | return NGX_OK; |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | static ngx_int_t |
| 2753 | ngx_http_upstream_copy_content_type(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2754 | ngx_uint_t offset) |
| 2755 | { |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2756 | u_char *p, *last; |
| 2757 | |
| 2758 | r->headers_out.content_type_len = h->value.len; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2759 | r->headers_out.content_type = h->value; |
| 2760 | |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2761 | for (p = h->value.data; *p; p++) { |
| 2762 | |
| 2763 | if (*p != ';') { |
| 2764 | continue; |
| 2765 | } |
| 2766 | |
| 2767 | last = p; |
| 2768 | |
| 2769 | while (*++p == ' ') { /* void */ } |
| 2770 | |
Igor Sysoev | 523e09e | 2008-04-11 09:15:07 +0000 | [diff] [blame] | 2771 | if (*p == '\0') { |
| 2772 | return NGX_OK; |
| 2773 | } |
| 2774 | |
Igor Sysoev | 722231f | 2007-02-14 18:51:19 +0000 | [diff] [blame] | 2775 | if (ngx_strncasecmp(p, (u_char *) "charset=", 8) != 0) { |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2776 | continue; |
| 2777 | } |
| 2778 | |
| 2779 | p += 8; |
| 2780 | |
| 2781 | r->headers_out.content_type_len = last - h->value.data; |
| 2782 | |
Igor Sysoev | 3165425 | 2008-09-09 11:58:45 +0000 | [diff] [blame] | 2783 | if (*p == '"') { |
| 2784 | p++; |
| 2785 | } |
| 2786 | |
| 2787 | last = h->value.data + h->value.len; |
| 2788 | |
| 2789 | if (*(last - 1) == '"') { |
| 2790 | last--; |
| 2791 | } |
| 2792 | |
| 2793 | r->headers_out.charset.len = last - p; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2794 | r->headers_out.charset.data = p; |
Igor Sysoev | 77d7f80 | 2007-03-23 11:33:47 +0000 | [diff] [blame] | 2795 | |
| 2796 | return NGX_OK; |
Igor Sysoev | ef809b8 | 2006-06-28 16:00:26 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2799 | return NGX_OK; |
| 2800 | } |
| 2801 | |
| 2802 | |
| 2803 | static ngx_int_t |
| 2804 | ngx_http_upstream_copy_content_length(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2805 | ngx_uint_t offset) |
| 2806 | { |
| 2807 | ngx_table_elt_t *ho; |
| 2808 | |
| 2809 | ho = ngx_list_push(&r->headers_out.headers); |
| 2810 | if (ho == NULL) { |
| 2811 | return NGX_ERROR; |
| 2812 | } |
| 2813 | |
| 2814 | *ho = *h; |
| 2815 | |
| 2816 | r->headers_out.content_length = ho; |
| 2817 | r->headers_out.content_length_n = ngx_atoof(h->value.data, h->value.len); |
| 2818 | |
| 2819 | return NGX_OK; |
| 2820 | } |
| 2821 | |
| 2822 | |
| 2823 | static ngx_int_t |
| 2824 | ngx_http_upstream_rewrite_location(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2825 | ngx_uint_t offset) |
| 2826 | { |
| 2827 | ngx_int_t rc; |
| 2828 | ngx_table_elt_t *ho; |
| 2829 | |
| 2830 | ho = ngx_list_push(&r->headers_out.headers); |
| 2831 | if (ho == NULL) { |
| 2832 | return NGX_ERROR; |
| 2833 | } |
| 2834 | |
| 2835 | *ho = *h; |
| 2836 | |
| 2837 | if (r->upstream->rewrite_redirect) { |
| 2838 | rc = r->upstream->rewrite_redirect(r, ho, 0); |
| 2839 | |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 2840 | if (rc == NGX_DECLINED) { |
| 2841 | return NGX_OK; |
| 2842 | } |
| 2843 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2844 | if (rc == NGX_OK) { |
| 2845 | r->headers_out.location = ho; |
| 2846 | |
| 2847 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2848 | "rewritten location: \"%V\"", &ho->value); |
| 2849 | } |
| 2850 | |
| 2851 | return rc; |
| 2852 | } |
| 2853 | |
Igor Sysoev | 685fd08 | 2007-11-27 11:52:37 +0000 | [diff] [blame] | 2854 | if (ho->value.data[0] != '/') { |
| 2855 | r->headers_out.location = ho; |
| 2856 | } |
| 2857 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2858 | /* |
| 2859 | * we do not set r->headers_out.location here to avoid the handling |
| 2860 | * the local redirects without a host name by ngx_http_header_filter() |
| 2861 | */ |
| 2862 | |
| 2863 | return NGX_OK; |
| 2864 | } |
| 2865 | |
| 2866 | |
| 2867 | static ngx_int_t |
| 2868 | ngx_http_upstream_rewrite_refresh(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 2869 | ngx_uint_t offset) |
| 2870 | { |
| 2871 | u_char *p; |
| 2872 | ngx_int_t rc; |
| 2873 | ngx_table_elt_t *ho; |
| 2874 | |
| 2875 | ho = ngx_list_push(&r->headers_out.headers); |
| 2876 | if (ho == NULL) { |
| 2877 | return NGX_ERROR; |
| 2878 | } |
| 2879 | |
| 2880 | *ho = *h; |
| 2881 | |
| 2882 | if (r->upstream->rewrite_redirect) { |
| 2883 | |
Igor Sysoev | f532941 | 2007-10-01 12:57:47 +0000 | [diff] [blame] | 2884 | p = ngx_strcasestrn(ho->value.data, "url=", 4 - 1); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2885 | |
| 2886 | if (p) { |
| 2887 | rc = r->upstream->rewrite_redirect(r, ho, p + 4 - ho->value.data); |
| 2888 | |
| 2889 | } else { |
| 2890 | return NGX_OK; |
| 2891 | } |
| 2892 | |
Igor Sysoev | 187b7d9 | 2005-07-14 12:51:53 +0000 | [diff] [blame] | 2893 | if (rc == NGX_DECLINED) { |
| 2894 | return NGX_OK; |
| 2895 | } |
| 2896 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2897 | if (rc == NGX_OK) { |
Igor Sysoev | 32717eb | 2007-11-27 11:56:18 +0000 | [diff] [blame] | 2898 | r->headers_out.refresh = ho; |
| 2899 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2900 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2901 | "rewritten refresh: \"%V\"", &ho->value); |
| 2902 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2903 | |
| 2904 | return rc; |
| 2905 | } |
| 2906 | |
Igor Sysoev | 32717eb | 2007-11-27 11:56:18 +0000 | [diff] [blame] | 2907 | r->headers_out.refresh = ho; |
| 2908 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2909 | return NGX_OK; |
| 2910 | } |
| 2911 | |
| 2912 | |
| 2913 | #if (NGX_HTTP_GZIP) |
| 2914 | |
| 2915 | static ngx_int_t |
| 2916 | ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r, |
| 2917 | ngx_table_elt_t *h, ngx_uint_t offset) |
| 2918 | { |
| 2919 | ngx_table_elt_t *ho; |
| 2920 | |
| 2921 | ho = ngx_list_push(&r->headers_out.headers); |
| 2922 | if (ho == NULL) { |
| 2923 | return NGX_ERROR; |
| 2924 | } |
| 2925 | |
| 2926 | *ho = *h; |
| 2927 | |
| 2928 | r->headers_out.content_encoding = ho; |
| 2929 | |
| 2930 | return NGX_OK; |
| 2931 | } |
| 2932 | |
| 2933 | #endif |
| 2934 | |
| 2935 | |
Igor Sysoev | aa82861 | 2005-02-09 14:31:07 +0000 | [diff] [blame] | 2936 | static ngx_int_t |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2937 | ngx_http_upstream_add_variables(ngx_conf_t *cf) |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2938 | { |
Igor Sysoev | a8c54c0 | 2006-11-27 14:46:15 +0000 | [diff] [blame] | 2939 | ngx_http_variable_t *var, *v; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2940 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2941 | for (v = ngx_http_upstream_vars; v->name.len; v++) { |
| 2942 | var = ngx_http_add_variable(cf, &v->name, v->flags); |
| 2943 | if (var == NULL) { |
| 2944 | return NGX_ERROR; |
| 2945 | } |
| 2946 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 2947 | var->get_handler = v->get_handler; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2948 | var->data = v->data; |
| 2949 | } |
| 2950 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2951 | return NGX_OK; |
| 2952 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2953 | |
| 2954 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 2955 | static ngx_int_t |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 2956 | ngx_http_upstream_addr_variable(ngx_http_request_t *r, |
| 2957 | ngx_http_variable_value_t *v, uintptr_t data) |
| 2958 | { |
| 2959 | u_char *p; |
| 2960 | size_t len; |
| 2961 | ngx_uint_t i; |
| 2962 | ngx_http_upstream_state_t *state; |
| 2963 | |
| 2964 | v->valid = 1; |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 2965 | v->no_cacheable = 0; |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 2966 | v->not_found = 0; |
| 2967 | |
| 2968 | if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
| 2969 | v->not_found = 1; |
| 2970 | return NGX_OK; |
| 2971 | } |
| 2972 | |
| 2973 | len = 0; |
| 2974 | state = r->upstream_states->elts; |
| 2975 | |
| 2976 | for (i = 0; i < r->upstream_states->nelts; i++) { |
| 2977 | if (state[i].peer) { |
| 2978 | len += state[i].peer->len + 2; |
| 2979 | |
| 2980 | } else { |
| 2981 | len += 3; |
| 2982 | } |
| 2983 | } |
| 2984 | |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 2985 | p = ngx_pnalloc(r->pool, len); |
Igor Sysoev | 29f5912 | 2007-04-23 16:56:17 +0000 | [diff] [blame] | 2986 | if (p == NULL) { |
| 2987 | return NGX_ERROR; |
| 2988 | } |
| 2989 | |
| 2990 | v->data = p; |
| 2991 | |
| 2992 | i = 0; |
| 2993 | |
| 2994 | for ( ;; ) { |
| 2995 | if (state[i].peer) { |
| 2996 | p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len); |
| 2997 | } |
| 2998 | |
| 2999 | if (++i == r->upstream_states->nelts) { |
| 3000 | break; |
| 3001 | } |
| 3002 | |
| 3003 | if (state[i].peer) { |
| 3004 | *p++ = ','; |
| 3005 | *p++ = ' '; |
| 3006 | |
| 3007 | } else { |
| 3008 | *p++ = ' '; |
| 3009 | *p++ = ':'; |
| 3010 | *p++ = ' '; |
| 3011 | |
| 3012 | if (++i == r->upstream_states->nelts) { |
| 3013 | break; |
| 3014 | } |
| 3015 | |
| 3016 | continue; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | v->len = p - v->data; |
| 3021 | |
| 3022 | return NGX_OK; |
| 3023 | } |
| 3024 | |
| 3025 | |
| 3026 | static ngx_int_t |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3027 | ngx_http_upstream_status_variable(ngx_http_request_t *r, |
| 3028 | ngx_http_variable_value_t *v, uintptr_t data) |
| 3029 | { |
| 3030 | u_char *p; |
| 3031 | size_t len; |
| 3032 | ngx_uint_t i; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3033 | ngx_http_upstream_state_t *state; |
| 3034 | |
| 3035 | v->valid = 1; |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 3036 | v->no_cacheable = 0; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3037 | v->not_found = 0; |
| 3038 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3039 | if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3040 | v->not_found = 1; |
| 3041 | return NGX_OK; |
| 3042 | } |
| 3043 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3044 | len = r->upstream_states->nelts * (3 + 2); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3045 | |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 3046 | p = ngx_pnalloc(r->pool, len); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3047 | if (p == NULL) { |
| 3048 | return NGX_ERROR; |
| 3049 | } |
| 3050 | |
| 3051 | v->data = p; |
| 3052 | |
| 3053 | i = 0; |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3054 | state = r->upstream_states->elts; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3055 | |
| 3056 | for ( ;; ) { |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3057 | if (state[i].status) { |
| 3058 | p = ngx_sprintf(p, "%ui", state[i].status); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3059 | |
| 3060 | } else { |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3061 | *p++ = '-'; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3064 | if (++i == r->upstream_states->nelts) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3065 | break; |
| 3066 | } |
| 3067 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3068 | if (state[i].peer) { |
| 3069 | *p++ = ','; |
| 3070 | *p++ = ' '; |
| 3071 | |
| 3072 | } else { |
| 3073 | *p++ = ' '; |
| 3074 | *p++ = ':'; |
| 3075 | *p++ = ' '; |
| 3076 | |
| 3077 | if (++i == r->upstream_states->nelts) { |
| 3078 | break; |
| 3079 | } |
| 3080 | |
| 3081 | continue; |
| 3082 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | v->len = p - v->data; |
| 3086 | |
| 3087 | return NGX_OK; |
| 3088 | } |
| 3089 | |
| 3090 | |
| 3091 | static ngx_int_t |
| 3092 | ngx_http_upstream_response_time_variable(ngx_http_request_t *r, |
| 3093 | ngx_http_variable_value_t *v, uintptr_t data) |
| 3094 | { |
| 3095 | u_char *p; |
| 3096 | size_t len; |
| 3097 | ngx_uint_t i; |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 3098 | ngx_msec_int_t ms; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3099 | ngx_http_upstream_state_t *state; |
| 3100 | |
| 3101 | v->valid = 1; |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 3102 | v->no_cacheable = 0; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3103 | v->not_found = 0; |
| 3104 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3105 | if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3106 | v->not_found = 1; |
| 3107 | return NGX_OK; |
| 3108 | } |
| 3109 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3110 | len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3111 | |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 3112 | p = ngx_pnalloc(r->pool, len); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3113 | if (p == NULL) { |
| 3114 | return NGX_ERROR; |
| 3115 | } |
| 3116 | |
| 3117 | v->data = p; |
| 3118 | |
| 3119 | i = 0; |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3120 | state = r->upstream_states->elts; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3121 | |
| 3122 | for ( ;; ) { |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3123 | if (state[i].status) { |
Igor Sysoev | 1d04b14 | 2007-11-15 14:26:36 +0000 | [diff] [blame] | 3124 | ms = (ngx_msec_int_t) |
| 3125 | (state[i].response_sec * 1000 + state[i].response_msec); |
Igor Sysoev | 8f98581 | 2006-12-06 11:33:18 +0000 | [diff] [blame] | 3126 | ms = (ms >= 0) ? ms : 0; |
| 3127 | p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000); |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3128 | |
| 3129 | } else { |
| 3130 | *p++ = '-'; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3133 | if (++i == r->upstream_states->nelts) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3134 | break; |
| 3135 | } |
| 3136 | |
Igor Sysoev | e0f8662 | 2007-04-18 20:23:19 +0000 | [diff] [blame] | 3137 | if (state[i].peer) { |
| 3138 | *p++ = ','; |
| 3139 | *p++ = ' '; |
| 3140 | |
| 3141 | } else { |
| 3142 | *p++ = ' '; |
| 3143 | *p++ = ':'; |
| 3144 | *p++ = ' '; |
| 3145 | |
| 3146 | if (++i == r->upstream_states->nelts) { |
| 3147 | break; |
| 3148 | } |
| 3149 | |
| 3150 | continue; |
| 3151 | } |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | v->len = p - v->data; |
| 3155 | |
| 3156 | return NGX_OK; |
| 3157 | } |
| 3158 | |
| 3159 | |
Igor Sysoev | 371766c | 2008-12-11 15:30:52 +0000 | [diff] [blame] | 3160 | static ngx_int_t |
| 3161 | ngx_http_upstream_response_length_variable(ngx_http_request_t *r, |
| 3162 | ngx_http_variable_value_t *v, uintptr_t data) |
| 3163 | { |
| 3164 | u_char *p; |
| 3165 | size_t len; |
| 3166 | ngx_uint_t i; |
| 3167 | ngx_http_upstream_state_t *state; |
| 3168 | |
| 3169 | v->valid = 1; |
| 3170 | v->no_cacheable = 0; |
| 3171 | v->not_found = 0; |
| 3172 | |
| 3173 | if (r->upstream_states == NULL || r->upstream_states->nelts == 0) { |
| 3174 | v->not_found = 1; |
| 3175 | return NGX_OK; |
| 3176 | } |
| 3177 | |
| 3178 | len = r->upstream_states->nelts * (NGX_OFF_T_LEN + 2); |
| 3179 | |
| 3180 | p = ngx_pnalloc(r->pool, len); |
| 3181 | if (p == NULL) { |
| 3182 | return NGX_ERROR; |
| 3183 | } |
| 3184 | |
| 3185 | v->data = p; |
| 3186 | |
| 3187 | i = 0; |
| 3188 | state = r->upstream_states->elts; |
| 3189 | |
| 3190 | for ( ;; ) { |
| 3191 | p = ngx_sprintf(p, "%O", state[i].response_length); |
| 3192 | |
| 3193 | if (++i == r->upstream_states->nelts) { |
| 3194 | break; |
| 3195 | } |
| 3196 | |
| 3197 | if (state[i].peer) { |
| 3198 | *p++ = ','; |
| 3199 | *p++ = ' '; |
| 3200 | |
| 3201 | } else { |
| 3202 | *p++ = ' '; |
| 3203 | *p++ = ':'; |
| 3204 | *p++ = ' '; |
| 3205 | |
| 3206 | if (++i == r->upstream_states->nelts) { |
| 3207 | break; |
| 3208 | } |
| 3209 | |
| 3210 | continue; |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | v->len = p - v->data; |
| 3215 | |
| 3216 | return NGX_OK; |
| 3217 | } |
| 3218 | |
| 3219 | |
Igor Sysoev | 2723361 | 2007-04-10 07:08:06 +0000 | [diff] [blame] | 3220 | ngx_int_t |
| 3221 | ngx_http_upstream_header_variable(ngx_http_request_t *r, |
| 3222 | ngx_http_variable_value_t *v, uintptr_t data) |
| 3223 | { |
| 3224 | if (r->upstream == NULL) { |
| 3225 | v->not_found = 1; |
| 3226 | return NGX_OK; |
| 3227 | } |
| 3228 | |
| 3229 | return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, |
| 3230 | &r->upstream->headers_in.headers.part, |
| 3231 | sizeof("upstream_http_") - 1); |
| 3232 | } |
| 3233 | |
| 3234 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3235 | static char * |
| 3236 | ngx_http_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) |
| 3237 | { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3238 | char *rv; |
| 3239 | void *mconf; |
| 3240 | ngx_str_t *value; |
| 3241 | ngx_url_t u; |
| 3242 | ngx_uint_t m; |
| 3243 | ngx_conf_t pcf; |
| 3244 | ngx_http_module_t *module; |
| 3245 | ngx_http_conf_ctx_t *ctx, *http_ctx; |
| 3246 | ngx_http_upstream_srv_conf_t *uscf; |
| 3247 | |
| 3248 | ngx_memzero(&u, sizeof(ngx_url_t)); |
| 3249 | |
| 3250 | value = cf->args->elts; |
| 3251 | u.host = value[1]; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3252 | u.no_resolve = 1; |
| 3253 | |
| 3254 | uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE |
| 3255 | |NGX_HTTP_UPSTREAM_WEIGHT |
| 3256 | |NGX_HTTP_UPSTREAM_MAX_FAILS |
| 3257 | |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT |
| 3258 | |NGX_HTTP_UPSTREAM_DOWN |
| 3259 | |NGX_HTTP_UPSTREAM_BACKUP); |
| 3260 | if (uscf == NULL) { |
| 3261 | return NGX_CONF_ERROR; |
| 3262 | } |
| 3263 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3264 | |
| 3265 | ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
| 3266 | if (ctx == NULL) { |
| 3267 | return NGX_CONF_ERROR; |
| 3268 | } |
| 3269 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3270 | http_ctx = cf->ctx; |
| 3271 | ctx->main_conf = http_ctx->main_conf; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3272 | |
| 3273 | /* the upstream{}'s srv_conf */ |
| 3274 | |
| 3275 | ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); |
| 3276 | if (ctx->srv_conf == NULL) { |
| 3277 | return NGX_CONF_ERROR; |
| 3278 | } |
| 3279 | |
| 3280 | ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf; |
| 3281 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3282 | uscf->srv_conf = ctx->srv_conf; |
| 3283 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3284 | |
| 3285 | /* the upstream{}'s loc_conf */ |
| 3286 | |
| 3287 | ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); |
| 3288 | if (ctx->loc_conf == NULL) { |
| 3289 | return NGX_CONF_ERROR; |
| 3290 | } |
| 3291 | |
| 3292 | for (m = 0; ngx_modules[m]; m++) { |
| 3293 | if (ngx_modules[m]->type != NGX_HTTP_MODULE) { |
| 3294 | continue; |
| 3295 | } |
| 3296 | |
| 3297 | module = ngx_modules[m]->ctx; |
| 3298 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3299 | if (module->create_srv_conf) { |
| 3300 | mconf = module->create_srv_conf(cf); |
| 3301 | if (mconf == NULL) { |
| 3302 | return NGX_CONF_ERROR; |
| 3303 | } |
| 3304 | |
| 3305 | ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf; |
| 3306 | } |
| 3307 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3308 | if (module->create_loc_conf) { |
| 3309 | mconf = module->create_loc_conf(cf); |
| 3310 | if (mconf == NULL) { |
| 3311 | return NGX_CONF_ERROR; |
| 3312 | } |
| 3313 | |
| 3314 | ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | |
| 3319 | /* parse inside upstream{} */ |
| 3320 | |
| 3321 | pcf = *cf; |
| 3322 | cf->ctx = ctx; |
| 3323 | cf->cmd_type = NGX_HTTP_UPS_CONF; |
| 3324 | |
| 3325 | rv = ngx_conf_parse(cf, NULL); |
| 3326 | |
| 3327 | *cf = pcf; |
| 3328 | |
| 3329 | if (rv != NGX_CONF_OK) { |
| 3330 | return rv; |
| 3331 | } |
| 3332 | |
| 3333 | if (uscf->servers == NULL) { |
| 3334 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 3335 | "no servers are inside upstream"); |
| 3336 | return NGX_CONF_ERROR; |
| 3337 | } |
| 3338 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3339 | return rv; |
| 3340 | } |
| 3341 | |
| 3342 | |
| 3343 | static char * |
| 3344 | ngx_http_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 3345 | { |
| 3346 | ngx_http_upstream_srv_conf_t *uscf = conf; |
| 3347 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3348 | time_t fail_timeout; |
| 3349 | ngx_str_t *value, s; |
| 3350 | ngx_url_t u; |
| 3351 | ngx_int_t weight, max_fails; |
| 3352 | ngx_uint_t i; |
| 3353 | ngx_http_upstream_server_t *us; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3354 | |
| 3355 | if (uscf->servers == NULL) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3356 | uscf->servers = ngx_array_create(cf->pool, 4, |
| 3357 | sizeof(ngx_http_upstream_server_t)); |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3358 | if (uscf->servers == NULL) { |
| 3359 | return NGX_CONF_ERROR; |
| 3360 | } |
| 3361 | } |
| 3362 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3363 | us = ngx_array_push(uscf->servers); |
| 3364 | if (us == NULL) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3365 | return NGX_CONF_ERROR; |
| 3366 | } |
| 3367 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3368 | ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); |
| 3369 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3370 | value = cf->args->elts; |
| 3371 | |
| 3372 | ngx_memzero(&u, sizeof(ngx_url_t)); |
| 3373 | |
| 3374 | u.url = value[1]; |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3375 | u.default_port = 80; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3376 | |
Igor Sysoev | 7ed63ee | 2007-10-08 08:55:12 +0000 | [diff] [blame] | 3377 | if (ngx_parse_url(cf->pool, &u) != NGX_OK) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3378 | if (u.err) { |
| 3379 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 3380 | "%s in upstream \"%V\"", u.err, &u.url); |
| 3381 | } |
| 3382 | |
| 3383 | return NGX_CONF_ERROR; |
| 3384 | } |
| 3385 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3386 | weight = 1; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3387 | max_fails = 1; |
| 3388 | fail_timeout = 10; |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3389 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3390 | for (i = 2; i < cf->args->nelts; i++) { |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3391 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3392 | if (ngx_strncmp(value[i].data, "weight=", 7) == 0) { |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3393 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3394 | if (!(uscf->flags & NGX_HTTP_UPSTREAM_WEIGHT)) { |
| 3395 | goto invalid; |
| 3396 | } |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3397 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3398 | weight = ngx_atoi(&value[i].data[7], value[i].len - 7); |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3399 | |
| 3400 | if (weight == NGX_ERROR || weight == 0) { |
| 3401 | goto invalid; |
| 3402 | } |
| 3403 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3404 | continue; |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3405 | } |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) { |
| 3408 | |
| 3409 | if (!(uscf->flags & NGX_HTTP_UPSTREAM_MAX_FAILS)) { |
| 3410 | goto invalid; |
| 3411 | } |
| 3412 | |
| 3413 | max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10); |
| 3414 | |
| 3415 | if (max_fails == NGX_ERROR) { |
| 3416 | goto invalid; |
| 3417 | } |
| 3418 | |
| 3419 | continue; |
| 3420 | } |
| 3421 | |
| 3422 | if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) { |
| 3423 | |
| 3424 | if (!(uscf->flags & NGX_HTTP_UPSTREAM_FAIL_TIMEOUT)) { |
| 3425 | goto invalid; |
| 3426 | } |
| 3427 | |
| 3428 | s.len = value[i].len - 13; |
| 3429 | s.data = &value[i].data[13]; |
| 3430 | |
| 3431 | fail_timeout = ngx_parse_time(&s, 1); |
| 3432 | |
Igor Sysoev | 6a2ea3f | 2008-04-17 14:32:11 +0000 | [diff] [blame] | 3433 | if (fail_timeout == NGX_ERROR) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3434 | goto invalid; |
| 3435 | } |
| 3436 | |
| 3437 | continue; |
| 3438 | } |
| 3439 | |
Igor Sysoev | 6876bcd | 2007-08-09 13:54:33 +0000 | [diff] [blame] | 3440 | if (ngx_strncmp(value[i].data, "backup", 6) == 0) { |
| 3441 | |
| 3442 | if (!(uscf->flags & NGX_HTTP_UPSTREAM_BACKUP)) { |
| 3443 | goto invalid; |
| 3444 | } |
| 3445 | |
| 3446 | us->backup = 1; |
| 3447 | |
| 3448 | continue; |
| 3449 | } |
| 3450 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3451 | if (ngx_strncmp(value[i].data, "down", 4) == 0) { |
| 3452 | |
| 3453 | if (!(uscf->flags & NGX_HTTP_UPSTREAM_DOWN)) { |
| 3454 | goto invalid; |
| 3455 | } |
| 3456 | |
| 3457 | us->down = 1; |
| 3458 | |
| 3459 | continue; |
| 3460 | } |
| 3461 | |
| 3462 | goto invalid; |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3465 | us->addrs = u.addrs; |
| 3466 | us->naddrs = u.naddrs; |
| 3467 | us->weight = weight; |
| 3468 | us->max_fails = max_fails; |
| 3469 | us->fail_timeout = fail_timeout; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3470 | |
| 3471 | return NGX_CONF_OK; |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3472 | |
| 3473 | invalid: |
| 3474 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3475 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 3476 | "invalid parameter \"%V\"", &value[i]); |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3477 | |
| 3478 | return NGX_CONF_ERROR; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | |
| 3482 | ngx_http_upstream_srv_conf_t * |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3483 | ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags) |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3484 | { |
| 3485 | ngx_uint_t i; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3486 | ngx_http_upstream_server_t *us; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3487 | ngx_http_upstream_srv_conf_t *uscf, **uscfp; |
| 3488 | ngx_http_upstream_main_conf_t *umcf; |
| 3489 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3490 | if (!(flags & NGX_HTTP_UPSTREAM_CREATE)) { |
| 3491 | |
Igor Sysoev | 7ed63ee | 2007-10-08 08:55:12 +0000 | [diff] [blame] | 3492 | if (ngx_parse_url(cf->pool, u) != NGX_OK) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3493 | if (u->err) { |
| 3494 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 3495 | "%s in upstream \"%V\"", u->err, &u->url); |
| 3496 | } |
| 3497 | |
| 3498 | return NULL; |
| 3499 | } |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module); |
| 3503 | |
| 3504 | uscfp = umcf->upstreams.elts; |
| 3505 | |
| 3506 | for (i = 0; i < umcf->upstreams.nelts; i++) { |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3507 | |
| 3508 | if (uscfp[i]->host.len != u->host.len |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3509 | || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) |
| 3510 | != 0) |
| 3511 | { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3512 | continue; |
| 3513 | } |
| 3514 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3515 | if ((flags & NGX_HTTP_UPSTREAM_CREATE) |
| 3516 | && (uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE)) |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3517 | { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3518 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 3519 | "duplicate upstream \"%V\"", &u->host); |
| 3520 | return NULL; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3521 | } |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3522 | |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3523 | if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) { |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3524 | ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3525 | "upstream \"%V\" may not have port %d", |
| 3526 | &u->host, u->port); |
| 3527 | return NULL; |
| 3528 | } |
| 3529 | |
| 3530 | if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) { |
| 3531 | ngx_log_error(NGX_LOG_WARN, cf->log, 0, |
| 3532 | "upstream \"%V\" may not have port %d in %s:%ui", |
| 3533 | &u->host, uscfp[i]->port, |
Igor Sysoev | 4b96baa | 2007-09-15 17:11:06 +0000 | [diff] [blame] | 3534 | uscfp[i]->file_name, uscfp[i]->line); |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3535 | return NULL; |
| 3536 | } |
| 3537 | |
| 3538 | if (uscfp[i]->port != u->port) { |
| 3539 | continue; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
Igor Sysoev | 32b7875 | 2007-07-23 12:35:21 +0000 | [diff] [blame] | 3542 | if (uscfp[i]->default_port && u->default_port |
| 3543 | && uscfp[i]->default_port != u->default_port) |
| 3544 | { |
| 3545 | continue; |
| 3546 | } |
| 3547 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3548 | return uscfp[i]; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | uscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_srv_conf_t)); |
| 3552 | if (uscf == NULL) { |
| 3553 | return NULL; |
| 3554 | } |
| 3555 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3556 | uscf->flags = flags; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3557 | uscf->host = u->host; |
Igor Sysoev | 4b96baa | 2007-09-15 17:11:06 +0000 | [diff] [blame] | 3558 | uscf->file_name = cf->conf_file->file.name.data; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3559 | uscf->line = cf->conf_file->line; |
Igor Sysoev | bf3aaac | 2006-12-12 16:46:16 +0000 | [diff] [blame] | 3560 | uscf->port = u->port; |
| 3561 | uscf->default_port = u->default_port; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3562 | |
| 3563 | if (u->naddrs == 1) { |
| 3564 | uscf->servers = ngx_array_create(cf->pool, 1, |
| 3565 | sizeof(ngx_http_upstream_server_t)); |
| 3566 | if (uscf->servers == NULL) { |
| 3567 | return NGX_CONF_ERROR; |
| 3568 | } |
| 3569 | |
| 3570 | us = ngx_array_push(uscf->servers); |
| 3571 | if (us == NULL) { |
| 3572 | return NGX_CONF_ERROR; |
| 3573 | } |
| 3574 | |
| 3575 | ngx_memzero(us, sizeof(ngx_http_upstream_server_t)); |
| 3576 | |
| 3577 | us->addrs = u->addrs; |
| 3578 | us->naddrs = u->naddrs; |
| 3579 | } |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3580 | |
| 3581 | uscfp = ngx_array_push(&umcf->upstreams); |
| 3582 | if (uscfp == NULL) { |
| 3583 | return NULL; |
| 3584 | } |
| 3585 | |
| 3586 | *uscfp = uscf; |
| 3587 | |
| 3588 | return uscf; |
| 3589 | } |
| 3590 | |
| 3591 | |
Igor Sysoev | cb54061 | 2007-12-09 18:03:20 +0000 | [diff] [blame] | 3592 | ngx_int_t |
| 3593 | ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf, |
| 3594 | ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev, |
| 3595 | ngx_str_t *default_hide_headers, ngx_hash_init_t *hash) |
| 3596 | { |
| 3597 | ngx_str_t *h; |
| 3598 | ngx_uint_t i, j; |
| 3599 | ngx_array_t hide_headers; |
| 3600 | ngx_hash_key_t *hk; |
| 3601 | |
| 3602 | if (conf->hide_headers == NGX_CONF_UNSET_PTR |
| 3603 | && conf->pass_headers == NGX_CONF_UNSET_PTR) |
| 3604 | { |
| 3605 | conf->hide_headers_hash = prev->hide_headers_hash; |
| 3606 | |
| 3607 | if (conf->hide_headers_hash.buckets) { |
| 3608 | return NGX_OK; |
| 3609 | } |
| 3610 | |
| 3611 | conf->hide_headers = prev->hide_headers; |
| 3612 | conf->pass_headers = prev->pass_headers; |
| 3613 | |
| 3614 | } else { |
| 3615 | if (conf->hide_headers == NGX_CONF_UNSET_PTR) { |
| 3616 | conf->hide_headers = prev->hide_headers; |
| 3617 | } |
| 3618 | |
| 3619 | if (conf->pass_headers == NGX_CONF_UNSET_PTR) { |
| 3620 | conf->pass_headers = prev->pass_headers; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) |
| 3625 | != NGX_OK) |
| 3626 | { |
| 3627 | return NGX_ERROR; |
| 3628 | } |
| 3629 | |
| 3630 | for (h = default_hide_headers; h->len; h++) { |
| 3631 | hk = ngx_array_push(&hide_headers); |
| 3632 | if (hk == NULL) { |
| 3633 | return NGX_ERROR; |
| 3634 | } |
| 3635 | |
| 3636 | hk->key = *h; |
| 3637 | hk->key_hash = ngx_hash_key_lc(h->data, h->len); |
| 3638 | hk->value = (void *) 1; |
| 3639 | } |
| 3640 | |
| 3641 | if (conf->hide_headers != NGX_CONF_UNSET_PTR) { |
| 3642 | |
| 3643 | h = conf->hide_headers->elts; |
| 3644 | |
| 3645 | for (i = 0; i < conf->hide_headers->nelts; i++) { |
| 3646 | |
| 3647 | hk = hide_headers.elts; |
| 3648 | |
| 3649 | for (j = 0; j < hide_headers.nelts; j++) { |
| 3650 | if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) { |
| 3651 | goto exist; |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | hk = ngx_array_push(&hide_headers); |
| 3656 | if (hk == NULL) { |
| 3657 | return NGX_ERROR; |
| 3658 | } |
| 3659 | |
| 3660 | hk->key = h[i]; |
| 3661 | hk->key_hash = ngx_hash_key_lc(h[i].data, h[i].len); |
| 3662 | hk->value = (void *) 1; |
| 3663 | |
| 3664 | exist: |
| 3665 | |
| 3666 | continue; |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | if (conf->pass_headers != NGX_CONF_UNSET_PTR) { |
| 3671 | |
| 3672 | h = conf->pass_headers->elts; |
| 3673 | hk = hide_headers.elts; |
| 3674 | |
| 3675 | for (i = 0; i < conf->pass_headers->nelts; i++) { |
| 3676 | for (j = 0; j < hide_headers.nelts; j++) { |
| 3677 | |
| 3678 | if (hk[j].key.data == NULL) { |
| 3679 | continue; |
| 3680 | } |
| 3681 | |
| 3682 | if (ngx_strcasecmp(h[i].data, hk[j].key.data) == 0) { |
| 3683 | hk[j].key.data = NULL; |
| 3684 | break; |
| 3685 | } |
| 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | hash->hash = &conf->hide_headers_hash; |
Igor Sysoev | 3fc85af | 2007-12-10 12:30:09 +0000 | [diff] [blame] | 3691 | hash->key = ngx_hash_key_lc; |
Igor Sysoev | cb54061 | 2007-12-09 18:03:20 +0000 | [diff] [blame] | 3692 | hash->pool = cf->pool; |
| 3693 | hash->temp_pool = NULL; |
Igor Sysoev | 3fc85af | 2007-12-10 12:30:09 +0000 | [diff] [blame] | 3694 | |
Igor Sysoev | cb54061 | 2007-12-09 18:03:20 +0000 | [diff] [blame] | 3695 | return ngx_hash_init(hash, hide_headers.elts, hide_headers.nelts); |
| 3696 | } |
| 3697 | |
| 3698 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3699 | static void * |
| 3700 | ngx_http_upstream_create_main_conf(ngx_conf_t *cf) |
| 3701 | { |
| 3702 | ngx_http_upstream_main_conf_t *umcf; |
| 3703 | |
| 3704 | umcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_main_conf_t)); |
| 3705 | if (umcf == NULL) { |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3709 | if (ngx_array_init(&umcf->upstreams, cf->pool, 4, |
| 3710 | sizeof(ngx_http_upstream_srv_conf_t *)) |
| 3711 | != NGX_OK) |
| 3712 | { |
| 3713 | return NGX_CONF_ERROR; |
| 3714 | } |
| 3715 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3716 | return umcf; |
| 3717 | } |
| 3718 | |
| 3719 | |
| 3720 | static char * |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3721 | ngx_http_upstream_init_main_conf(ngx_conf_t *cf, void *conf) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3722 | { |
| 3723 | ngx_http_upstream_main_conf_t *umcf = conf; |
| 3724 | |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3725 | ngx_uint_t i; |
| 3726 | ngx_array_t headers_in; |
| 3727 | ngx_hash_key_t *hk; |
| 3728 | ngx_hash_init_t hash; |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3729 | ngx_http_upstream_init_pt init; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3730 | ngx_http_upstream_header_t *header; |
| 3731 | ngx_http_upstream_srv_conf_t **uscfp; |
| 3732 | |
| 3733 | uscfp = umcf->upstreams.elts; |
| 3734 | |
| 3735 | for (i = 0; i < umcf->upstreams.nelts; i++) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3736 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3737 | init = uscfp[i]->peer.init_upstream ? uscfp[i]->peer.init_upstream: |
| 3738 | ngx_http_upstream_init_round_robin; |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3739 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3740 | if (init(cf, uscfp[i]) != NGX_OK) { |
Igor Sysoev | 6f134cc | 2006-05-23 14:54:58 +0000 | [diff] [blame] | 3741 | return NGX_CONF_ERROR; |
| 3742 | } |
| 3743 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3744 | |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 3745 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 3746 | /* upstream_headers_in_hash */ |
| 3747 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 3748 | if (ngx_array_init(&headers_in, cf->temp_pool, 32, sizeof(ngx_hash_key_t)) |
| 3749 | != NGX_OK) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3750 | { |
| 3751 | return NGX_CONF_ERROR; |
| 3752 | } |
| 3753 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 3754 | for (header = ngx_http_upstream_headers_in; header->name.len; header++) { |
| 3755 | hk = ngx_array_push(&headers_in); |
| 3756 | if (hk == NULL) { |
| 3757 | return NGX_CONF_ERROR; |
| 3758 | } |
| 3759 | |
| 3760 | hk->key = header->name; |
| 3761 | hk->key_hash = ngx_hash_key_lc(header->name.data, header->name.len); |
| 3762 | hk->value = header; |
| 3763 | } |
| 3764 | |
| 3765 | hash.hash = &umcf->headers_in_hash; |
| 3766 | hash.key = ngx_hash_key_lc; |
| 3767 | hash.max_size = 512; |
Igor Sysoev | aec57e2 | 2006-10-09 14:03:16 +0000 | [diff] [blame] | 3768 | hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 3769 | hash.name = "upstream_headers_in_hash"; |
| 3770 | hash.pool = cf->pool; |
| 3771 | hash.temp_pool = NULL; |
| 3772 | |
| 3773 | if (ngx_hash_init(&hash, headers_in.elts, headers_in.nelts) != NGX_OK) { |
| 3774 | return NGX_CONF_ERROR; |
| 3775 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 3776 | |
| 3777 | return NGX_CONF_OK; |
| 3778 | } |