comparison src/http/modules/proxy/ngx_http_proxy_upstream.c @ 343:6bdf858bff8c

nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
author Igor Sysoev <igor@sysoev.ru>
date Fri, 28 May 2004 15:49:23 +0000
parents 39b6f2df45c0
children e366ba5db8f8
comparison
equal deleted inserted replaced
342:0ee0642af5f1 343:6bdf858bff8c
106 106
107 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p) 107 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p)
108 { 108 {
109 size_t len; 109 size_t len;
110 ngx_uint_t i; 110 ngx_uint_t i;
111 ngx_hunk_t *h; 111 ngx_buf_t *b;
112 ngx_chain_t *chain; 112 ngx_chain_t *chain;
113 ngx_table_elt_t *header; 113 ngx_table_elt_t *header;
114 ngx_http_request_t *r; 114 ngx_http_request_t *r;
115 ngx_http_proxy_upstream_conf_t *uc; 115 ngx_http_proxy_upstream_conf_t *uc;
116 116
175 175
176 /* 2 is for ": " and 2 is for "\r\n" */ 176 /* 2 is for ": " and 2 is for "\r\n" */
177 len += header[i].key.len + 2 + header[i].value.len + 2; 177 len += header[i].key.len + 2 + header[i].value.len + 2;
178 } 178 }
179 179
180 /* STUB */ len++; 180 #if (NGX_DEBUG)
181 181 len++;
182 ngx_test_null(h, ngx_create_temp_hunk(r->pool, len), NULL); 182 #endif
183 ngx_alloc_link_and_set_hunk(chain, h, r->pool, NULL); 183
184 ngx_test_null(b, ngx_create_temp_buf(r->pool, len), NULL);
185 ngx_alloc_link_and_set_buf(chain, b, r->pool, NULL);
184 186
185 187
186 /* the request line */ 188 /* the request line */
187 189
188 if (p->upstream->method) { 190 if (p->upstream->method) {
189 h->last = ngx_cpymem(h->last, 191 b->last = ngx_cpymem(b->last,
190 http_methods[p->upstream->method - 1].data, 192 http_methods[p->upstream->method - 1].data,
191 http_methods[p->upstream->method - 1].len); 193 http_methods[p->upstream->method - 1].len);
192 } else { 194 } else {
193 h->last = ngx_cpymem(h->last, r->method_name.data, r->method_name.len); 195 b->last = ngx_cpymem(b->last, r->method_name.data, r->method_name.len);
194 } 196 }
195 197
196 h->last = ngx_cpymem(h->last, uc->uri.data, uc->uri.len); 198 b->last = ngx_cpymem(b->last, uc->uri.data, uc->uri.len);
197 199
198 h->last = ngx_cpymem(h->last, 200 b->last = ngx_cpymem(b->last,
199 r->uri.data + uc->location->len, 201 r->uri.data + uc->location->len,
200 r->uri.len - uc->location->len); 202 r->uri.len - uc->location->len);
201 203
202 if (r->args.len > 0) { 204 if (r->args.len > 0) {
203 *(h->last++) = '?'; 205 *(b->last++) = '?';
204 h->last = ngx_cpymem(h->last, r->args.data, r->args.len); 206 b->last = ngx_cpymem(b->last, r->args.data, r->args.len);
205 } 207 }
206 208
207 h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1); 209 b->last = ngx_cpymem(b->last, http_version, sizeof(http_version) - 1);
208 210
209 211
210 /* the "Connection: close" header */ 212 /* the "Connection: close" header */
211 213
212 h->last = ngx_cpymem(h->last, connection_close_header, 214 b->last = ngx_cpymem(b->last, connection_close_header,
213 sizeof(connection_close_header) - 1); 215 sizeof(connection_close_header) - 1);
214 216
215 217
216 /* the "Host" header */ 218 /* the "Host" header */
217 219
218 h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1); 220 b->last = ngx_cpymem(b->last, host_header, sizeof(host_header) - 1);
219 221
220 if (p->lcf->preserve_host && r->headers_in.host) { 222 if (p->lcf->preserve_host && r->headers_in.host) {
221 h->last = ngx_cpymem(h->last, r->headers_in.host->value.data, 223 b->last = ngx_cpymem(b->last, r->headers_in.host->value.data,
222 r->headers_in.host_name_len); 224 r->headers_in.host_name_len);
223 225
224 if (!uc->default_port) { 226 if (!uc->default_port) {
225 *(h->last++) = ':'; 227 *(b->last++) = ':';
226 h->last = ngx_cpymem(h->last, uc->port_text.data, 228 b->last = ngx_cpymem(b->last, uc->port_text.data,
227 uc->port_text.len); 229 uc->port_text.len);
228 } 230 }
229 231
230 } else { 232 } else {
231 h->last = ngx_cpymem(h->last, uc->host_header.data, 233 b->last = ngx_cpymem(b->last, uc->host_header.data,
232 uc->host_header.len); 234 uc->host_header.len);
233 } 235 }
234 *(h->last++) = CR; *(h->last++) = LF; 236 *(b->last++) = CR; *(b->last++) = LF;
235 237
236 238
237 /* the "X-Real-IP" header */ 239 /* the "X-Real-IP" header */
238 240
239 if (p->lcf->set_x_real_ip) { 241 if (p->lcf->set_x_real_ip) {
240 h->last = ngx_cpymem(h->last, x_real_ip_header, 242 b->last = ngx_cpymem(b->last, x_real_ip_header,
241 sizeof(x_real_ip_header) - 1); 243 sizeof(x_real_ip_header) - 1);
242 h->last = ngx_cpymem(h->last, r->connection->addr_text.data, 244 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
243 r->connection->addr_text.len); 245 r->connection->addr_text.len);
244 *(h->last++) = CR; *(h->last++) = LF; 246 *(b->last++) = CR; *(b->last++) = LF;
245 } 247 }
246 248
247 249
248 /* the "X-Forwarded-For" header */ 250 /* the "X-Forwarded-For" header */
249 251
250 if (p->lcf->add_x_forwarded_for) { 252 if (p->lcf->add_x_forwarded_for) {
251 if (r->headers_in.x_forwarded_for) { 253 if (r->headers_in.x_forwarded_for) {
252 h->last = ngx_cpymem(h->last, x_forwarded_for_header, 254 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
253 sizeof(x_forwarded_for_header) - 1); 255 sizeof(x_forwarded_for_header) - 1);
254 256
255 h->last = ngx_cpymem(h->last, 257 b->last = ngx_cpymem(b->last,
256 r->headers_in.x_forwarded_for->value.data, 258 r->headers_in.x_forwarded_for->value.data,
257 r->headers_in.x_forwarded_for->value.len); 259 r->headers_in.x_forwarded_for->value.len);
258 260
259 *(h->last++) = ','; *(h->last++) = ' '; 261 *(b->last++) = ','; *(b->last++) = ' ';
260 262
261 } else { 263 } else {
262 h->last = ngx_cpymem(h->last, x_forwarded_for_header, 264 b->last = ngx_cpymem(b->last, x_forwarded_for_header,
263 sizeof(x_forwarded_for_header) - 1); 265 sizeof(x_forwarded_for_header) - 1);
264 } 266 }
265 267
266 h->last = ngx_cpymem(h->last, r->connection->addr_text.data, 268 b->last = ngx_cpymem(b->last, r->connection->addr_text.data,
267 r->connection->addr_text.len); 269 r->connection->addr_text.len);
268 *(h->last++) = CR; *(h->last++) = LF; 270 *(b->last++) = CR; *(b->last++) = LF;
269 } 271 }
270 272
271 273
272 for (i = 0; i < r->headers_in.headers.nelts; i++) { 274 for (i = 0; i < r->headers_in.headers.nelts; i++) {
273 275
287 && p->lcf->add_x_forwarded_for) 289 && p->lcf->add_x_forwarded_for)
288 { 290 {
289 continue; 291 continue;
290 } 292 }
291 293
292 h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len); 294 b->last = ngx_cpymem(b->last, header[i].key.data, header[i].key.len);
293 295
294 *(h->last++) = ':'; *(h->last++) = ' '; 296 *(b->last++) = ':'; *(b->last++) = ' ';
295 297
296 h->last = ngx_cpymem(h->last, header[i].value.data, 298 b->last = ngx_cpymem(b->last, header[i].value.data,
297 header[i].value.len); 299 header[i].value.len);
298 300
299 *(h->last++) = CR; *(h->last++) = LF; 301 *(b->last++) = CR; *(b->last++) = LF;
300 302
301 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 303 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
302 "http proxy header: \"%s: %s\"", 304 "http proxy header: \"%s: %s\"",
303 header[i].key.data, header[i].value.data); 305 header[i].key.data, header[i].value.data);
304 } 306 }
305 307
306 /* add "\r\n" at the header end */ 308 /* add "\r\n" at the header end */
307 *(h->last++) = CR; *(h->last++) = LF; 309 *(b->last++) = CR; *(b->last++) = LF;
308 310
309 #if (NGX_DEBUG) 311 #if (NGX_DEBUG)
310 *(h->last) = '\0'; 312 *(b->last) = '\0';
311 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 313 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
312 "http proxy header:\n\"%s\"", h->pos); 314 "http proxy header:\n\"%s\"", b->pos);
313 #endif 315 #endif
314 316
315 return chain; 317 return chain;
316 } 318 }
317 319
387 p->upstream->output_chain_ctx = output; 389 p->upstream->output_chain_ctx = output;
388 390
389 output->sendfile = r->sendfile; 391 output->sendfile = r->sendfile;
390 output->pool = r->pool; 392 output->pool = r->pool;
391 output->bufs.num = 1; 393 output->bufs.num = 1;
392 output->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; 394 output->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
393 output->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer; 395 output->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer;
394 396
395 if (!(writer = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) { 397 if (!(writer = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) {
396 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 398 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
397 return; 399 return;
418 ngx_output_chain_ctx_t *output; 420 ngx_output_chain_ctx_t *output;
419 421
420 /* reinit the request chain */ 422 /* reinit the request chain */
421 423
422 for (cl = p->request->request_body->bufs; cl; cl = cl->next) { 424 for (cl = p->request->request_body->bufs; cl; cl = cl->next) {
423 cl->hunk->pos = cl->hunk->start; 425 cl->buf->pos = cl->buf->start;
424 cl->hunk->file_pos = 0; 426 cl->buf->file_pos = 0;
425 } 427 }
426 428
427 /* reinit the ngx_output_chain() context */ 429 /* reinit the ngx_output_chain() context */
428 430
429 output = p->upstream->output_chain_ctx; 431 output = p->upstream->output_chain_ctx;
430 432
431 output->hunk = NULL; 433 output->buf = NULL;
432 output->in = NULL; 434 output->in = NULL;
433 output->free = NULL; 435 output->free = NULL;
434 output->busy = NULL; 436 output->busy = NULL;
435 437
436 /* reinit r->header_in buffer */ 438 /* reinit r->header_in buffer */
612 if (!(output->free = ngx_alloc_chain_link(r->pool))) { 614 if (!(output->free = ngx_alloc_chain_link(r->pool))) {
613 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 615 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
614 return; 616 return;
615 } 617 }
616 618
617 output->free->hunk = r->request_body->buf; 619 output->free->buf = r->request_body->buf;
618 output->free->next = NULL; 620 output->free->buf = NULL;
619 output->hunks = 1; 621 output->allocated = 1;
620 622
621 r->request_body->buf->pos = r->request_body->buf->start; 623 r->request_body->buf->pos = r->request_body->buf->start;
622 } 624 }
623 625
624 p->request_sent = 0; 626 p->request_sent = 0;
791 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT); 793 ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
792 return; 794 return;
793 } 795 }
794 796
795 if (p->header_in == NULL) { 797 if (p->header_in == NULL) {
796 p->header_in = ngx_create_temp_hunk(p->request->pool, 798 p->header_in = ngx_create_temp_buf(p->request->pool,
797 p->lcf->header_buffer_size); 799 p->lcf->header_buffer_size);
798 if (p->header_in == NULL) { 800 if (p->header_in == NULL) {
799 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR); 801 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
800 return; 802 return;
801 } 803 }
802 p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; 804 p->header_in->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
803 805
804 if (p->cache) { 806 if (p->cache) {
805 p->header_in->pos += p->cache->ctx.header_size; 807 p->header_in->pos += p->cache->ctx.header_size;
806 p->header_in->last = p->header_in->pos; 808 p->header_in->last = p->header_in->pos;
807 } 809 }
1174 1176
1175 ep->input_filter = ngx_event_pipe_copy_input_filter; 1177 ep->input_filter = ngx_event_pipe_copy_input_filter;
1176 ep->output_filter = (ngx_event_pipe_output_filter_pt) 1178 ep->output_filter = (ngx_event_pipe_output_filter_pt)
1177 ngx_http_output_filter; 1179 ngx_http_output_filter;
1178 ep->output_ctx = r; 1180 ep->output_ctx = r;
1179 ep->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module; 1181 ep->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
1180 ep->bufs = p->lcf->bufs; 1182 ep->bufs = p->lcf->bufs;
1181 ep->busy_size = p->lcf->busy_buffers_size; 1183 ep->busy_size = p->lcf->busy_buffers_size;
1182 ep->upstream = p->upstream->peer.connection; 1184 ep->upstream = p->upstream->peer.connection;
1183 ep->downstream = r->connection; 1185 ep->downstream = r->connection;
1184 ep->pool = r->pool; 1186 ep->pool = r->pool;
1204 } 1206 }
1205 1207
1206 ep->max_temp_file_size = p->lcf->max_temp_file_size; 1208 ep->max_temp_file_size = p->lcf->max_temp_file_size;
1207 ep->temp_file_write_size = p->lcf->temp_file_write_size; 1209 ep->temp_file_write_size = p->lcf->temp_file_write_size;
1208 1210
1209 ep->preread_hunks = ngx_alloc_chain_link(r->pool); 1211 if (!(ep->preread_bufs = ngx_alloc_chain_link(r->pool))) {
1210 if (ep->preread_hunks == NULL) {
1211 ngx_http_proxy_finalize_request(p, 0); 1212 ngx_http_proxy_finalize_request(p, 0);
1212 return; 1213 return;
1213 } 1214 }
1214 ep->preread_hunks->hunk = p->header_in; 1215 ep->preread_bufs->buf = p->header_in;
1215 ep->preread_hunks->next = NULL; 1216 ep->preread_bufs->next = NULL;
1216 1217
1217 ep->preread_size = p->header_in->last - p->header_in->pos; 1218 ep->preread_size = p->header_in->last - p->header_in->pos;
1218 1219
1219 if (p->cachable) { 1220 if (p->cachable) {
1220 ep->hunk_to_file = ngx_calloc_hunk(r->pool); 1221 ep->buf_to_file = ngx_calloc_buf(r->pool);
1221 if (ep->hunk_to_file == NULL) { 1222 if (ep->buf_to_file == NULL) {
1222 ngx_http_proxy_finalize_request(p, 0); 1223 ngx_http_proxy_finalize_request(p, 0);
1223 return; 1224 return;
1224 } 1225 }
1225 ep->hunk_to_file->pos = p->header_in->start; 1226 ep->buf_to_file->pos = p->header_in->start;
1226 ep->hunk_to_file->last = p->header_in->pos; 1227 ep->buf_to_file->last = p->header_in->pos;
1227 ep->hunk_to_file->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; 1228 ep->buf_to_file->temporary = 1;
1228 } 1229 }
1229 1230
1230 if (ngx_event_flags & NGX_USE_AIO_EVENT) { 1231 if (ngx_event_flags & NGX_USE_AIO_EVENT) {
1231 /* the posted aio operation can currupt a shadow buffer */ 1232 /* the posted aio operation can currupt a shadow buffer */
1232 ep->single_buf = 1; 1233 ep->single_buf = 1;
1233 } 1234 }
1234 1235
1235 /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_hunks() */ 1236 /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_bufs() */
1236 ep->free_bufs = 1; 1237 ep->free_bufs = 1;
1237 1238
1238 /* 1239 /*
1239 * event_pipe would do p->header_in->last += ep->preread_size 1240 * event_pipe would do p->header_in->last += ep->preread_size
1240 * as though these bytes were read. 1241 * as though these bytes were read.