Mercurial > hg > nginx
comparison src/http/ngx_http_event.c @ 91:637625a2acdb
nginx-0.0.1-2003-05-19-20:39:14 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 19 May 2003 16:39:14 +0000 |
parents | 29bf798b583f |
children | 19cc647ecd91 |
comparison
equal
deleted
inserted
replaced
90:37530da31268 | 91:637625a2acdb |
---|---|
135 } | 135 } |
136 | 136 |
137 | 137 |
138 static void ngx_http_init_request(ngx_event_t *rev) | 138 static void ngx_http_init_request(ngx_event_t *rev) |
139 { | 139 { |
140 ngx_connection_t *c; | 140 ngx_connection_t *c; |
141 ngx_http_request_t *r; | 141 ngx_http_request_t *r; |
142 ngx_http_conf_ctx_t *ctx; | 142 ngx_http_conf_ctx_t *ctx; |
143 | 143 ngx_http_core_main_conf_t *cmcf; |
144 c = (ngx_connection_t *) rev->data; | 144 |
145 c = rev->data; | |
146 ctx = c->ctx; | |
147 | |
148 cmcf = ngx_http_get_module_main_conf(ctx, ngx_http_core_module_ctx); | |
145 | 149 |
146 if (c->buffer == NULL) { | 150 if (c->buffer == NULL) { |
147 c->buffer = ngx_create_temp_hunk(c->pool, | 151 c->buffer = ngx_create_temp_hunk(c->pool, |
148 ngx_http_client_header_buffer_size, | 152 cmcf->client_header_buffer_size, |
149 0, 0); | 153 0, 0); |
150 if (c->buffer == NULL) { | 154 if (c->buffer == NULL) { |
151 ngx_http_close_connection(c); | 155 ngx_http_close_connection(c); |
152 return; | 156 return; |
153 } | 157 } |
157 if (r == NULL) { | 161 if (r == NULL) { |
158 ngx_http_close_connection(c); | 162 ngx_http_close_connection(c); |
159 return; | 163 return; |
160 } | 164 } |
161 | 165 |
162 r->pool = ngx_create_pool(ngx_http_request_pool_size, c->log); | 166 r->pool = ngx_create_pool(cmcf->request_pool_size, c->log); |
163 if (r->pool == NULL) { | 167 if (r->pool == NULL) { |
164 ngx_http_close_connection(c); | 168 ngx_http_close_connection(c); |
165 return; | 169 return; |
166 } | 170 } |
167 | 171 |
177 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 181 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
178 ngx_http_close_connection(c); | 182 ngx_http_close_connection(c); |
179 return; | 183 return; |
180 } | 184 } |
181 | 185 |
182 ctx = (ngx_http_conf_ctx_t *) c->ctx; | 186 r->main_conf = ctx->main_conf; |
183 r->srv_conf = ctx->srv_conf; | 187 r->srv_conf = ctx->srv_conf; |
184 r->loc_conf = ctx->loc_conf; | 188 r->loc_conf = ctx->loc_conf; |
185 | 189 |
186 c->sent = 0; | 190 c->sent = 0; |
187 c->data = r; | 191 c->data = r; |
200 } | 204 } |
201 | 205 |
202 | 206 |
203 static void ngx_http_process_request_line(ngx_event_t *rev) | 207 static void ngx_http_process_request_line(ngx_event_t *rev) |
204 { | 208 { |
205 int rc, offset; | 209 int rc, offset; |
206 ssize_t n; | 210 ssize_t n; |
207 ngx_connection_t *c; | 211 ngx_connection_t *c; |
208 ngx_http_request_t *r; | 212 ngx_http_request_t *r; |
209 ngx_http_log_ctx_t *lcx; | 213 ngx_http_log_ctx_t *lcx; |
210 | 214 ngx_http_core_main_conf_t *cmcf; |
211 c = (ngx_connection_t *) rev->data; | 215 |
212 r = (ngx_http_request_t *) c->data; | 216 c = rev->data; |
217 r = c->data; | |
213 | 218 |
214 ngx_log_debug(rev->log, "http process request line"); | 219 ngx_log_debug(rev->log, "http process request line"); |
215 | 220 |
216 if (rev->timedout) { | 221 if (rev->timedout) { |
217 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); | 222 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
229 | 234 |
230 if (rc == NGX_OK) { | 235 if (rc == NGX_OK) { |
231 | 236 |
232 /* the request line has been parsed successfully */ | 237 /* the request line has been parsed successfully */ |
233 | 238 |
239 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); | |
240 | |
234 if (r->http_version >= NGX_HTTP_VERSION_10 | 241 if (r->http_version >= NGX_HTTP_VERSION_10 |
235 && ngx_http_large_client_header == 0 | 242 && cmcf->large_client_header == 0 |
236 && r->header_in->pos == r->header_in->end) | 243 && r->header_in->pos == r->header_in->end) |
237 { | 244 { |
238 /* no space for "\r\n" at the end of the header */ | 245 /* no space for "\r\n" at the end of the header */ |
239 | 246 |
240 ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI); | 247 ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI); |
257 return; | 264 return; |
258 } | 265 } |
259 | 266 |
260 ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1); | 267 ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1); |
261 | 268 |
269 #if 1 /* needed to log url on errors in proxy only ? */ | |
270 | |
271 /* copy unparsed URI */ | |
272 | |
273 r->unparsed_uri.len = r->uri_end - r->uri_start; | |
274 r->unparsed_uri.data = ngx_palloc(r->pool, r->unparsed_uri.len + 1); | |
275 if (r->unparsed_uri.data == NULL) { | |
276 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
277 ngx_http_close_connection(c); | |
278 return; | |
279 } | |
280 | |
281 ngx_cpystrn(r->unparsed_uri.data, r->uri_start, | |
282 r->unparsed_uri.len + 1); | |
283 | |
284 #endif | |
285 | |
262 r->request_line.len = r->request_end - r->request_start; | 286 r->request_line.len = r->request_end - r->request_start; |
263 | 287 |
264 /* if the large client headers are enabled then | 288 /* if the large client headers are enabled then |
265 we need to copy a request line */ | 289 we need to copy a request line */ |
266 | 290 |
267 if (ngx_http_large_client_header) { | 291 if (cmcf->large_client_header) { |
268 | 292 |
269 r->request_line.data = ngx_palloc(r->pool, r->request_line.len + 1); | 293 r->request_line.data = ngx_palloc(r->pool, r->request_line.len + 1); |
270 if (r->request_line.data == NULL) { | 294 if (r->request_line.data == NULL) { |
271 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 295 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
272 ngx_http_close_connection(c); | 296 ngx_http_close_connection(c); |
323 r->uri.data _ r->exten.data _ r->args.data); | 347 r->uri.data _ r->exten.data _ r->args.data); |
324 if (r->exten.data[0] == '\0') { r->exten.data = NULL; } | 348 if (r->exten.data[0] == '\0') { r->exten.data = NULL; } |
325 if (r->args.data[0] == '\0') { r->args.data = NULL; } | 349 if (r->args.data[0] == '\0') { r->args.data = NULL; } |
326 #endif | 350 #endif |
327 | 351 |
328 lcx = c->log->data; | |
329 | |
330 if (ngx_http_url_in_error_log) { | |
331 lcx->url = ngx_palloc(r->pool, r->uri_end - r->uri_start + 1); | |
332 if (lcx->url == NULL) { | |
333 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
334 ngx_http_close_connection(c); | |
335 return; | |
336 } | |
337 | |
338 ngx_cpystrn(lcx->url, r->uri_start, r->uri_end - r->uri_start + 1); | |
339 } | |
340 | |
341 if (r->http_version == NGX_HTTP_VERSION_9) { | 352 if (r->http_version == NGX_HTTP_VERSION_9) { |
342 if (ngx_http_find_server_conf(r) == NGX_ERROR) { | 353 if (ngx_http_find_server_conf(r) == NGX_ERROR) { |
343 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 354 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
344 ngx_http_close_connection(c); | 355 ngx_http_close_connection(c); |
345 return; | 356 return; |
348 rev->event_handler = ngx_http_block_read; | 359 rev->event_handler = ngx_http_block_read; |
349 ngx_http_handler(r); | 360 ngx_http_handler(r); |
350 return; | 361 return; |
351 } | 362 } |
352 | 363 |
364 lcx = c->log->data; | |
353 lcx->action = "reading client request headers"; | 365 lcx->action = "reading client request headers"; |
366 lcx->url = r->unparsed_uri.data; | |
354 r->headers_in.headers = ngx_create_table(r->pool, 10); | 367 r->headers_in.headers = ngx_create_table(r->pool, 10); |
355 | 368 |
356 if (ngx_http_large_client_header | 369 if (cmcf->large_client_header |
357 && r->header_in->pos == r->header_in->last) | 370 && r->header_in->pos == r->header_in->last) |
358 { | 371 { |
359 r->header_in->pos = r->header_in->last = r->header_in->start; | 372 r->header_in->pos = r->header_in->last = r->header_in->start; |
360 } | 373 } |
361 | 374 |
382 then we need to copy it to the start of the r->header_in hunk. | 395 then we need to copy it to the start of the r->header_in hunk. |
383 We need to copy it here only if the large client headers | 396 We need to copy it here only if the large client headers |
384 are enabled otherwise a request line had been already copied | 397 are enabled otherwise a request line had been already copied |
385 to the start of the r->header_in hunk in ngx_http_set_keepalive() */ | 398 to the start of the r->header_in hunk in ngx_http_set_keepalive() */ |
386 | 399 |
387 if (ngx_http_large_client_header) { | 400 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); |
401 | |
402 if (cmcf->large_client_header) { | |
388 offset = r->request_start - r->header_in->start; | 403 offset = r->request_start - r->header_in->start; |
389 | 404 |
390 if (offset == 0) { | 405 if (offset == 0) { |
391 ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI); | 406 ngx_http_header_parse_error(r, NGX_HTTP_PARSE_TOO_LONG_URI); |
392 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE); | 407 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE); |
420 } | 435 } |
421 | 436 |
422 | 437 |
423 static void ngx_http_process_request_headers(ngx_event_t *rev) | 438 static void ngx_http_process_request_headers(ngx_event_t *rev) |
424 { | 439 { |
425 int rc, i, offset; | 440 int rc, i, offset; |
426 size_t len; | 441 size_t len; |
427 ssize_t n; | 442 ssize_t n; |
428 ngx_table_elt_t *h; | 443 ngx_table_elt_t *h; |
429 ngx_connection_t *c; | 444 ngx_connection_t *c; |
430 ngx_http_request_t *r; | 445 ngx_http_request_t *r; |
431 ngx_http_log_ctx_t *ctx; | 446 ngx_http_log_ctx_t *ctx; |
432 | 447 ngx_http_core_main_conf_t *cmcf; |
433 c = (ngx_connection_t *) rev->data; | 448 |
434 r = (ngx_http_request_t *) c->data; | 449 c = rev->data; |
450 r = c->data; | |
435 | 451 |
436 ngx_log_debug(rev->log, "http process request header line"); | 452 ngx_log_debug(rev->log, "http process request header line"); |
437 | 453 |
438 if (rev->timedout) { | 454 if (rev->timedout) { |
439 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); | 455 ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
469 h->value.len = r->header_end - r->header_start; | 485 h->value.len = r->header_end - r->header_start; |
470 | 486 |
471 /* if the large client headers are enabled then | 487 /* if the large client headers are enabled then |
472 we need to copy the header name and value */ | 488 we need to copy the header name and value */ |
473 | 489 |
474 if (ngx_http_large_client_header) { | 490 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); |
491 | |
492 if (cmcf->large_client_header) { | |
475 h->key.data = ngx_palloc(r->pool, | 493 h->key.data = ngx_palloc(r->pool, |
476 h->key.len + 1 + h->value.len + 1); | 494 h->key.len + 1 + h->value.len + 1); |
477 if (h->key.data == NULL) { | 495 if (h->key.data == NULL) { |
478 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | 496 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
479 ngx_http_close_connection(c); | 497 ngx_http_close_connection(c); |
503 } | 521 } |
504 | 522 |
505 ngx_log_debug(r->connection->log, "HTTP header: '%s: %s'" _ | 523 ngx_log_debug(r->connection->log, "HTTP header: '%s: %s'" _ |
506 h->key.data _ h->value.data); | 524 h->key.data _ h->value.data); |
507 | 525 |
508 if (ngx_http_large_client_header | 526 if (cmcf->large_client_header |
509 && r->header_in->pos == r->header_in->last) | 527 && r->header_in->pos == r->header_in->last) |
510 { | 528 { |
511 r->header_in->pos = r->header_in->last = r->header_in->start; | 529 r->header_in->pos = r->header_in->last = r->header_in->start; |
512 } | 530 } |
513 | 531 |
572 if (r->header_in->last == r->header_in->end) { | 590 if (r->header_in->last == r->header_in->end) { |
573 | 591 |
574 /* if the large client headers are enabled then | 592 /* if the large client headers are enabled then |
575 we need to compact r->header_in hunk */ | 593 we need to compact r->header_in hunk */ |
576 | 594 |
577 if (ngx_http_large_client_header) { | 595 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); |
596 | |
597 if (cmcf->large_client_header) { | |
578 offset = r->header_name_start - r->header_in->start; | 598 offset = r->header_name_start - r->header_in->start; |
579 | 599 |
580 if (offset == 0) { | 600 if (offset == 0) { |
581 ngx_http_header_parse_error(r, | 601 ngx_http_header_parse_error(r, |
582 NGX_HTTP_PARSE_TOO_LONG_HEADER); | 602 NGX_HTTP_PARSE_TOO_LONG_HEADER); |
604 } | 624 } |
605 | 625 |
606 | 626 |
607 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r) | 627 static ssize_t ngx_http_read_request_header(ngx_http_request_t *r) |
608 { | 628 { |
609 int event; | 629 int event; |
610 ssize_t n; | 630 ssize_t n; |
611 ngx_event_t *rev; | 631 ngx_event_t *rev; |
632 ngx_http_core_main_conf_t *cmcf; | |
612 | 633 |
613 n = r->header_in->last - r->header_in->pos; | 634 n = r->header_in->last - r->header_in->pos; |
614 | 635 |
615 if (n > 0) { | 636 if (n > 0) { |
616 return n; | 637 return n; |
627 ngx_del_timer(rev); | 648 ngx_del_timer(rev); |
628 } else { | 649 } else { |
629 rev->timer_set = 1; | 650 rev->timer_set = 1; |
630 } | 651 } |
631 | 652 |
632 ngx_add_timer(rev, ngx_http_client_header_timeout); | 653 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); |
654 | |
655 ngx_add_timer(rev, cmcf->client_header_timeout); | |
633 r->header_timeout_set = 1; | 656 r->header_timeout_set = 1; |
634 } | 657 } |
635 | 658 |
636 if (!rev->active) { | 659 if (!rev->active) { |
637 if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { | 660 if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { |
722 | 745 |
723 void ngx_http_set_write_handler(ngx_http_request_t *r) | 746 void ngx_http_set_write_handler(ngx_http_request_t *r) |
724 { | 747 { |
725 int event; | 748 int event; |
726 ngx_event_t *wev; | 749 ngx_event_t *wev; |
727 ngx_http_core_loc_conf_t *lcf; | 750 ngx_http_core_loc_conf_t *clcf; |
728 | 751 |
729 wev = r->connection->write; | 752 wev = r->connection->write; |
730 wev->event_handler = ngx_http_writer; | 753 wev->event_handler = ngx_http_writer; |
731 | 754 |
732 if (wev->delayed && wev->ready) { | 755 if (wev->delayed && wev->ready) { |
733 return; | 756 return; |
734 } | 757 } |
735 | 758 |
736 lcf = (ngx_http_core_loc_conf_t *) | 759 clcf = (ngx_http_core_loc_conf_t *) |
737 ngx_http_get_module_loc_conf(r->main ? r->main : r, | 760 ngx_http_get_module_loc_conf(r->main ? r->main : r, |
738 ngx_http_core_module_ctx); | 761 ngx_http_core_module_ctx); |
739 ngx_add_timer(wev, lcf->send_timeout); | 762 ngx_add_timer(wev, clcf->send_timeout); |
740 wev->timer_set = 1; | 763 wev->timer_set = 1; |
741 | 764 |
742 if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) { | 765 if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) { |
743 /* aio, iocp, epoll */ | 766 /* aio, iocp, epoll */ |
744 return; | 767 return; |
745 } | 768 } |
746 | 769 |
747 #if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */ | 770 #if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */ |
748 | 771 |
749 if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) { | 772 if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) { |
750 wev->lowat = lcf->send_lowat; | 773 wev->lowat = clcf->send_lowat; |
751 } | 774 } |
752 | 775 |
753 #endif | 776 #endif |
754 | 777 |
755 if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { | 778 if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { |
821 wev->timer_set = 0; | 844 wev->timer_set = 0; |
822 } | 845 } |
823 | 846 |
824 if (r->keepalive != 0) { | 847 if (r->keepalive != 0) { |
825 ngx_http_set_keepalive(r); | 848 ngx_http_set_keepalive(r); |
826 } | 849 |
827 | 850 } else if (r->lingering_close) { |
828 if (r->lingering_close) { | |
829 ngx_http_set_lingering_close(r); | 851 ngx_http_set_lingering_close(r); |
830 | 852 |
831 } else { | 853 } else { |
832 ngx_http_close_request(r, 0); | 854 ngx_http_close_request(r, 0); |
833 ngx_http_close_connection(r->connection); | 855 ngx_http_close_connection(r->connection); |
968 } | 990 } |
969 | 991 |
970 | 992 |
971 static void ngx_http_set_keepalive(ngx_http_request_t *r) | 993 static void ngx_http_set_keepalive(ngx_http_request_t *r) |
972 { | 994 { |
973 int len, blocked; | 995 int len, blocked; |
974 ngx_hunk_t *h; | 996 ngx_hunk_t *h; |
975 ngx_event_t *rev, *wev; | 997 ngx_event_t *rev, *wev; |
976 ngx_connection_t *c; | 998 ngx_connection_t *c; |
977 ngx_http_log_ctx_t *ctx; | 999 ngx_http_log_ctx_t *ctx; |
1000 ngx_http_core_main_conf_t *cmcf; | |
978 | 1001 |
979 c = (ngx_connection_t *) r->connection; | 1002 c = (ngx_connection_t *) r->connection; |
980 rev = c->read; | 1003 rev = c->read; |
981 | 1004 |
982 ctx = (ngx_http_log_ctx_t *) c->log->data; | 1005 ctx = (ngx_http_log_ctx_t *) c->log->data; |
1005 so if the large client headers are not enabled | 1028 so if the large client headers are not enabled |
1006 we need to copy the data to the start of c->buffer. | 1029 we need to copy the data to the start of c->buffer. |
1007 This copy should be rare because clients that support | 1030 This copy should be rare because clients that support |
1008 pipelined requests (Mozilla 1.x, Opera 6.x) are still rare */ | 1031 pipelined requests (Mozilla 1.x, Opera 6.x) are still rare */ |
1009 | 1032 |
1010 if (!ngx_http_large_client_header) { | 1033 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module_ctx); |
1034 | |
1035 if (!cmcf->large_client_header) { | |
1011 len = h->last - h->pos; | 1036 len = h->last - h->pos; |
1012 ngx_memcpy(h->start, h->pos, len); | 1037 ngx_memcpy(h->start, h->pos, len); |
1013 h->pos = h->start; | 1038 h->pos = h->start; |
1014 h->last = h->start + len; | 1039 h->last = h->start + len; |
1015 } | 1040 } |