doc: Documented htpipe.
[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 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");
97     }
98     bprintf(dst, "</head>\n");
99     bprintf(dst, "<body>\n");
100     bprintf(dst, "<h1>Index of %s</h1>\n", title);
101     free(title);
102 }
103
104 static void foot(struct charbuf *dst)
105 {
106     bprintf(dst, "</body>\n");
107     bprintf(dst, "</html>\n");
108 }
109
110 static void mkindex(char *name, DIR *dir, struct charbuf *dst)
111 {
112     struct {
113         struct dentry *b;
114         size_t s, d;
115     } dirbuf;
116     struct dentry f;
117     struct dirent *dent;
118     char *fn;
119     int i;
120     
121     bufinit(dirbuf);
122     while((dent = readdir(dir)) != NULL) {
123         if(*dent->d_name == '.')
124             continue;
125         memset(&f, 0, sizeof(f));
126         f.name = sstrdup(dent->d_name);
127         fn = sprintf3("%s/%s", name, dent->d_name);
128         if(access(fn, R_OK))
129             continue;
130         if(stat(fn, &f.sb))
131             continue;
132         if(!access(fn, W_OK))
133             f.w = 1;
134         if(!access(fn, X_OK))
135             f.x = 1;
136         else if(S_ISDIR(f.sb.st_mode))
137             continue;
138         bufadd(dirbuf, f);
139     }
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");
146         } else {
147             if(dirbuf.b[i].x)
148                 bprintf(dst, " exec");
149         }
150         if(dirbuf.b[i].w)
151             bprintf(dst, " writable");
152         bprintf(dst, "\">");    
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>");
156         if(dispsize) {
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>");
161         }
162         if(dispmtime)
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);
166     }
167     bprintf(dst, "</table></div>\n");
168 }
169
170 static void usage(void)
171 {
172     flog(LOG_ERR, "usage: htls [-hms] [-c STYLESHEET] METHOD URL REST");
173 }
174
175 int main(int argc, char **argv)
176 {
177     int c;
178     char *dname;
179     DIR *dir;
180     struct charbuf buf;
181     struct stat sb;
182     
183     setlocale(LC_ALL, "");
184     while((c = getopt(argc, argv, "hmsc:")) >= 0) {
185         switch(c) {
186         case 'h':
187             usage();
188             exit(0);
189         case 'm':
190             dispmtime = 1;
191             break;
192         case 's':
193             dispsize = 1;
194             break;
195         case 'c':
196             stylesheet = optarg;
197             break;
198         default:
199             usage();
200             exit(1);
201         }
202     }
203     if(argc - optind < 3) {
204         usage();
205         exit(1);
206     }
207     if((dname = getenv("REQ_X_ASH_FILE")) == NULL) {
208         flog(LOG_ERR, "htls: needs to be called with the X-Ash-File header");
209         exit(1);
210     }
211     if(*argv[optind + 2]) {
212         simpleerror(1, 404, "Not Found", "The requested URL has no corresponding resource.");
213         exit(0);
214     }
215
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.");
219         exit(1);
220     }
221     checkcache(&sb);
222     
223     bufinit(buf);
224     head(argv[optind + 1], &buf);
225     mkindex(dname, dir, &buf);
226     foot(&buf);
227     closedir(dir);
228     
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);
233     printf("\n");
234     fwrite(buf.b, 1, buf.d, stdout);
235     buffree(buf);
236     return(0);
237 }