version 1.25, 2015/12/29 04:21:46 |
version 1.25.8.2, 2019/06/12 10:32:00 |
|
|
/* $eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $ */ |
/* $eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $ */ |
|
|
/* |
/* |
* Copyright (c) 1997-2014 Matthew R. Green |
* Copyright (c) 1997-2019 Matthew R. Green |
* All rights reserved. |
* All rights reserved. |
* |
* |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
|
|
|
|
#include "bozohttpd.h" |
#include "bozohttpd.h" |
|
|
static void |
|
directory_hr(bozohttpd_t *httpd) |
|
{ |
|
|
|
bozo_printf(httpd, |
|
"<hr noshade align=\"left\" width=\"80%%\">\r\n\r\n"); |
|
} |
|
|
|
/* |
/* |
* output a directory index. return 1 if it actually did something.. |
* output a directory index. return 1 if it actually did something.. |
*/ |
*/ |
Line 62 bozo_dir_index(bozo_httpreq_t *request, |
|
Line 54 bozo_dir_index(bozo_httpreq_t *request, |
|
bozohttpd_t *httpd = request->hr_httpd; |
bozohttpd_t *httpd = request->hr_httpd; |
struct stat sb; |
struct stat sb; |
struct dirent **de, **deo; |
struct dirent **de, **deo; |
struct tm *tm; |
|
DIR *dp; |
DIR *dp; |
char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
char spacebuf[48]; |
char *file = NULL, *printname = NULL, *p; |
char *file = NULL, *printname = NULL; |
int k, j; |
int l, k, j, i; |
|
|
|
if (!isindex || !httpd->dir_indexing) |
if (!isindex || !httpd->dir_indexing) |
return 0; |
return 0; |
Line 80 bozo_dir_index(bozo_httpreq_t *request, |
|
Line 70 bozo_dir_index(bozo_httpreq_t *request, |
|
file[strlen(file) - strlen(httpd->index_html)] = '\0'; |
file[strlen(file) - strlen(httpd->index_html)] = '\0'; |
dirpath = file; |
dirpath = file; |
} |
} |
debug((httpd, DEBUG_FAT, "bozo_dir_index: dirpath ``%s''", dirpath)); |
debug((httpd, DEBUG_FAT, "bozo_dir_index: dirpath '%s'", dirpath)); |
if (stat(dirpath, &sb) < 0 || |
if (stat(dirpath, &sb) < 0 || |
(dp = opendir(dirpath)) == NULL) { |
(dp = opendir(dirpath)) == NULL) { |
if (errno == EPERM) |
if (errno == EPERM) |
(void)bozo_http_error(httpd, 403, request, |
bozo_http_error(httpd, 403, request, |
"no permission to open directory"); |
"no permission to open directory"); |
else if (errno == ENOENT) |
else if (errno == ENOENT) |
(void)bozo_http_error(httpd, 404, request, "no file"); |
bozo_http_error(httpd, 404, request, "no file"); |
else |
else |
(void)bozo_http_error(httpd, 500, request, |
bozo_http_error(httpd, 500, request, "open directory"); |
"open directory"); |
|
goto done; |
goto done; |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
Line 117 bozo_dir_index(bozo_httpreq_t *request, |
|
Line 106 bozo_dir_index(bozo_httpreq_t *request, |
|
#else |
#else |
printname = bozostrdup(httpd, request, request->hr_file); |
printname = bozostrdup(httpd, request, request->hr_file); |
#endif /* !NO_USER_SUPPORT */ |
#endif /* !NO_USER_SUPPORT */ |
|
if ((p = strstr(printname, httpd->index_html)) != NULL) { |
|
if (strcmp(printname, httpd->index_html) == 0) |
|
strcpy(printname, "/"); /* is ``slashdir'' */ |
|
else |
|
*p = '\0'; /* strip unwanted ``index_html'' */ |
|
} |
|
if ((p = bozo_escape_html(httpd, printname)) != NULL) { |
|
free(printname); |
|
printname = p; |
|
} |
|
|
bozo_printf(httpd, |
bozo_printf(httpd, |
"<html><head><title>Index of %s</title></head>\r\n", |
"<!DOCTYPE html>\r\n" |
|
"<html><head><meta charset=\"utf-8\"/>\r\n" |
|
"<style type=\"text/css\">\r\n" |
|
"table {\r\n" |
|
"\tborder-top: 1px solid black;\r\n" |
|
"\tborder-bottom: 1px solid black;\r\n" |
|
"}\r\n" |
|
"th { background: aquamarine; }\r\n" |
|
"tr:nth-child(even) { background: lavender; }\r\n" |
|
"</style>\r\n"); |
|
bozo_printf(httpd, "<title>Index of %s</title></head>\r\n", |
printname); |
printname); |
bozo_printf(httpd, "<body><h1>Index of %s</h1>\r\n", |
bozo_printf(httpd, "<body><h1>Index of %s</h1>\r\n", |
printname); |
printname); |
bozo_printf(httpd, "<pre>\r\n"); |
bozo_printf(httpd, |
#define NAMELEN 40 |
"<table cols=3>\r\n<thead>\r\n" |
#define LMODLEN 19 |
"<tr><th>Name<th>Last modified<th align=right>Size\r\n" |
bozo_printf(httpd, "Name " |
"<tbody>\r\n"); |
"Last modified " |
|
"Size\n"); |
|
bozo_printf(httpd, "</pre>"); |
|
directory_hr(httpd); |
|
bozo_printf(httpd, "<pre>"); |
|
|
|
for (j = k = scandir(dirpath, &de, NULL, alphasort), deo = de; |
for (j = k = scandir(dirpath, &de, NULL, alphasort), deo = de; |
j--; de++) { |
j--; de++) { |
Line 144 bozo_dir_index(bozo_httpreq_t *request, |
|
Line 148 bozo_dir_index(bozo_httpreq_t *request, |
|
httpd->hide_dots && name[0] == '.')) |
httpd->hide_dots && name[0] == '.')) |
continue; |
continue; |
|
|
|
if (bozo_check_special_files(request, name, false)) |
|
continue; |
|
|
snprintf(buf, sizeof buf, "%s/%s", dirpath, name); |
snprintf(buf, sizeof buf, "%s/%s", dirpath, name); |
if (stat(buf, &sb)) |
if (stat(buf, &sb)) |
nostat = 1; |
nostat = 1; |
|
|
l = 0; |
|
|
|
urlname = bozo_escape_rfc3986(httpd, name, 0); |
urlname = bozo_escape_rfc3986(httpd, name, 0); |
htmlname = bozo_escape_html(httpd, name); |
htmlname = bozo_escape_html(httpd, name); |
if (htmlname == NULL) |
if (htmlname == NULL) |
htmlname = name; |
htmlname = name; |
|
bozo_printf(httpd, "<tr><td>"); |
if (strcmp(name, "..") == 0) { |
if (strcmp(name, "..") == 0) { |
bozo_printf(httpd, "<a href=\"../\">"); |
bozo_printf(httpd, "<a href=\"../\">"); |
l += bozo_printf(httpd, "Parent Directory"); |
bozo_printf(httpd, "Parent Directory"); |
} else if (S_ISDIR(sb.st_mode)) { |
} else if (!nostat && S_ISDIR(sb.st_mode)) { |
bozo_printf(httpd, "<a href=\"%s/\">", urlname); |
bozo_printf(httpd, "<a href=\"%s/\">", urlname); |
l += bozo_printf(httpd, "%s/", htmlname); |
bozo_printf(httpd, "%s/", htmlname); |
} else if (strchr(name, ':') != NULL) { |
} else if (strchr(name, ':') != NULL) { |
/* RFC 3986 4.2 */ |
/* RFC 3986 4.2 */ |
bozo_printf(httpd, "<a href=\"./%s\">", urlname); |
bozo_printf(httpd, "<a href=\"./%s\">", urlname); |
l += bozo_printf(httpd, "%s", htmlname); |
bozo_printf(httpd, "%s", htmlname); |
} else { |
} else { |
bozo_printf(httpd, "<a href=\"%s\">", urlname); |
bozo_printf(httpd, "<a href=\"%s\">", urlname); |
l += bozo_printf(httpd, "%s", htmlname); |
bozo_printf(httpd, "%s", htmlname); |
} |
} |
if (htmlname != name) |
if (htmlname != name) |
free(htmlname); |
free(htmlname); |
bozo_printf(httpd, "</a>"); |
bozo_printf(httpd, "</a>"); |
|
|
/* NAMELEN spaces */ |
|
/*LINTED*/ |
|
assert(/*CONSTCOND*/sizeof(spacebuf) > NAMELEN); |
|
i = (l < NAMELEN) ? (NAMELEN - l) : 0; |
|
i++; |
|
memset(spacebuf, ' ', (size_t)i); |
|
spacebuf[i] = '\0'; |
|
bozo_printf(httpd, "%s", spacebuf); |
|
l += i; |
|
|
|
if (nostat) |
if (nostat) |
bozo_printf(httpd, "? ?"); |
bozo_printf(httpd, "<td>?<td>?\r\n"); |
else { |
else { |
tm = gmtime(&sb.st_mtime); |
unsigned long long len; |
strftime(buf, sizeof buf, "%d-%b-%Y %R", tm); |
|
l += bozo_printf(httpd, "%s", buf); |
strftime(buf, sizeof buf, "%d-%b-%Y %R", gmtime(&sb.st_mtime)); |
|
bozo_printf(httpd, "<td>%s", buf); |
/* LMODLEN spaces */ |
|
/*LINTED*/ |
|
assert(/*CONSTCOND*/sizeof(spacebuf) > LMODLEN); |
|
i = (l < (LMODLEN+NAMELEN+1)) ? |
|
((LMODLEN+NAMELEN+1) - l) : 0; |
|
i++; |
|
memset(spacebuf, ' ', (size_t)i); |
|
spacebuf[i] = '\0'; |
|
bozo_printf(httpd, "%s", spacebuf); |
|
|
|
bozo_printf(httpd, "%12llukB", |
len = ((unsigned long long)sb.st_size + 1023) / 1024; |
(unsigned long long)sb.st_size >> 10); |
bozo_printf(httpd, "<td align=right>%llukB", len); |
} |
} |
bozo_printf(httpd, "\r\n"); |
bozo_printf(httpd, "\r\n"); |
} |
} |
Line 209 bozo_dir_index(bozo_httpreq_t *request, |
|
Line 196 bozo_dir_index(bozo_httpreq_t *request, |
|
while (k--) |
while (k--) |
free(deo[k]); |
free(deo[k]); |
free(deo); |
free(deo); |
bozo_printf(httpd, "</pre>"); |
bozo_printf(httpd, "</table>\r\n"); |
directory_hr(httpd); |
|
bozo_printf(httpd, "</body></html>\r\n\r\n"); |
bozo_printf(httpd, "</body></html>\r\n\r\n"); |
bozo_flush(httpd, stdout); |
bozo_flush(httpd, stdout); |
|
|
|
|
return 1; |
return 1; |
} |
} |
#endif /* NO_DIRINDEX_SUPPORT */ |
#endif /* NO_DIRINDEX_SUPPORT */ |
|
|