diff options
Diffstat (limited to 'src/http/modules')
-rw-r--r-- | src/http/modules/ngx_http_autoindex_module.c | 118 | ||||
-rw-r--r-- | src/http/modules/ngx_http_index_module.c | 114 | ||||
-rw-r--r-- | src/http/modules/ngx_http_static_module.c | 157 |
3 files changed, 143 insertions, 246 deletions
diff --git a/src/http/modules/ngx_http_autoindex_module.c b/src/http/modules/ngx_http_autoindex_module.c index f56896489..70f7974c5 100644 --- a/src/http/modules/ngx_http_autoindex_module.c +++ b/src/http/modules/ngx_http_autoindex_module.c @@ -39,13 +39,15 @@ typedef struct { } ngx_http_autoindex_loc_conf_t; -#define NGX_HTTP_AUTOINDEX_NAME_LEN 50 +#define NGX_HTTP_AUTOINDEX_PREALLOCATE 50 + +#define NGX_HTTP_AUTOINDEX_NAME_LEN 50 static int ngx_libc_cdecl ngx_http_autoindex_cmp_entries(const void *one, const void *two); static ngx_int_t ngx_http_autoindex_error(ngx_http_request_t *r, - ngx_dir_t *dir, u_char *name); + ngx_dir_t *dir, ngx_str_t *name); static ngx_int_t ngx_http_autoindex_init(ngx_cycle_t *cycle); static void *ngx_http_autoindex_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_autoindex_merge_loc_conf(ngx_conf_t *cf, @@ -131,20 +133,19 @@ static u_char tail[] = static ngx_int_t ngx_http_autoindex_handler(ngx_http_request_t *r) { - u_char *last, scale; + u_char *last, *filename, scale; off_t length; - size_t len, copy; + size_t len, copy, allocated; ngx_tm_t tm; - ngx_int_t rc, size; - ngx_uint_t i, level; ngx_err_t err; ngx_buf_t *b; - ngx_chain_t out; - ngx_str_t dname, fname; + ngx_int_t rc, size; + ngx_str_t path; ngx_dir_t dir; + ngx_uint_t i, level; ngx_pool_t *pool; + ngx_chain_t out; ngx_array_t entries; - ngx_http_core_loc_conf_t *clcf; ngx_http_autoindex_entry_t *entry; ngx_http_autoindex_loc_conf_t *alcf; @@ -166,41 +167,21 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) return NGX_DECLINED; } - /* TODO: pool should be temporary pool */ - pool = r->pool; - - clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); + /* NGX_DIR_MASK_LEN is lesser than NGX_HTTP_AUTOINDEX_PREALLOCATE */ - if (clcf->alias) { - dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len - + NGX_DIR_MASK_LEN + 1 - - clcf->name.len); - if (dname.data == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len); - last = ngx_cpystrn(last, r->uri.data + clcf->name.len, - r->uri.len - clcf->name.len + 1); - - } else { - dname.data = ngx_palloc(pool, clcf->root.len + r->uri.len - + NGX_DIR_MASK_LEN); - if (dname.data == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - last = ngx_cpymem(dname.data, clcf->root.data, clcf->root.len); - last = ngx_cpystrn(last, r->uri.data, r->uri.len); + last = ngx_http_map_uri_to_path(r, &path, NGX_HTTP_AUTOINDEX_PREALLOCATE); + if (last == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; } - dname.len = last - dname.data; + allocated = path.len; + path.len = last - path.data - 1; + path.data[path.len] = '\0'; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "http autoindex: \"%s\"", dname.data); - + "http autoindex: \"%s\"", path.data); - if (ngx_open_dir(&dname, &dir) == NGX_ERROR) { + if (ngx_open_dir(&path, &dir) == NGX_ERROR) { err = ngx_errno; if (err == NGX_ENOENT @@ -220,20 +201,25 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) } ngx_log_error(level, r->connection->log, err, - ngx_open_dir_n " \"%s\" failed", dname.data); + ngx_open_dir_n " \"%s\" failed", path.data); return rc; } #if (NGX_SUPPRESS_WARN) + /* MSVC thinks 'entries' may be used without having been initialized */ ngx_memzero(&entries, sizeof(ngx_array_t)); + #endif - if (ngx_array_init(&entries, pool, 50, sizeof(ngx_http_autoindex_entry_t)) - == NGX_ERROR) + /* TODO: pool should be temporary pool */ + pool = r->pool; + + if (ngx_array_init(&entries, pool, 40, sizeof(ngx_http_autoindex_entry_t)) + != NGX_OK) { - return ngx_http_autoindex_error(r, &dir, dname.data); + return ngx_http_autoindex_error(r, &dir, &path); } r->headers_out.status = NGX_HTTP_OK; @@ -246,10 +232,8 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) return rc; } - fname.len = 0; -#if (NGX_SUPPRESS_WARN) - fname.data = NULL; -#endif + filename = path.data; + filename[path.len] = '/'; for ( ;; ) { ngx_set_errno(0); @@ -259,8 +243,8 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) if (err != NGX_ENOMOREFILES) { ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, - ngx_read_dir_n " \"%s\" failed", dname.data); - return ngx_http_autoindex_error(r, &dir, dname.data); + ngx_read_dir_n " \"%V\" failed", &path); + return ngx_http_autoindex_error(r, &dir, &path); } break; @@ -277,49 +261,51 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) if (!dir.valid_info) { - if (dname.len + 1 + len + 1 > fname.len) { - fname.len = dname.len + 1 + len + 1 + 32; + /* 1 byte for '/' and 1 byte for terminating '\0' */ + + if (path.len + 1 + len + 1 > allocated) { + allocated = path.len + 1 + len + 1 + + NGX_HTTP_AUTOINDEX_PREALLOCATE; - fname.data = ngx_palloc(pool, fname.len); - if (fname.data == NULL) { - return ngx_http_autoindex_error(r, &dir, dname.data); + filename = ngx_palloc(pool, allocated); + if (filename == NULL) { + return ngx_http_autoindex_error(r, &dir, &path); } - last = ngx_cpystrn(fname.data, dname.data, - dname.len + 1); + last = ngx_cpystrn(filename, path.data, path.len + 1); *last++ = '/'; } ngx_cpystrn(last, ngx_de_name(&dir), len + 1); - if (ngx_de_info(fname.data, &dir) == NGX_FILE_ERROR) { + if (ngx_de_info(filename, &dir) == NGX_FILE_ERROR) { err = ngx_errno; if (err != NGX_ENOENT) { ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, - ngx_de_info_n " \"%s\" failed", fname.data); - return ngx_http_autoindex_error(r, &dir, dname.data); + ngx_de_info_n " \"%s\" failed", filename); + return ngx_http_autoindex_error(r, &dir, &path); } - if (ngx_de_link_info(fname.data, &dir) == NGX_FILE_ERROR) { + if (ngx_de_link_info(filename, &dir) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, ngx_de_link_info_n " \"%s\" failed", - fname.data); - return ngx_http_autoindex_error(r, &dir, dname.data); + filename); + return ngx_http_autoindex_error(r, &dir, &path); } } } entry = ngx_array_push(&entries); if (entry == NULL) { - return ngx_http_autoindex_error(r, &dir, dname.data); + return ngx_http_autoindex_error(r, &dir, &path); } entry->name.len = len; entry->name.data = ngx_palloc(pool, len + 1); if (entry->name.data == NULL) { - return ngx_http_autoindex_error(r, &dir, dname.data); + return ngx_http_autoindex_error(r, &dir, &path); } ngx_cpystrn(entry->name.data, ngx_de_name(&dir), len + 1); @@ -340,7 +326,7 @@ ngx_http_autoindex_handler(ngx_http_request_t *r) if (ngx_close_dir(&dir) == NGX_ERROR) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, - ngx_close_dir_n " \"%s\" failed", dname.data); + ngx_close_dir_n " \"%s\" failed", &path); } len = sizeof(title) - 1 @@ -586,11 +572,11 @@ ngx_http_autoindex_alloc(ngx_http_autoindex_ctx_t *ctx, size_t size) static ngx_int_t -ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, u_char *name) +ngx_http_autoindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) { if (ngx_close_dir(dir) == NGX_ERROR) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno, - ngx_close_dir_n " \"%s\" failed", name); + ngx_close_dir_n " \"%V\" failed", name); } return NGX_HTTP_INTERNAL_SERVER_ERROR; diff --git a/src/http/modules/ngx_http_index_module.c b/src/http/modules/ngx_http_index_module.c index abab26807..01b6d0cab 100644 --- a/src/http/modules/ngx_http_index_module.c +++ b/src/http/modules/ngx_http_index_module.c @@ -24,10 +24,8 @@ typedef struct { typedef struct { ngx_uint_t current; - size_t allocated; - u_char *path; - ngx_str_t uri; + ngx_str_t path; ngx_str_t index; ngx_uint_t tested; /* unsigned tested:1 */ @@ -37,8 +35,6 @@ typedef struct { #define NGX_HTTP_DEFAULT_INDEX "index.html" -static ngx_int_t ngx_http_index_alloc(ngx_http_request_t *r, size_t size, - ngx_http_index_ctx_t *ctx, ngx_http_core_loc_conf_t *clcf); static ngx_int_t ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx); static ngx_int_t ngx_http_index_error(ngx_http_request_t *r, @@ -120,11 +116,12 @@ ngx_module_t ngx_http_index_module = { static ngx_int_t ngx_http_index_handler(ngx_http_request_t *r) { - u_char *name; + u_char *last; size_t len; ngx_fd_t fd; ngx_int_t rc; ngx_err_t err; + ngx_str_t uri; ngx_log_t *log; ngx_uint_t i; ngx_http_index_t *index; @@ -152,7 +149,6 @@ ngx_http_index_handler(ngx_http_request_t *r) * and may be called several times */ - clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module); ctx = ngx_http_get_module_ctx(r, ngx_http_index_module); @@ -184,7 +180,9 @@ ngx_http_index_handler(ngx_http_request_t *r) e.ip = index[i].lengths->elts; e.request = r; - len = 1; + /* 1 byte for terminating '\0' and 4 bytes is preallocation */ + + len = 1 + 4; while (*(uintptr_t *) e.ip) { lcode = *(ngx_http_script_len_code_pt *) e.ip; @@ -194,14 +192,21 @@ ngx_http_index_handler(ngx_http_request_t *r) ctx->index.len = len; } - if (len > ctx->allocated) { - if (ngx_http_index_alloc(r, len, ctx, clcf) != NGX_OK) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; + if (len > ctx->path.len) { + + last = ngx_http_map_uri_to_path(r, &ctx->path, len); + if (last == NULL) { + return NGX_ERROR; } + + ctx->index.data = last; } if (index[i].values == NULL) { - ngx_memcpy(ctx->index.data, index[i].name.data, ctx->index.len); + + /* index[i].name.len includes the terminating '\0' */ + + ngx_memcpy(ctx->index.data, index[i].name.data, index[i].name.len); } else { e.ip = index[i].values->elts; @@ -220,12 +225,10 @@ ngx_http_index_handler(ngx_http_request_t *r) *e.pos++ = '\0'; } - name = ctx->path; - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, - "open index \"%s\"", name); + "open index \"%s\"", ctx->path.data); - fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN); + fd = ngx_open_file(ctx->path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN); if (fd == (ngx_fd_t) NGX_AGAIN) { ctx->current = i; @@ -236,7 +239,7 @@ ngx_http_index_handler(ngx_http_request_t *r) err = ngx_errno; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, err, - ngx_open_file_n " \"%s\" failed", name); + ngx_open_file_n " \"%s\" failed", ctx->path.data); if (err == NGX_ENOTDIR) { return ngx_http_index_error(r, ctx, err); @@ -260,7 +263,7 @@ ngx_http_index_handler(ngx_http_request_t *r) } ngx_log_error(NGX_LOG_ERR, log, err, - ngx_open_file_n " \"%s\" failed", name); + ngx_open_file_n " \"%s\" failed", ctx->path.data); return NGX_HTTP_INTERNAL_SERVER_ERROR; } @@ -272,64 +275,34 @@ ngx_http_index_handler(ngx_http_request_t *r) } cln->fd = fd; - cln->name = name; + cln->name = ctx->path.data; cln->log = r->pool->log; if (ngx_pool_cleanup_add(r->pool, ngx_pool_cleanup_file, cln) == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); - if (clcf->alias) { - name = ngx_cpymem(ctx->uri.data, r->uri.data, r->uri.len); - ngx_memcpy(name, ctx->index.data, ctx->index.len - 1); - } - - ctx->uri.len = r->uri.len + ctx->index.len - 1; - - return ngx_http_internal_redirect(r, &ctx->uri, &r->args); - } - - return NGX_DECLINED; -} - - -static ngx_int_t -ngx_http_index_alloc(ngx_http_request_t *r, size_t size, - ngx_http_index_ctx_t *ctx, ngx_http_core_loc_conf_t *clcf) -{ - ctx->allocated = size; - - if (!clcf->alias) { - ctx->path = ngx_palloc(r->pool, clcf->root.len + r->uri.len + size); - if (ctx->path == NULL) { - return NGX_ERROR; - } + uri.len = r->uri.len + ctx->index.len - 1; - ctx->uri.data = ngx_cpymem(ctx->path, clcf->root.data, clcf->root.len); + if (!clcf->alias) { + uri.data = ctx->path.data + clcf->root.len; - ctx->index.data = ngx_cpymem(ctx->uri.data, r->uri.data, r->uri.len); - - } else { - ctx->path = ngx_palloc(r->pool, - clcf->root.len + r->uri.len - clcf->name.len + size); - if (ctx->path == NULL) { - return NGX_ERROR; - } + } else { + uri.data = ngx_palloc(r->pool, uri.len); + if (uri.data == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } - ctx->uri.data = ngx_palloc(r->pool, r->uri.len + size); - if (ctx->uri.data == NULL) { - return NGX_ERROR; + last = ngx_cpymem(uri.data, r->uri.data, r->uri.len); + ngx_memcpy(last, ctx->index.data, ctx->index.len - 1); } - ngx_memcpy(ctx->path, clcf->root.data, clcf->root.len); - - ctx->index.data = ngx_cpymem(ctx->path + clcf->root.len, - r->uri.data + clcf->name.len, - r->uri.len - clcf->name.len); + return ngx_http_internal_redirect(r, &uri, &r->args); } - return NGX_OK; + return NGX_DECLINED; } @@ -342,9 +315,9 @@ ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx) *(ctx->index.data - 1) = '\0'; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "http index check dir: \"%s\"", ctx->path); + "http index check dir: \"%s\"", ctx->path.data); - if (ngx_file_info(ctx->path, &fi) == -1) { + if (ngx_file_info(ctx->path.data, &fi) == -1) { err = ngx_errno; @@ -354,7 +327,7 @@ ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx) } ngx_log_error(NGX_LOG_CRIT, r->connection->log, err, - ngx_file_info_n " \"%s\" failed", ctx->path); + ngx_file_info_n " \"%s\" failed", ctx->path.data); return NGX_HTTP_INTERNAL_SERVER_ERROR; } @@ -366,7 +339,7 @@ ngx_http_index_test_dir(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx) } ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, - "\"%s\" is not a directory", ctx->path); + "\"%s\" is not a directory", ctx->path.data); return NGX_HTTP_INTERNAL_SERVER_ERROR; } @@ -378,13 +351,13 @@ ngx_http_index_error(ngx_http_request_t *r, ngx_http_index_ctx_t *ctx, { if (err == NGX_EACCES) { ngx_log_error(NGX_LOG_ERR, r->connection->log, err, - "\"%s\" is forbidden", ctx->path); + "\"%s\" is forbidden", ctx->path.data); return NGX_HTTP_FORBIDDEN; } ngx_log_error(NGX_LOG_ERR, r->connection->log, err, - "\"%s\" is not found", ctx->path); + "\"%s\" is not found", ctx->path.data); return NGX_HTTP_NOT_FOUND; } @@ -494,14 +467,15 @@ ngx_http_index_set_index(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) n = ngx_http_script_variables_count(&value[i]); if (n == 0) { - index->name.len++; - if (ilcf->max_index_len != 0 && ilcf->max_index_len < index->name.len) { ilcf->max_index_len = index->name.len; } + /* include the terminating '\0' to the length to use ngx_memcpy() */ + index->name.len++; + continue; } diff --git a/src/http/modules/ngx_http_static_module.c b/src/http/modules/ngx_http_static_module.c index afdbf7864..c5d5af855 100644 --- a/src/http/modules/ngx_http_static_module.c +++ b/src/http/modules/ngx_http_static_module.c @@ -72,18 +72,18 @@ ngx_module_t ngx_http_static_module = { static ngx_int_t ngx_http_static_handler(ngx_http_request_t *r) { - u_char *last; - ngx_fd_t fd; - ngx_int_t rc; - ngx_uint_t level; - ngx_str_t name, location; - ngx_err_t err; - ngx_log_t *log; - ngx_buf_t *b; - ngx_chain_t out; - ngx_file_info_t fi; - ngx_pool_cleanup_file_t *cln; - ngx_http_core_loc_conf_t *clcf; + u_char *last, *location; + ngx_fd_t fd; + ngx_int_t rc; + ngx_uint_t level; + ngx_str_t path; + ngx_err_t err; + ngx_log_t *log; + ngx_buf_t *b; + ngx_chain_t out; + ngx_file_info_t fi; + ngx_pool_cleanup_file_t *cln; + ngx_http_core_loc_conf_t *clcf; if (r->uri.data[r->uri.len - 1] == '/') { return NGX_DECLINED; @@ -106,99 +106,20 @@ ngx_http_static_handler(ngx_http_request_t *r) log = r->connection->log; - clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); - /* - * make a file name, reserve 2 bytes for a trailing '/' - * in a possible redirect and for the last '\0' + * ngx_http_map_uri_to_path() allocates memory for terminating '\0' + * so we do not need to reserve memory for '/' for possible redirect */ - if (!clcf->alias) { - name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2); - if (name.data == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - location.data = ngx_cpymem(name.data, clcf->root.data, clcf->root.len); - last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1); - - name.len = last - name.data; - location.len = last - location.data + 1; - - } else { - name.data = ngx_palloc(r->pool, clcf->root.len + r->uri.len + 2 - - clcf->name.len); - if (name.data == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - last = ngx_cpymem(name.data, clcf->root.data, clcf->root.len); - last = ngx_cpystrn(last, r->uri.data + clcf->name.len, - r->uri.len + 1 - clcf->name.len); - - name.len = last - name.data; - - location.data = ngx_palloc(r->pool, r->uri.len + 2); - if (location.data == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - last = ngx_cpystrn(location.data, r->uri.data, r->uri.len + 1); - - location.len = last - location.data + 1; + last = ngx_http_map_uri_to_path(r, &path, 0); + if (last == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, - "http filename: \"%s\"", name.data); - - /* open file */ - -#if (NGX_WIN9X) - - if (ngx_win32_version < NGX_WIN_NT) { - - /* - * there is no way to open a file or a directory in Win9X with - * one syscall because Win9X has no FILE_FLAG_BACKUP_SEMANTICS flag - * so we need to check its type before the opening - */ - - if (ngx_file_info(name.data, &fi) == NGX_FILE_ERROR) { - err = ngx_errno; - ngx_log_error(NGX_LOG_ERR, log, err, - ngx_file_info_n " \"%s\" failed", name.data); - - if (err == NGX_ENOENT || err == NGX_ENOTDIR) { - return NGX_HTTP_NOT_FOUND; - - } else if (err == NGX_EACCES) { - return NGX_HTTP_FORBIDDEN; - - } else { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - } - - if (ngx_is_dir(&fi)) { - r->headers_out.location = ngx_http_add_header(&r->headers_out, - ngx_http_headers_out); - if (r->headers_out.location == NULL) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - *last++ = '/'; - *last = '\0'; - r->headers_out.location->value.len = last - location; - r->headers_out.location->value.data = location; - - return NGX_HTTP_MOVED_PERMANENTLY; - } - } - -#endif + "http filename: \"%s\"", path.data); - - fd = ngx_open_file(name.data, NGX_FILE_RDONLY, NGX_FILE_OPEN); + fd = ngx_open_file(path.data, NGX_FILE_RDONLY, NGX_FILE_OPEN); if (fd == NGX_INVALID_FILE) { err = ngx_errno; @@ -219,9 +140,11 @@ ngx_http_static_handler(ngx_http_request_t *r) rc = NGX_HTTP_INTERNAL_SERVER_ERROR; } + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); + if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) { ngx_log_error(level, log, err, - ngx_open_file_n " \"%s\" failed", name.data); + ngx_open_file_n " \"%s\" failed", path.data); } return rc; @@ -231,11 +154,11 @@ ngx_http_static_handler(ngx_http_request_t *r) if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, - ngx_fd_info_n " \"%s\" failed", name.data); + ngx_fd_info_n " \"%s\" failed", path.data); if (ngx_close_file(fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, - ngx_close_file_n " \"%s\" failed", name.data); + ngx_close_file_n " \"%s\" failed", path.data); } return NGX_HTTP_INTERNAL_SERVER_ERROR; @@ -247,23 +170,37 @@ ngx_http_static_handler(ngx_http_request_t *r) if (ngx_close_file(fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, - ngx_close_file_n " \"%s\" failed", name.data); + ngx_close_file_n " \"%s\" failed", path.data); } - *last++ = '/'; - *last = '\0'; - r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t)); if (r->headers_out.location == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); + + if (!clcf->alias) { + location = path.data + clcf->root.len; + + } else { + location = ngx_palloc(r->pool, r->uri.len + 1); + if (location == NULL) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; + } + + last = ngx_cpymem(location, r->uri.data, r->uri.len); + } + + *last = '/'; + /* * we do not need to set the r->headers_out.location->hash and * r->headers_out.location->key fields */ - r->headers_out.location->value = location; + r->headers_out.location->value.len = r->uri.len + 1; + r->headers_out.location->value.data = location; return NGX_HTTP_MOVED_PERMANENTLY; } @@ -272,11 +209,11 @@ ngx_http_static_handler(ngx_http_request_t *r) if (!ngx_is_file(&fi)) { ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, - "\"%s\" is not a regular file", name.data); + "\"%s\" is not a regular file", path.data); if (ngx_close_file(fd) == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, - ngx_close_file_n " \"%s\" failed", name.data); + ngx_close_file_n " \"%s\" failed", path.data); } return NGX_HTTP_NOT_FOUND; @@ -292,7 +229,7 @@ ngx_http_static_handler(ngx_http_request_t *r) } cln->fd = fd; - cln->name = name.data; + cln->name = path.data; cln->log = r->pool->log; if (ngx_pool_cleanup_add(r->pool, ngx_pool_cleanup_file, cln) == NULL) { @@ -349,7 +286,7 @@ ngx_http_static_handler(ngx_http_request_t *r) b->file_last = ngx_file_size(&fi); b->file->fd = fd; - b->file->name = name; + b->file->name = path; b->file->log = log; out.buf = b; |