file: explicitely cast st_mtime to uint64_t when generating ETag
[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 #include <stdint.h>
30
31 #include <libubox/blobmsg.h>
32
33 #include "uhttpd.h"
34 #include "mimetypes.h"
35
36 static LIST_HEAD(index_files);
37 static LIST_HEAD(dispatch_handlers);
38 static LIST_HEAD(pending_requests);
39 static int n_requests;
40
41 struct deferred_request {
42 struct list_head list;
43 struct dispatch_handler *d;
44 struct client *cl;
45 struct path_info pi;
46 bool called, path;
47 };
48
49 struct index_file {
50 struct list_head list;
51 const char *name;
52 };
53
54 enum file_hdr {
55 HDR_AUTHORIZATION,
56 HDR_IF_MODIFIED_SINCE,
57 HDR_IF_UNMODIFIED_SINCE,
58 HDR_IF_MATCH,
59 HDR_IF_NONE_MATCH,
60 HDR_IF_RANGE,
61 __HDR_MAX
62 };
63
64 void uh_index_add(const char *filename)
65 {
66 struct index_file *idx;
67
68 idx = calloc(1, sizeof(*idx));
69 idx->name = filename;
70 list_add_tail(&idx->list, &index_files);
71 }
72
73 static char * canonpath(const char *path, char *path_resolved)
74 {
75 const char *path_cpy = path;
76 char *path_res = path_resolved;
77
78 if (conf.no_symlinks)
79 return realpath(path, path_resolved);
80
81 /* normalize */
82 while ((*path_cpy != '\0') && (path_cpy < (path + PATH_MAX - 2))) {
83 if (*path_cpy != '/')
84 goto next;
85
86 /* skip repeating / */
87 if (path_cpy[1] == '/') {
88 path_cpy++;
89 continue;
90 }
91
92 /* /./ or /../ */
93 if (path_cpy[1] == '.') {
94 /* skip /./ */
95 if ((path_cpy[2] == '/') || (path_cpy[2] == '\0')) {
96 path_cpy += 2;
97 continue;
98 }
99
100 /* collapse /x/../ */
101 if ((path_cpy[2] == '.') &&
102 ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))) {
103 while ((path_res > path_resolved) && (*--path_res != '/'));
104
105 path_cpy += 3;
106 continue;
107 }
108 }
109
110 next:
111 *path_res++ = *path_cpy++;
112 }
113
114 /* remove trailing slash if not root / */
115 if ((path_res > (path_resolved+1)) && (path_res[-1] == '/'))
116 path_res--;
117 else if (path_res == path_resolved)
118 *path_res++ = '/';
119
120 *path_res = '\0';
121
122 return path_resolved;
123 }
124
125 /* Returns NULL on error.
126 ** NB: improperly encoded URL should give client 400 [Bad Syntax]; returning
127 ** NULL here causes 404 [Not Found], but that's not too unreasonable. */
128 static struct path_info *
129 uh_path_lookup(struct client *cl, const char *url)
130 {
131 static char path_phys[PATH_MAX];
132 static char path_info[PATH_MAX];
133 static struct path_info p;
134
135 const char *docroot = conf.docroot;
136 int docroot_len = strlen(docroot);
137 char *pathptr = NULL;
138 bool slash;
139
140 int i = 0;
141 int len;
142 struct stat s;
143 struct index_file *idx;
144
145 /* back out early if url is undefined */
146 if (url == NULL)
147 return NULL;
148
149 memset(&p, 0, sizeof(p));
150 path_phys[0] = 0;
151 path_info[0] = 0;
152
153 strcpy(uh_buf, docroot);
154
155 /* separate query string from url */
156 if ((pathptr = strchr(url, '?')) != NULL) {
157 p.query = pathptr[1] ? pathptr + 1 : NULL;
158
159 /* urldecode component w/o query */
160 if (pathptr > url) {
161 if (uh_urldecode(&uh_buf[docroot_len],
162 sizeof(uh_buf) - docroot_len - 1,
163 url, pathptr - url ) < 0)
164 return NULL;
165 }
166 }
167
168 /* no query string, decode all of url */
169 else if (uh_urldecode(&uh_buf[docroot_len],
170 sizeof(uh_buf) - docroot_len - 1,
171 url, strlen(url) ) < 0)
172 return NULL;
173
174 /* create canon path */
175 len = strlen(uh_buf);
176 slash = len && uh_buf[len - 1] == '/';
177 len = min(len, sizeof(path_phys) - 1);
178
179 for (i = len; i >= 0; i--) {
180 char ch = uh_buf[i];
181 bool exists;
182
183 if (ch != 0 && ch != '/')
184 continue;
185
186 uh_buf[i] = 0;
187 exists = !!canonpath(uh_buf, path_phys);
188 uh_buf[i] = ch;
189
190 if (!exists)
191 continue;
192
193 /* test current path */
194 if (stat(path_phys, &p.stat))
195 continue;
196
197 snprintf(path_info, sizeof(path_info), "%s", uh_buf + i);
198 break;
199 }
200
201 /* check whether found path is within docroot */
202 if (strncmp(path_phys, docroot, docroot_len) != 0 ||
203 (path_phys[docroot_len] != 0 &&
204 path_phys[docroot_len] != '/'))
205 return NULL;
206
207 /* is a regular file */
208 if (p.stat.st_mode & S_IFREG) {
209 p.root = docroot;
210 p.phys = path_phys;
211 p.name = &path_phys[docroot_len];
212 p.info = path_info[0] ? path_info : NULL;
213 return &p;
214 }
215
216 if (!(p.stat.st_mode & S_IFDIR))
217 return NULL;
218
219 if (path_info[0])
220 return NULL;
221
222 pathptr = path_phys + strlen(path_phys);
223
224 /* ensure trailing slash */
225 if (pathptr[-1] != '/') {
226 pathptr[0] = '/';
227 pathptr[1] = 0;
228 pathptr++;
229 }
230
231 /* if requested url resolves to a directory and a trailing slash
232 is missing in the request url, redirect the client to the same
233 url with trailing slash appended */
234 if (!slash) {
235 uh_http_header(cl, 302, "Found");
236 ustream_printf(cl->us, "Content-Length: 0\r\n");
237 ustream_printf(cl->us, "Location: %s%s%s\r\n\r\n",
238 &path_phys[docroot_len],
239 p.query ? "?" : "",
240 p.query ? p.query : "");
241 uh_request_done(cl);
242 p.redirected = 1;
243 return &p;
244 }
245
246 /* try to locate index file */
247 len = path_phys + sizeof(path_phys) - pathptr - 1;
248 list_for_each_entry(idx, &index_files, list) {
249 if (strlen(idx->name) > len)
250 continue;
251
252 strcpy(pathptr, idx->name);
253 if (!stat(path_phys, &s) && (s.st_mode & S_IFREG)) {
254 memcpy(&p.stat, &s, sizeof(p.stat));
255 break;
256 }
257
258 *pathptr = 0;
259 }
260
261 p.root = docroot;
262 p.phys = path_phys;
263 p.name = &path_phys[docroot_len];
264
265 return p.phys ? &p : NULL;
266 }
267
268 static const char * uh_file_mime_lookup(const char *path)
269 {
270 const struct mimetype *m = &uh_mime_types[0];
271 const char *e;
272
273 while (m->extn) {
274 e = &path[strlen(path)-1];
275
276 while (e >= path) {
277 if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn))
278 return m->mime;
279
280 e--;
281 }
282
283 m++;
284 }
285
286 return "application/octet-stream";
287 }
288
289 static const char * uh_file_mktag(struct stat *s, char *buf, int len)
290 {
291 snprintf(buf, len, "\"%" PRIx64 "-%" PRIx64 "-%" PRIx64 "\"",
292 s->st_ino, s->st_size, (uint64_t)s->st_mtime);
293
294 return buf;
295 }
296
297 static time_t uh_file_date2unix(const char *date)
298 {
299 struct tm t;
300
301 memset(&t, 0, sizeof(t));
302
303 if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL)
304 return timegm(&t);
305
306 return 0;
307 }
308
309 static char * uh_file_unix2date(time_t ts, char *buf, int len)
310 {
311 struct tm *t = gmtime(&ts);
312
313 strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", t);
314
315 return buf;
316 }
317
318 static char *uh_file_header(struct client *cl, int idx)
319 {
320 if (!cl->dispatch.file.hdr[idx])
321 return NULL;
322
323 return (char *) blobmsg_data(cl->dispatch.file.hdr[idx]);
324 }
325
326 static void uh_file_response_ok_hdrs(struct client *cl, struct stat *s)
327 {
328 char buf[128];
329
330 if (s) {
331 ustream_printf(cl->us, "ETag: %s\r\n", uh_file_mktag(s, buf, sizeof(buf)));
332 ustream_printf(cl->us, "Last-Modified: %s\r\n",
333 uh_file_unix2date(s->st_mtime, buf, sizeof(buf)));
334 }
335 ustream_printf(cl->us, "Date: %s\r\n",
336 uh_file_unix2date(time(NULL), buf, sizeof(buf)));
337 }
338
339 static void uh_file_response_200(struct client *cl, struct stat *s)
340 {
341 uh_http_header(cl, 200, "OK");
342 return uh_file_response_ok_hdrs(cl, s);
343 }
344
345 static void uh_file_response_304(struct client *cl, struct stat *s)
346 {
347 uh_http_header(cl, 304, "Not Modified");
348
349 return uh_file_response_ok_hdrs(cl, s);
350 }
351
352 static void uh_file_response_412(struct client *cl)
353 {
354 uh_http_header(cl, 412, "Precondition Failed");
355 }
356
357 static bool uh_file_if_match(struct client *cl, struct stat *s)
358 {
359 char buf[128];
360 const char *tag = uh_file_mktag(s, buf, sizeof(buf));
361 char *hdr = uh_file_header(cl, HDR_IF_MATCH);
362 char *p;
363 int i;
364
365 if (!hdr)
366 return true;
367
368 p = &hdr[0];
369 for (i = 0; i < strlen(hdr); i++)
370 {
371 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
372 hdr[i++] = 0;
373 p = &hdr[i];
374 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
375 return true;
376 }
377 }
378
379 uh_file_response_412(cl);
380 return false;
381 }
382
383 static int uh_file_if_modified_since(struct client *cl, struct stat *s)
384 {
385 char *hdr = uh_file_header(cl, HDR_IF_MODIFIED_SINCE);
386
387 if (!hdr)
388 return true;
389
390 if (uh_file_date2unix(hdr) >= s->st_mtime) {
391 uh_file_response_304(cl, s);
392 return false;
393 }
394
395 return true;
396 }
397
398 static int uh_file_if_none_match(struct client *cl, struct stat *s)
399 {
400 char buf[128];
401 const char *tag = uh_file_mktag(s, buf, sizeof(buf));
402 char *hdr = uh_file_header(cl, HDR_IF_NONE_MATCH);
403 char *p;
404 int i;
405
406 if (!hdr)
407 return true;
408
409 p = &hdr[0];
410 for (i = 0; i < strlen(hdr); i++) {
411 if ((hdr[i] == ' ') || (hdr[i] == ',')) {
412 hdr[i++] = 0;
413 p = &hdr[i];
414 } else if (!strcmp(p, "*") || !strcmp(p, tag)) {
415 if ((cl->request.method == UH_HTTP_MSG_GET) ||
416 (cl->request.method == UH_HTTP_MSG_HEAD))
417 uh_file_response_304(cl, s);
418 else
419 uh_file_response_412(cl);
420
421 return false;
422 }
423 }
424
425 return true;
426 }
427
428 static int uh_file_if_range(struct client *cl, struct stat *s)
429 {
430 char *hdr = uh_file_header(cl, HDR_IF_RANGE);
431
432 if (hdr) {
433 uh_file_response_412(cl);
434 return false;
435 }
436
437 return true;
438 }
439
440 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s)
441 {
442 char *hdr = uh_file_header(cl, HDR_IF_UNMODIFIED_SINCE);
443
444 if (hdr && uh_file_date2unix(hdr) <= s->st_mtime) {
445 uh_file_response_412(cl);
446 return false;
447 }
448
449 return true;
450 }
451
452 static int dirent_cmp(const struct dirent **a, const struct dirent **b)
453 {
454 bool dir_a = !!((*a)->d_type & DT_DIR);
455 bool dir_b = !!((*b)->d_type & DT_DIR);
456
457 /* directories first */
458 if (dir_a != dir_b)
459 return dir_b - dir_a;
460
461 return alphasort(a, b);
462 }
463
464 static void list_entries(struct client *cl, struct dirent **files, int count,
465 const char *path, char *local_path)
466 {
467 const char *suffix = "/";
468 const char *type = "directory";
469 unsigned int mode = S_IXOTH;
470 struct stat s;
471 char *file;
472 char buf[128];
473 int i;
474
475 file = local_path + strlen(local_path);
476 for (i = 0; i < count; i++) {
477 const char *name = files[i]->d_name;
478 bool dir = !!(files[i]->d_type & DT_DIR);
479
480 if (name[0] == '.' && name[1] == 0)
481 goto next;
482
483 sprintf(file, "%s", name);
484 if (stat(local_path, &s))
485 goto next;
486
487 if (!dir) {
488 suffix = "";
489 mode = S_IROTH;
490 type = uh_file_mime_lookup(local_path);
491 }
492
493 if (!(s.st_mode & mode))
494 goto next;
495
496 uh_chunk_printf(cl,
497 "<li><strong><a href='%s%s%s'>%s</a>%s"
498 "</strong><br /><small>modified: %s"
499 "<br />%s - %.02f kbyte<br />"
500 "<br /></small></li>",
501 path, name, suffix,
502 name, suffix,
503 uh_file_unix2date(s.st_mtime, buf, sizeof(buf)),
504 type, s.st_size / 1024.0);
505
506 *file = 0;
507 next:
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: %" PRIu64 "\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 }