Implemented sending README as an iframe

pull/4/head
Adrian Perez 2007-08-28 14:38:39 +02:00
parent c2f086207b
commit e73cb8e3bc
3 changed files with 102 additions and 4 deletions

5
.todo
View File

@ -1,6 +1,9 @@
<todo version="0.1.19"> <todo version="0.1.19">
<note priority="medium" time="1187975642"> <note priority="medium" time="1187975642" done="1188304701">
readme:iframe readme:iframe
<comment>
done in r23
</comment>
</note> </note>
<note priority="medium" time="1187975645"> <note priority="medium" time="1187975645">
readme:div readme:div

View File

@ -84,6 +84,9 @@ typedef struct {
#define NGX_HTTP_FANCYINDEX_NAME_LEN 50 #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, static int ngx_libc_cdecl ngx_http_fancyindex_cmp_entries(const void *one,
const void *two); const void *two);
static ngx_int_t ngx_http_fancyindex_error(ngx_http_request_t *r, 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_buf_t *b;
ngx_int_t rc, size; ngx_int_t rc, size;
ngx_str_t path; ngx_str_t path;
ngx_str_t readme_path;
ngx_dir_t dir; ngx_dir_t dir;
ngx_uint_t i, level; ngx_uint_t i, level;
ngx_pool_t *pool; ngx_pool_t *pool;
@ -292,11 +296,9 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r)
} }
#if (NGX_SUPPRESS_WARN) #if (NGX_SUPPRESS_WARN)
/* MSVC thinks 'entries' may be used without having been initialized */ /* MSVC thinks 'entries' may be used without having been initialized */
ngx_memzero(&entries, sizeof(ngx_array_t)); ngx_memzero(&entries, sizeof(ngx_array_t));
#endif /* NGX_SUPPRESS_WARN */
#endif
/* TODO: pool should be temporary pool */ /* TODO: pool should be temporary pool */
pool = r->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 <h1> in the HTML body */ + r->uri.len /* HTML head and as an <h1> in the HTML body */
; ;
/*
* If including an <iframe> for the readme file, add the length of the
* URI, plus the length of the readme file name and the length of the
* needed markup.
*/
readme_path = nfi_get_readme_path(r, &path);
if (readme_path.len == 0) goto skip_readme_len;
if (nfi_has_flag(alcf->readme_flags, NGX_HTTP_FANCYINDEX_README_IFRAME)) {
len += 3 /* CR+LF+'/' */
+ nfi_sizeof_ssz("<iframe id=\"readme\" src=\"")
+ r->uri.len + alcf->readme.len
+ nfi_sizeof_ssz("\">(readme file)</iframe>")
;
}
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; entry = entries.elts;
for (i = 0; i < entries.nelts; i++) { 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_str(b->last, r->uri);
b->last = nfi_cpymem_ssz(b->last, t04_body2); 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, "<iframe id=\"readme\" src=\""); \
b->last = nfi_cpymem_str(b->last, r->uri); \
*b->last++ = '/'; \
b->last = nfi_cpymem_str(b->last, alcf->readme); \
b->last = nfi_cpymem_ssz(b->last, "\">(readme file)</iframe>"); \
*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 */ /* Output table header */
b->last = nfi_cpymem_ssz(b->last, t05_list1); 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 * TODO: Output readme
*/ */
b->last = nfi_cpymem_ssz(b->last, t07_body3); 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); b->last = nfi_cpymem_ssz(b->last, t08_body4);
/* Output page footer */ /* Output page footer */
@ -676,6 +742,32 @@ ngx_http_fancyindex_alloc(ngx_http_fancyindex_ctx_t *ctx, size_t size)
#endif #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 static ngx_int_t
ngx_http_fancyindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name) ngx_http_fancyindex_error(ngx_http_request_t *r, ngx_dir_t *dir, ngx_str_t *name)
{ {

View File

@ -34,6 +34,9 @@
#define nfi_maybe_cpymem_ssz(__p, __t) \ #define nfi_maybe_cpymem_ssz(__p, __t) \
if ((__t)[0] != '\0') nfi_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__ */ #endif /* !__ngx_http_fancyindex_module_h__ */
/* vim:ft=c /* vim:ft=c
*/ */