diff options
author | Igor Sysoev <igor@sysoev.ru> | 2004-09-23 20:39:34 +0400 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2004-09-23 20:39:34 +0400 |
commit | 236e0457517d901278cc7938f324298ddc49d24a (patch) | |
tree | cfd4023727a3dd832f83dbe9d4cc7eeb4d343fb3 /src/http | |
parent | f7abd72716bf9499581a010d7c2bf21da7987ba9 (diff) |
nginx-0.0.12-2004-09-23-20:39:34 import
Diffstat (limited to 'src/http')
-rw-r--r-- | src/http/modules/ngx_http_rewrite_handler.c | 79 | ||||
-rw-r--r-- | src/http/modules/proxy/ngx_http_proxy_handler.c | 8 | ||||
-rw-r--r-- | src/http/ngx_http_core_module.c | 15 | ||||
-rw-r--r-- | src/http/ngx_http_core_module.h | 1 | ||||
-rw-r--r-- | src/http/ngx_http_request.c | 125 |
5 files changed, 170 insertions, 58 deletions
diff --git a/src/http/modules/ngx_http_rewrite_handler.c b/src/http/modules/ngx_http_rewrite_handler.c index 5c25c89e4..6f00db7e8 100644 --- a/src/http/modules/ngx_http_rewrite_handler.c +++ b/src/http/modules/ngx_http_rewrite_handler.c @@ -37,14 +37,24 @@ typedef struct { } ngx_http_rewrite_srv_conf_t; +typedef struct { + ngx_str_t redirect; +} ngx_http_rewrite_loc_conf_t; + + static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf); static char *ngx_http_rewrite_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child); +static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data); static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle); +static ngx_conf_post_handler_pt ngx_http_redirect_p = ngx_http_redirect; + + static ngx_command_t ngx_http_rewrite_commands[] = { { ngx_string("rewrite"), @@ -54,6 +64,13 @@ static ngx_command_t ngx_http_rewrite_commands[] = { 0, NULL }, + { ngx_string("redirect"), + NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, + ngx_conf_set_str_slot, + NGX_HTTP_LOC_CONF_OFFSET, + 0, + &ngx_http_redirect_p }, + { ngx_string("rewrite_log"), NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, ngx_conf_set_flag_slot, @@ -74,7 +91,7 @@ ngx_http_module_t ngx_http_rewrite_module_ctx = { ngx_http_rewrite_create_srv_conf, /* create server configuration */ ngx_http_rewrite_merge_srv_conf, /* merge server configuration */ - NULL, /* create location configration */ + ngx_http_rewrite_create_loc_conf, /* create location configration */ NULL, /* merge location configration */ }; @@ -203,6 +220,43 @@ static ngx_int_t ngx_http_rewrite_handler(ngx_http_request_t *r) } +static ngx_int_t ngx_http_redirect_handler(ngx_http_request_t *r) +{ + u_char *p; + ngx_http_rewrite_loc_conf_t *rlcf; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "http redirect handler"); + + rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); + + r->headers_out.location = ngx_list_push(&r->headers_out.headers); + if (r->headers_out.location == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } + + if (rlcf->redirect.data[0] != '/') { + r->headers_out.location->key.len = sizeof("Location") - 1; + r->headers_out.location->key.data = (u_char *) "Location"; + } + + r->headers_out.location->value.len = rlcf->redirect.len + + r->unparsed_uri.len; + r->headers_out.location->value.data = ngx_palloc(r->pool, + r->headers_out.location->value.len); + + if (r->headers_out.location->value.data == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } + + p = ngx_cpymem(r->headers_out.location->value.data, rlcf->redirect.data, + rlcf->redirect.len); + p = ngx_cpystrn(p, r->unparsed_uri.data + 1, r->unparsed_uri.len); + + return NGX_HTTP_MOVED_TEMPORARILY; +} + + static void *ngx_http_rewrite_create_srv_conf(ngx_conf_t *cf) { ngx_http_rewrite_srv_conf_t *conf; @@ -232,6 +286,18 @@ static char *ngx_http_rewrite_merge_srv_conf(ngx_conf_t *cf, } +static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf) +{ + ngx_http_rewrite_loc_conf_t *conf; + + if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)))) { + return NGX_CONF_ERROR; + } + + return conf; +} + + static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { @@ -366,6 +432,17 @@ static char *ngx_http_rewrite_rule(ngx_conf_t *cf, ngx_command_t *cmd, } +static char *ngx_http_redirect(ngx_conf_t *cf, void *post, void *data) +{ + ngx_http_core_loc_conf_t *clcf; + + clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); + clcf->handler = ngx_http_redirect_handler; + + return NGX_CONF_OK; +} + + static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle) { ngx_http_handler_pt *h; diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c index f057d3c98..af5e54636 100644 --- a/src/http/modules/proxy/ngx_http_proxy_handler.c +++ b/src/http/modules/proxy/ngx_http_proxy_handler.c @@ -401,7 +401,7 @@ void ngx_http_proxy_check_broken_connection(ngx_event_t *ev) if (!p->cachable && p->upstream->peer.connection) { ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, - "kevent() reported that client have closed " + "kevent() reported that client closed " "prematurely connection, " "so upstream connection is closed too"); ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST); @@ -409,7 +409,7 @@ void ngx_http_proxy_check_broken_connection(ngx_event_t *ev) } ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno, - "kevent() reported that client have closed " + "kevent() reported that client closed " "prematurely connection"); if (p->upstream == NULL || p->upstream->peer.connection == NULL) { @@ -464,14 +464,14 @@ void ngx_http_proxy_check_broken_connection(ngx_event_t *ev) if (!p->cachable && p->upstream->peer.connection) { ngx_log_error(NGX_LOG_INFO, ev->log, err, - "client have closed prematurely connection, " + "client closed prematurely connection, " "so upstream connection is closed too"); ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST); return; } ngx_log_error(NGX_LOG_INFO, ev->log, err, - "client have closed prematurely connection"); + "client closed prematurely connection"); if (p->upstream == NULL || p->upstream->peer.connection == NULL) { ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST); diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c index 9f29c5706..58fa59d1e 100644 --- a/src/http/ngx_http_core_module.c +++ b/src/http/ngx_http_core_module.c @@ -234,13 +234,6 @@ static ngx_command_t ngx_http_core_commands[] = { 0, NULL }, - { ngx_string("keepalive_buffers"), - NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, - ngx_conf_set_flag_slot, - NGX_HTTP_LOC_CONF_OFFSET, - offsetof(ngx_http_core_loc_conf_t, keepalive_buffers), - NULL }, - { ngx_string("lingering_time"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, @@ -1395,7 +1388,6 @@ static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf) lcf->limit_rate = NGX_CONF_UNSET_SIZE; lcf->keepalive_timeout = NGX_CONF_UNSET_MSEC; lcf->keepalive_header = NGX_CONF_UNSET; - lcf->keepalive_buffers = NGX_CONF_UNSET; lcf->lingering_time = NGX_CONF_UNSET_MSEC; lcf->lingering_timeout = NGX_CONF_UNSET_MSEC; lcf->reset_timedout_connection = NGX_CONF_UNSET; @@ -1484,7 +1476,6 @@ static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf, prev->keepalive_timeout, 75000); ngx_conf_merge_sec_value(conf->keepalive_header, prev->keepalive_header, 0); - ngx_conf_merge_value(conf->keepalive_buffers, prev->keepalive_buffers, 1); ngx_conf_merge_msec_value(conf->lingering_time, prev->lingering_time, 30000); ngx_conf_merge_msec_value(conf->lingering_timeout, @@ -1545,6 +1536,7 @@ static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) } port = ngx_atoi(&addr[p], args[1].len - p); + if (port == NGX_ERROR && p == 0) { /* "listen host" */ @@ -1564,9 +1556,10 @@ static char *ngx_set_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ls->addr = INADDR_ANY; ls->port = (in_port_t) port; return NGX_CONF_OK; - } - ls->port = (in_port_t) port; + } else { + ls->port = (in_port_t) port; + } ls->addr = inet_addr((const char *) addr); if (ls->addr == INADDR_NONE) { diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h index 7de7d1bed..5e0e344d9 100644 --- a/src/http/ngx_http_core_module.h +++ b/src/http/ngx_http_core_module.h @@ -164,7 +164,6 @@ struct ngx_http_core_loc_conf_s { time_t keepalive_header; /* keepalive_timeout */ - ngx_flag_t keepalive_buffers; /* keepalive_buffers */ ngx_flag_t sendfile; /* sendfile */ ngx_flag_t tcp_nopush; /* tcp_nopush */ ngx_flag_t reset_timedout_connection; /* reset_timedout_connection */ diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c index 8144d0a47..42ede7865 100644 --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -197,7 +197,13 @@ static void ngx_http_init_request(ngx_event_t *rev) hc = c->data; - if (hc == NULL) { + if (hc) { + +#if (NGX_STAT_STUB) + (*ngx_stat_reading)++; +#endif + + } else { if (!(hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t)))) { #if (NGX_STAT_STUB) @@ -220,10 +226,6 @@ static void ngx_http_init_request(ngx_event_t *rev) r->header_in = hc->busy[0]; } -#if (NGX_STAT_STUB) - (*ngx_stat_reading)++; -#endif - } else { if (!(r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)))) { @@ -683,7 +685,7 @@ static void ngx_http_process_request_line(ngx_event_t *rev) /* NGX_AGAIN: a request line parsing is still incomplete */ - if (r->header_in->last == r->header_in->end) { + if (r->header_in->pos == r->header_in->end) { rv = ngx_http_alloc_large_header_buffer(r, 1); @@ -728,7 +730,7 @@ static void ngx_http_process_request_headers(ngx_event_t *rev) if (rc == NGX_AGAIN) { - if (r->header_in->last == r->header_in->end) { + if (r->header_in->pos == r->header_in->end) { rv = ngx_http_alloc_large_header_buffer(r, 0); @@ -971,6 +973,10 @@ static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, if (hc->nfree) { b = hc->free[--hc->nfree]; + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "http large header free: " PTR_FMT " " SIZE_T_FMT, + b->pos, b->end - b->last); + } else if (hc->nbusy < cscf->large_client_header_buffers.num) { if (hc->busy == NULL) { @@ -987,6 +993,10 @@ static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, return NGX_ERROR; } + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "http large header alloc: " PTR_FMT " " SIZE_T_FMT, + b->pos, b->end - b->last); + } else { return NGX_DECLINED; } @@ -1005,12 +1015,15 @@ static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, return NGX_OK; } + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "http large header copy: %d", r->header_in->pos - old); + new = b->start; - ngx_memcpy(new, old, r->header_in->last - old); + ngx_memcpy(new, old, r->header_in->pos - old); b->pos = new + (r->header_in->pos - old); - b->last = new + (r->header_in->last - old); + b->last = new + (r->header_in->pos - old); if (request_line) { r->request_start = new; @@ -1552,14 +1565,9 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) hc = r->http_connection; b = r->header_in; - clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); - - if (b->pos < b->last || clcf->keepalive_buffers) { + if (b->pos < b->last) { - /* - * the pipelined request or we like to keep the allocated - * ngx_http_request_t and the client header buffers while keepalive - */ + /* the pipelined request */ if (b != c->buffer) { @@ -1570,6 +1578,7 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) if (hc->free == NULL) { hc->free = ngx_palloc(c->pool, cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *)); + if (hc->free == NULL) { ngx_http_close_connection(c); return; @@ -1591,6 +1600,7 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) ngx_http_close_request(r, 0); c->data = hc; + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); ngx_add_timer(rev, clcf->keepalive_timeout); if (ngx_handle_level_read_event(rev) == NGX_ERROR) { @@ -1603,8 +1613,6 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) if (b->pos < b->last) { - /* the pipelined request */ - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request"); hc->pipeline = 1; @@ -1615,35 +1623,42 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) hc->pipeline = 0; - b->pos = b->last = b->start; + if (ngx_pfree(c->pool, r) == NGX_OK) { + hc->request = NULL; + } - if (!clcf->keepalive_buffers) { + b = c->buffer; - if (ngx_pfree(c->pool, r) == NGX_OK) { - hc->request = NULL; - } + if (ngx_pfree(c->pool, b->start) == NGX_OK) { + b->pos = NULL; - if (ngx_pfree(c->pool, c->buffer->start) == NGX_OK) { - c->buffer = NULL; - } + } else { + b->pos = b->start; + b->last = b->start; + } - if (hc->free) { - for (i = 0; i < hc->nfree; i++) { - ngx_pfree(c->pool, hc->free[i]); - hc->free[i] = NULL; - } + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: " PTR_FMT " %d", + hc->free, hc->nfree); - hc->nfree = 0; + if (hc->free) { + for (i = 0; i < hc->nfree; i++) { + ngx_pfree(c->pool, hc->free[i]); + hc->free[i] = NULL; } - if (hc->busy) { - for (i = 0; i < hc->nbusy; i++) { - ngx_pfree(c->pool, hc->busy[i]); - hc->busy[i] = NULL; - } + hc->nfree = 0; + } + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: " PTR_FMT " %d", + hc->busy, hc->nbusy); - hc->nbusy = 0; + if (hc->busy) { + for (i = 0; i < hc->nbusy; i++) { + ngx_pfree(c->pool, hc->busy[i]); + hc->busy[i] = NULL; } + + hc->nbusy = 0; } rev->event_handler = ngx_http_keepalive_handler; @@ -1689,6 +1704,7 @@ static void ngx_http_set_keepalive(ngx_http_request_t *r) static void ngx_http_keepalive_handler(ngx_event_t *rev) { + size_t size; ssize_t n; ngx_buf_t *b; ngx_connection_t *c; @@ -1704,8 +1720,36 @@ static void ngx_http_keepalive_handler(ngx_event_t *rev) return; } + ctx = (ngx_http_log_ctx_t *) rev->log->data; + +#if (HAVE_KQUEUE) + + if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { + if (rev->pending_eof) { + ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno, + "kevent() reported that client %s closed " + "keepalive connection", ctx->client); + ngx_http_close_connection(c); + return; + } + } + +#endif + hc = c->data; - b = hc->nbusy ? hc->busy[0] : c->buffer; + b = c->buffer; + size = b->end - b->start; + + if (b->pos == NULL) { + if (!(b->pos = ngx_palloc(c->pool, size))) { + ngx_http_close_connection(c); + return; + } + + b->start = b->pos; + b->last = b->pos; + b->end = b->pos + size; + } /* * MSIE closes a keepalive connection with RST flag @@ -1715,7 +1759,7 @@ static void ngx_http_keepalive_handler(ngx_event_t *rev) c->log_error = NGX_ERROR_IGNORE_ECONNRESET; ngx_set_socket_errno(0); - n = c->recv(c, b->last, b->end - b->last); + n = c->recv(c, b->last, size); c->log_error = NGX_ERROR_INFO; if (n == NGX_AGAIN) { @@ -1727,7 +1771,6 @@ static void ngx_http_keepalive_handler(ngx_event_t *rev) return; } - ctx = (ngx_http_log_ctx_t *) rev->log->data; rev->log->handler = NULL; if (n == 0) { |