diff options
author | dwmalone <dwmalone@FreeBSD.org> | 2004-08-16 09:38:34 +0000 |
---|---|---|
committer | dwmalone <dwmalone@FreeBSD.org> | 2004-08-16 09:38:34 +0000 |
commit | 78988cd40a872687a5487a25e0aadefbd9eb2b1b (patch) | |
tree | 65e3c39eb1079a74ea0cfc2ab5b03481bbd4d307 /release/picobsd/tinyware | |
parent | 597fea43c03116bda26e0dff296436350980b2df (diff) | |
download | FreeBSD-src-78988cd40a872687a5487a25e0aadefbd9eb2b1b.zip FreeBSD-src-78988cd40a872687a5487a25e0aadefbd9eb2b1b.tar.gz |
Improve MIME handling. This patch is based on Eugene's patch, but
with the following changes:
1) Don't make a mime_types.h 'cos we should avoid creating variables
in header files,
2) Use strrchr to find the extension, rather than strchr,
3) Slightly simplify the mime-type matching loop.
any goof are likely to be mine. Note that there are links to more
improvements by Eugene in the PR.
PR: 29725
Submitted by: Eugene Grosbein <eugen@kuzbass.ru>
Diffstat (limited to 'release/picobsd/tinyware')
-rw-r--r-- | release/picobsd/tinyware/simple_httpd/simple_httpd.c | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/release/picobsd/tinyware/simple_httpd/simple_httpd.c b/release/picobsd/tinyware/simple_httpd/simple_httpd.c index 6be3809..ab35b55 100644 --- a/release/picobsd/tinyware/simple_httpd/simple_httpd.c +++ b/release/picobsd/tinyware/simple_httpd/simple_httpd.c @@ -71,6 +71,19 @@ static char httpd_server_ident[] = "Server: FreeBSD/PicoBSD simple_httpd 1.1\r"; static char http_200[] = "HTTP/1.0 200 OK\r"; +const char *default_mime_type = "application/octet-stream"; + +const char *mime_type[][2] = { + { "txt", "text/plain" }, + { "htm", "text/html" }, + { "html", "text/html" }, + { "gif", "image/gif" }, + { "jpg", "image/jpeg" }, + { "mp3", "audio/mpeg" } +}; + +const int mime_type_max = sizeof(mime_type) / sizeof(mime_type[0]) - 1; + /* Two parts, HTTP Header and then HTML */ static const char *http_404[2] = {"HTTP/1.0 404 Not found\r\n", @@ -205,7 +218,7 @@ http_request(void) int fd, lg, i; int cmd = 0; char *p, *par; - const char *filename, *c; + const char *filename, *c, *ext, *type; struct stat file_status; char req[1024]; char buff[8192]; @@ -321,21 +334,17 @@ http_request(void) sprintf(buff, "Content-length: %lld\r\n", file_status.st_size); write(con_sock, buff, strlen(buff)); - if (strstr(filename,".txt")) { - strcpy(buff,"Content-type: text/plain\r\n"); - } else if (strstr(filename,".html") || strstr(filename,".htm")) { - strcpy(buff,"Content-type: text/html\r\n"); - } else if (strstr(filename,".gif")) { - strcpy(buff,"Content-type: image/gif\r\n"); - } else if (strstr(filename,".jpg")) { - strcpy(buff,"Content-type: image/jpeg\r\n"); - } else { - /* Take a guess at content if we don't have something already */ - strcpy(buff,"Content-type: "); - strcat(buff,strstr(filename,".")+1); - strcat(buff,"\r\n"); + strcpy(buff, "Content-type: "); + type = default_mime_type; + if ((ext = strrchr(filename, '.')) != NULL) { + for (i = mime_type_max; i >= 0; i--) + if (strcmp(ext + 1, mime_type[i][0]) == 0) { + type = mime_type[i][1]; + break; + } } - write(con_sock, buff, strlen(buff)); + strcat(buff, type); + http_output(buff); strftime(buff, 50, "Last-Modified: %a, %d %h %Y %H:%M:%S %Z\r\n\r\n", gmtime(&file_status.st_mtime)); write(con_sock, buff, strlen(buff)); |