2 * uhttpd - Tiny non-forking 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() ... */
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];
34 ps
= strrchr(path
, '/');
35 pd
= strrchr(path
, '.');
37 /* use either slash or dot as separator, whatever comes last */
38 p
= (ps
&& pd
&& (ps
> pd
)) ? ps
: pd
;
40 if( (p
!= NULL
) && (*(++p
) != 0) )
44 if( ! strcasecmp(p
, m
->extn
) )
51 return "application/octet-stream";
54 static const char * uh_file_mktag(struct stat
*s
)
58 snprintf(tag
, sizeof(tag
), "\"%x-%x-%x\"",
59 (unsigned int) s
->st_ino
,
60 (unsigned int) s
->st_size
,
61 (unsigned int) s
->st_mtime
67 static time_t uh_file_date2unix(const char *date
)
71 memset(&t
, 0, sizeof(t
));
73 if( strptime(date
, "%a, %d %b %Y %H:%M:%S %Z", &t
) != NULL
)
79 static char * uh_file_unix2date(time_t ts
)
82 struct tm
*t
= gmtime(&ts
);
84 strftime(str
, sizeof(str
), "%a, %d %b %Y %H:%M:%S GMT", t
);
89 static char * uh_file_header_lookup(struct http_request
*req
, const char *name
)
93 foreach_header(i
, req
->headers
)
95 if( ! strcasecmp(req
->headers
[i
], name
) )
96 return req
->headers
[i
+1];
102 static void uh_file_response_ok_hdrs(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
106 uh_http_sendf(cl
, NULL
, "ETag: %s\r\n", uh_file_mktag(s
));
107 uh_http_sendf(cl
, NULL
, "Last-Modified: %s\r\n", uh_file_unix2date(s
->st_mtime
));
110 uh_http_sendf(cl
, NULL
, "Date: %s\r\n", uh_file_unix2date(time(NULL
)));
113 static void uh_file_response_200(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
115 uh_http_sendf(cl
, NULL
, "HTTP/%.1f 200 OK\r\n", req
->version
);
116 uh_file_response_ok_hdrs(cl
, req
, s
);
119 static void uh_file_response_304(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
121 uh_http_sendf(cl
, NULL
, "HTTP/%.1f 304 Not Modified\r\n", req
->version
);
122 uh_file_response_ok_hdrs(cl
, req
, s
);
125 static void uh_file_response_412(struct client
*cl
, struct http_request
*req
)
127 uh_http_sendf(cl
, NULL
, "HTTP/%.1f 412 Precondition Failed\r\n",
131 static int uh_file_if_match(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
133 const char *tag
= uh_file_mktag(s
);
134 char *hdr
= uh_file_header_lookup(req
, "If-Match");
142 for( i
= 0; i
< strlen(hdr
); i
++ )
144 if( (hdr
[i
] == ' ') || (hdr
[i
] == ',') )
149 else if( !strcmp(p
, "*") || !strcmp(p
, tag
) )
155 uh_file_response_412(cl
, req
);
162 static int uh_file_if_modified_since(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
164 char *hdr
= uh_file_header_lookup(req
, "If-Modified-Since");
168 if( uh_file_date2unix(hdr
) < s
->st_mtime
)
174 uh_file_response_304(cl
, req
, s
);
182 static int uh_file_if_none_match(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
184 const char *tag
= uh_file_mktag(s
);
185 char *hdr
= uh_file_header_lookup(req
, "If-None-Match");
193 for( i
= 0; i
< strlen(hdr
); i
++ )
195 if( (hdr
[i
] == ' ') || (hdr
[i
] == ',') )
200 else if( !strcmp(p
, "*") || !strcmp(p
, tag
) )
202 if( (req
->method
== UH_HTTP_MSG_GET
) ||
203 (req
->method
== UH_HTTP_MSG_HEAD
) )
204 uh_file_response_304(cl
, req
, s
);
206 uh_file_response_412(cl
, req
);
216 static int uh_file_if_range(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
218 char *hdr
= uh_file_header_lookup(req
, "If-Range");
222 uh_file_response_412(cl
, req
);
229 static int uh_file_if_unmodified_since(struct client
*cl
, struct http_request
*req
, struct stat
*s
)
231 char *hdr
= uh_file_header_lookup(req
, "If-Unmodified-Since");
235 if( uh_file_date2unix(hdr
) <= s
->st_mtime
)
237 uh_file_response_412(cl
, req
);
246 static int uh_file_scandir_filter_dir(const struct dirent
*e
)
248 return strcmp(e
->d_name
, ".") ? 1 : 0;
251 static void uh_file_dirlist(struct client
*cl
, struct http_request
*req
, struct uh_path_info
*pi
)
254 char filename
[PATH_MAX
];
256 struct dirent
**files
= NULL
;
259 uh_http_sendf(cl
, req
,
260 "<html><head><title>Index of %s</title></head>"
261 "<body><h1>Index of %s</h1><hr /><ol>",
265 if( (count
= scandir(pi
->phys
, &files
, uh_file_scandir_filter_dir
, alphasort
)) > 0 )
267 memset(filename
, 0, sizeof(filename
));
268 memcpy(filename
, pi
->phys
, sizeof(filename
));
269 pathptr
= &filename
[strlen(filename
)];
272 for( i
= 0; i
< count
; i
++ )
274 strncat(filename
, files
[i
]->d_name
,
275 sizeof(filename
) - strlen(files
[i
]->d_name
));
277 if( !stat(filename
, &s
) && (s
.st_mode
& S_IFDIR
) )
278 uh_http_sendf(cl
, req
,
279 "<li><strong><a href='%s%s'>%s</a>/</strong><br />"
280 "<small>modified: %s<br />directory - %.02f kbyte"
281 "<br /><br /></small></li>",
282 pi
->name
, files
[i
]->d_name
, files
[i
]->d_name
,
283 uh_file_unix2date(s
.st_mtime
), s
.st_size
/ 1024.0
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
) && !(s
.st_mode
& S_IFDIR
) )
296 uh_http_sendf(cl
, req
,
297 "<li><strong><a href='%s%s'>%s</a></strong><br />"
298 "<small>modified: %s<br />%s - %.02f kbyte<br />"
299 "<br /></small></li>",
300 pi
->name
, files
[i
]->d_name
, files
[i
]->d_name
,
301 uh_file_unix2date(s
.st_mtime
),
302 uh_file_mime_lookup(filename
), s
.st_size
/ 1024.0
312 uh_http_sendf(cl
, req
, "</ol><hr /></body></html>");
313 uh_http_sendf(cl
, req
, "");
317 void uh_file_request(struct client
*cl
, struct http_request
*req
, struct uh_path_info
*pi
)
320 char buf
[UH_LIMIT_MSGHEAD
];
323 if( (pi
->stat
.st_mode
& S_IFREG
) && ((fd
= open(pi
->phys
, O_RDONLY
)) > 0) )
325 /* test preconditions */
327 uh_file_if_modified_since(cl
, req
, &pi
->stat
) &&
328 uh_file_if_match(cl
, req
, &pi
->stat
) &&
329 uh_file_if_range(cl
, req
, &pi
->stat
) &&
330 uh_file_if_unmodified_since(cl
, req
, &pi
->stat
) &&
331 uh_file_if_none_match(cl
, req
, &pi
->stat
)
334 uh_file_response_200(cl
, req
, &pi
->stat
);
336 uh_http_sendf(cl
, NULL
, "Content-Type: %s\r\n", uh_file_mime_lookup(pi
->name
));
337 uh_http_sendf(cl
, NULL
, "Content-Length: %i\r\n", pi
->stat
.st_size
);
339 /* if request was HTTP 1.1 we'll respond chunked */
340 if( (req
->version
> 1.0) && (req
->method
!= UH_HTTP_MSG_HEAD
) )
341 uh_http_send(cl
, NULL
, "Transfer-Encoding: chunked\r\n", -1);
344 uh_http_send(cl
, NULL
, "\r\n", -1);
347 if( req
->method
!= UH_HTTP_MSG_HEAD
)
350 while( (rlen
= read(fd
, buf
, sizeof(buf
))) > 0 )
352 uh_http_send(cl
, req
, buf
, rlen
);
355 /* send trailer in chunked mode */
356 uh_http_send(cl
, req
, "", 0);
360 /* one of the preconditions failed, terminate opened header and exit */
363 uh_http_send(cl
, NULL
, "\r\n", -1);
370 else if( pi
->stat
.st_mode
& S_IFDIR
)
373 uh_file_response_200(cl
, req
, NULL
);
375 if( req
->version
> 1.0 )
376 uh_http_send(cl
, NULL
, "Transfer-Encoding: chunked\r\n", -1);
378 uh_http_send(cl
, NULL
, "Content-Type: text/html\r\n\r\n", -1);
381 uh_file_dirlist(cl
, req
, pi
);
387 uh_http_sendhf(cl
, 403, "Forbidden",
388 "Access to this resource is forbidden");