diff --git a/inliner.c b/inliner.c index da0917a..f5c6ea9 100644 --- a/inliner.c +++ b/inliner.c @@ -13,11 +13,56 @@ ngx_buf_t* nfi_inline_getbuf(ngx_http_request_t *r, const ngx_str_t const * path, ngx_int_t mode) { - ngx_buf_t *b; + void *dummy; + size_t root; + u_char *last; + ngx_str_t resolved; + ngx_buf_t *buf; + ngx_file_t *bfile; + ngx_file_info_t bfileinfo; - if ((b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t))) == NULL) + resolved.len = path->len; + resolved.data = ngx_palloc(r->pool, resolved.len + 1); + if (resolved.data == NULL) return NULL; - return b; + last = ngx_http_map_uri_to_path(r, &resolved, &root, 0); + if (last == NULL) + return NULL; + + /* + * Append final '\0' so we can use it to call ngx_file_info() below. + * + * If stat() fails, we know the file does *not* exist. We also check + * whether the file is readable. Note that if the file exists and is + * readable, the file info will be in the kernel inode cache so we + * do not incur in a lot of overhead by retrieving the information + * first. + */ + resolved.data[resolved.len] = '\0'; + if (ngx_file_info(resolved.data, &bfileinfo) != 0) + return NULL; + + if (!ngx_is_file(&bfileinfo) || /* we read regular files... */ + !(ngx_file_access(&bfileinfo) & 0444) /* ...which can be read... */ + || ngx_is_link(&bfileinfo)) /* ...and are not symlinks. */ + return NULL; + + bfile = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); + if (bfile == NULL) + return NULL; + + /* Fill in the file structure with sensible values. */ + bfile->fd = -1; + bfile->name.len = resolved.len; + bfile->name.data = resolved.data; + dummy = ngx_cpymem(&bfile->info, &bfileinfo, sizeof(ngx_file_info_t)); + bfile->valid_info = 1; + + buf = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); + if (buf == NULL) + return NULL; + + return buf; } diff --git a/ngx_http_fancyindex_module.c b/ngx_http_fancyindex_module.c index d41189e..2077286 100644 --- a/ngx_http_fancyindex_module.c +++ b/ngx_http_fancyindex_module.c @@ -50,20 +50,6 @@ typedef struct { -typedef struct { - ngx_flag_t enable; - ngx_flag_t localtime; - ngx_flag_t exact_size; - - ngx_str_t header; - ngx_str_t footer; - ngx_str_t readme; - - ngx_uint_t readme_flags; - ngx_uint_t include_mode; -} ngx_http_fancyindex_loc_conf_t; - - static ngx_inline ngx_str_t diff --git a/ngx_http_fancyindex_module.h b/ngx_http_fancyindex_module.h index 0823103..34f0ba6 100644 --- a/ngx_http_fancyindex_module.h +++ b/ngx_http_fancyindex_module.h @@ -31,6 +31,27 @@ #define NGX_HTTP_FANCYINDEX_INCLUDE_CACHED 1 + +/** + * Configuration structure for the fancyindex module. The configuration + * commands defined in the module do fill in the members of this structure. + */ +typedef struct { + ngx_flag_t enable; /**< Module is enabled. */ + ngx_flag_t localtime; /**< File mtime dates are sent in local time. */ + ngx_flag_t exact_size; /**< Sizes are sent always in bytes. */ + + ngx_str_t header; /**< File name for header, or empty if none. */ + ngx_str_t footer; /**< File name for footer, or empty if none. */ + ngx_str_t readme; /**< File name for readme, or empty if none. */ + + ngx_uint_t readme_flags; /**< Options for readme file inclusion. */ + ngx_uint_t include_mode; /**< Controls how to include footer/header. */ +} ngx_http_fancyindex_loc_conf_t; + + + + #define nfi_sizeof_ssz(_s) (sizeof(_s) - 1)