2 * uhttpd - Tiny single-threaded httpd - Static file handler
4 * Copyright (C) 2010 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
== '.') && !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
65 static time_t uh_file_date2unix(const char *date
)
69 memset(&t
, 0, sizeof(t
));
71 if( strptime(date
, "%a, %d %b %Y %H:%M:%S %Z", &t
) != NULL
)
77 static char * uh_file_unix2date(time_t ts
)
80 struct tm
*t
= gmtime(&ts
);
82 strftime(str
, sizeof(str
), "%a, %d %b %Y %H:%M:%S GMT", t
);
87 static char * uh_file_header_lookup(struct http_request
*req
, const char *name
)
91 foreach_header(i
, req
->headers
)
93 if( ! strcasecmp(req
->headers
[i
], name
) )
94 return req
->headers
[i
+1];
100 #define ensure_ret(x) \
101 do { if( x < 0 ) return; } while(0)
103 static void uh_file_response_ok_hdrs(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
105 ensure_ret(uh_http_sendf(cl
, NULL
, "Connection: close\r\n"));
109 ensure_ret(uh_http_sendf(cl
, NULL
, "ETag: %s\r\n", uh_file_mktag(s
)));
110 ensure_ret(uh_http_sendf(cl
, NULL
, "Last-Modified: %s\r\n", uh_file_unix2date(s
->st_mtime
)));
113 ensure_ret(uh_http_sendf(cl
, NULL
, "Date: %s\r\n", uh_file_unix2date(time(NULL
))));
116 static void uh_file_response_200(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
118 ensure_ret(uh_http_sendf(cl
, NULL
, "HTTP/%.1f 200 OK\r\n", req
->version
));
119 uh_file_response_ok_hdrs(cl
, req
, s
);
122 static void uh_file_response_304(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
124 ensure_ret(uh_http_sendf(cl
, NULL
, "HTTP/%.1f 304 Not Modified\r\n", req
->version
));
125 uh_file_response_ok_hdrs(cl
, req
, s
);
128 static void uh_file_response_412(struct client
*cl
, struct http_request
*req
)
130 ensure_ret(uh_http_sendf(cl
, NULL
,
131 "HTTP/%.1f 412 Precondition Failed\r\n"
132 "Connection: close\r\n", req
->version
));
135 static int uh_file_if_match(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
137 const char *tag
= uh_file_mktag(s
);
138 char *hdr
= uh_file_header_lookup(req
, "If-Match");
146 for( i
= 0; i
< strlen(hdr
); i
++ )
148 if( (hdr
[i
] == ' ') || (hdr
[i
] == ',') )
153 else if( !strcmp(p
, "*") || !strcmp(p
, tag
) )
159 uh_file_response_412(cl
, req
);
166 static int uh_file_if_modified_since(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
168 char *hdr
= uh_file_header_lookup(req
, "If-Modified-Since");
172 if( uh_file_date2unix(hdr
) < s
->st_mtime
)
178 uh_file_response_304(cl
, req
, s
);
186 static int uh_file_if_none_match(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
188 const char *tag
= uh_file_mktag(s
);
189 char *hdr
= uh_file_header_lookup(req
, "If-None-Match");
197 for( i
= 0; i
< strlen(hdr
); i
++ )
199 if( (hdr
[i
] == ' ') || (hdr
[i
] == ',') )
204 else if( !strcmp(p
, "*") || !strcmp(p
, tag
) )
206 if( (req
->method
== UH_HTTP_MSG_GET
) ||
207 (req
->method
== UH_HTTP_MSG_HEAD
) )
208 uh_file_response_304(cl
, req
, s
);
210 uh_file_response_412(cl
, req
);
220 static int uh_file_if_range(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
222 char *hdr
= uh_file_header_lookup(req
, "If-Range");
226 uh_file_response_412(cl
, req
);
233 static int uh_file_if_unmodified_since(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
235 char *hdr
= uh_file_header_lookup(req
, "If-Unmodified-Since");
239 if( uh_file_date2unix(hdr
) <= s
->st_mtime
)
241 uh_file_response_412(cl
, req
);
250 #define ensure_out(x) \
251 do { if( x < 0 ) goto out; } while(0)
253 static int uh_file_scandir_filter_dir(const struct dirent
*e
)
255 return strcmp(e
->d_name
, ".") ? 1 : 0;
258 static void uh_file_dirlist(struct client
*cl
, struct http_request
*req
, struct path_info
*pi
)
262 char filename
[PATH_MAX
];
264 struct dirent
**files
= NULL
;
267 ensure_out(uh_http_sendf(cl
, req
,
268 "<html><head><title>Index of %s</title></head>"
269 "<body><h1>Index of %s</h1><hr /><ol>",
273 if( (count
= scandir(pi
->phys
, &files
, uh_file_scandir_filter_dir
, alphasort
)) > 0 )
275 memset(filename
, 0, sizeof(filename
));
276 memcpy(filename
, pi
->phys
, sizeof(filename
));
277 pathptr
= &filename
[strlen(filename
)];
280 for( i
= 0; i
< count
; i
++ )
282 strncat(filename
, files
[i
]->d_name
,
283 sizeof(filename
) - strlen(files
[i
]->d_name
));
285 if( !stat(filename
, &s
) &&
286 (s
.st_mode
& S_IFDIR
) && (s
.st_mode
& S_IXOTH
)
288 ensure_out(uh_http_sendf(cl
, req
,
289 "<li><strong><a href='%s%s'>%s</a>/</strong><br />"
290 "<small>modified: %s<br />directory - %.02f kbyte"
291 "<br /><br /></small></li>",
292 pi
->name
, files
[i
]->d_name
, files
[i
]->d_name
,
293 uh_file_unix2date(s
.st_mtime
), s
.st_size
/ 1024.0
300 for( i
= 0; i
< count
; i
++ )
302 strncat(filename
, files
[i
]->d_name
,
303 sizeof(filename
) - strlen(files
[i
]->d_name
));
305 if( !stat(filename
, &s
) &&
306 !(s
.st_mode
& S_IFDIR
) && (s
.st_mode
& S_IROTH
)
308 ensure_out(uh_http_sendf(cl
, req
,
309 "<li><strong><a href='%s%s'>%s</a></strong><br />"
310 "<small>modified: %s<br />%s - %.02f kbyte<br />"
311 "<br /></small></li>",
312 pi
->name
, files
[i
]->d_name
, files
[i
]->d_name
,
313 uh_file_unix2date(s
.st_mtime
),
314 uh_file_mime_lookup(filename
), s
.st_size
/ 1024.0
321 ensure_out(uh_http_sendf(cl
, req
, "</ol><hr /></body></html>"));
322 ensure_out(uh_http_sendf(cl
, req
, ""));
327 for( i
= 0; i
< count
; i
++ )
335 void uh_file_request(struct client
*cl
, struct http_request
*req
, struct path_info
*pi
)
339 char buf
[UH_LIMIT_MSGHEAD
];
342 if( (pi
->stat
.st_mode
& S_IFREG
) && ((fd
= open(pi
->phys
, O_RDONLY
)) > 0) )
344 /* test preconditions */
346 uh_file_if_modified_since(cl
, req
, &pi
->stat
) &&
347 uh_file_if_match(cl
, req
, &pi
->stat
) &&
348 uh_file_if_range(cl
, req
, &pi
->stat
) &&
349 uh_file_if_unmodified_since(cl
, req
, &pi
->stat
) &&
350 uh_file_if_none_match(cl
, req
, &pi
->stat
)
353 uh_file_response_200(cl
, req
, &pi
->stat
);
355 ensure_out(uh_http_sendf(cl
, NULL
, "Content-Type: %s\r\n", uh_file_mime_lookup(pi
->name
)));
356 ensure_out(uh_http_sendf(cl
, NULL
, "Content-Length: %i\r\n", pi
->stat
.st_size
));
358 /* if request was HTTP 1.1 we'll respond chunked */
359 if( (req
->version
> 1.0) && (req
->method
!= UH_HTTP_MSG_HEAD
) )
360 ensure_out(uh_http_send(cl
, NULL
, "Transfer-Encoding: chunked\r\n", -1));
363 ensure_out(uh_http_send(cl
, NULL
, "\r\n", -1));
366 if( req
->method
!= UH_HTTP_MSG_HEAD
)
369 while( (rlen
= read(fd
, buf
, sizeof(buf
))) > 0 )
370 ensure_out(uh_http_send(cl
, req
, buf
, rlen
));
372 /* send trailer in chunked mode */
373 ensure_out(uh_http_send(cl
, req
, "", 0));
377 /* one of the preconditions failed, terminate opened header and exit */
380 ensure_out(uh_http_send(cl
, NULL
, "\r\n", -1));
385 else if( (pi
->stat
.st_mode
& S_IFDIR
) && !cl
->server
->conf
->no_dirlists
)
388 uh_file_response_200(cl
, req
, NULL
);
390 if( req
->version
> 1.0 )
391 ensure_out(uh_http_send(cl
, NULL
, "Transfer-Encoding: chunked\r\n", -1));
393 ensure_out(uh_http_send(cl
, NULL
, "Content-Type: text/html\r\n\r\n", -1));
396 uh_file_dirlist(cl
, req
, pi
);
402 ensure_out(uh_http_sendhf(cl
, 403, "Forbidden",
403 "Access to this resource is forbidden"));