2 * uhttpd - Tiny single-threaded httpd - Static file handler
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #define _XOPEN_SOURCE 500 /* strptime() */
20 #define _BSD_SOURCE /* scandir(), timegm() */
23 #include "uhttpd-utils.h"
24 #include "uhttpd-file.h"
26 #include "uhttpd-mimetypes.h"
29 static const char * uh_file_mime_lookup(const char *path)
31 struct mimetype *m = &uh_mime_types[0];
36 e = &path[strlen(path)-1];
40 if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn))
49 return "application/octet-stream";
52 static const char * uh_file_mktag(struct stat *s)
56 snprintf(tag, sizeof(tag), "\"%x-%x-%x\"",
57 (unsigned int) s->st_ino,
58 (unsigned int) s->st_size,
59 (unsigned int) s->st_mtime);
64 static time_t uh_file_date2unix(const char *date)
68 memset(&t, 0, sizeof(t));
70 if (strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL)
76 static char * uh_file_unix2date(time_t ts)
79 struct tm *t = gmtime(&ts);
81 strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", t);
86 static char * uh_file_header_lookup(struct client *cl, const char *name)
90 foreach_header(i, cl->request.headers)
92 if (!strcasecmp(cl->request.headers[i], name))
93 return cl->request.headers[i+1];
100 static int uh_file_response_ok_hdrs(struct client *cl, struct stat *s)
102 ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
106 ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)));
107 ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n",
108 uh_file_unix2date(s->st_mtime)));
111 return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
114 static int uh_file_response_200(struct client *cl, struct stat *s)
116 ensure_ret(uh_http_sendf(cl, NULL, "%s 200 OK\r\n",
117 http_versions[cl->request.version]));
119 return uh_file_response_ok_hdrs(cl, s);
122 static int uh_file_response_304(struct client *cl, struct stat *s)
124 ensure_ret(uh_http_sendf(cl, NULL, "%s 304 Not Modified\r\n",
125 http_versions[cl->request.version]));
127 return uh_file_response_ok_hdrs(cl, s);
130 static int uh_file_response_412(struct client *cl)
132 return uh_http_sendf(cl, NULL,
133 "%s 412 Precondition Failed\r\n"
134 "Connection: close\r\n",
135 http_versions[cl->request.version]);
138 static int uh_file_if_match(struct client *cl, struct stat *s, int *ok)
140 const char *tag = uh_file_mktag(s);
141 char *hdr = uh_file_header_lookup(cl, "If-Match");
149 for (i = 0; i < strlen(hdr); i++)
151 if ((hdr[i] == ' ') || (hdr[i] == ','))
156 else if (!strcmp(p, "*") || !strcmp(p, tag))
164 ensure_ret(uh_file_response_412(cl));
172 static int uh_file_if_modified_since(struct client *cl, struct stat *s, int *ok)
174 char *hdr = uh_file_header_lookup(cl, "If-Modified-Since");
179 if (uh_file_date2unix(hdr) >= s->st_mtime)
182 ensure_ret(uh_file_response_304(cl, s));
189 static int uh_file_if_none_match(struct client *cl, struct stat *s, int *ok)
191 const char *tag = uh_file_mktag(s);
192 char *hdr = uh_file_header_lookup(cl, "If-None-Match");
201 for (i = 0; i < strlen(hdr); i++)
203 if ((hdr[i] == ' ') || (hdr[i] == ','))
208 else if (!strcmp(p, "*") || !strcmp(p, tag))
212 if ((cl->request.method == UH_HTTP_MSG_GET) ||
213 (cl->request.method == UH_HTTP_MSG_HEAD))
215 ensure_ret(uh_file_response_304(cl, s));
219 ensure_ret(uh_file_response_412(cl));
230 static int uh_file_if_range(struct client *cl, struct stat *s, int *ok)
232 char *hdr = uh_file_header_lookup(cl, "If-Range");
238 ensure_ret(uh_file_response_412(cl));
244 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s,
247 char *hdr = uh_file_header_lookup(cl, "If-Unmodified-Since");
252 if (uh_file_date2unix(hdr) <= s->st_mtime)
255 ensure_ret(uh_file_response_412(cl));
263 static int uh_file_scandir_filter_dir(const struct dirent *e)
265 return strcmp(e->d_name, ".") ? 1 : 0;
268 static void uh_file_dirlist(struct client *cl, struct path_info *pi)
272 char filename[PATH_MAX];
274 struct dirent **files = NULL;
277 ensure_out(uh_http_sendf(cl, &cl->request,
278 "<html><head><title>Index of %s</title></head>"
279 "<body><h1>Index of %s</h1><hr /><ol>",
280 pi->name, pi->name));
282 if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
285 memset(filename, 0, sizeof(filename));
286 memcpy(filename, pi->phys, sizeof(filename));
287 pathptr = &filename[strlen(filename)];
290 for (i = 0; i < count; i++)
292 strncat(filename, files[i]->d_name,
293 sizeof(filename) - strlen(files[i]->d_name));
295 if (!stat(filename, &s) &&
296 (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
298 ensure_out(uh_http_sendf(cl, &cl->request,
299 "<li><strong><a href='%s%s'>%s</a>/"
300 "</strong><br /><small>modified: %s"
301 "<br />directory - %.02f kbyte<br />"
302 "<br /></small></li>",
303 pi->name, files[i]->d_name,
305 uh_file_unix2date(s.st_mtime),
306 s.st_size / 1024.0));
313 for (i = 0; i < count; i++)
315 strncat(filename, files[i]->d_name,
316 sizeof(filename) - strlen(files[i]->d_name));
318 if (!stat(filename, &s) &&
319 !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))
321 ensure_out(uh_http_sendf(cl, &cl->request,
322 "<li><strong><a href='%s%s'>%s</a>"
323 "</strong><br /><small>modified: %s"
324 "<br />%s - %.02f kbyte<br />"
325 "<br /></small></li>",
326 pi->name, files[i]->d_name,
328 uh_file_unix2date(s.st_mtime),
329 uh_file_mime_lookup(filename),
330 s.st_size / 1024.0));
337 ensure_out(uh_http_sendf(cl, &cl->request, "</ol><hr /></body></html>"));
338 ensure_out(uh_http_sendf(cl, &cl->request, ""));
343 for (i = 0; i < count; i++)
351 bool uh_file_request(struct client *cl, struct path_info *pi)
356 char buf[UH_LIMIT_MSGHEAD];
359 if ((pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0))
361 /* test preconditions */
362 if (ok) ensure_out(uh_file_if_modified_since(cl, &pi->stat, &ok));
363 if (ok) ensure_out(uh_file_if_match(cl, &pi->stat, &ok));
364 if (ok) ensure_out(uh_file_if_range(cl, &pi->stat, &ok));
365 if (ok) ensure_out(uh_file_if_unmodified_since(cl, &pi->stat, &ok));
366 if (ok) ensure_out(uh_file_if_none_match(cl, &pi->stat, &ok));
371 ensure_out(uh_file_response_200(cl, &pi->stat));
373 ensure_out(uh_http_sendf(cl, NULL, "Content-Type: %s\r\n",
374 uh_file_mime_lookup(pi->name)));
376 ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n",
379 /* if request was HTTP 1.1 we'll respond chunked */
380 if ((cl->request.version > 1.0) &&
381 (cl->request.method != UH_HTTP_MSG_HEAD))
383 ensure_out(uh_http_send(cl, NULL,
384 "Transfer-Encoding: chunked\r\n", -1));
388 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
391 if (cl->request.method != UH_HTTP_MSG_HEAD)
394 while ((rlen = read(fd, buf, sizeof(buf))) > 0)
395 ensure_out(uh_http_send(cl, &cl->request, buf, rlen));
397 /* send trailer in chunked mode */
398 ensure_out(uh_http_send(cl, &cl->request, "", 0));
402 /* one of the preconditions failed, terminate opened header and exit */
405 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
410 else if ((pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists)
413 ensure_out(uh_file_response_200(cl, NULL));
415 if (cl->request.version > 1.0)
416 ensure_out(uh_http_send(cl, NULL,
417 "Transfer-Encoding: chunked\r\n", -1));
419 ensure_out(uh_http_send(cl, NULL,
420 "Content-Type: text/html\r\n\r\n", -1));
423 uh_file_dirlist(cl, pi);
429 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
430 "Access to this resource is forbidden"));