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, "HTTP/%.1f 200 OK\r\n",
117 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, "HTTP/%.1f 304 Not Modified\r\n",
125 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 "HTTP/%.1f 412 Precondition Failed\r\n"
134 "Connection: close\r\n", cl->request.version);
137 static int uh_file_if_match(struct client *cl, struct stat *s, int *ok)
139 const char *tag = uh_file_mktag(s);
140 char *hdr = uh_file_header_lookup(cl, "If-Match");
148 for (i = 0; i < strlen(hdr); i++)
150 if ((hdr[i] == ' ') || (hdr[i] == ','))
155 else if (!strcmp(p, "*") || !strcmp(p, tag))
163 ensure_ret(uh_file_response_412(cl));
171 static int uh_file_if_modified_since(struct client *cl, struct stat *s, int *ok)
173 char *hdr = uh_file_header_lookup(cl, "If-Modified-Since");
178 if (uh_file_date2unix(hdr) >= s->st_mtime)
181 ensure_ret(uh_file_response_304(cl, s));
188 static int uh_file_if_none_match(struct client *cl, struct stat *s, int *ok)
190 const char *tag = uh_file_mktag(s);
191 char *hdr = uh_file_header_lookup(cl, "If-None-Match");
200 for (i = 0; i < strlen(hdr); i++)
202 if ((hdr[i] == ' ') || (hdr[i] == ','))
207 else if (!strcmp(p, "*") || !strcmp(p, tag))
211 if ((cl->request.method == UH_HTTP_MSG_GET) ||
212 (cl->request.method == UH_HTTP_MSG_HEAD))
214 ensure_ret(uh_file_response_304(cl, s));
218 ensure_ret(uh_file_response_412(cl));
229 static int uh_file_if_range(struct client *cl, struct stat *s, int *ok)
231 char *hdr = uh_file_header_lookup(cl, "If-Range");
237 ensure_ret(uh_file_response_412(cl));
243 static int uh_file_if_unmodified_since(struct client *cl, struct stat *s,
246 char *hdr = uh_file_header_lookup(cl, "If-Unmodified-Since");
251 if (uh_file_date2unix(hdr) <= s->st_mtime)
254 ensure_ret(uh_file_response_412(cl));
262 static int uh_file_scandir_filter_dir(const struct dirent *e)
264 return strcmp(e->d_name, ".") ? 1 : 0;
267 static void uh_file_dirlist(struct client *cl, struct path_info *pi)
271 char filename[PATH_MAX];
273 struct dirent **files = NULL;
276 ensure_out(uh_http_sendf(cl, &cl->request,
277 "<html><head><title>Index of %s</title></head>"
278 "<body><h1>Index of %s</h1><hr /><ol>",
279 pi->name, pi->name));
281 if ((count = scandir(pi->phys, &files, uh_file_scandir_filter_dir,
284 memset(filename, 0, sizeof(filename));
285 memcpy(filename, pi->phys, sizeof(filename));
286 pathptr = &filename[strlen(filename)];
289 for (i = 0; i < count; i++)
291 strncat(filename, files[i]->d_name,
292 sizeof(filename) - strlen(files[i]->d_name));
294 if (!stat(filename, &s) &&
295 (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH))
297 ensure_out(uh_http_sendf(cl, &cl->request,
298 "<li><strong><a href='%s%s'>%s</a>/"
299 "</strong><br /><small>modified: %s"
300 "<br />directory - %.02f kbyte<br />"
301 "<br /></small></li>",
302 pi->name, files[i]->d_name,
304 uh_file_unix2date(s.st_mtime),
305 s.st_size / 1024.0));
312 for (i = 0; i < count; i++)
314 strncat(filename, files[i]->d_name,
315 sizeof(filename) - strlen(files[i]->d_name));
317 if (!stat(filename, &s) &&
318 !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH))
320 ensure_out(uh_http_sendf(cl, &cl->request,
321 "<li><strong><a href='%s%s'>%s</a>"
322 "</strong><br /><small>modified: %s"
323 "<br />%s - %.02f kbyte<br />"
324 "<br /></small></li>",
325 pi->name, files[i]->d_name,
327 uh_file_unix2date(s.st_mtime),
328 uh_file_mime_lookup(filename),
329 s.st_size / 1024.0));
336 ensure_out(uh_http_sendf(cl, &cl->request, "</ol><hr /></body></html>"));
337 ensure_out(uh_http_sendf(cl, &cl->request, ""));
342 for (i = 0; i < count; i++)
350 bool uh_file_request(struct client *cl, struct path_info *pi)
355 char buf[UH_LIMIT_MSGHEAD];
358 if ((pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0))
360 /* test preconditions */
361 if (ok) ensure_out(uh_file_if_modified_since(cl, &pi->stat, &ok));
362 if (ok) ensure_out(uh_file_if_match(cl, &pi->stat, &ok));
363 if (ok) ensure_out(uh_file_if_range(cl, &pi->stat, &ok));
364 if (ok) ensure_out(uh_file_if_unmodified_since(cl, &pi->stat, &ok));
365 if (ok) ensure_out(uh_file_if_none_match(cl, &pi->stat, &ok));
370 ensure_out(uh_file_response_200(cl, &pi->stat));
372 ensure_out(uh_http_sendf(cl, NULL, "Content-Type: %s\r\n",
373 uh_file_mime_lookup(pi->name)));
375 ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n",
378 /* if request was HTTP 1.1 we'll respond chunked */
379 if ((cl->request.version > 1.0) &&
380 (cl->request.method != UH_HTTP_MSG_HEAD))
382 ensure_out(uh_http_send(cl, NULL,
383 "Transfer-Encoding: chunked\r\n", -1));
387 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
390 if (cl->request.method != UH_HTTP_MSG_HEAD)
393 while ((rlen = read(fd, buf, sizeof(buf))) > 0)
394 ensure_out(uh_http_send(cl, &cl->request, buf, rlen));
396 /* send trailer in chunked mode */
397 ensure_out(uh_http_send(cl, &cl->request, "", 0));
401 /* one of the preconditions failed, terminate opened header and exit */
404 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
409 else if ((pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists)
412 ensure_out(uh_file_response_200(cl, NULL));
414 if (cl->request.version > 1.0)
415 ensure_out(uh_http_send(cl, NULL,
416 "Transfer-Encoding: chunked\r\n", -1));
418 ensure_out(uh_http_send(cl, NULL,
419 "Content-Type: text/html\r\n\r\n", -1));
422 uh_file_dirlist(cl, pi);
428 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
429 "Access to this resource is forbidden"));