set the docroot to the current working directory if none is specified, fixes random...
[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, "Content-Length: 0\r\n");
571                 ustream_printf(cl->us, "\r\n");
572                 uh_request_done(cl);
573                 close(fd);
574                 return;
575         }
576
577         /* write status */
578         uh_file_response_200(cl, &pi->stat);
579
580         ustream_printf(cl->us, "Content-Type: %s\r\n",
581                            uh_file_mime_lookup(pi->name));
582
583         ustream_printf(cl->us, "Content-Length: %i\r\n\r\n",
584                            pi->stat.st_size);
585
586
587         /* send body */
588         if (cl->request.method == UH_HTTP_MSG_HEAD) {
589                 uh_request_done(cl);
590                 close(fd);
591                 return;
592         }
593
594         cl->dispatch.file.fd = fd;
595         cl->dispatch.write_cb = file_write_cb;
596         cl->dispatch.free = uh_file_free;
597         cl->dispatch.close_fds = uh_file_free;
598         file_write_cb(cl);
599 }
600
601 static void uh_file_request(struct client *cl, const char *url,
602                             struct path_info *pi, struct blob_attr **tb)
603 {
604         int fd;
605
606         if (!(pi->stat.st_mode & S_IROTH))
607                 goto error;
608
609         if (pi->stat.st_mode & S_IFREG) {
610                 fd = open(pi->phys, O_RDONLY);
611                 if (fd < 0)
612                         goto error;
613
614                 cl->dispatch.file.hdr = tb;
615                 uh_file_data(cl, pi, fd);
616                 cl->dispatch.file.hdr = NULL;
617                 return;
618         }
619
620         if ((pi->stat.st_mode & S_IFDIR)) {
621                 if (conf.no_dirlists)
622                         goto error;
623
624                 uh_file_dirlist(cl, pi);
625                 return;
626         }
627
628 error:
629         uh_client_error(cl, 403, "Forbidden",
630                         "You don't have permission to access %s on this server.",
631                         url);
632 }
633
634 void uh_dispatch_add(struct dispatch_handler *d)
635 {
636         list_add_tail(&d->list, &dispatch_handlers);
637 }
638
639 static struct dispatch_handler *
640 dispatch_find(const char *url, struct path_info *pi)
641 {
642         struct dispatch_handler *d;
643
644         list_for_each_entry(d, &dispatch_handlers, list) {
645                 if (pi) {
646                         if (d->check_url)
647                                 continue;
648
649                         if (d->check_path(pi, url))
650                                 return d;
651                 } else {
652                         if (d->check_path)
653                                 continue;
654
655                         if (d->check_url(url))
656                                 return d;
657                 }
658         }
659
660         return NULL;
661 }
662
663 static void
664 uh_invoke_script(struct client *cl, struct dispatch_handler *d, struct path_info *pi)
665 {
666         char *url = blobmsg_data(blob_data(cl->hdr.head));
667
668         n_requests++;
669         d->handle_request(cl, url, pi);
670 }
671
672 static void uh_complete_request(struct client *cl)
673 {
674         struct deferred_request *dr;
675
676         n_requests--;
677
678         while (!list_empty(&pending_requests)) {
679                 if (n_requests >= conf.max_script_requests)
680                         return;
681
682                 dr = list_first_entry(&pending_requests, struct deferred_request, list);
683                 list_del(&dr->list);
684
685                 dr->called = true;
686                 uh_invoke_script(dr->cl, dr->d, dr->path ? &dr->pi : NULL);
687         }
688 }
689
690
691 static void
692 uh_free_pending_request(struct client *cl)
693 {
694         struct deferred_request *dr = cl->dispatch.req_data;
695
696         if (dr->called)
697                 uh_complete_request(cl);
698         else
699                 list_del(&dr->list);
700         free(dr);
701 }
702
703 static int field_len(const char *ptr)
704 {
705         if (!ptr)
706                 return 0;
707
708         return strlen(ptr) + 1;
709 }
710
711 #define path_info_fields \
712         _field(root) \
713         _field(phys) \
714         _field(name) \
715         _field(info) \
716         _field(query) \
717         _field(auth)
718
719 static void
720 uh_defer_script(struct client *cl, struct dispatch_handler *d, struct path_info *pi)
721 {
722         struct deferred_request *dr;
723         char *_root, *_phys, *_name, *_info, *_query, *_auth;
724
725         cl->dispatch.req_free = uh_free_pending_request;
726
727         if (pi) {
728                 /* allocate enough memory to duplicate all path_info strings in one block */
729 #undef _field
730 #define _field(_name) &_##_name, field_len(pi->_name),
731                 dr = calloc_a(sizeof(*dr), path_info_fields NULL);
732
733                 memcpy(&dr->pi, pi, sizeof(*pi));
734                 dr->path = true;
735
736                 /* copy all path_info strings */
737 #undef _field
738 #define _field(_name) if (pi->_name) dr->pi._name = strcpy(_##_name, pi->_name);
739                 path_info_fields
740         } else {
741                 dr = calloc(1, sizeof(*dr));
742         }
743
744         cl->dispatch.req_data = dr;
745         dr->cl = cl;
746         dr->d = d;
747         list_add(&dr->list, &pending_requests);
748 }
749
750 static void
751 uh_invoke_handler(struct client *cl, struct dispatch_handler *d, char *url, struct path_info *pi)
752 {
753         if (!d->script)
754                 return d->handle_request(cl, url, pi);
755
756         if (n_requests >= conf.max_script_requests)
757                 return uh_defer_script(cl, d, pi);
758
759         cl->dispatch.req_free = uh_complete_request;
760         uh_invoke_script(cl, d, pi);
761 }
762
763 static bool __handle_file_request(struct client *cl, char *url)
764 {
765         static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
766                 [HDR_AUTHORIZATION] = { "authorization", BLOBMSG_TYPE_STRING },
767                 [HDR_IF_MODIFIED_SINCE] = { "if-modified-since", BLOBMSG_TYPE_STRING },
768                 [HDR_IF_UNMODIFIED_SINCE] = { "if-unmodified-since", BLOBMSG_TYPE_STRING },
769                 [HDR_IF_MATCH] = { "if-match", BLOBMSG_TYPE_STRING },
770                 [HDR_IF_NONE_MATCH] = { "if-none-match", BLOBMSG_TYPE_STRING },
771                 [HDR_IF_RANGE] = { "if-range", BLOBMSG_TYPE_STRING },
772         };
773         struct dispatch_handler *d;
774         struct blob_attr *tb[__HDR_MAX];
775         struct path_info *pi;
776
777         pi = uh_path_lookup(cl, url);
778         if (!pi)
779                 return false;
780
781         if (pi->redirected)
782                 return true;
783
784         blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(cl->hdr.head), blob_len(cl->hdr.head));
785         if (tb[HDR_AUTHORIZATION])
786                 pi->auth = blobmsg_data(tb[HDR_AUTHORIZATION]);
787
788         if (!uh_auth_check(cl, pi))
789                 return true;
790
791         d = dispatch_find(url, pi);
792         if (d)
793                 uh_invoke_handler(cl, d, url, pi);
794         else
795                 uh_file_request(cl, url, pi, tb);
796
797         return true;
798 }
799
800 void uh_handle_request(struct client *cl)
801 {
802         struct http_request *req = &cl->request;
803         struct dispatch_handler *d;
804         char *url = blobmsg_data(blob_data(cl->hdr.head));
805         char *error_handler;
806
807         req->redirect_status = 200;
808         d = dispatch_find(url, NULL);
809         if (d)
810                 return uh_invoke_handler(cl, d, url, NULL);
811
812         if (__handle_file_request(cl, url))
813                 return;
814
815         req->redirect_status = 404;
816         if (conf.error_handler) {
817                 error_handler = alloca(strlen(conf.error_handler) + 1);
818                 strcpy(error_handler, conf.error_handler);
819                 if (__handle_file_request(cl, error_handler))
820                         return;
821         }
822
823         uh_client_error(cl, 404, "Not Found", "The requested URL %s was not found on this server.", url);
824 }