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