mimetypes: add json and jsonp (distinct from js)
[project/uhttpd.git] / file.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #define _BSD_SOURCE
21 #define _DARWIN_C_SOURCE
22 #define _XOPEN_SOURCE 700
23
24 #include <sys/types.h>
25 #include <sys/dir.h>
26 #include <time.h>
27 #include <strings.h>
28 #include <dirent.h>
29
30 #include <libubox/blobmsg.h>
31
32 #include "uhttpd.h"
33 #include "mimetypes.h"
34
35 static LIST_HEAD(index_files);
36 static LIST_HEAD(dispatch_handlers);
37 static LIST_HEAD(pending_requests);
38 static int n_requests;
39
40 struct deferred_request {
41         struct list_head list;
42         struct dispatch_handler *d;
43         struct client *cl;
44         struct path_info pi;
45         bool called, path;
46 };
47
48 struct index_file {
49         struct list_head list;
50         const char *name;
51 };
52
53 enum file_hdr {
54         HDR_AUTHORIZATION,
55         HDR_IF_MODIFIED_SINCE,
56         HDR_IF_UNMODIFIED_SINCE,
57         HDR_IF_MATCH,
58         HDR_IF_NONE_MATCH,
59         HDR_IF_RANGE,
60         __HDR_MAX
61 };
62
63 void uh_index_add(const char *filename)
64 {
65         struct index_file *idx;
66
67         idx = calloc(1, sizeof(*idx));
68         idx->name = filename;
69         list_add_tail(&idx->list, &index_files);
70 }
71
72 static char * canonpath(const char *path, char *path_resolved)
73 {
74         const char *path_cpy = path;
75         char *path_res = path_resolved;
76
77         if (conf.no_symlinks)
78                 return realpath(path, path_resolved);
79
80         /* normalize */
81         while ((*path_cpy != '\0') && (path_cpy < (path + PATH_MAX - 2))) {
82                 if (*path_cpy != '/')
83                         goto next;
84
85                 /* skip repeating / */
86                 if (path_cpy[1] == '/') {
87                         path_cpy++;
88                         continue;
89                 }
90
91                 /* /./ or /../ */
92                 if (path_cpy[1] == '.') {
93                         /* skip /./ */
94                         if ((path_cpy[2] == '/') || (path_cpy[2] == '\0')) {
95                                 path_cpy += 2;
96                                 continue;
97                         }
98
99                         /* collapse /x/../ */
100                         if ((path_cpy[2] == '.') &&
101                             ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))) {
102                                 while ((path_res > path_resolved) && (*--path_res != '/'));
103
104                                 path_cpy += 3;
105                                 continue;
106                         }
107                 }
108
109 next:
110                 *path_res++ = *path_cpy++;
111         }
112
113         /* remove trailing slash if not root / */
114         if ((path_res > (path_resolved+1)) && (path_res[-1] == '/'))
115                 path_res--;
116         else if (path_res == path_resolved)
117                 *path_res++ = '/';
118
119         *path_res = '\0';
120
121         return path_resolved;
122 }
123
124 /* Returns NULL on error.
125 ** NB: improperly encoded URL should give client 400 [Bad Syntax]; returning
126 ** NULL here causes 404 [Not Found], but that's not too unreasonable. */
127 static struct path_info *
128 uh_path_lookup(struct client *cl, const char *url)
129 {
130         static char path_phys[PATH_MAX];
131         static char path_info[PATH_MAX];
132         static struct path_info p;
133
134         const char *docroot = conf.docroot;
135         int docroot_len = strlen(docroot);
136         char *pathptr = NULL;
137         bool slash;
138
139         int i = 0;
140         int len;
141         struct stat s;
142         struct index_file *idx;
143
144         /* back out early if url is undefined */
145         if (url == NULL)
146                 return NULL;
147
148         memset(&p, 0, sizeof(p));
149         path_phys[0] = 0;
150         path_info[0] = 0;
151
152         strcpy(uh_buf, docroot);
153
154         /* separate query string from url */
155         if ((pathptr = strchr(url, '?')) != NULL) {
156                 p.query = pathptr[1] ? pathptr + 1 : NULL;
157
158                 /* urldecode component w/o query */
159                 if (pathptr > url) {
160                         if (uh_urldecode(&uh_buf[docroot_len],
161                                          sizeof(uh_buf) - docroot_len - 1,
162                                          url, pathptr - url ) < 0)
163                                 return NULL;
164                 }
165         }
166
167         /* no query string, decode all of url */
168         else if (uh_urldecode(&uh_buf[docroot_len],
169                               sizeof(uh_buf) - docroot_len - 1,
170                               url, strlen(url) ) < 0)
171                 return NULL;
172
173         /* create canon path */
174         len = strlen(uh_buf);
175         slash = len && uh_buf[len - 1] == '/';
176         len = min(len, sizeof(path_phys) - 1);
177
178         for (i = len; i >= 0; i--) {
179                 char ch = uh_buf[i];
180                 bool exists;
181
182                 if (ch != 0 && ch != '/')
183                         continue;
184
185                 uh_buf[i] = 0;
186                 exists = !!canonpath(uh_buf, path_phys);
187                 uh_buf[i] = ch;
188
189                 if (!exists)
190                         continue;
191
192                 /* test current path */
193                 if (stat(path_phys, &p.stat))
194                         continue;
195
196                 snprintf(path_info, sizeof(path_info), "%s", uh_buf + i);
197                 break;
198         }
199
200         /* check whether found path is within docroot */
201         if (strncmp(path_phys, docroot, docroot_len) != 0 ||
202             (path_phys[docroot_len] != 0 &&
203              path_phys[docroot_len] != '/'))
204                 return NULL;
205
206         /* is a regular file */
207         if (p.stat.st_mode & S_IFREG) {
208                 p.root = docroot;
209                 p.phys = path_phys;
210                 p.name = &path_phys[docroot_len];
211                 p.info = path_info[0] ? path_info : NULL;
212                 return &p;
213         }
214
215         if (!(p.stat.st_mode & S_IFDIR))
216                 return NULL;
217
218         if (path_info[0])
219             return NULL;
220
221         pathptr = path_phys + strlen(path_phys);
222
223         /* ensure trailing slash */
224         if (pathptr[-1] != '/') {
225                 pathptr[0] = '/';
226                 pathptr[1] = 0;
227                 pathptr++;
228         }
229
230         /* if requested url resolves to a directory and a trailing slash
231            is missing in the request url, redirect the client to the same
232            url with trailing slash appended */
233         if (!slash) {
234                 uh_http_header(cl, 302, "Found");
235                 ustream_printf(cl->us, "Content-Length: 0\r\n");
236                 ustream_printf(cl->us, "Location: %s%s%s\r\n\r\n",
237                                 &path_phys[docroot_len],
238                                 p.query ? "?" : "",
239                                 p.query ? p.query : "");
240                 uh_request_done(cl);
241                 p.redirected = 1;
242                 return &p;
243         }
244
245         /* try to locate index file */
246         len = path_phys + sizeof(path_phys) - pathptr - 1;
247         list_for_each_entry(idx, &index_files, list) {
248                 if (strlen(idx->name) > len)
249                         continue;
250
251                 strcpy(pathptr, idx->name);
252                 if (!stat(path_phys, &s) && (s.st_mode & S_IFREG)) {
253                         memcpy(&p.stat, &s, sizeof(p.stat));
254                         break;
255                 }
256
257                 *pathptr = 0;
258         }
259
260         p.root = docroot;
261         p.phys = path_phys;
262         p.name = &path_phys[docroot_len];
263
264         return p.phys ? &p : NULL;
265 }
266
267 static const char * uh_file_mime_lookup(const char *path)
268 {
269         const struct mimetype *m = &uh_mime_types[0];
270         const char *e;
271
272         while (m->extn) {
273                 e = &path[strlen(path)-1];
274
275                 while (e >= path) {
276                         if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn))
277                                 return m->mime;
278
279                         e--;
280                 }
281
282                 m++;
283         }
284
285         return "application/octet-stream";
286 }
287
288 static const char * uh_file_mktag(struct stat *s, char *buf, int len)
289 {
290         snprintf(buf, len, "\"%x-%x-%x\"",
291                          (unsigned int) s->st_ino,
292                          (unsigned int) s->st_size,
293                          (unsigned int) s->st_mtime);
294
295         return buf;
296 }
297
298 static time_t uh_file_date2unix(const char *date)
299 {
300         struct tm t;
301
302         memset(&t, 0, sizeof(t));
303
304         if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL)
305                 return timegm(&t);
306
307         return 0;
308 }
309
310 static char * uh_file_unix2date(time_t ts, char *buf, int len)
311 {
312         struct tm *t = gmtime(&ts);
313
314         strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", t);
315
316         return buf;
317 }
318
319 static char *uh_file_header(struct client *cl, int idx)
320 {
321         if (!cl->dispatch.file.hdr[idx])
322                 return NULL;
323
324         return (char *) blobmsg_data(cl->dispatch.file.hdr[idx]);
325 }
326
327 static void uh_file_response_ok_hdrs(struct client *cl, struct stat *s)
328 {
329         char buf[128];
330
331         if (s) {
332                 ustream_printf(cl->us, "ETag: %s\r\n", uh_file_mktag(s, buf, sizeof(buf)));
333                 ustream_printf(cl->us, "Last-Modified: %s\r\n",
334                                uh_file_unix2date(s->st_mtime, buf, sizeof(buf)));
335         }
336         ustream_printf(cl->us, "Date: %s\r\n",
337                        uh_file_unix2date(time(NULL), buf, sizeof(buf)));
338 }
339
340 static void uh_file_response_200(struct client *cl, struct stat *s)
341 {
342         uh_http_header(cl, 200, "OK");
343         return uh_file_response_ok_hdrs(cl, s);
344 }
345
346 static void uh_file_response_304(struct client *cl, struct stat *s)
347 {
348         uh_http_header(cl, 304, "Not Modified");
349
350         return uh_file_response_ok_hdrs(cl, s);
351 }
352
353 static void uh_file_response_412(struct client *cl)
354 {
355         uh_http_header(cl, 412, "Precondition Failed");
356 }
357
358 static bool uh_file_if_match(struct client *cl, struct stat *s)
359 {
360         char buf[128];
361         const char *tag = uh_file_mktag(s, buf, sizeof(buf));
362         char *hdr = uh_file_header(cl, HDR_IF_MATCH);
363         char *p;
364         int i;
365
366         if (!hdr)
367                 return true;
368
369         p = &hdr[0];
370         for (i = 0; i < strlen(hdr); i++)
371         {
372                 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
373                         hdr[i++] = 0;
374                         p = &hdr[i];
375                 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
376                         return true;
377                 }
378         }
379
380         uh_file_response_412(cl);
381         return false;
382 }
383
384 static int uh_file_if_modified_since(struct client *cl, struct stat *s)
385 {
386         char *hdr = uh_file_header(cl, HDR_IF_MODIFIED_SINCE);
387
388         if (!hdr)
389                 return true;
390
391         if (uh_file_date2unix(hdr) >= s->st_mtime) {
392                 uh_file_response_304(cl, s);
393                 return false;
394         }
395
396         return true;
397 }
398
399 static int uh_file_if_none_match(struct client *cl, struct stat *s)
400 {
401         char buf[128];
402         const char *tag = uh_file_mktag(s, buf, sizeof(buf));
403         char *hdr = uh_file_header(cl, HDR_IF_NONE_MATCH);
404         char *p;
405         int i;
406
407         if (!hdr)
408                 return true;
409
410         p = &hdr[0];
411         for (i = 0; i < strlen(hdr); i++) {
412                 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
413                         hdr[i++] = 0;
414                         p = &hdr[i];
415                 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
416                         if ((cl->request.method == UH_HTTP_MSG_GET) ||
417                                 (cl->request.method == UH_HTTP_MSG_HEAD))
418                                 uh_file_response_304(cl, s);
419                         else
420                                 uh_file_response_412(cl);
421
422                         return false;
423                 }
424         }
425
426         return true;
427 }
428
429 static int uh_file_if_range(struct client *cl, struct stat *s)
430 {
431         char *hdr = uh_file_header(cl, HDR_IF_RANGE);
432
433         if (hdr) {
434                 uh_file_response_412(cl);
435                 return false;
436         }
437
438         return true;
439 }
440
441 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s)
442 {
443         char *hdr = uh_file_header(cl, HDR_IF_UNMODIFIED_SINCE);
444
445         if (hdr && uh_file_date2unix(hdr) <= s->st_mtime) {
446                 uh_file_response_412(cl);
447                 return false;
448         }
449
450         return true;
451 }
452
453 static int dirent_cmp(const struct dirent **a, const struct dirent **b)
454 {
455         bool dir_a = !!((*a)->d_type & DT_DIR);
456         bool dir_b = !!((*b)->d_type & DT_DIR);
457
458         /* directories first */
459         if (dir_a != dir_b)
460                 return dir_b - dir_a;
461
462         return alphasort(a, b);
463 }
464
465 static void list_entries(struct client *cl, struct dirent **files, int count,
466                          const char *path, char *local_path)
467 {
468         const char *suffix = "/";
469         const char *type = "directory";
470         unsigned int mode = S_IXOTH;
471         struct stat s;
472         char *file;
473         char buf[128];
474         int i;
475
476         file = local_path + strlen(local_path);
477         for (i = 0; i < count; i++) {
478                 const char *name = files[i]->d_name;
479                 bool dir = !!(files[i]->d_type & DT_DIR);
480
481                 if (name[0] == '.' && name[1] == 0)
482                         continue;
483
484                 sprintf(file, "%s", name);
485                 if (stat(local_path, &s))
486                         continue;
487
488                 if (!dir) {
489                         suffix = "";
490                         mode = S_IROTH;
491                         type = uh_file_mime_lookup(local_path);
492                 }
493
494                 if (!(s.st_mode & mode))
495                         continue;
496
497                 uh_chunk_printf(cl,
498                                 "<li><strong><a href='%s%s%s'>%s</a>%s"
499                                 "</strong><br /><small>modified: %s"
500                                 "<br />%s - %.02f kbyte<br />"
501                                 "<br /></small></li>",
502                                 path, name, suffix,
503                                 name, suffix,
504                                 uh_file_unix2date(s.st_mtime, buf, sizeof(buf)),
505                                 type, s.st_size / 1024.0);
506
507                 *file = 0;
508                 free(files[i]);
509         }
510 }
511
512 static void uh_file_dirlist(struct client *cl, struct path_info *pi)
513 {
514         struct dirent **files = NULL;
515         int count = 0;
516
517         uh_file_response_200(cl, NULL);
518         ustream_printf(cl->us, "Content-Type: text/html\r\n\r\n");
519
520         uh_chunk_printf(cl,
521                 "<html><head><title>Index of %s</title></head>"
522                 "<body><h1>Index of %s</h1><hr /><ol>",
523                 pi->name, pi->name);
524
525         count = scandir(pi->phys, &files, NULL, dirent_cmp);
526         if (count > 0) {
527                 strcpy(uh_buf, pi->phys);
528                 list_entries(cl, files, count, pi->name, uh_buf);
529         }
530         free(files);
531
532         uh_chunk_printf(cl, "</ol><hr /></body></html>");
533         uh_request_done(cl);
534 }
535
536 static void file_write_cb(struct client *cl)
537 {
538         int fd = cl->dispatch.file.fd;
539         int r;
540
541         while (cl->us->w.data_bytes < 256) {
542                 r = read(fd, uh_buf, sizeof(uh_buf));
543                 if (r < 0) {
544                         if (errno == EINTR)
545                                 continue;
546                 }
547
548                 if (!r) {
549                         uh_request_done(cl);
550                         return;
551                 }
552
553                 uh_chunk_write(cl, uh_buf, r);
554         }
555 }
556
557 static void uh_file_free(struct client *cl)
558 {
559         close(cl->dispatch.file.fd);
560 }
561
562 static void uh_file_data(struct client *cl, struct path_info *pi, int fd)
563 {
564         /* test preconditions */
565         if (!uh_file_if_modified_since(cl, &pi->stat) ||
566                 !uh_file_if_match(cl, &pi->stat) ||
567                 !uh_file_if_range(cl, &pi->stat) ||
568                 !uh_file_if_unmodified_since(cl, &pi->stat) ||
569                 !uh_file_if_none_match(cl, &pi->stat)) {
570                 ustream_printf(cl->us, "\r\n");
571                 uh_request_done(cl);
572                 close(fd);
573                 return;
574         }
575
576         /* write status */
577         uh_file_response_200(cl, &pi->stat);
578
579         ustream_printf(cl->us, "Content-Type: %s\r\n",
580                            uh_file_mime_lookup(pi->name));
581
582         ustream_printf(cl->us, "Content-Length: %i\r\n\r\n",
583                            pi->stat.st_size);
584
585
586         /* send body */
587         if (cl->request.method == UH_HTTP_MSG_HEAD) {
588                 uh_request_done(cl);
589                 close(fd);
590                 return;
591         }
592
593         cl->dispatch.file.fd = fd;
594         cl->dispatch.write_cb = file_write_cb;
595         cl->dispatch.free = uh_file_free;
596         cl->dispatch.close_fds = uh_file_free;
597         file_write_cb(cl);
598 }
599
600 static bool __handle_file_request(struct client *cl, char *url);
601
602 static void uh_file_request(struct client *cl, const char *url,
603                             struct path_info *pi, struct blob_attr **tb)
604 {
605         int fd;
606         struct http_request *req = &cl->request;
607         char *error_handler;
608
609         if (!(pi->stat.st_mode & S_IROTH))
610                 goto error;
611
612         if (pi->stat.st_mode & S_IFREG) {
613                 fd = open(pi->phys, O_RDONLY);
614                 if (fd < 0)
615                         goto error;
616
617                 cl->dispatch.file.hdr = tb;
618                 uh_file_data(cl, pi, fd);
619                 cl->dispatch.file.hdr = NULL;
620                 return;
621         }
622
623         if ((pi->stat.st_mode & S_IFDIR)) {
624                 if (conf.no_dirlists)
625                         goto error;
626
627                 uh_file_dirlist(cl, pi);
628                 return;
629         }
630
631 error:
632         /* check for a previously set 403 redirect status to prevent infinite
633            recursion when the error page itself lacks sufficient permissions */
634         if (conf.error_handler && req->redirect_status != 403) {
635                 req->redirect_status = 403;
636                 error_handler = alloca(strlen(conf.error_handler) + 1);
637                 strcpy(error_handler, conf.error_handler);
638                 if (__handle_file_request(cl, error_handler))
639                         return;
640         }
641
642         uh_client_error(cl, 403, "Forbidden",
643                         "You don't have permission to access %s on this server.",
644                         url);
645 }
646
647 void uh_dispatch_add(struct dispatch_handler *d)
648 {
649         list_add_tail(&d->list, &dispatch_handlers);
650 }
651
652 static struct dispatch_handler *
653 dispatch_find(const char *url, struct path_info *pi)
654 {
655         struct dispatch_handler *d;
656
657         list_for_each_entry(d, &dispatch_handlers, list) {
658                 if (pi) {
659                         if (d->check_url)
660                                 continue;
661
662                         if (d->check_path(pi, url))
663                                 return d;
664                 } else {
665                         if (d->check_path)
666                                 continue;
667
668                         if (d->check_url(url))
669                                 return d;
670                 }
671         }
672
673         return NULL;
674 }
675
676 static void
677 uh_invoke_script(struct client *cl, struct dispatch_handler *d, struct path_info *pi)
678 {
679         char *url = blobmsg_data(blob_data(cl->hdr.head));
680
681         n_requests++;
682         d->handle_request(cl, url, pi);
683 }
684
685 static void uh_complete_request(struct client *cl)
686 {
687         struct deferred_request *dr;
688
689         n_requests--;
690
691         while (!list_empty(&pending_requests)) {
692                 if (n_requests >= conf.max_script_requests)
693                         return;
694
695                 dr = list_first_entry(&pending_requests, struct deferred_request, list);
696                 list_del(&dr->list);
697
698                 dr->called = true;
699                 uh_invoke_script(dr->cl, dr->d, dr->path ? &dr->pi : NULL);
700         }
701 }
702
703
704 static void
705 uh_free_pending_request(struct client *cl)
706 {
707         struct deferred_request *dr = cl->dispatch.req_data;
708
709         if (dr->called)
710                 uh_complete_request(cl);
711         else
712                 list_del(&dr->list);
713         free(dr);
714 }
715
716 static int field_len(const char *ptr)
717 {
718         if (!ptr)
719                 return 0;
720
721         return strlen(ptr) + 1;
722 }
723
724 #define path_info_fields \
725         _field(root) \
726         _field(phys) \
727         _field(name) \
728         _field(info) \
729         _field(query) \
730         _field(auth)
731
732 static void
733 uh_defer_script(struct client *cl, struct dispatch_handler *d, struct path_info *pi)
734 {
735         struct deferred_request *dr;
736         char *_root, *_phys, *_name, *_info, *_query, *_auth;
737
738         cl->dispatch.req_free = uh_free_pending_request;
739
740         if (pi) {
741                 /* allocate enough memory to duplicate all path_info strings in one block */
742 #undef _field
743 #define _field(_name) &_##_name, field_len(pi->_name),
744                 dr = calloc_a(sizeof(*dr), path_info_fields NULL);
745
746                 memcpy(&dr->pi, pi, sizeof(*pi));
747                 dr->path = true;
748
749                 /* copy all path_info strings */
750 #undef _field
751 #define _field(_name) if (pi->_name) dr->pi._name = strcpy(_##_name, pi->_name);
752                 path_info_fields
753         } else {
754                 dr = calloc(1, sizeof(*dr));
755         }
756
757         cl->dispatch.req_data = dr;
758         dr->cl = cl;
759         dr->d = d;
760         list_add(&dr->list, &pending_requests);
761 }
762
763 static void
764 uh_invoke_handler(struct client *cl, struct dispatch_handler *d, char *url, struct path_info *pi)
765 {
766         if (!d->script)
767                 return d->handle_request(cl, url, pi);
768
769         if (n_requests >= conf.max_script_requests)
770                 return uh_defer_script(cl, d, pi);
771
772         cl->dispatch.req_free = uh_complete_request;
773         uh_invoke_script(cl, d, pi);
774 }
775
776 static bool __handle_file_request(struct client *cl, char *url)
777 {
778         static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
779                 [HDR_AUTHORIZATION] = { "authorization", BLOBMSG_TYPE_STRING },
780                 [HDR_IF_MODIFIED_SINCE] = { "if-modified-since", BLOBMSG_TYPE_STRING },
781                 [HDR_IF_UNMODIFIED_SINCE] = { "if-unmodified-since", BLOBMSG_TYPE_STRING },
782                 [HDR_IF_MATCH] = { "if-match", BLOBMSG_TYPE_STRING },
783                 [HDR_IF_NONE_MATCH] = { "if-none-match", BLOBMSG_TYPE_STRING },
784                 [HDR_IF_RANGE] = { "if-range", BLOBMSG_TYPE_STRING },
785         };
786         struct dispatch_handler *d;
787         struct blob_attr *tb[__HDR_MAX];
788         struct path_info *pi;
789
790         pi = uh_path_lookup(cl, url);
791         if (!pi)
792                 return false;
793
794         if (pi->redirected)
795                 return true;
796
797         blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
798         if (tb[HDR_AUTHORIZATION])
799                 pi->auth = blobmsg_data(tb[HDR_AUTHORIZATION]);
800
801         if (!uh_auth_check(cl, pi))
802                 return true;
803
804         d = dispatch_find(url, pi);
805         if (d)
806                 uh_invoke_handler(cl, d, url, pi);
807         else
808                 uh_file_request(cl, url, pi, tb);
809
810         return true;
811 }
812
813 void uh_handle_request(struct client *cl)
814 {
815         struct http_request *req = &cl->request;
816         struct dispatch_handler *d;
817         char *url = blobmsg_data(blob_data(cl->hdr.head));
818         char *error_handler;
819
820         req->redirect_status = 200;
821         d = dispatch_find(url, NULL);
822         if (d)
823                 return uh_invoke_handler(cl, d, url, NULL);
824
825         if (__handle_file_request(cl, url))
826                 return;
827
828         req->redirect_status = 404;
829         if (conf.error_handler) {
830                 error_handler = alloca(strlen(conf.error_handler) + 1);
831                 strcpy(error_handler, conf.error_handler);
832                 if (__handle_file_request(cl, error_handler))
833                         return;
834         }
835
836         uh_client_error(cl, 404, "Not Found", "The requested URL %s was not found on this server.", url);
837 }