From f3cfbd01b1e4a37fa57dba9e0120f9db31534df0 Mon Sep 17 00:00:00 2001 From: Adrian Perez Date: Thu, 23 Aug 2007 13:11:51 +0200 Subject: [PATCH] - Added missing template pieces into templates/templates.h - Renamed some ngx_http_fancyindex_* symbols to nfi_* ones (shorter :P) - Defined some memory-copy utility macros in templates/templates.h - Updated output size calculation. --- ngx_http_fancyindex_module.c | 51 +++++++++++++++------------- templates/templates.h | 66 ++++++++++++++++++++++++++++++++++++ templates/templates.inc | 33 ------------------ 3 files changed, 94 insertions(+), 56 deletions(-) create mode 100644 templates/templates.h delete mode 100644 templates/templates.inc diff --git a/ngx_http_fancyindex_module.c b/ngx_http_fancyindex_module.c index 35f4c3e..7ff7435 100644 --- a/ngx_http_fancyindex_module.c +++ b/ngx_http_fancyindex_module.c @@ -179,7 +179,7 @@ ngx_module_t ngx_http_fancyindex_module = { }; -#include "templates/templates.inc" +#include "templates/templates.h" static ngx_int_t @@ -388,12 +388,10 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) ngx_close_dir_n " \"%s\" failed", &path); } - len = NGX_HTTP_FANCYINDEX_TEMPLATE_SIZE - + r->uri.len - + r->uri.len - + sizeof("") - 1 - + sizeof("
../" CRLF) - 1
-          + sizeof("

") - 1; + len = NFI_TEMPLATE_SIZE + + r->uri.len /* URI is included two times: as in the */ + + r->uri.len /* HTML head and as an <h1> in the HTML body */ + ; entry = entries.elts; for (i = 0; i < entries.nelts; i++) { @@ -420,22 +418,21 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) ngx_http_fancyindex_cmp_entries); } - b->last = ngx_cpymem(b->last, t01_head1, sizeof(t01_head1) - 1); - b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); - b->last = ngx_cpymem(b->last, t02_head2, sizeof(t02_head2) - 1); + b->last = nfi_cpymem_ssz(b->last, t01_head1); + b->last = nfi_cpymem_str(b->last, r->uri); + b->last = nfi_cpymem_ssz(b->last, t02_head2); - b->last = ngx_cpymem(b->last, t03_body1, sizeof(t03_body1) - 1); - b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); - b->last = ngx_cpymem(b->last, t03_body1, sizeof(t03_body1) - 1); - b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1); + b->last = nfi_cpymem_ssz(b->last, t03_body1); + b->last = nfi_cpymem_str(b->last, r->uri); + b->last = nfi_cpymem_ssz(b->last, t04_body2); - b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, - sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); + /* Output table header */ + b->last = nfi_cpymem_ssz(b->last, t05_list1); tp = ngx_timeofday(); for (i = 0; i < entries.nelts; i++) { - b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1); + b->last = nfi_cpymem_ssz(b->last, "<a href=\""); if (entry[i].escape) { ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, @@ -501,16 +498,14 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) if (alcf->exact_size) { if (entry[i].dir) { - b->last = ngx_cpymem(b->last, " -", - sizeof(" -") - 1); + b->last = nfi_cpymem_ssz(b->last, " -"); } else { b->last = ngx_sprintf(b->last, "%19O", entry[i].size); } } else { if (entry[i].dir) { - b->last = ngx_cpymem(b->last, " -", - sizeof(" -") - 1); + b->last = nfi_cpymem_ssz(b->last, " -"); } else { length = entry[i].size; @@ -558,9 +553,19 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r) /* TODO: free temporary pool */ - b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); + /* Output table bottom */ + b->last = nfi_cpymem_ssz(b->last, t06_list2); - b->last = ngx_cpymem(b->last, t09_foot1, sizeof(t09_foot1) - 1); + /* + * Output body end, including readme if requested. The t07_body3 and + * t08_body4 templates may be empty, so use ngx_http_fancyindex_tcpy_if + * TODO: Output readme + */ + b->last = nfi_cpymem_ssz(b->last, t07_body3); + b->last = nfi_cpymem_ssz(b->last, t08_body4); + + /* Output page footer */ + b->last = nfi_cpymem_ssz(b->last, t09_foot1); if (r == r->main) { b->last_buf = 1; diff --git a/templates/templates.h b/templates/templates.h new file mode 100644 index 0000000..a87a899 --- /dev/null +++ b/templates/templates.h @@ -0,0 +1,66 @@ + +#ifndef __templates_inc__ +#define __templates_inc__ + +static const u_char t01_head1[] = +#include "01-head1.inc" +; +static const u_char t02_head2[] = +#include "02-head2.inc" +; +static const u_char t03_body1[] = +#include "03-body1.inc" +; +static const u_char t04_body2[] = +#include "04-body2.inc" +; +static const u_char t05_list1[] = +#include "05-list1.inc" +; +static const u_char t06_list2[] = +#include "06-list2.inc" +; +static const u_char t07_body3[] = +#include "07-body3.inc" +; +static const u_char t08_body4[] = +#include "08-body4.inc" +; +static const u_char t09_foot1[] = +#include "09-foot1.inc" +; + +#define NFI_TEMPLATE_SIZE \ + ( (sizeof(t01_head1) - 1) \ + + (sizeof(t02_head2) - 1) \ + + (sizeof(t03_body1) - 1) \ + + (sizeof(t04_body2) - 1) \ + + (sizeof(t05_list1) - 1) \ + + (sizeof(t06_list2) - 1) \ + + (sizeof(t07_body3) - 1) \ + + (sizeof(t08_body4) - 1) \ + + (sizeof(t09_foot1) - 1) ) + +/** + * Copy a static zero-terminated string. Useful to output template + * string pieces into a temporary buffer. + */ +#define nfi_cpymem_ssz(_p, _t) \ + (ngx_cpymem((_p), (_t), sizeof(_t) - 1)) + +/** + * Copy a ngx_str_t. + */ +#define nfi_cpymem_str(_p, _s) \ + (ngx_cpymem((_p), (_s).data, (_s).len)) + +/** + * Copy a static zero-terminated string, but only if the string is + * non-empty. Using this may avoid jumping into ngx_cpymem(). + */ +#define nfi_maybe_cpymem_ssz(__p, __t) \ + if ((__t)[0] != '\0') nfi_cpymem_ssz((__p), (__t)) + +#endif /* !__templates_inc__ */ +/* vim:ft=c + */ diff --git a/templates/templates.inc b/templates/templates.inc deleted file mode 100644 index 1ad9cc8..0000000 --- a/templates/templates.inc +++ /dev/null @@ -1,33 +0,0 @@ - -#ifndef __templates_inc__ -#define __templates_inc__ - -static const u_char t01_head1[] = -#include "01-head1.inc" -; -static const u_char t02_head2[] = -#include "02-head2.inc" -; -static const u_char t03_body1[] = -#include "03-body1.inc" -; -static const u_char t04_body2[] = -#include "04-body2.inc" -; - - -static const u_char t09_foot1[] = -#include "09-foot1.inc" -; - -#define NGX_HTTP_FANCYINDEX_TEMPLATE_SIZE \ - ( (sizeof(t01_head1) - 1) \ - + (sizeof(t02_head2) - 1) \ - + (sizeof(t03_body1) - 1) \ - + (sizeof(t04_body2) - 1) \ - \ - + (sizeof(t09_foot1) - 1) ) - -#endif /* !__templates_inc__ */ -/* vim:ft=c - */