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("
in the */
+ + r->uri.len /* HTML head and as an 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, "
", sizeof("") - 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, "
../" CRLF,
- sizeof("
../" 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, "last = nfi_cpymem_ssz(b->last, "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, "
", sizeof("
") - 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
- */