htls: Remember to quote URL characters in filenames.
[ashd.git] / src / htls.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
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.
9
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.
14
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/>.
17 */
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <dirent.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <time.h>
27 #include <stdint.h>
28 #include <locale.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <utils.h>
34 #include <resp.h>
35 #include <log.h>
36 #include <resp.h>
37
38 struct dentry {
39     char *name;
40     struct stat sb;
41     int w, x;
42 };
43
44 static int dispmtime = 0;
45 static int dispsize = 0;
46 static char *stylesheet = NULL;
47
48 static void checkcache(struct stat *sb)
49 {
50     char *hdr;
51     
52     if((hdr = getenv("REQ_IF_MODIFIED_SINCE")) != NULL) {
53         if(parsehttpdate(hdr) < sb->st_mtime)
54             return;
55         printf("HTTP/1.1 304 Not Modified\n");
56         printf("Date: %s\n", fmthttpdate(time(NULL)));
57         printf("Content-Length: 0\n");
58         printf("\n");
59         exit(0);
60     }
61 }
62
63 static int dcmp(const void *ap, const void *bp)
64 {
65     const struct dentry *a = ap, *b = bp;
66     
67     if(S_ISDIR(a->sb.st_mode) && !S_ISDIR(b->sb.st_mode))
68         return(-1);
69     if(!S_ISDIR(a->sb.st_mode) && S_ISDIR(b->sb.st_mode))
70         return(1);
71     return(strcoll(a->name, b->name));
72 }
73
74 static void head(char *name, struct charbuf *dst)
75 {
76     char *title;
77     
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);
84     if(stylesheet) {
85         bprintf(dst, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n", htmlquote(stylesheet));
86     } else {
87         bprintf(dst, "<style>\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, ".exec {background: #dfd;}\n");
95         bprintf(dst, "</style>\n");
96     }
97     bprintf(dst, "</head>\n");
98     bprintf(dst, "<body>\n");
99     bprintf(dst, "<h1>Index of %s</h1>\n", title);
100     free(title);
101 }
102
103 static void foot(struct charbuf *dst)
104 {
105     bprintf(dst, "</body>\n");
106     bprintf(dst, "</html>\n");
107 }
108
109 static void mkindex(char *name, DIR *dir, struct charbuf *dst)
110 {
111     struct {
112         struct dentry *b;
113         size_t s, d;
114     } dirbuf;
115     struct dentry f;
116     struct dirent *dent;
117     char *fn;
118     int i;
119     
120     bufinit(dirbuf);
121     while((dent = readdir(dir)) != NULL) {
122         if(*dent->d_name == '.')
123             continue;
124         memset(&f, 0, sizeof(f));
125         f.name = sstrdup(dent->d_name);
126         fn = sprintf3("%s/%s", name, dent->d_name);
127         if(access(fn, R_OK))
128             continue;
129         if(stat(fn, &f.sb))
130             continue;
131         if(!access(fn, W_OK))
132             f.w = 1;
133         if(!access(fn, X_OK))
134             f.x = 1;
135         else if(S_ISDIR(f.sb.st_mode))
136             continue;
137         bufadd(dirbuf, f);
138     }
139     qsort(dirbuf.b, dirbuf.d, sizeof(struct dentry), dcmp);
140     bprintf(dst, "<div class=\"dirindex\"><table>\n");
141     for(i = 0; i < dirbuf.d; i++) {
142         bprintf(dst, "<tr class=\"dentry");
143         if(S_ISDIR(dirbuf.b[i].sb.st_mode)) {
144             bprintf(dst, " dir");
145         } else {
146             if(dirbuf.b[i].x)
147                 bprintf(dst, " exec");
148         }
149         if(dirbuf.b[i].w)
150             bprintf(dst, " writable");
151         bprintf(dst, "\">");    
152         bprintf(dst, "<td class=\"filename\"><a href=\"%s\">", htmlquote(urlquote(dirbuf.b[i].name)));
153         bprintf(dst, "%s", htmlquote(dirbuf.b[i].name));
154         bprintf(dst, "</a></td>");
155         if(dispsize) {
156             bprintf(dst, "<td class=\"filesize\">");
157             if(!S_ISDIR(dirbuf.b[i].sb.st_mode))
158                 bprintf(dst, "%ji", (intmax_t)dirbuf.b[i].sb.st_size);
159             bprintf(dst, "</td>");
160         }
161         if(dispmtime)
162             bprintf(dst, "<td class=\"filemtime\">%s</td>", fmthttpdate(dirbuf.b[i].sb.st_mtime));
163         bprintf(dst, "</tr>\n");
164         free(dirbuf.b[i].name);
165     }
166     bprintf(dst, "</table></div>\n");
167 }
168
169 static void usage(void)
170 {
171     flog(LOG_ERR, "usage: htls [-hms] [-c STYLESHEET] METHOD URL REST");
172 }
173
174 int main(int argc, char **argv)
175 {
176     int c;
177     char *dname;
178     DIR *dir;
179     struct charbuf buf;
180     struct stat sb;
181     
182     setlocale(LC_ALL, "");
183     while((c = getopt(argc, argv, "hmsc:")) >= 0) {
184         switch(c) {
185         case 'h':
186             usage();
187             exit(0);
188         case 'm':
189             dispmtime = 1;
190             break;
191         case 's':
192             dispsize = 1;
193             break;
194         case 'c':
195             stylesheet = optarg;
196             break;
197         default:
198             usage();
199             exit(1);
200         }
201     }
202     if(argc - optind < 3) {
203         usage();
204         exit(1);
205     }
206     if((dname = getenv("REQ_X_ASH_FILE")) == NULL) {
207         flog(LOG_ERR, "htls: needs to be called with the X-Ash-File header");
208         exit(1);
209     }
210     if(*argv[optind + 2]) {
211         simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
212         exit(0);
213     }
214
215     if(stat(dname, &sb) || ((dir = opendir(dname)) == NULL)) {
216         flog(LOG_ERR, "htls: could not open directory `%s': %s", dname, strerror(errno));
217         simpleerror(1, 500, "Server Error", "Could not produce directory index.");
218         exit(1);
219     }
220     checkcache(&sb);
221     
222     bufinit(buf);
223     head(argv[optind + 1], &buf);
224     mkindex(dname, dir, &buf);
225     foot(&buf);
226     closedir(dir);
227     
228     printf("HTTP/1.1 200 OK\n");
229     printf("Content-Type: text/html; charset=UTF-8\n");
230     printf("Last-Modified: %s\n", fmthttpdate(sb.st_mtime));
231     printf("Content-Length: %zi\n", buf.d);
232     printf("\n");
233     fwrite(buf.b, 1, buf.d, stdout);
234     buffree(buf);
235     return(0);
236 }