25a5f6ece3534b0ff2198e350d2fe9c14efc939e
[openwrt/svn-archive/archive.git] / package / uhttpd / src / uhttpd-file.c
1 /*
2 * uhttpd - Tiny single-threaded httpd - Static file handler
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 #define _XOPEN_SOURCE 500 /* strptime() */
20 #define _BSD_SOURCE /* scandir(), timegm() */
21
22 #include "uhttpd.h"
23 #include "uhttpd-utils.h"
24 #include "uhttpd-file.h"
25
26 #include "uhttpd-mimetypes.h"
27
28
29 static const char * uh_file_mime_lookup(const char *path)
30 {
31 struct mimetype *m = &uh_mime_types[0];
32 const char *e;
33
34 while( m->extn )
35 {
36 e = &path[strlen(path)-1];
37
38 while( e >= path )
39 {
40 if( (*e == '.') && !strcasecmp(&e[1], m->extn) )
41 return m->mime;
42
43 e--;
44 }
45
46 m++;
47 }
48
49 return "application/octet-stream";
50 }
51
52 static const char * uh_file_mktag(struct stat *s)
53 {
54 static char tag[128];
55
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
60 );
61
62 return tag;
63 }
64
65 static time_t uh_file_date2unix(const char *date)
66 {
67 struct tm t;
68
69 memset(&t, 0, sizeof(t));
70
71 if( strptime(date, "%a, %d %b %Y %H:%M:%S %Z", &t) != NULL )
72 return timegm(&t);
73
74 return 0;
75 }
76
77 static char * uh_file_unix2date(time_t ts)
78 {
79 static char str[128];
80 struct tm *t = gmtime(&ts);
81
82 strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", t);
83
84 return str;
85 }
86
87 static char * uh_file_header_lookup(struct http_request *req, const char *name)
88 {
89 int i;
90
91 foreach_header(i, req->headers)
92 {
93 if( ! strcasecmp(req->headers[i], name) )
94 return req->headers[i+1];
95 }
96
97 return NULL;
98 }
99
100 #define ensure_ret(x) \
101 do { if( x < 0 ) return -1; } while(0)
102
103 static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s)
104 {
105 ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
106
107 if( s )
108 {
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)));
111 }
112
113 return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
114 }
115
116 static int uh_file_response_200(struct client *cl, struct http_request *req, struct stat *s)
117 {
118 ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version));
119 return uh_file_response_ok_hdrs(cl, req, s);
120 }
121
122 static int uh_file_response_304(struct client *cl, struct http_request *req, struct stat *s)
123 {
124 ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version));
125 return uh_file_response_ok_hdrs(cl, req, s);
126 }
127
128 static int uh_file_response_412(struct client *cl, struct http_request *req)
129 {
130 return uh_http_sendf(cl, NULL,
131 "HTTP/%.1f 412 Precondition Failed\r\n"
132 "Connection: close\r\n", req->version);
133 }
134
135 static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s)
136 {
137 const char *tag = uh_file_mktag(s);
138 char *hdr = uh_file_header_lookup(req, "If-Match");
139 char *p;
140 int i;
141
142 if( hdr )
143 {
144 p = &hdr[0];
145
146 for( i = 0; i < strlen(hdr); i++ )
147 {
148 if( (hdr[i] == ' ') || (hdr[i] == ',') )
149 {
150 hdr[i++] = 0;
151 p = &hdr[i];
152 }
153 else if( !strcmp(p, "*") || !strcmp(p, tag) )
154 {
155 return 1;
156 }
157 }
158
159 uh_file_response_412(cl, req);
160 return 0;
161 }
162
163 return 1;
164 }
165
166 static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s)
167 {
168 char *hdr = uh_file_header_lookup(req, "If-Modified-Since");
169
170 if( hdr )
171 {
172 if( uh_file_date2unix(hdr) < s->st_mtime )
173 {
174 return 1;
175 }
176 else
177 {
178 uh_file_response_304(cl, req, s);
179 return 0;
180 }
181 }
182
183 return 1;
184 }
185
186 static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s)
187 {
188 const char *tag = uh_file_mktag(s);
189 char *hdr = uh_file_header_lookup(req, "If-None-Match");
190 char *p;
191 int i;
192
193 if( hdr )
194 {
195 p = &hdr[0];
196
197 for( i = 0; i < strlen(hdr); i++ )
198 {
199 if( (hdr[i] == ' ') || (hdr[i] == ',') )
200 {
201 hdr[i++] = 0;
202 p = &hdr[i];
203 }
204 else if( !strcmp(p, "*") || !strcmp(p, tag) )
205 {
206 if( (req->method == UH_HTTP_MSG_GET) ||
207 (req->method == UH_HTTP_MSG_HEAD) )
208 uh_file_response_304(cl, req, s);
209 else
210 uh_file_response_412(cl, req);
211
212 return 0;
213 }
214 }
215 }
216
217 return 1;
218 }
219
220 static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s)
221 {
222 char *hdr = uh_file_header_lookup(req, "If-Range");
223
224 if( hdr )
225 {
226 uh_file_response_412(cl, req);
227 return 0;
228 }
229
230 return 1;
231 }
232
233 static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s)
234 {
235 char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since");
236
237 if( hdr )
238 {
239 if( uh_file_date2unix(hdr) <= s->st_mtime )
240 {
241 uh_file_response_412(cl, req);
242 return 0;
243 }
244 }
245
246 return 1;
247 }
248
249
250 #define ensure_out(x) \
251 do { if( x < 0 ) goto out; } while(0)
252
253 static int uh_file_scandir_filter_dir(const struct dirent *e)
254 {
255 return strcmp(e->d_name, ".") ? 1 : 0;
256 }
257
258 static void uh_file_dirlist(struct client *cl, struct http_request *req, struct path_info *pi)
259 {
260 int i;
261 int count = 0;
262 char filename[PATH_MAX];
263 char *pathptr;
264 struct dirent **files = NULL;
265 struct stat s;
266
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>",
270 pi->name, pi->name
271 ));
272
273 if( (count = scandir(pi->phys, &files, uh_file_scandir_filter_dir, alphasort)) > 0 )
274 {
275 memset(filename, 0, sizeof(filename));
276 memcpy(filename, pi->phys, sizeof(filename));
277 pathptr = &filename[strlen(filename)];
278
279 /* list subdirs */
280 for( i = 0; i < count; i++ )
281 {
282 strncat(filename, files[i]->d_name,
283 sizeof(filename) - strlen(files[i]->d_name));
284
285 if( !stat(filename, &s) &&
286 (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH)
287 )
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
294 ));
295
296 *pathptr = 0;
297 }
298
299 /* list files */
300 for( i = 0; i < count; i++ )
301 {
302 strncat(filename, files[i]->d_name,
303 sizeof(filename) - strlen(files[i]->d_name));
304
305 if( !stat(filename, &s) &&
306 !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH)
307 )
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
315 ));
316
317 *pathptr = 0;
318 }
319 }
320
321 ensure_out(uh_http_sendf(cl, req, "</ol><hr /></body></html>"));
322 ensure_out(uh_http_sendf(cl, req, ""));
323
324 out:
325 if( files )
326 {
327 for( i = 0; i < count; i++ )
328 free(files[i]);
329
330 free(files);
331 }
332 }
333
334
335 void uh_file_request(struct client *cl, struct http_request *req, struct path_info *pi)
336 {
337 int rlen;
338 int fd = -1;
339 char buf[UH_LIMIT_MSGHEAD];
340
341 /* we have a file */
342 if( (pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0) )
343 {
344 /* test preconditions */
345 if(
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)
351 ) {
352 /* write status */
353 ensure_out(uh_file_response_200(cl, req, &pi->stat));
354
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));
357
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));
361
362 /* close header */
363 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
364
365 /* send body */
366 if( req->method != UH_HTTP_MSG_HEAD )
367 {
368 /* pump file data */
369 while( (rlen = read(fd, buf, sizeof(buf))) > 0 )
370 ensure_out(uh_http_send(cl, req, buf, rlen));
371
372 /* send trailer in chunked mode */
373 ensure_out(uh_http_send(cl, req, "", 0));
374 }
375 }
376
377 /* one of the preconditions failed, terminate opened header and exit */
378 else
379 {
380 ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
381 }
382 }
383
384 /* directory */
385 else if( (pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists )
386 {
387 /* write status */
388 ensure_out(uh_file_response_200(cl, req, NULL));
389
390 if( req->version > 1.0 )
391 ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1));
392
393 ensure_out(uh_http_send(cl, NULL, "Content-Type: text/html\r\n\r\n", -1));
394
395 /* content */
396 uh_file_dirlist(cl, req, pi);
397 }
398
399 /* 403 */
400 else
401 {
402 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
403 "Access to this resource is forbidden"));
404 }
405
406 out:
407 if( fd > -1 )
408 close(fd);
409 }
410