Fixed: [PATCH 2/3] uhttpd URL-codec enhancements.
[openwrt.git] / package / uhttpd / src / uhttpd-file.c
1 /*
2  * uhttpd - Tiny single-threaded httpd - Static file handler
3  *
4  *   Copyright (C) 2010-2011 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 == '.' || *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
101 static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s)
102 {
103         ensure_ret(uh_http_sendf(cl, NULL, "Connection: close\r\n"));
104
105         if( s )
106         {
107                 ensure_ret(uh_http_sendf(cl, NULL, "ETag: %s\r\n", uh_file_mktag(s)));
108                 ensure_ret(uh_http_sendf(cl, NULL, "Last-Modified: %s\r\n", uh_file_unix2date(s->st_mtime)));
109         }
110
111         return uh_http_sendf(cl, NULL, "Date: %s\r\n", uh_file_unix2date(time(NULL)));
112 }
113
114 static int uh_file_response_200(struct client *cl, struct http_request *req, struct stat *s)
115 {
116         ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 200 OK\r\n", req->version));
117         return uh_file_response_ok_hdrs(cl, req, s);
118 }
119
120 static int uh_file_response_304(struct client *cl, struct http_request *req, struct stat *s)
121 {
122         ensure_ret(uh_http_sendf(cl, NULL, "HTTP/%.1f 304 Not Modified\r\n", req->version));
123         return uh_file_response_ok_hdrs(cl, req, s);
124 }
125
126 static int uh_file_response_412(struct client *cl, struct http_request *req)
127 {
128         return uh_http_sendf(cl, NULL,
129                 "HTTP/%.1f 412 Precondition Failed\r\n"
130                 "Connection: close\r\n", req->version);
131 }
132
133 static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s, int *ok)
134 {
135         const char *tag = uh_file_mktag(s);
136         char *hdr = uh_file_header_lookup(req, "If-Match");
137         char *p;
138         int i;
139
140         if( hdr )
141         {
142                 p = &hdr[0];
143
144                 for( i = 0; i < strlen(hdr); i++ )
145                 {
146                         if( (hdr[i] == ' ') || (hdr[i] == ',') )
147                         {
148                                 hdr[i++] = 0;
149                                 p = &hdr[i];
150                         }
151                         else if( !strcmp(p, "*") || !strcmp(p, tag) )
152                         {
153                                 *ok = 1;
154                                 return *ok;
155                         }
156                 }
157
158                 *ok = 0;
159                 ensure_ret(uh_file_response_412(cl, req));
160                 return *ok;
161         }
162
163         *ok = 1;
164         return *ok;
165 }
166
167 static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok)
168 {
169         char *hdr = uh_file_header_lookup(req, "If-Modified-Since");
170         *ok = 1;
171
172         if( hdr )
173         {
174                 if( uh_file_date2unix(hdr) >= s->st_mtime )
175                 {
176                         *ok = 0;
177                         ensure_ret(uh_file_response_304(cl, req, s));
178                 }
179         }
180
181         return *ok;
182 }
183
184 static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s, int *ok)
185 {
186         const char *tag = uh_file_mktag(s);
187         char *hdr = uh_file_header_lookup(req, "If-None-Match");
188         char *p;
189         int i;
190         *ok = 1;
191
192         if( hdr )
193         {
194                 p = &hdr[0];
195
196                 for( i = 0; i < strlen(hdr); i++ )
197                 {
198                         if( (hdr[i] == ' ') || (hdr[i] == ',') )
199                         {
200                                 hdr[i++] = 0;
201                                 p = &hdr[i];
202                         }
203                         else if( !strcmp(p, "*") || !strcmp(p, tag) )
204                         {
205                                 *ok = 0;
206
207                                 if( (req->method == UH_HTTP_MSG_GET) ||
208                                     (req->method == UH_HTTP_MSG_HEAD) )
209                                         ensure_ret(uh_file_response_304(cl, req, s));
210                                 else
211                                         ensure_ret(uh_file_response_412(cl, req));
212
213                                 break;
214                         }
215                 }
216         }
217
218         return *ok;
219 }
220
221 static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s, int *ok)
222 {
223         char *hdr = uh_file_header_lookup(req, "If-Range");
224         *ok = 1;
225
226         if( hdr )
227         {
228                 *ok = 0;
229                 ensure_ret(uh_file_response_412(cl, req));
230         }
231
232         return *ok;
233 }
234
235 static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok)
236 {
237         char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since");
238         *ok = 1;
239
240         if( hdr )
241         {
242                 if( uh_file_date2unix(hdr) <= s->st_mtime )
243                 {
244                         *ok = 0;
245                         ensure_ret(uh_file_response_412(cl, req));
246                 }
247         }
248
249         return *ok;
250 }
251
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 ok = 1;
339         int fd = -1;
340         char buf[UH_LIMIT_MSGHEAD];
341
342         /* we have a file */
343         if( (pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0) )
344         {
345                 /* test preconditions */
346                 if(ok) ensure_out(uh_file_if_modified_since(cl, req, &pi->stat, &ok));
347                 if(ok) ensure_out(uh_file_if_match(cl, req, &pi->stat, &ok));
348                 if(ok) ensure_out(uh_file_if_range(cl, req, &pi->stat, &ok));
349                 if(ok) ensure_out(uh_file_if_unmodified_since(cl, req, &pi->stat, &ok));
350                 if(ok) ensure_out(uh_file_if_none_match(cl, req, &pi->stat, &ok));
351
352                 if( ok > 0 )
353                 {
354                         /* write status */
355                         ensure_out(uh_file_response_200(cl, req, &pi->stat));
356
357                         ensure_out(uh_http_sendf(cl, NULL, "Content-Type: %s\r\n", uh_file_mime_lookup(pi->name)));
358                         ensure_out(uh_http_sendf(cl, NULL, "Content-Length: %i\r\n", pi->stat.st_size));
359
360                         /* if request was HTTP 1.1 we'll respond chunked */
361                         if( (req->version > 1.0) && (req->method != UH_HTTP_MSG_HEAD) )
362                                 ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1));
363
364                         /* close header */
365                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
366
367                         /* send body */
368                         if( req->method != UH_HTTP_MSG_HEAD )
369                         {
370                                 /* pump file data */
371                                 while( (rlen = read(fd, buf, sizeof(buf))) > 0 )
372                                         ensure_out(uh_http_send(cl, req, buf, rlen));
373
374                                 /* send trailer in chunked mode */
375                                 ensure_out(uh_http_send(cl, req, "", 0));
376                         }
377                 }
378
379                 /* one of the preconditions failed, terminate opened header and exit */
380                 else
381                 {
382                         ensure_out(uh_http_send(cl, NULL, "\r\n", -1));
383                 }
384         }
385
386         /* directory */
387         else if( (pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists )
388         {
389                 /* write status */
390                 ensure_out(uh_file_response_200(cl, req, NULL));
391
392                 if( req->version > 1.0 )
393                         ensure_out(uh_http_send(cl, NULL, "Transfer-Encoding: chunked\r\n", -1));
394
395                 ensure_out(uh_http_send(cl, NULL, "Content-Type: text/html\r\n\r\n", -1));
396
397                 /* content */
398                 uh_file_dirlist(cl, req, pi);
399         }
400
401         /* 403 */
402         else
403         {
404                 ensure_out(uh_http_sendhf(cl, 403, "Forbidden",
405                         "Access to this resource is forbidden"));
406         }
407
408 out:
409         if( fd > -1 )
410                 close(fd);
411 }
412