comparison src/http/modules/proxy/ngx_http_proxy_upstream.c @ 287:35a6a9df2d25

nginx-0.0.2-2004-03-12-19:57:08 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 12 Mar 2004 16:57:08 +0000
parents 70e1c7d2b83d
children f81d075ad172
comparison
equal deleted inserted replaced
286:fc8dc489247e 287:35a6a9df2d25
36 }; 36 };
37 37
38 38
39 static char http_version[] = " HTTP/1.0" CRLF; 39 static char http_version[] = " HTTP/1.0" CRLF;
40 static char host_header[] = "Host: "; 40 static char host_header[] = "Host: ";
41 static char x_real_ip_header[] = "X-Real-IP: ";
42 static char x_forwarded_for_header[] = "X-Forwarded-For: ";
41 static char connection_close_header[] = "Connection: close" CRLF; 43 static char connection_close_header[] = "Connection: close" CRLF;
42 44
43 45
44 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p) 46 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p)
45 { 47 {
114 + sizeof(host_header) - 1 + uc->host_header.len + 2 116 + sizeof(host_header) - 1 + uc->host_header.len + 2
115 /* 2 is for "\r\n" */ 117 /* 2 is for "\r\n" */
116 + sizeof(connection_close_header) - 1 118 + sizeof(connection_close_header) - 1
117 + 2; /* 2 is for "\r\n" at the header end */ 119 + 2; /* 2 is for "\r\n" at the header end */
118 120
121 if (p->lcf->set_x_real_ip) {
122 /* 2 is for "\r\n" */
123 len += sizeof(x_real_ip_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
124 }
125
126
127 if (p->lcf->add_x_forwarded_for) {
128 if (r->headers_in.x_forwarded_for) {
129 len += r->headers_in.x_forwarded_for->key.len
130 + 2 /* 2 is ofr ": " */
131 + r->headers_in.x_forwarded_for->value.len
132 + 2 /* 2 is ofr ", " */
133 + INET_ADDRSTRLEN - 1
134 + 2; /* 2 is for "\r\n" */
135 } else {
136 len += sizeof(x_forwarded_for_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
137 /* 2 is for "\r\n" */
138 }
139 }
140
141
119 header = (ngx_table_elt_t *) r->headers_in.headers->elts; 142 header = (ngx_table_elt_t *) r->headers_in.headers->elts;
120 for (i = 0; i < r->headers_in.headers->nelts; i++) { 143 for (i = 0; i < r->headers_in.headers->nelts; i++) {
121 144
122 if (&header[i] == r->headers_in.host) { 145 if (&header[i] == r->headers_in.host) {
123 continue; 146 continue;
154 } 177 }
155 178
156 h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1); 179 h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1);
157 180
158 181
159 /* "Host" header */ 182 /* the "Host" header */
160 183
161 h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1); 184 h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1);
162 h->last = ngx_cpymem(h->last, uc->host_header.data, uc->host_header.len); 185 h->last = ngx_cpymem(h->last, uc->host_header.data, uc->host_header.len);
163 *(h->last++) = CR; *(h->last++) = LF; 186 *(h->last++) = CR; *(h->last++) = LF;
164 187
165 188
166 /* "Connection: close" header */ 189 /* the "Connection: close" header */
167 190
168 h->last = ngx_cpymem(h->last, connection_close_header, 191 h->last = ngx_cpymem(h->last, connection_close_header,
169 sizeof(connection_close_header) - 1); 192 sizeof(connection_close_header) - 1);
170 193
171 194
195 /* the "X-Real-IP" header */
196
197 if (p->lcf->set_x_real_ip) {
198 h->last = ngx_cpymem(h->last, x_real_ip_header,
199 sizeof(x_real_ip_header) - 1);
200 h->last = ngx_cpymem(h->last, r->connection->addr_text.data,
201 r->connection->addr_text.len);
202 *(h->last++) = CR; *(h->last++) = LF;
203 }
204
205
206 /* the "X-Forwarded-For" header */
207
208 if (p->lcf->add_x_forwarded_for) {
209 if (r->headers_in.x_forwarded_for) {
210 h->last = ngx_cpymem(h->last,
211 r->headers_in.x_forwarded_for->key.data,
212 r->headers_in.x_forwarded_for->key.len);
213
214 *(h->last++) = ':'; *(h->last++) = ' ';
215
216 h->last = ngx_cpymem(h->last,
217 r->headers_in.x_forwarded_for->value.data,
218 r->headers_in.x_forwarded_for->value.len);
219
220 *(h->last++) = ','; *(h->last++) = ' ';
221
222 } else {
223 h->last = ngx_cpymem(h->last, x_forwarded_for_header,
224 sizeof(x_forwarded_for_header) - 1);
225 }
226
227 h->last = ngx_cpymem(h->last, r->connection->addr_text.data,
228 r->connection->addr_text.len);
229 *(h->last++) = CR; *(h->last++) = LF;
230 }
231
232
172 for (i = 0; i < r->headers_in.headers->nelts; i++) { 233 for (i = 0; i < r->headers_in.headers->nelts; i++) {
173 234
174 if (&header[i] == r->headers_in.host) { 235 if (&header[i] == r->headers_in.host) {
175 continue; 236 continue;
176 } 237 }
178 if (&header[i] == r->headers_in.connection) { 239 if (&header[i] == r->headers_in.connection) {
179 continue; 240 continue;
180 } 241 }
181 242
182 if (&header[i] == r->headers_in.keep_alive) { 243 if (&header[i] == r->headers_in.keep_alive) {
244 continue;
245 }
246
247 if (&header[i] == r->headers_in.x_forwarded_for
248 && p->lcf->add_x_forwarded_for)
249 {
183 continue; 250 continue;
184 } 251 }
185 252
186 h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len); 253 h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len);
187 254
375 } else { 442 } else {
376 /* rc == NGX_ERROR */ 443 /* rc == NGX_ERROR */
377 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING; 444 ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
378 } 445 }
379 446
447 #if (NGX_HTTP_CACHE)
448
380 if (p->stale && (p->lcf->use_stale & ft_type)) { 449 if (p->stale && (p->lcf->use_stale & ft_type)) {
381 ngx_http_proxy_finalize_request(p, 450 ngx_http_proxy_finalize_request(p,
382 ngx_http_proxy_send_cached_response(p)); 451 ngx_http_proxy_send_cached_response(p));
383 return; 452 return;
384 } 453 }
454
455 #endif
385 456
386 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE; 457 p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
387 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE); 458 ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
388 } 459 }
389 460
690 { 761 {
691 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500); 762 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500);
692 return; 763 return;
693 } 764 }
694 765
766 #if (NGX_HTTP_CACHE)
767
695 if (p->upstream->peer.tries == 0 768 if (p->upstream->peer.tries == 0
696 && p->stale 769 && p->stale
697 && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500)) 770 && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
698 { 771 {
699 ngx_http_proxy_finalize_request(p, 772 ngx_http_proxy_finalize_request(p,
700 ngx_http_proxy_send_cached_response(p)); 773 ngx_http_proxy_send_cached_response(p));
701 774
702 return; 775 return;
703 } 776 }
777
778 #endif
704 } 779 }
705 780
706 if (p->status == NGX_HTTP_NOT_FOUND 781 if (p->status == NGX_HTTP_NOT_FOUND
707 && p->upstream->peer.tries > 1 782 && p->upstream->peer.tries > 1
708 && p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_404) 783 && p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_404)
839 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, 914 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
840 "http proxy header done"); 915 "http proxy header done");
841 916
842 /* TODO: hook to process the upstream header */ 917 /* TODO: hook to process the upstream header */
843 918
919 #if (NGX_HTTP_CACHE)
920
844 if (p->cachable) { 921 if (p->cachable) {
845 p->cachable = ngx_http_proxy_is_cachable(p); 922 p->cachable = ngx_http_proxy_is_cachable(p);
846 } 923 }
924
925 #endif
847 926
848 ngx_http_proxy_send_response(p); 927 ngx_http_proxy_send_response(p);
849 return; 928 return;
850 929
851 } else if (rc != NGX_AGAIN) { 930 } else if (rc != NGX_AGAIN) {
1116 return; 1195 return;
1117 } 1196 }
1118 } 1197 }
1119 1198
1120 if (p->upstream->peer.connection) { 1199 if (p->upstream->peer.connection) {
1200
1201 #if (NGX_HTTP_FILE_CACHE)
1202
1121 if (ep->upstream_done && p->cachable) { 1203 if (ep->upstream_done && p->cachable) {
1122 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) { 1204 if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
1123 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock); 1205 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
1124 ngx_http_proxy_finalize_request(p, 0); 1206 ngx_http_proxy_finalize_request(p, 0);
1125 return; 1207 return;
1134 ngx_http_proxy_finalize_request(p, 0); 1216 ngx_http_proxy_finalize_request(p, 0);
1135 return; 1217 return;
1136 } 1218 }
1137 } 1219 }
1138 1220
1221 #endif
1222
1139 if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) { 1223 if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
1140 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, 1224 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
1141 "http proxy upstream exit"); 1225 "http proxy upstream exit");
1142 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock); 1226 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
1143 ngx_http_proxy_finalize_request(p, 0); 1227 ngx_http_proxy_finalize_request(p, 0);
1212 if (status) { 1296 if (status) {
1213 p->state->status = status; 1297 p->state->status = status;
1214 1298
1215 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type)) 1299 if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
1216 { 1300 {
1301
1302 #if (NGX_HTTP_CACHE)
1303
1217 if (p->stale && (p->lcf->use_stale & ft_type)) { 1304 if (p->stale && (p->lcf->use_stale & ft_type)) {
1218 ngx_http_proxy_finalize_request(p, 1305 ngx_http_proxy_finalize_request(p,
1219 ngx_http_proxy_send_cached_response(p)); 1306 ngx_http_proxy_send_cached_response(p));
1220 return; 1307 return;
1221 } 1308 }
1222 1309
1310 #endif
1311
1223 ngx_http_proxy_finalize_request(p, status); 1312 ngx_http_proxy_finalize_request(p, status);
1224 return; 1313 return;
1225 } 1314 }
1226 } 1315 }
1227 1316