2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
44 static int dispmtime = 0;
45 static int dispsize = 0;
46 static char *stylesheet = NULL;
48 static void checkcache(struct stat *sb)
52 if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
53 if(parsehttpdate(hdr) < sb->st_mtime)
55 printf("HTTP/1.1 304 Not Modified\n");
56 printf("Date: %s\n", fmthttpdate(time(NULL)));
57 printf("Content-Length: 0\n");
63 static int dcmp(const void *ap, const void *bp)
65 const struct dentry *a = ap, *b = bp;
67 if(S_ISDIR(a->sb.st_mode) && !S_ISDIR(b->sb.st_mode))
69 if(!S_ISDIR(a->sb.st_mode) && S_ISDIR(b->sb.st_mode))
71 return(strcoll(a->name, b->name));
74 static void head(char *name, struct charbuf *dst)
78 title = sstrdup(htmlquote(name));
79 bprintf(dst, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
80 bprintf(dst, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
81 bprintf(dst, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
82 bprintf(dst, "<head>\n");
83 bprintf(dst, "<title>Index of %s</title>\n", title);
85 bprintf(dst, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n", htmlquote(stylesheet));
87 bprintf(dst, "<style type=\"text/css\">\n");
88 bprintf(dst, "body {font-family: sans-serif; background: #eee;}\n");
89 bprintf(dst, ".dirindex {background: white; padding: 1em; border: thin solid #ccc;}\n");
90 bprintf(dst, ".dirindex table {border-collapse: collapse;}\n");
91 bprintf(dst, ".dirindex td {padding: 0 1em;}\n");
92 bprintf(dst, ".dentry:hover {background: #ddd;}\n");
93 bprintf(dst, ".dir {background: #ddf;}\n");
94 bprintf(dst, ".dir .filename:before {content: \"\342\206\263 \"}\n");
95 bprintf(dst, ".exec {background: #dfd;}\n");
96 bprintf(dst, "</style>\n");
98 bprintf(dst, "</head>\n");
99 bprintf(dst, "<body>\n");
100 bprintf(dst, "<h1>Index of %s</h1>\n", title);
104 static void foot(struct charbuf *dst)
106 bprintf(dst, "</body>\n");
107 bprintf(dst, "</html>\n");
110 static void mkindex(char *name, DIR *dir, struct charbuf *dst)
122 while((dent = readdir(dir)) != NULL) {
123 if(*dent->d_name == '.')
125 memset(&f, 0, sizeof(f));
126 f.name = sstrdup(dent->d_name);
127 fn = sprintf3("%s/%s", name, dent->d_name);
132 if(!access(fn, W_OK))
134 if(!access(fn, X_OK))
136 else if(S_ISDIR(f.sb.st_mode))
140 qsort(dirbuf.b, dirbuf.d, sizeof(struct dentry), dcmp);
141 bprintf(dst, "<div class=\"dirindex\"><table>\n");
142 for(i = 0; i < dirbuf.d; i++) {
143 bprintf(dst, "<tr class=\"dentry");
144 if(S_ISDIR(dirbuf.b[i].sb.st_mode)) {
145 bprintf(dst, " dir");
148 bprintf(dst, " exec");
151 bprintf(dst, " writable");
153 bprintf(dst, "<td class=\"filename\"><a href=\"%s\">", htmlquote(urlquote(dirbuf.b[i].name)));
154 bprintf(dst, "%s", htmlquote(dirbuf.b[i].name));
155 bprintf(dst, "</a></td>");
157 bprintf(dst, "<td class=\"filesize\">");
158 if(!S_ISDIR(dirbuf.b[i].sb.st_mode))
159 bprintf(dst, "%ji", (intmax_t)dirbuf.b[i].sb.st_size);
160 bprintf(dst, "</td>");
163 bprintf(dst, "<td class=\"filemtime\">%s</td>", fmthttpdate(dirbuf.b[i].sb.st_mtime));
164 bprintf(dst, "</tr>\n");
165 free(dirbuf.b[i].name);
167 bprintf(dst, "</table></div>\n");
170 static void usage(void)
172 flog(LOG_ERR, "usage: htls [-hms] [-c STYLESHEET] METHOD URL REST");
175 int main(int argc, char **argv)
183 setlocale(LC_ALL, "");
184 while((c = getopt(argc, argv, "hmsc:")) >= 0) {
203 if(argc - optind < 3) {
207 if((dname = getenv("REQ_X_ASH_FILE")) == NULL) {
208 flog(LOG_ERR, "htls: needs to be called with the X-Ash-File header");
211 if(*argv[optind + 2]) {
212 simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
216 if(stat(dname, &sb) || ((dir = opendir(dname)) == NULL)) {
217 flog(LOG_ERR, "htls: could not open directory `%s': %s", dname, strerror(errno));
218 simpleerror(1, 500, "Server Error", "Could not produce directory index.");
224 head(argv[optind + 1], &buf);
225 mkindex(dname, dir, &buf);
229 printf("HTTP/1.1 200 OK\n");
230 printf("Content-Type: text/html; charset=UTF-8\n");
231 printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
232 printf("Content-Length: %zi\n", buf.d);
234 fwrite(buf.b, 1, buf.d, stdout);