Klaus Demo nginx / 236e045
nginx-0.0.12-2004-09-23-20:39:34 import Igor Sysoev 17 years ago
7 changed file(s) with 184 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
11 #define _NGINX_H_INCLUDED_
22
33
4 #define NGINX_VER "nginx/0.0.11"
4 #define NGINX_VER "nginx/0.0.12"
55
66 #define NGINX_VAR "NGINX"
77 #define NGX_NEWPID_EXT ".newbin"
3636 } ngx_http_rewrite_srv_conf_t;
3737
3838
39 typedef struct {
40 ngx_str_t redirect;
41 } ngx_http_rewrite_loc_conf_t;
42
43
3944 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf);
4045 static char *ngx_http_rewrite_merge_srv_conf(ngx_conf_t *cf,
4146 void *parent, void *child);
47 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
4248 static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd,
4349 void *conf);
50 static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data);
4451 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle);
52
53
54 static ngx_conf_post_handler_pt ngx_http_redirect_p = ngx_http_redirect;
4555
4656
4757 static ngx_command_t ngx_http_rewrite_commands[] = {
5363 0,
5464 NULL },
5565
66 { ngx_string("redirect"),
67 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
68 ngx_conf_set_str_slot,
69 NGX_HTTP_LOC_CONF_OFFSET,
70 0,
71 &ngx_http_redirect_p },
72
5673 { ngx_string("rewrite_log"),
5774 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
5875 ngx_conf_set_flag_slot,
7390 ngx_http_rewrite_create_srv_conf, /* create server configuration */
7491 ngx_http_rewrite_merge_srv_conf, /* merge server configuration */
7592
76 NULL, /* create location configration */
93 ngx_http_rewrite_create_loc_conf, /* create location configration */
7794 NULL, /* merge location configration */
7895 };
7996
202219 }
203220
204221
222 static ngx_int_t ngx_http_redirect_handler(ngx_http_request_t *r)
223 {
224 u_char *p;
225 ngx_http_rewrite_loc_conf_t *rlcf;
226
227 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
228 "http redirect handler");
229
230 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
231
232 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
233 if (r->headers_out.location == NULL) {
234 return NGX_HTTP_INTERNAL_SERVER_ERROR;
235 }
236
237 if (rlcf->redirect.data[0] != '/') {
238 r->headers_out.location->key.len = sizeof("Location") - 1;
239 r->headers_out.location->key.data = (u_char *) "Location";
240 }
241
242 r->headers_out.location->value.len = rlcf->redirect.len
243 + r->unparsed_uri.len;
244 r->headers_out.location->value.data = ngx_palloc(r->pool,
245 r->headers_out.location->value.len);
246
247 if (r->headers_out.location->value.data == NULL) {
248 return NGX_HTTP_INTERNAL_SERVER_ERROR;
249 }
250
251 p = ngx_cpymem(r->headers_out.location->value.data, rlcf->redirect.data,
252 rlcf->redirect.len);
253 p = ngx_cpystrn(p, r->unparsed_uri.data + 1, r->unparsed_uri.len);
254
255 return NGX_HTTP_MOVED_TEMPORARILY;
256 }
257
258
205259 static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf)
206260 {
207261 ngx_http_rewrite_srv_conf_t *conf;
228282 ngx_conf_merge_value(conf->log, prev->log, 0);
229283
230284 return NGX_CONF_OK;
285 }
286
287
288 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
289 {
290 ngx_http_rewrite_loc_conf_t *conf;
291
292 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)))) {
293 return NGX_CONF_ERROR;
294 }
295
296 return conf;
231297 }
232298
233299
365431 }
366432
367433
434 static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data)
435 {
436 ngx_http_core_loc_conf_t *clcf;
437
438 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
439 clcf->handler = ngx_http_redirect_handler;
440
441 return NGX_CONF_OK;
442 }
443
444
368445 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle)
369446 {
370447 ngx_http_handler_pt *h;
400400
401401 if (!p->cachable && p->upstream->peer.connection) {
402402 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
403 "kevent() reported that client have closed "
403 "kevent() reported that client closed "
404404 "prematurely connection, "
405405 "so upstream connection is closed too");
406406 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
408408 }
409409
410410 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
411 "kevent() reported that client have closed "
411 "kevent() reported that client closed "
412412 "prematurely connection");
413413
414414 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
463463
464464 if (!p->cachable && p->upstream->peer.connection) {
465465 ngx_log_error(NGX_LOG_INFO, ev->log, err,
466 "client have closed prematurely connection, "
466 "client closed prematurely connection, "
467467 "so upstream connection is closed too");
468468 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
469469 return;
470470 }
471471
472472 ngx_log_error(NGX_LOG_INFO, ev->log, err,
473 "client have closed prematurely connection");
473 "client closed prematurely connection");
474474
475475 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
476476 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
233233 0,
234234 NULL },
235235
236 { ngx_string("keepalive_buffers"),
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
238 ngx_conf_set_flag_slot,
239 NGX_HTTP_LOC_CONF_OFFSET,
240 offsetof(ngx_http_core_loc_conf_t, keepalive_buffers),
241 NULL },
242
243236 { ngx_string("lingering_time"),
244237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
245238 ngx_conf_set_msec_slot,
13941387 lcf->limit_rate = NGX_CONF_UNSET_SIZE;
13951388 lcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
13961389 lcf->keepalive_header = NGX_CONF_UNSET;
1397 lcf->keepalive_buffers = NGX_CONF_UNSET;
13981390 lcf->lingering_time = NGX_CONF_UNSET_MSEC;
13991391 lcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
14001392 lcf->reset_timedout_connection = NGX_CONF_UNSET;
14831475 prev->keepalive_timeout, 75000);
14841476 ngx_conf_merge_sec_value(conf->keepalive_header,
14851477 prev->keepalive_header, 0);
1486 ngx_conf_merge_value(conf->keepalive_buffers, prev->keepalive_buffers, 1);
14871478 ngx_conf_merge_msec_value(conf->lingering_time,
14881479 prev->lingering_time, 30000);
14891480 ngx_conf_merge_msec_value(conf->lingering_timeout,
15441535 }
15451536
15461537 port = ngx_atoi(&addr[p], args[1].len - p);
1538
15471539 if (port == NGX_ERROR && p == 0) {
15481540
15491541 /* "listen host" */
15631555 ls->addr = INADDR_ANY;
15641556 ls->port = (in_port_t) port;
15651557 return NGX_CONF_OK;
1566 }
1567
1568 ls->port = (in_port_t) port;
1558
1559 } else {
1560 ls->port = (in_port_t) port;
1561 }
15691562
15701563 ls->addr = inet_addr((const char *) addr);
15711564 if (ls->addr == INADDR_NONE) {
163163
164164 time_t keepalive_header; /* keepalive_timeout */
165165
166 ngx_flag_t keepalive_buffers; /* keepalive_buffers */
167166 ngx_flag_t sendfile; /* sendfile */
168167 ngx_flag_t tcp_nopush; /* tcp_nopush */
169168 ngx_flag_t reset_timedout_connection; /* reset_timedout_connection */
196196
197197 hc = c->data;
198198
199 if (hc == NULL) {
199 if (hc) {
200
201 #if (NGX_STAT_STUB)
202 (*ngx_stat_reading)++;
203 #endif
204
205 } else {
200206 if (!(hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t)))) {
201207
202208 #if (NGX_STAT_STUB)
218224 if (hc->nbusy) {
219225 r->header_in = hc->busy[0];
220226 }
221
222 #if (NGX_STAT_STUB)
223 (*ngx_stat_reading)++;
224 #endif
225227
226228 } else {
227229 if (!(r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)))) {
682684
683685 /* NGX_AGAIN: a request line parsing is still incomplete */
684686
685 if (r->header_in->last == r->header_in->end) {
687 if (r->header_in->pos == r->header_in->end) {
686688
687689 rv = ngx_http_alloc_large_header_buffer(r, 1);
688690
727729
728730 if (rc == NGX_AGAIN) {
729731
730 if (r->header_in->last == r->header_in->end) {
732 if (r->header_in->pos == r->header_in->end) {
731733
732734 rv = ngx_http_alloc_large_header_buffer(r, 0);
733735
970972 if (hc->nfree) {
971973 b = hc->free[--hc->nfree];
972974
975 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
976 "http large header free: " PTR_FMT " " SIZE_T_FMT,
977 b->pos, b->end - b->last);
978
973979 } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
974980
975981 if (hc->busy == NULL) {
985991 if (b == NULL) {
986992 return NGX_ERROR;
987993 }
994
995 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
996 "http large header alloc: " PTR_FMT " " SIZE_T_FMT,
997 b->pos, b->end - b->last);
988998
989999 } else {
9901000 return NGX_DECLINED;
10041014 return NGX_OK;
10051015 }
10061016
1017 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1018 "http large header copy: %d", r->header_in->pos - old);
1019
10071020 new = b->start;
10081021
1009 ngx_memcpy(new, old, r->header_in->last - old);
1022 ngx_memcpy(new, old, r->header_in->pos - old);
10101023
10111024 b->pos = new + (r->header_in->pos - old);
1012 b->last = new + (r->header_in->last - old);
1025 b->last = new + (r->header_in->pos - old);
10131026
10141027 if (request_line) {
10151028 r->request_start = new;
15511564 hc = r->http_connection;
15521565 b = r->header_in;
15531566
1554 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1555
1556 if (b->pos < b->last || clcf->keepalive_buffers) {
1557
1558 /*
1559 * the pipelined request or we like to keep the allocated
1560 * ngx_http_request_t and the client header buffers while keepalive
1561 */
1567 if (b->pos < b->last) {
1568
1569 /* the pipelined request */
15621570
15631571 if (b != c->buffer) {
15641572
15691577 if (hc->free == NULL) {
15701578 hc->free = ngx_palloc(c->pool,
15711579 cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
1580
15721581 if (hc->free == NULL) {
15731582 ngx_http_close_connection(c);
15741583 return;
15901599 ngx_http_close_request(r, 0);
15911600 c->data = hc;
15921601
1602 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
15931603 ngx_add_timer(rev, clcf->keepalive_timeout);
15941604
15951605 if (ngx_handle_level_read_event(rev) == NGX_ERROR) {
16011611 wev->event_handler = ngx_http_empty_handler;
16021612
16031613 if (b->pos < b->last) {
1604
1605 /* the pipelined request */
16061614
16071615 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
16081616
16141622
16151623 hc->pipeline = 0;
16161624
1617 b->pos = b->last = b->start;
1618
1619 if (!clcf->keepalive_buffers) {
1620
1621 if (ngx_pfree(c->pool, r) == NGX_OK) {
1622 hc->request = NULL;
1623 }
1624
1625 if (ngx_pfree(c->pool, c->buffer->start) == NGX_OK) {
1626 c->buffer = NULL;
1627 }
1628
1629 if (hc->free) {
1630 for (i = 0; i < hc->nfree; i++) {
1631 ngx_pfree(c->pool, hc->free[i]);
1632 hc->free[i] = NULL;
1633 }
1634
1635 hc->nfree = 0;
1636 }
1637
1638 if (hc->busy) {
1639 for (i = 0; i < hc->nbusy; i++) {
1640 ngx_pfree(c->pool, hc->busy[i]);
1641 hc->busy[i] = NULL;
1642 }
1643
1644 hc->nbusy = 0;
1645 }
1625 if (ngx_pfree(c->pool, r) == NGX_OK) {
1626 hc->request = NULL;
1627 }
1628
1629 b = c->buffer;
1630
1631 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
1632 b->pos = NULL;
1633
1634 } else {
1635 b->pos = b->start;
1636 b->last = b->start;
1637 }
1638
1639 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: " PTR_FMT " %d",
1640 hc->free, hc->nfree);
1641
1642 if (hc->free) {
1643 for (i = 0; i < hc->nfree; i++) {
1644 ngx_pfree(c->pool, hc->free[i]);
1645 hc->free[i] = NULL;
1646 }
1647
1648 hc->nfree = 0;
1649 }
1650
1651 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: " PTR_FMT " %d",
1652 hc->busy, hc->nbusy);
1653
1654 if (hc->busy) {
1655 for (i = 0; i < hc->nbusy; i++) {
1656 ngx_pfree(c->pool, hc->busy[i]);
1657 hc->busy[i] = NULL;
1658 }
1659
1660 hc->nbusy = 0;
16461661 }
16471662
16481663 rev->event_handler = ngx_http_keepalive_handler;
16881703
16891704 static void ngx_http_keepalive_handler(ngx_event_t *rev)
16901705 {
1706 size_t size;
16911707 ssize_t n;
16921708 ngx_buf_t *b;
16931709 ngx_connection_t *c;
17031719 return;
17041720 }
17051721
1722 ctx = (ngx_http_log_ctx_t *) rev->log->data;
1723
1724 #if (HAVE_KQUEUE)
1725
1726 if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) {
1727 if (rev->pending_eof) {
1728 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
1729 "kevent() reported that client %s closed "
1730 "keepalive connection", ctx->client);
1731 ngx_http_close_connection(c);
1732 return;
1733 }
1734 }
1735
1736 #endif
1737
17061738 hc = c->data;
1707 b = hc->nbusy ? hc->busy[0] : c->buffer;
1739 b = c->buffer;
1740 size = b->end - b->start;
1741
1742 if (b->pos == NULL) {
1743 if (!(b->pos = ngx_palloc(c->pool, size))) {
1744 ngx_http_close_connection(c);
1745 return;
1746 }
1747
1748 b->start = b->pos;
1749 b->last = b->pos;
1750 b->end = b->pos + size;
1751 }
17081752
17091753 /*
17101754 * MSIE closes a keepalive connection with RST flag
17141758 c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
17151759 ngx_set_socket_errno(0);
17161760
1717 n = c->recv(c, b->last, b->end - b->last);
1761 n = c->recv(c, b->last, size);
17181762 c->log_error = NGX_ERROR_INFO;
17191763
17201764 if (n == NGX_AGAIN) {
17261770 return;
17271771 }
17281772
1729 ctx = (ngx_http_log_ctx_t *) rev->log->data;
17301773 rev->log->handler = NULL;
17311774
17321775 if (n == 0) {
6565 #if (NGX_DEBUG && !NGX_NO_DEBUG_MALLOC)
6666
6767 #if __FreeBSD_version >= 500014
68 _malloc_options = "JAV";
68 _malloc_options = "J";
6969 #else
70 malloc_options = "JAV";
70 malloc_options = "J";
7171 #endif
7272
7373 #endif