- 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.
pull/4/head
Adrian Perez 2007-08-23 13:11:51 +02:00
parent 27a0fbf338
commit f3cfbd01b1
3 changed files with 94 additions and 56 deletions

View File

@ -179,7 +179,7 @@ ngx_module_t ngx_http_fancyindex_module = {
}; };
#include "templates/templates.inc" #include "templates/templates.h"
static ngx_int_t static ngx_int_t
@ -388,12 +388,10 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r)
ngx_close_dir_n " \"%s\" failed", &path); ngx_close_dir_n " \"%s\" failed", &path);
} }
len = NGX_HTTP_FANCYINDEX_TEMPLATE_SIZE len = NFI_TEMPLATE_SIZE
+ r->uri.len + r->uri.len /* URI is included two times: as <title> in the */
+ r->uri.len + r->uri.len /* HTML head and as an <h1> in the HTML body */
+ sizeof("</h1>") - 1 ;
+ sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1
+ sizeof("</pre><hr>") - 1;
entry = entries.elts; entry = entries.elts;
for (i = 0; i < entries.nelts; i++) { 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); ngx_http_fancyindex_cmp_entries);
} }
b->last = ngx_cpymem(b->last, t01_head1, sizeof(t01_head1) - 1); b->last = nfi_cpymem_ssz(b->last, t01_head1);
b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); b->last = nfi_cpymem_str(b->last, r->uri);
b->last = ngx_cpymem(b->last, t02_head2, sizeof(t02_head2) - 1); b->last = nfi_cpymem_ssz(b->last, t02_head2);
b->last = ngx_cpymem(b->last, t03_body1, sizeof(t03_body1) - 1); b->last = nfi_cpymem_ssz(b->last, t03_body1);
b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len); b->last = nfi_cpymem_str(b->last, r->uri);
b->last = ngx_cpymem(b->last, t03_body1, sizeof(t03_body1) - 1); b->last = nfi_cpymem_ssz(b->last, t04_body2);
b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1);
b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF, /* Output table header */
sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1); b->last = nfi_cpymem_ssz(b->last, t05_list1);
tp = ngx_timeofday(); tp = ngx_timeofday();
for (i = 0; i < entries.nelts; i++) { 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) { if (entry[i].escape) {
ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len, 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 (alcf->exact_size) {
if (entry[i].dir) { if (entry[i].dir) {
b->last = ngx_cpymem(b->last, " -", b->last = nfi_cpymem_ssz(b->last, " -");
sizeof(" -") - 1);
} else { } else {
b->last = ngx_sprintf(b->last, "%19O", entry[i].size); b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
} }
} else { } else {
if (entry[i].dir) { if (entry[i].dir) {
b->last = ngx_cpymem(b->last, " -", b->last = nfi_cpymem_ssz(b->last, " -");
sizeof(" -") - 1);
} else { } else {
length = entry[i].size; length = entry[i].size;
@ -558,9 +553,19 @@ ngx_http_fancyindex_handler(ngx_http_request_t *r)
/* TODO: free temporary pool */ /* 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) { if (r == r->main) {
b->last_buf = 1; b->last_buf = 1;

66
templates/templates.h Normal file
View File

@ -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
*/

View File

@ -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
*/