aboutsummaryrefslogtreecommitdiff
path: root/src/http/modules
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/modules')
-rw-r--r--src/http/modules/ngx_http_gzip_filter.c8
-rw-r--r--src/http/modules/ngx_http_index_handler.c156
-rw-r--r--src/http/modules/proxy/ngx_http_proxy_handler.c29
3 files changed, 106 insertions, 87 deletions
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index 3e80c0b13..f5e83842a 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -23,7 +23,7 @@ typedef struct {
ngx_hunk_t *out_hunk;
int hunks;
- int length;
+ off_t length;
void *alloc;
unsigned flush:4;
@@ -90,7 +90,9 @@ ngx_module_t ngx_http_gzip_filter_module = {
};
-static char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
+static char gzheader[10] = { 0x1f,
+ (char) 0x8b, /* suppress MSVC warning */
+ Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
#if (HAVE_LITTLE_ENDIAN)
@@ -197,7 +199,7 @@ static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
#if 0
ngx_test_null(ctx->alloc, ngx_alloc(200K, r->log), NGX_ERROR);
#else
- ctx->alloc = (void *) ~NULL;
+ ctx->alloc = (void *) -1;
#endif
rc = deflateInit2(&ctx->zstream, /**/ 1, Z_DEFLATED,
diff --git a/src/http/modules/ngx_http_index_handler.c b/src/http/modules/ngx_http_index_handler.c
index 7c348e2ad..1bf2bd99d 100644
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -10,10 +10,20 @@ typedef struct {
} ngx_http_index_conf_t;
+typedef struct {
+ int index;
+ unsigned tested:1;
+} ngx_http_index_ctx_t;
+
+
#define NGX_HTTP_DEFAULT_INDEX "index.html"
-static int ngx_http_index_test_dir(ngx_http_request_t *r);
+static int ngx_http_index_test_dir(ngx_http_request_t *r,
+ ngx_http_index_ctx_t *ctx);
+static int ngx_http_index_error(ngx_http_request_t *r,
+ ngx_http_index_ctx_t *ctx, ngx_err_t err);
+
static int ngx_http_index_init(ngx_cycle_t *cycle);
static void *ngx_http_index_create_conf(ngx_conf_t *cf);
static char *ngx_http_index_merge_conf(ngx_conf_t *cf,
@@ -58,14 +68,14 @@ ngx_module_t ngx_http_index_module = {
/*
- Try to open the first index file before the directory existence test
- because the valid requests should be many more than invalid ones.
- If open() failed then stat() should be more quickly because some data
- is already cached in the kernel.
- Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
- Unix has ENOTDIR error, although it less helpfull - it shows only
- that path contains the usual file in place of the directory.
-*/
+ * Try to open the first index file before the test of the directory existence
+ * because the valid requests should be many more than invalid ones.
+ * If open() failed then stat() should be more quickly because some data
+ * is already cached in the kernel.
+ * Besides Win32 has ERROR_PATH_NOT_FOUND (NGX_ENOTDIR).
+ * Unix has ENOTDIR error, although it less helpfull - it shows only
+ * that path contains the usual file in place of the directory.
+ */
int ngx_http_index_handler(ngx_http_request_t *r)
{
@@ -74,38 +84,58 @@ int ngx_http_index_handler(ngx_http_request_t *r)
ngx_str_t redirect, *index;
ngx_err_t err;
ngx_fd_t fd;
+ ngx_http_index_ctx_t *ctx;
ngx_http_index_conf_t *icf;
ngx_http_core_loc_conf_t *clcf;
- icf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+ if (r->uri.data[r->uri.len - 1] != '/') {
+ return NGX_DECLINED;
+ }
- ngx_test_null(r->path.data,
- ngx_palloc(r->pool,
- clcf->doc_root.len + r->uri.len
- + icf->max_index_len),
- NGX_HTTP_INTERNAL_SERVER_ERROR);
+ ctx = ngx_http_get_module_ctx(r, ngx_http_index_module);
+ if (ctx == NULL) {
+ ngx_http_create_ctx(r, ctx, ngx_http_index_module,
+ sizeof(ngx_http_index_ctx_t),
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+ }
- redirect.data = ngx_cpymem(r->path.data, clcf->doc_root.data,
- clcf->doc_root.len);
- file = ngx_cpystrn(redirect.data, r->uri.data, r->uri.len + 1);
- r->path.len = file - r->path.data;
+ icf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
- test_dir = 1;
- path_not_found = 1;
+ if (r->path.data == NULL) {
+ r->path_allocated = clcf->doc_root.len + r->uri.len
+ + icf->max_index_len;
+ ngx_test_null(r->path.data,
+ ngx_palloc(r->pool, r->path_allocated),
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+
+ redirect.data = ngx_cpymem(r->path.data, clcf->doc_root.data,
+ clcf->doc_root.len);
+ file = ngx_cpystrn(redirect.data, r->uri.data, r->uri.len + 1);
+ r->path.len = file - r->path.data;
+
+ } else{
+ redirect.data = r->path.data + r->path.len;
+ file = redirect.data + r->uri.len;
+ }
index = icf->indices.elts;
- for (i = 0; i < icf->indices.nelts; i++) {
+ for (/* void */; ctx->index < icf->indices.nelts; ctx->index++) {
- if (index[i].data[0] != '/') {
- ngx_memcpy(file, index[i].data, index[i].len + 1);
- name = r->path.data;
+ if (index[ctx->index].data[0] == '/') {
+ name = index[ctx->index].data;
} else {
- name = index[i].data;
+ ngx_memcpy(file, index[ctx->index].data, index[ctx->index].len + 1);
+ name = r->path.data;
}
fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN);
+
+ if (fd == NGX_AGAIN) {
+ return NGX_AGAIN;
+ }
+
if (fd == NGX_INVALID_FILE) {
err = ngx_errno;
@@ -113,25 +143,20 @@ ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
"DEBUG: " ngx_open_file_n " %s failed", name);
if (err == NGX_ENOTDIR) {
- path_not_found = 1;
+ return ngx_http_index_error(r, ctx, err);
} else if (err == NGX_EACCES) {
- r->path_err = err;
- return NGX_HTTP_FORBIDDEN;
+ return ngx_http_index_error(r, ctx, err);
}
- if (test_dir) {
- if (path_not_found) {
- r->path_err = err;
- return NGX_HTTP_NOT_FOUND;
- }
+ if (!ctx->tested) {
+ rc = ngx_http_index_test_dir(r, ctx);
- rc = ngx_http_index_test_dir(r);
if (rc != NGX_OK) {
return rc;
}
- test_dir = 0;
+ ctx->tested = 1;
}
if (err == NGX_ENOENT) {
@@ -147,14 +172,15 @@ ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
r->file.name.data = name;
r->file.fd = fd;
- if (index[i].data[0] == '/') {
- r->file.name.len = index[i].len;
- redirect.len = index[i].len;
- redirect.data = index[i].data;
+ if (index[ctx->index].data[0] == '/') {
+ r->file.name.len = index[ctx->index].len;
+ redirect.len = index[ctx->index].len;
+ redirect.data = index[ctx->index].data;
} else {
- redirect.len = r->uri.len + index[i].len;
- r->file.name.len = clcf->doc_root.len + r->uri.len + index[i].len;
+ redirect.len = r->uri.len + index[ctx->index].len;
+ r->file.name.len = clcf->doc_root.len + r->uri.len
+ + index[ctx->index].len;
}
return ngx_http_internal_redirect(r, &redirect, NULL);
@@ -164,29 +190,26 @@ ngx_log_error(NGX_LOG_DEBUG, r->connection->log, err,
}
-static int ngx_http_index_test_dir(ngx_http_request_t *r)
+static int ngx_http_index_test_dir(ngx_http_request_t *r,
+ ngx_http_index_ctx_t *ctx)
{
+ ngx_err_t err;
+
r->path.data[r->path.len - 1] = '\0';
r->path.data[r->path.len] = '\0';
ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
-#if 0
- if (r->path_err == NGX_EACCES) {
- return NGX_HTTP_FORBIDDEN;
- }
-#endif
-
if (ngx_file_type(r->path.data, &r->file.info) == -1) {
- r->path_err = ngx_errno;
+ err = ngx_errno;
- if (r->path_err == NGX_ENOENT) {
+ if (err == NGX_ENOENT) {
r->path.data[r->path.len - 1] = '/';
- return NGX_HTTP_NOT_FOUND;
+ return ngx_http_index_error(r, ctx, err);
}
- ngx_log_error(NGX_LOG_CRIT, r->connection->log, r->path_err,
+ ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
ngx_file_type_n " %s failed", r->path.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -196,10 +219,26 @@ ngx_log_debug(r->connection->log, "IS_DIR: %s" _ r->path.data);
if (ngx_is_dir(r->file.info)) {
return NGX_OK;
+ }
- } else {
- return NGX_HTTP_NOT_FOUND;
+ /* THINK: not reached ??? */
+ return ngx_http_index_error(r, ctx, 0);
+}
+
+
+static int ngx_http_index_error(ngx_http_request_t *r,
+ ngx_http_index_ctx_t *ctx, ngx_err_t err)
+{
+ if (err == NGX_EACCES) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
+ "\"%s\" is forbidden", r->path.data);
+
+ return NGX_HTTP_FORBIDDEN;
}
+
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, err,
+ "\"%s\" is not found", r->path.data);
+ return NGX_HTTP_NOT_FOUND;
}
@@ -212,8 +251,9 @@ static int ngx_http_index_init(ngx_cycle_t *cycle)
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
- ngx_test_null(h, ngx_push_array(&cmcf->index_handlers), NGX_ERROR);
-
+ ngx_test_null(h, ngx_push_array(
+ &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers),
+ NGX_ERROR);
*h = ngx_http_index_handler;
return NGX_OK;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index adb9be5c4..b21eac47a 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -292,10 +292,8 @@ static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
if (chain) {
ngx_add_timer(c->write, p->lcf->send_timeout);
- c->write->timer_set = 1;
} else {
- c->write->timer_set = 0;
/* TODO: del event */
}
@@ -376,7 +374,6 @@ static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
/* rc == NGX_AGAIN */
ngx_add_timer(c->write, p->lcf->connect_timeout);
- c->write->timer_set = 1;
return;
}
@@ -600,29 +597,11 @@ static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
p->header_in->end - p->header_in->last);
if (n == NGX_AGAIN) {
- if (rev->timer_set) {
- ngx_del_timer(rev);
- } else {
- rev->timer_set = 1;
- }
-
ngx_add_timer(rev, p->lcf->read_timeout);
- if (!rev->active) {
- if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
- /* kqueue */
- event = NGX_CLEAR_EVENT;
-
- } else {
- /* select, poll, /dev/poll */
- event = NGX_LEVEL_EVENT;
- }
-
- if (ngx_add_event(rev, NGX_READ_EVENT, event) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
+ if (ngx_handle_read_event(rev) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return NGX_ERROR;
}
return NGX_AGAIN;
@@ -1052,12 +1031,10 @@ static void ngx_http_proxy_close_connection(ngx_connection_t *c)
if (c->read->timer_set) {
ngx_del_timer(c->read);
- c->read->timer_set = 0;
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
- c->write->timer_set = 0;
}
/* TODO: move connection to the connection pool */