packages: sort network related packages into package/network/
[openwrt.git] / package / network / services / uhttpd / src / uhttpd-file.c
1 /*
2  * uhttpd - Tiny single-threaded httpd - Static file handler
3  *
4  *   Copyright (C) 2010-2012 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 client *cl, const char *name)
87 {
88         int i;
89
90         foreach_header(i, cl->request.headers)
91         {
92                 if (!strcasecmp(cl->request.headers[i], name))
93                         return cl->request.headers[i+1];
94         }
95
96         return NULL;
97 }
98
99
100 static int uh_file_response_ok_hdrs(struct client *cl, struct stat *s)
101 {
102         ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
103
104         if (s)
105         {
106                 ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)));
107                 ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n",
108                                                                  uh_file_unix2date(s->st_mtime)));
109         }
110
111         return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
112 }
113
114 static int uh_file_response_200(struct client *cl, struct stat *s)
115 {
116         ensure_ret(uh_http_sendf(cl, NULL, "%s 200 OK\r\n",
117                                                          http_versions[cl->request.version]));
118
119         return uh_file_response_ok_hdrs(cl, s);
120 }
121
122 static int uh_file_response_304(struct client *cl, struct stat *s)
123 {
124         ensure_ret(uh_http_sendf(cl, NULL, "%s 304 Not Modified\r\n",
125                                                          http_versions[cl->request.version]));
126
127         return uh_file_response_ok_hdrs(cl, s);
128 }
129
130 static int uh_file_response_412(struct client *cl)
131 {
132         return uh_http_sendf(cl, NULL,
133                                                  "%s 412 Precondition Failed\r\n"
134                                                  "Connection: close\r\n",
135                              http_versions[cl->request.version]);
136 }
137
138 static int uh_file_if_match(struct client *cl, struct stat *s, int *ok)
139 {
140         const char *tag = uh_file_mktag(s);
141         char *hdr = uh_file_header_lookup(cl, "If-Match");
142         char *p;
143         int i;
144
145         if (hdr)
146         {
147                 p = &hdr[0];
148
149                 for (i = 0; i < strlen(hdr); i++)
150                 {
151                         if ((hdr[i] == ' ') || (hdr[i] == ','))
152                         {
153                                 hdr[i++] = 0;
154                                 p = &hdr[i];
155                         }
156                         else if (!strcmp(p, "*") || !strcmp(p, tag))
157                         {
158                                 *ok = 1;
159                                 return *ok;
160                         }
161                 }
162
163                 *ok = 0;
164                 ensure_ret(uh_file_response_412(cl));
165                 return *ok;
166         }
167
168         *ok = 1;
169         return *ok;
170 }
171
172 static int uh_file_if_modified_since(struct client *cl, struct stat *s, int *ok)
173 {
174         char *hdr = uh_file_header_lookup(cl, "If-Modified-Since");
175         *ok = 1;
176
177         if (hdr)
178         {
179                 if (uh_file_date2unix(hdr) >= s->st_mtime)
180                 {
181                         *ok = 0;
182                         ensure_ret(uh_file_response_304(cl, s));
183                 }
184         }
185
186         return *ok;
187 }
188
189 static int uh_file_if_none_match(struct client *cl, struct stat *s, int *ok)
190 {
191         const char *tag = uh_file_mktag(s);
192         char *hdr = uh_file_header_lookup(cl, "If-None-Match");
193         char *p;
194         int i;
195         *ok = 1;
196
197         if (hdr)
198         {
199                 p = &hdr[0];
200
201                 for (i = 0; i < strlen(hdr); i++)
202                 {
203                         if ((hdr[i] == ' ') || (hdr[i] == ','))
204                         {
205                                 hdr[i++] = 0;
206                                 p = &hdr[i];
207                         }
208                         else if (!strcmp(p, "*") || !strcmp(p, tag))
209                         {
210                                 *ok = 0;
211
212                                 if ((cl->request.method == UH_HTTP_MSG_GET) ||
213                                     (cl->request.method == UH_HTTP_MSG_HEAD))
214                                 {
215                                         ensure_ret(uh_file_response_304(cl, s));
216                                 }
217                                 else
218                                 {
219                                         ensure_ret(uh_file_response_412(cl));
220                                 }
221
222                                 break;
223                         }
224                 }
225         }
226
227         return *ok;
228 }
229
230 static int uh_file_if_range(struct client *cl, struct stat *s, int *ok)
231 {
232         char *hdr = uh_file_header_lookup(cl, "If-Range");
233         *ok = 1;
234
235         if (hdr)
236         {
237                 *ok = 0;
238                 ensure_ret(uh_file_response_412(cl));
239         }
240
241         return *ok;
242 }
243
244 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s,
245                                                                            int *ok)
246 {
247         char *hdr = uh_file_header_lookup(cl, "If-Unmodified-Since");
248         *ok = 1;
249
250         if (hdr)
251         {
252                 if (uh_file_date2unix(hdr) <= s->st_mtime)
253                 {
254                         *ok = 0;
255                         ensure_ret(uh_file_response_412(cl));
256                 }
257         }
258
259         return *ok;
260 }
261
262
263 static int uh_file_scandir_filter_dir(const struct dirent *e)
264 {
265         return strcmp(e->d_name, ".") ? 1 : 0;
266 }
267
268 static void uh_file_dirlist(struct client *cl, struct path_info *pi)
269 {
270         int i;
271         int count = 0;
272         char filename[PATH_MAX];
273         char *pathptr;
274         struct dirent **files = NULL;
275         struct stat s;
276
277         ensure_out(uh_http_sendf(cl, &cl->request,
278                                                          "<html><head><title>Index of %s</title></head>"
279                                                          "<body><h1>Index of %s</h1><hr /><ol>",
280                                                          pi->name, pi->name));
281
282         if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
283                                                  alphasort)) > 0)
284         {
285                 memset(filename, 0, sizeof(filename));
286                 memcpy(filename, pi->phys, sizeof(filename));
287                 pathptr = &filename[strlen(filename)];
288
289                 /* list subdirs */
290                 for (i = 0; i < count; i++)
291                 {
292                         strncat(filename, files[i]->d_name,
293                                         sizeof(filename) - strlen(files[i]->d_name));
294
295                         if (!stat(filename, &s) &&
296                                 (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
297                         {
298                                 ensure_out(uh_http_sendf(cl, &cl->request,
299                                                                                  "<li><strong><a href='%s%s'>%s</a>/"
300                                                                                  "</strong><br /><small>modified: %s"
301                                                                                  "<br />directory - %.02f kbyte<br />"
302                                                                                  "<br /></small></li>",
303                                                                                  pi->name, files[i]->d_name,
304                                                                                  files[i]->d_name,
305                                                                                  uh_file_unix2date(s.st_mtime),
306                                                                                  s.st_size / 1024.0));
307                         }
308
309                         *pathptr = 0;
310                 }
311
312                 /* list files */
313                 for (i = 0; i < count; i++)
314                 {
315                         strncat(filename, files[i]->d_name,
316                                         sizeof(filename) - strlen(files[i]->d_name));
317
318                         if (!stat(filename, &s) &&
319                                 !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))
320                         {
321                                 ensure_out(uh_http_sendf(cl, &cl->request,
322                                                                                  "<li><strong><a href='%s%s'>%s</a>"
323                                                                                  "</strong><br /><small>modified: %s"
324                                                                                  "<br />%s - %.02f kbyte<br />"
325                                                                                  "<br /></small></li>",
326                                                                                  pi->name, files[i]->d_name,
327                                                                                  files[i]->d_name,
328                                                                                  uh_file_unix2date(s.st_mtime),
329                                                                                  uh_file_mime_lookup(filename),
330                                                                                  s.st_size / 1024.0));
331                         }
332
333                         *pathptr = 0;
334                 }
335         }
336
337         ensure_out(uh_http_sendf(cl, &cl->request, "</ol><hr /></body></html>"));
338         ensure_out(uh_http_sendf(cl, &cl->request, ""));
339
340 out:
341         if (files)
342         {
343                 for (i = 0; i < count; i++)
344                         free(files[i]);
345
346                 free(files);
347         }
348 }
349
350
351 bool uh_file_request(struct client *cl, struct path_info *pi)
352 {
353         int rlen;
354         int ok = 1;
355         int fd = -1;
356         char buf[UH_LIMIT_MSGHEAD];
357
358         /* we have a file */
359         if ((pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0))
360         {
361                 /* test preconditions */
362                 if (ok) ensure_out(uh_file_if_modified_since(cl, &pi->stat, &ok));
363                 if (ok) ensure_out(uh_file_if_match(cl, &pi->stat, &ok));
364                 if (ok) ensure_out(uh_file_if_range(cl, &pi->stat, &ok));
365                 if (ok) ensure_out(uh_file_if_unmodified_since(cl, &pi->stat, &ok));
366                 if (ok) ensure_out(uh_file_if_none_match(cl, &pi->stat, &ok));
367
368                 if (ok > 0)
369                 {
370                         /* write status */
371                         ensure_out(uh_file_response_200(cl, &pi->stat));
372
373                         ensure_out(uh_http_sendf(cl, NULL, "Content-Type: %s\r\n",
374                                                                          uh_file_mime_lookup(pi->name)));
375
376                         ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n",
377                                                                          pi->stat.st_size));
378
379                         /* if request was HTTP 1.1 we'll respond chunked */
380                         if ((cl->request.version > 1.0) &&
381                                 (cl->request.method != UH_HTTP_MSG_HEAD))
382                         {
383                                 ensure_out(uh_http_send(cl, NULL,
384                                                                                 "Transfer-Encoding: chunked\r\n", -1));
385                         }
386
387                         /* close header */
388                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
389
390                         /* send body */
391                         if (cl->request.method != UH_HTTP_MSG_HEAD)
392                         {
393                                 /* pump file data */
394                                 while ((rlen = read(fd, buf, sizeof(buf))) > 0)
395                                         ensure_out(uh_http_send(cl, &cl->request, buf, rlen));
396
397                                 /* send trailer in chunked mode */
398                                 ensure_out(uh_http_send(cl, &cl->request, "", 0));
399                         }
400                 }
401
402                 /* one of the preconditions failed, terminate opened header and exit */
403                 else
404                 {
405                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
406                 }
407         }
408
409         /* directory */
410         else if ((pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists)
411         {
412                 /* write status */
413                 ensure_out(uh_file_response_200(cl, NULL));
414
415                 if (cl->request.version > 1.0)
416                         ensure_out(uh_http_send(cl, NULL,
417                                                                         "Transfer-Encoding: chunked\r\n", -1));
418
419                 ensure_out(uh_http_send(cl, NULL,
420                                                                 "Content-Type: text/html\r\n\r\n", -1));
421
422                 /* content */
423                 uh_file_dirlist(cl, pi);
424         }
425
426         /* 403 */
427         else
428         {
429                 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
430                                                                   "Access to this resource is forbidden"));
431         }
432
433 out:
434         if (fd > -1)
435                 close(fd);
436
437         return false;
438 }