#include <utils.h>
#include <resp.h>
+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 == '<')
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, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n");
bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
bufcatstr(buf, "</head>\r\n");
bufcatstr(buf, "<body>\r\n");
bprintf(&buf, "<h1>%s</h1>\r\n", msg);
- bprintf(&buf, "<p>%s</p>\r\n", tmp2);
+ bprintf(&buf, "<p>%s</p>\r\n", htmlquote(tmp));
bufcatstr(buf, "</body>\r\n");
bufcatstr(buf, "</html>\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");
static int dispmtime = 0;
static int dispsize = 0;
+static char *stylesheet = NULL;
static void checkcache(struct stat *sb)
{
static void head(char *name, struct charbuf *dst)
{
- char *title;
+ char *title, *tmp;
+ title = sstrdup(htmlquote(name));
bprintf(dst, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
bprintf(dst, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
bprintf(dst, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
bprintf(dst, "<head>\n");
- title = htmlquote(name);
bprintf(dst, "<title>Index of %s</title>\n", title);
+ if(stylesheet) {
+ bprintf(dst, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n", htmlquote(tmp));
+ } else {
+ bprintf(dst, "<style>\n");
+ bprintf(dst, "body {font-family: sans-serif; background: #eee;}\n");
+ bprintf(dst, ".dirindex {background: white; padding: 1em; border: thin solid #ccc;}\n");
+ bprintf(dst, ".dirindex table {border-collapse: collapse;}\n");
+ bprintf(dst, ".dirindex td {padding: 0 1em;}\n");
+ bprintf(dst, ".dentry:hover {background: #ddd;}\n");
+ bprintf(dst, ".dir {background: #ddf;}\n");
+ bprintf(dst, ".exec {background: #dfd;}\n");
+ bprintf(dst, "</style>\n");
+ }
bprintf(dst, "</head>\n");
bprintf(dst, "<body>\n");
bprintf(dst, "<h1>Index of %s</h1>\n", title);
bufadd(dirbuf, f);
}
qsort(dirbuf.b, dirbuf.d, sizeof(struct dentry), dcmp);
- bprintf(dst, "<table class=\"dirindex\">\n");
+ bprintf(dst, "<div class=\"dirindex\"><table>\n");
for(i = 0; i < dirbuf.d; i++) {
bprintf(dst, "<tr class=\"dentry");
- if(S_ISDIR(dirbuf.b[i].sb.st_mode))
+ if(S_ISDIR(dirbuf.b[i].sb.st_mode)) {
bprintf(dst, " dir");
+ } else {
+ if(dirbuf.b[i].x)
+ bprintf(dst, " exec");
+ }
if(dirbuf.b[i].w)
bprintf(dst, " writable");
- if(dirbuf.b[i].x)
- bprintf(dst, " exec");
bprintf(dst, "\">");
fn = htmlquote(dirbuf.b[i].name);
bprintf(dst, "<td class=\"filename\"><a href=\"%s\">%s</a></td>", fn, fn);
- free(fn);
- if(dispsize && !S_ISDIR(dirbuf.b[i].sb.st_mode))
- bprintf(dst, "<td class=\"filesize\">%ji</td>", (intmax_t)dirbuf.b[i].sb.st_size);
+ if(dispsize) {
+ bprintf(dst, "<td class=\"filesize\">");
+ if(!S_ISDIR(dirbuf.b[i].sb.st_mode))
+ bprintf(dst, "%ji", (intmax_t)dirbuf.b[i].sb.st_size);
+ bprintf(dst, "</td>");
+ }
if(dispmtime)
bprintf(dst, "<td class=\"filemtime\">%s</td>", fmthttpdate(dirbuf.b[i].sb.st_mtime));
bprintf(dst, "</tr>\n");
free(dirbuf.b[i].name);
}
- bprintf(dst, "</table>\n");
+ bprintf(dst, "</table></div>\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)
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();
case 's':
dispsize = 1;
break;
+ case 'c':
+ stylesheet = optarg;
+ break;
default:
usage();
exit(1);