diff --git a/.todo b/.todo index 0fa486b..7195972 100644 --- a/.todo +++ b/.todo @@ -1,6 +1,9 @@ - + readme:iframe + + done in r23 + readme:div diff --git a/ngx_http_fancyindex_module.c b/ngx_http_fancyindex_module.c index d2f07da..d4dbf0f 100644 --- a/ngx_http_fancyindex_module.c +++ b/ngx_http_fancyindex_module.c @@ -84,6 +84,9 @@ typedef struct { #define NGX_HTTP_FANCYINDEX_NAME_LEN 50 +static ngx_inline ngx_str_t + nfi_get_readme_path(ngx_http_request_t *r, const ngx_str_t *last); + static int ngx_libc_cdecl ngx_http_fancyindex_cmp_entries(const void *one, const void *two); static ngx_int_t ngx_http_fancyindex_error(ngx_http_request_t *r, @@ -220,6 +223,7 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) ngx_buf_t *b; ngx_int_t rc, size; ngx_str_t path; + ngx_str_t readme_path; ngx_dir_t dir; ngx_uint_t i, level; ngx_pool_t *pool; @@ -292,11 +296,9 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) } #if (NGX_SUPPRESS_WARN) - /* MSVC thinks 'entries' may be used without having been initialized */ ngx_memzero(&entries, sizeof(ngx_array_t)); - -#endif +#endif /* NGX_SUPPRESS_WARN */ /* TODO: pool should be temporary pool */ pool = r->pool; @@ -419,6 +421,29 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) + r->uri.len /* HTML head and as an

in the HTML body */ ; + /* + * If including an ") + ; + } + else { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "fancyindex: bad readme_flags combination %#x", + alcf->readme_flags); + } + + +skip_readme_len: entry = entries.elts; for (i = 0; i < entries.nelts; i++) { /* @@ -463,6 +488,32 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) b->last = nfi_cpymem_str(b->last, r->uri); b->last = nfi_cpymem_ssz(b->last, t04_body2); + /* Insert readme at top, if appropriate */ + if ((readme_path.len == 0) || + !nfi_has_flag(alcf->readme_flags, NGX_HTTP_FANCYINDEX_README_TOP)) + goto skip_readme_top; + +#define nfi_add_readme_iframe( ) \ + do { \ + b->last = nfi_cpymem_ssz(b->last, ""); \ + *b->last++ = CR; \ + *b->last++ = LF; \ + } while (0) + + if (nfi_has_flag(alcf->readme_flags, NGX_HTTP_FANCYINDEX_README_IFRAME)) + nfi_add_readme_iframe(); + else { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "fancyindex: bad readme_flags combination %#x", + alcf->readme_flags); + } + +skip_readme_top: + /* Output table header */ b->last = nfi_cpymem_ssz(b->last, t05_list1); @@ -600,6 +651,21 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) * TODO: Output readme */ b->last = nfi_cpymem_ssz(b->last, t07_body3); + + /* Insert readme at bottom, if appropriate */ + if ((readme_path.len == 0) || + !nfi_has_flag(alcf->readme_flags, NGX_HTTP_FANCYINDEX_README_BOTTOM)) + goto skip_readme_bottom; + + if (nfi_has_flag(alcf->readme_flags, NGX_HTTP_FANCYINDEX_README_IFRAME)) + nfi_add_readme_iframe(); + else { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "fancyindex: bad readme_flags combination %#x", + alcf->readme_flags); + } + +skip_readme_bottom: b->last = nfi_cpymem_ssz(b->last, t08_body4); /* Output page footer */ @@ -676,6 +742,32 @@ ngx_http_fancyindex_alloc(ngx_http_fancyindex_ctx_t *ctx, size_t size) #endif +static ngx_inline ngx_str_t +nfi_get_readme_path(ngx_http_request_t *r, const ngx_str_t *path) +{ + u_char *last; + ngx_file_info_t info; + ngx_str_t fullpath = ngx_null_string; + ngx_http_fancyindex_loc_conf_t *alcf = + ngx_http_get_module_loc_conf(r, ngx_http_fancyindex_module); + + if (alcf->readme.len == 0) /* Readme files are disabled */ + return fullpath; + + fullpath.len = path->len + 2 + alcf->readme.len; + fullpath.data = ngx_palloc(r->pool, fullpath.len); + + last = nfi_cpymem_str(fullpath.data, *path); *last++ = '/'; + last = nfi_cpymem_str(last, alcf->readme); *last++ = '\0'; + + /* File does not exists, or cannot be accessed */ + if (ngx_file_info(fullpath.data, &info) != 0) + fullpath.len = 0; + + return fullpath; +} + + static ngx_int_t ngx_http_fancyindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) { diff --git a/ngx_http_fancyindex_module.h b/ngx_http_fancyindex_module.h index 915665e..16f8fbe 100644 --- a/ngx_http_fancyindex_module.h +++ b/ngx_http_fancyindex_module.h @@ -34,6 +34,9 @@ #define nfi_maybe_cpymem_ssz(__p, __t) \ if ((__t)[0] != '\0') nfi_cpymem_ssz((__p), (__t)) +#define nfi_has_flag(_where, _what) \ + (((_where) & (_what)) == (_what)) + #endif /* !__ngx_http_fancyindex_module_h__ */ /* vim:ft=c */