From 3095582d21be4087f774f5a68a2a9abfa37dd1ff Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 30 Aug 2010 00:56:57 +0200 Subject: [PATCH] Beautified the directory listings. --- lib/resp.c | 48 +++++++++++++++++++++++++++++++++++++++++------- lib/resp.h | 1 + src/htls.c | 46 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 76 insertions(+), 19 deletions(-) diff --git a/lib/resp.c b/lib/resp.c index 9d7325b..1b08b00 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -30,10 +30,45 @@ #include #include +static char safechars[128] = { + /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, +}; + +char *urlquote(char *text) +{ + static char *ret = NULL; + struct charbuf buf; + unsigned char c; + + if(ret != NULL) + free(ret); + bufinit(buf); + for(; *text; text++) { + c = *text; + if(!c < 128 && safechars[(int)c]) + bufadd(buf, *text); + else + bprintf(&buf, "%%%02X", (int)c); + } + bufadd(buf, 0); + return(ret = buf.b); +} + char *htmlquote(char *text) { + static char *ret = NULL; struct charbuf buf; + if(ret != NULL) + free(ret); bufinit(buf); for(; *text; text++) { if(*text == '<') @@ -42,25 +77,25 @@ char *htmlquote(char *text) bufcatstr(buf, ">"); else if(*text == '&') bufcatstr(buf, "&"); + else if(*text == '\"') + bufcatstr(buf, """); else bufadd(buf, *text); } bufadd(buf, 0); - return(buf.b); + return(ret = buf.b); } void simpleerror(int fd, int code, char *msg, char *fmt, ...) { struct charbuf buf; - char *tmp1, *tmp2; + char *tmp; va_list args; FILE *out; va_start(args, fmt); - tmp1 = vsprintf2(fmt, args); + tmp = vsprintf2(fmt, args); va_end(args); - tmp2 = htmlquote(tmp1); - free(tmp1); bufinit(buf); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); @@ -70,10 +105,9 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); bprintf(&buf, "

%s

\r\n", msg); - bprintf(&buf, "

%s

\r\n", tmp2); + bprintf(&buf, "

%s

\r\n", htmlquote(tmp)); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); - free(tmp2); out = fdopen(dup(fd), "w"); fprintf(out, "HTTP/1.1 %i %s\n", code, msg); fprintf(out, "Content-Type: text/html\n"); diff --git a/lib/resp.h b/lib/resp.h index 4578b03..8f18f87 100644 --- a/lib/resp.h +++ b/lib/resp.h @@ -3,6 +3,7 @@ #include +char *urlquote(char *text); char *htmlquote(char *text); void simpleerror(int fd, int code, char *msg, char *fmt, ...); void stdredir(struct hthead *req, int fd, int code, char *dst); diff --git a/src/htls.c b/src/htls.c index e16004d..060d1bf 100644 --- a/src/htls.c +++ b/src/htls.c @@ -43,6 +43,7 @@ struct dentry { static int dispmtime = 0; static int dispsize = 0; +static char *stylesheet = NULL; static void checkcache(struct stat *sb) { @@ -72,14 +73,27 @@ static int dcmp(const void *ap, const void *bp) static void head(char *name, struct charbuf *dst) { - char *title; + char *title, *tmp; + title = sstrdup(htmlquote(name)); bprintf(dst, "\n"); bprintf(dst, "\n"); bprintf(dst, "\n"); bprintf(dst, "\n"); - title = htmlquote(name); bprintf(dst, "Index of %s\n", title); + if(stylesheet) { + bprintf(dst, "\n", htmlquote(tmp)); + } else { + bprintf(dst, "\n"); + } bprintf(dst, "\n"); bprintf(dst, "\n"); bprintf(dst, "

Index of %s

\n", title); @@ -123,32 +137,37 @@ static void mkindex(char *name, DIR *dir, struct charbuf *dst) bufadd(dirbuf, f); } qsort(dirbuf.b, dirbuf.d, sizeof(struct dentry), dcmp); - bprintf(dst, "\n"); + bprintf(dst, "
\n"); for(i = 0; i < dirbuf.d; i++) { bprintf(dst, ""); fn = htmlquote(dirbuf.b[i].name); bprintf(dst, "", fn, fn); - free(fn); - if(dispsize && !S_ISDIR(dirbuf.b[i].sb.st_mode)) - bprintf(dst, "", (intmax_t)dirbuf.b[i].sb.st_size); + if(dispsize) { + bprintf(dst, ""); + } if(dispmtime) bprintf(dst, "", fmthttpdate(dirbuf.b[i].sb.st_mtime)); bprintf(dst, "\n"); free(dirbuf.b[i].name); } - bprintf(dst, "
%s%ji"); + if(!S_ISDIR(dirbuf.b[i].sb.st_mode)) + bprintf(dst, "%ji", (intmax_t)dirbuf.b[i].sb.st_size); + bprintf(dst, "%s
\n"); + bprintf(dst, "\n"); } static void usage(void) { - flog(LOG_ERR, "usage: htls [-hms] METHOD URL REST"); + flog(LOG_ERR, "usage: htls [-hms] [-c STYLESHEET] METHOD URL REST"); } int main(int argc, char **argv) @@ -160,7 +179,7 @@ int main(int argc, char **argv) struct stat sb; setlocale(LC_ALL, ""); - while((c = getopt(argc, argv, "hms")) >= 0) { + while((c = getopt(argc, argv, "hmsc:")) >= 0) { switch(c) { case 'h': usage(); @@ -171,6 +190,9 @@ int main(int argc, char **argv) case 's': dispsize = 1; break; + case 'c': + stylesheet = optarg; + break; default: usage(); exit(1); -- 2.11.0