[package] uhttpd: display errors in init script, code formatting changes, bump packag...
[openwrt.git] / package / uhttpd / src / uhttpd-file.c
1 /*
2  * uhttpd - Tiny single-threaded httpd - Static file handler
3  *
4  *   Copyright (C) 2010-2011 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #define _XOPEN_SOURCE 500       /* strptime() */
20 #define _BSD_SOURCE                     /* scandir(), timegm() */
21
22 #include "uhttpd.h"
23 #include "uhttpd-utils.h"
24 #include "uhttpd-file.h"
25
26 #include "uhttpd-mimetypes.h"
27
28
29 static const char * uh_file_mime_lookup(const char *path)
30 {
31         struct mimetype *m = &uh_mime_types[0];
32         const char *e;
33
34         while (m->extn)
35         {
36                 e = &path[strlen(path)-1];
37
38                 while (e >= path)
39                 {
40                         if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn))
41                                 return m->mime;
42
43                         e--;
44                 }
45
46                 m++;
47         }
48
49         return "application/octet-stream";
50 }
51
52 static const char * uh_file_mktag(struct stat *s)
53 {
54         static char tag[128];
55
56         snprintf(tag, sizeof(tag), "\"%x-%x-%x\"",
57                          (unsigned int) s->st_ino,
58                          (unsigned int) s->st_size,
59                          (unsigned int) s->st_mtime);
60
61         return tag;
62 }
63
64 static time_t uh_file_date2unix(const char *date)
65 {
66         struct tm t;
67
68         memset(&t, 0, sizeof(t));
69
70         if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL)
71                 return timegm(&t);
72
73         return 0;
74 }
75
76 static char * uh_file_unix2date(time_t ts)
77 {
78         static char str[128];
79         struct tm *t = gmtime(&ts);
80
81         strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", t);
82
83         return str;
84 }
85
86 static char * uh_file_header_lookup(struct http_request *req, const char *name)
87 {
88         int i;
89
90         foreach_header(i, req->headers)
91         {
92                 if (!strcasecmp(req->headers[i], name))
93                         return req->headers[i+1];
94         }
95
96         return NULL;
97 }
98
99
100 static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req,
101                                                                         struct stat *s)
102 {
103         ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
104
105         if (s)
106         {
107                 ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)));
108                 ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n",
109                                                                  uh_file_unix2date(s->st_mtime)));
110         }
111
112         return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
113 }
114
115 static int uh_file_response_200(struct client *cl, struct http_request *req,
116                                                                 struct stat *s)
117 {
118         ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version));
119         return uh_file_response_ok_hdrs(cl, req, s);
120 }
121
122 static int uh_file_response_304(struct client *cl, struct http_request *req,
123                                                                 struct stat *s)
124 {
125         ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version));
126         return uh_file_response_ok_hdrs(cl, req, s);
127 }
128
129 static int uh_file_response_412(struct client *cl, struct http_request *req)
130 {
131         return uh_http_sendf(cl, NULL,
132                 "HTTP/%.1f 412 Precondition Failed\r\n"
133                 "Connection: close\r\n", req->version);
134 }
135
136 static int uh_file_if_match(struct client *cl, struct http_request *req,
137                                                         struct stat *s, int *ok)
138 {
139         const char *tag = uh_file_mktag(s);
140         char *hdr = uh_file_header_lookup(req, "If-Match");
141         char *p;
142         int i;
143
144         if (hdr)
145         {
146                 p = &hdr[0];
147
148                 for (i = 0; i < strlen(hdr); i++)
149                 {
150                         if ((hdr[i] == ' ') || (hdr[i] == ','))
151                         {
152                                 hdr[i++] = 0;
153                                 p = &hdr[i];
154                         }
155                         else if (!strcmp(p, "*") || !strcmp(p, tag))
156                         {
157                                 *ok = 1;
158                                 return *ok;
159                         }
160                 }
161
162                 *ok = 0;
163                 ensure_ret(uh_file_response_412(cl, req));
164                 return *ok;
165         }
166
167         *ok = 1;
168         return *ok;
169 }
170
171 static int uh_file_if_modified_since(struct client *cl,
172                                                                          struct http_request *req, struct stat *s,
173                                                                          int *ok)
174 {
175         char *hdr = uh_file_header_lookup(req, "If-Modified-Since");
176         *ok = 1;
177
178         if (hdr)
179         {
180                 if (uh_file_date2unix(hdr) >= s->st_mtime)
181                 {
182                         *ok = 0;
183                         ensure_ret(uh_file_response_304(cl, req, s));
184                 }
185         }
186
187         return *ok;
188 }
189
190 static int uh_file_if_none_match(struct client *cl, struct http_request *req,
191                                                                  struct stat *s, int *ok)
192 {
193         const char *tag = uh_file_mktag(s);
194         char *hdr = uh_file_header_lookup(req, "If-None-Match");
195         char *p;
196         int i;
197         *ok = 1;
198
199         if (hdr)
200         {
201                 p = &hdr[0];
202
203                 for (i = 0; i < strlen(hdr); i++)
204                 {
205                         if ((hdr[i] == ' ') || (hdr[i] == ','))
206                         {
207                                 hdr[i++] = 0;
208                                 p = &hdr[i];
209                         }
210                         else if (!strcmp(p, "*") || !strcmp(p, tag))
211                         {
212                                 *ok = 0;
213
214                                 if ((req->method == UH_HTTP_MSG_GET) ||
215                                     (req->method == UH_HTTP_MSG_HEAD))
216                                 {
217                                         ensure_ret(uh_file_response_304(cl, req, s));
218                                 }
219                                 else
220                                 {
221                                         ensure_ret(uh_file_response_412(cl, req));
222                                 }
223
224                                 break;
225                         }
226                 }
227         }
228
229         return *ok;
230 }
231
232 static int uh_file_if_range(struct client *cl, struct http_request *req,
233                                                         struct stat *s, int *ok)
234 {
235         char *hdr = uh_file_header_lookup(req, "If-Range");
236         *ok = 1;
237
238         if (hdr)
239         {
240                 *ok = 0;
241                 ensure_ret(uh_file_response_412(cl, req));
242         }
243
244         return *ok;
245 }
246
247 static int uh_file_if_unmodified_since(struct client *cl,
248                                                                            struct http_request *req, struct stat *s,
249                                                                            int *ok)
250 {
251         char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since");
252         *ok = 1;
253
254         if (hdr)
255         {
256                 if (uh_file_date2unix(hdr) <= s->st_mtime)
257                 {
258                         *ok = 0;
259                         ensure_ret(uh_file_response_412(cl, req));
260                 }
261         }
262
263         return *ok;
264 }
265
266
267 static int uh_file_scandir_filter_dir(const struct dirent *e)
268 {
269         return strcmp(e->d_name, ".") ? 1 : 0;
270 }
271
272 static void uh_file_dirlist(struct client *cl, struct http_request *req,
273                                                         struct path_info *pi)
274 {
275         int i;
276         int count = 0;
277         char filename[PATH_MAX];
278         char *pathptr;
279         struct dirent **files = NULL;
280         struct stat s;
281
282         ensure_out(uh_http_sendf(cl, req,
283                                                          "<html><head><title>Index of %s</title></head>"
284                                                          "<body><h1>Index of %s</h1><hr /><ol>",
285                                                          pi->name, pi->name));
286
287         if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
288                                                  alphasort)) > 0)
289         {
290                 memset(filename, 0, sizeof(filename));
291                 memcpy(filename, pi->phys, sizeof(filename));
292                 pathptr = &filename[strlen(filename)];
293
294                 /* list subdirs */
295                 for (i = 0; i < count; i++)
296                 {
297                         strncat(filename, files[i]->d_name,
298                                         sizeof(filename) - strlen(files[i]->d_name));
299
300                         if (!stat(filename, &s) &&
301                                 (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
302                         {
303                                 ensure_out(uh_http_sendf(cl, req,
304                                                                                  "<li><strong><a href='%s%s'>%s</a>/"
305                                                                                  "</strong><br /><small>modified: %s"
306                                                                                  "<br />directory - %.02f kbyte<br />"
307                                                                                  "<br /></small></li>",
308                                                                                  pi->name, files[i]->d_name,
309                                                                                  files[i]->d_name,
310                                                                                  uh_file_unix2date(s.st_mtime),
311                                                                                  s.st_size / 1024.0));
312                         }
313
314                         *pathptr = 0;
315                 }
316
317                 /* list files */
318                 for (i = 0; i < count; i++)
319                 {
320                         strncat(filename, files[i]->d_name,
321                                         sizeof(filename) - strlen(files[i]->d_name));
322
323                         if (!stat(filename, &s) &&
324                                 !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))
325                         {
326                                 ensure_out(uh_http_sendf(cl, req,
327                                                                                  "<li><strong><a href='%s%s'>%s</a>"
328                                                                                  "</strong><br /><small>modified: %s"
329                                                                                  "<br />%s - %.02f kbyte<br />"
330                                                                                  "<br /></small></li>",
331                                                                                  pi->name, files[i]->d_name,
332                                                                                  files[i]->d_name,
333                                                                                  uh_file_unix2date(s.st_mtime),
334                                                                                  uh_file_mime_lookup(filename),
335                                                                                  s.st_size / 1024.0));
336                         }
337
338                         *pathptr = 0;
339                 }
340         }
341
342         ensure_out(uh_http_sendf(cl, req, "</ol><hr /></body></html>"));
343         ensure_out(uh_http_sendf(cl, req, ""));
344
345 out:
346         if (files)
347         {
348                 for (i = 0; i < count; i++)
349                         free(files[i]);
350
351                 free(files);
352         }
353 }
354
355
356 void uh_file_request(struct client *cl, struct http_request *req, struct path_info *pi)
357 {
358         int rlen;
359         int ok = 1;
360         int fd = -1;
361         char buf[UH_LIMIT_MSGHEAD];
362
363         /* we have a file */
364         if ((pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0))
365         {
366                 /* test preconditions */
367                 if (ok) ensure_out(uh_file_if_modified_since(cl, req, &pi->stat, &ok));
368                 if (ok) ensure_out(uh_file_if_match(cl, req, &pi->stat, &ok));
369                 if (ok) ensure_out(uh_file_if_range(cl, req, &pi->stat, &ok));
370                 if (ok) ensure_out(uh_file_if_unmodified_since(cl, req, &pi->stat, &ok));
371                 if (ok) ensure_out(uh_file_if_none_match(cl, req, &pi->stat, &ok));
372
373                 if (ok > 0)
374                 {
375                         /* write status */
376                         ensure_out(uh_file_response_200(cl, req, &pi->stat));
377
378                         ensure_out(uh_http_sendf(cl, NULL, "Content-Type: %s\r\n", uh_file_mime_lookup(pi->name)));
379                         ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n", pi->stat.st_size));
380
381                         /* if request was HTTP 1.1 we'll respond chunked */
382                         if ((req->version > 1.0) && (req->method != UH_HTTP_MSG_HEAD))
383                                 ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1));
384
385                         /* close header */
386                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
387
388                         /* send body */
389                         if (req->method != UH_HTTP_MSG_HEAD)
390                         {
391                                 /* pump file data */
392                                 while ((rlen = read(fd, buf, sizeof(buf))) > 0)
393                                         ensure_out(uh_http_send(cl, req, buf, rlen));
394
395                                 /* send trailer in chunked mode */
396                                 ensure_out(uh_http_send(cl, req, "", 0));
397                         }
398                 }
399
400                 /* one of the preconditions failed, terminate opened header and exit */
401                 else
402                 {
403                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
404                 }
405         }
406
407         /* directory */
408         else if ((pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists)
409         {
410                 /* write status */
411                 ensure_out(uh_file_response_200(cl, req, NULL));
412
413                 if (req->version > 1.0)
414                         ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1));
415
416                 ensure_out(uh_http_send(cl, NULL, "Content-Type: text/html\r\n\r\n", -1));
417
418                 /* content */
419                 uh_file_dirlist(cl, req, pi);
420         }
421
422         /* 403 */
423         else
424         {
425                 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
426                         "Access to this resource is forbidden"));
427         }
428
429 out:
430         if (fd > -1)
431                 close(fd);
432 }