diff --git a/assets/html/foo.html b/assets/html/foo.html
new file mode 100644
index 0000000..b48b623
--- /dev/null
+++ b/assets/html/foo.html
@@ -0,0 +1,13 @@
+
+
+
+
+ This is just foo
+
+
+ A BIG FAT FOO
+
+
+
+
diff --git a/assets/html/robots.txt b/assets/html/robots.txt
new file mode 100644
index 0000000..e69de29
diff --git a/config/mime.types b/config/mime.types
new file mode 100644
index 0000000..69c067c
--- /dev/null
+++ b/config/mime.types
@@ -0,0 +1,184 @@
+evy application/envoy
+fif application/fractals
+spl application/futuresplash
+hta application/hta
+acx application/internet-property-stream
+hqx application/mac-binhex40
+doc application/msword
+dot application/msword
+* application/octet-stream
+oda application/oda
+axs application/olescript
+pdf application/pdf
+prf application/pics-rules
+p10 application/pkcs10
+crl application/pkix-crl
+ai application/postscript
+eps application/postscript
+ps application/postscript
+rtf application/rtf
+setpay application/set-payment-initiation
+setreg application/set-registration-initiation
+xla application/vnd.ms-excel
+xlc application/vnd.ms-excel
+xlm application/vnd.ms-excel
+xls application/vnd.ms-excel
+xlt application/vnd.ms-excel
+xlw application/vnd.ms-excel
+msg application/vnd.ms-outlook
+sst application/vnd.ms-pkicertstore
+cat application/vnd.ms-pkiseccat
+stl application/vnd.ms-pkistl
+pot application/vnd.ms-powerpoint
+pps application/vnd.ms-powerpoint
+ppt application/vnd.ms-powerpoint
+mpp application/vnd.ms-project
+wcm application/vnd.ms-works
+wdb application/vnd.ms-works
+wks application/vnd.ms-works
+wps application/vnd.ms-works
+hlp application/winhlp
+bcpio application/x-bcpio
+cdf application/x-cdf
+z application/x-compress
+tgz application/x-compressed
+cpio application/x-cpio
+csh application/x-csh
+dcr application/x-director
+dir application/x-director
+dxr application/x-director
+dvi application/x-dvi
+gtar application/x-gtar
+gz application/x-gzip
+hdf application/x-hdf
+ins application/x-internet-signup
+isp application/x-internet-signup
+iii application/x-iphone
+js application/x-javascript
+latex application/x-latex
+mdb application/x-msaccess
+crd application/x-mscardfile
+clp application/x-msclip
+dll application/x-msdownload
+m13 application/x-msmediaview
+m14 application/x-msmediaview
+mvb application/x-msmediaview
+wmf application/x-msmetafile
+mny application/x-msmoney
+pub application/x-mspublisher
+scd application/x-msschedule
+trm application/x-msterminal
+wri application/x-mswrite
+nc application/x-netcdf
+pma application/x-perfmon
+pmc application/x-perfmon
+pml application/x-perfmon
+pmr application/x-perfmon
+pmw application/x-perfmon
+p12 application/x-pkcs12
+pfx application/x-pkcs12
+p7b application/x-pkcs7-certificates
+spc application/x-pkcs7-certificates
+p7r application/x-pkcs7-certreqresp
+p7c application/x-pkcs7-mime
+p7m application/x-pkcs7-mime
+p7s application/x-pkcs7-signature
+sh application/x-sh
+shar application/x-shar
+swf application/x-shockwave-flash
+sit application/x-stuffit
+sv4cpio application/x-sv4cpio
+sv4crc application/x-sv4crc
+tar application/x-tar
+tcl application/x-tcl
+tex application/x-tex
+texi application/x-texinfo
+texinfo application/x-texinfo
+roff application/x-troff
+t application/x-troff
+tr application/x-troff
+man application/x-troff-man
+me application/x-troff-me
+ms application/x-troff-ms
+ustar application/x-ustar
+src application/x-wais-source
+cer application/x-x509-ca-cert
+crt application/x-x509-ca-cert
+der application/x-x509-ca-cert
+pko application/ynd.ms-pkipko
+zip application/zip
+au audio/basic
+snd audio/basic
+mid audio/mid
+rmi audio/mid
+mp3 audio/mpeg
+aif audio/x-aiff
+aifc audio/x-aiff
+aiff audio/x-aiff
+m3u audio/x-mpegurl
+ra audio/x-pn-realaudio
+ram audio/x-pn-realaudio
+wav audio/x-wav
+bmp image/bmp
+cod image/cis-cod
+gif image/gif
+ief image/ief
+jpe image/jpeg
+jpeg image/jpeg
+jpg image/jpeg
+jfif image/pipeg
+svg image/svg+xml
+tif image/tiff
+tiff image/tiff
+ras image/x-cmu-raster
+cmx image/x-cmx
+ico image/x-icon
+pnm image/x-portable-anymap
+pbm image/x-portable-bitmap
+pgm image/x-portable-graymap
+ppm image/x-portable-pixmap
+rgb image/x-rgb
+xbm image/x-xbitmap
+xpm image/x-xpixmap
+xwd image/x-xwindowdump
+mht message/rfc822
+mhtml message/rfc822
+nws message/rfc822
+css text/css
+323 text/h323
+htm text/html
+html text/html
+stm text/html
+uls text/iuls
+bas text/plain
+c text/plain
+h text/plain
+txt text/plain
+rtx text/richtext
+sct text/scriptlet
+tsv text/tab-separated-values
+htt text/webviewhtml
+htc text/x-component
+etx text/x-setext
+vcf text/x-vcard
+mp2 video/mpeg
+mpa video/mpeg
+mpe video/mpeg
+mpeg video/mpeg
+mpg video/mpeg
+mpv2 video/mpeg
+mov video/quicktime
+qt video/quicktime
+lsf video/x-la-asf
+lsx video/x-la-asf
+asf video/x-ms-asf
+asr video/x-ms-asf
+asx video/x-ms-asf
+avi video/x-msvideo
+movie video/x-sgi-movie
+flr x-world/x-vrml
+vrml x-world/x-vrml
+wrl x-world/x-vrml
+wrz x-world/x-vrml
+xaf x-world/x-vrml
+xof x-world/x-vrml
diff --git a/include/http/worker.h b/include/http/worker.h
index cfc7255..3af25b4 100644
--- a/include/http/worker.h
+++ b/include/http/worker.h
@@ -28,6 +28,7 @@
#include
#include "class.h"
+#include "hash.h"
#include "http/parser.h"
#include "http/writer.h"
#include "cbuf.h"
@@ -48,6 +49,8 @@ CLASS(HttpWorker) {
Cbuf pbuf;
Cbuf wbuf;
+ Hash mime_types;
+
HttpParser parser;
HttpWriter writer;
Session session;
diff --git a/src/http/Makefile.am b/src/http/Makefile.am
index f583d84..8c88b2f 100644
--- a/src/http/Makefile.am
+++ b/src/http/Makefile.am
@@ -29,7 +29,8 @@ WORKER = worker.c \
worker/process.c \
worker/answer.c \
worker/get_asset.c \
- worker/add_common_header.c
+ worker/add_common_header.c \
+ worker/get_mime_type.c
HEADER = header.c \
header/to_string.c
diff --git a/src/http/worker.c b/src/http/worker.c
index 6d6293c..f7b62c3 100644
--- a/src/http/worker.c
+++ b/src/http/worker.c
@@ -24,12 +24,15 @@
#include
#include
+#include
+#include
#include
#include
#include
#include "class.h"
#include "stream.h"
+#include "hash.h"
#include "http/worker.h"
#include "http/parser.h"
#include "http/writer.h"
@@ -49,6 +52,42 @@ httpWorkerCtor(void * _this, va_list * params)
this->val = va_arg(*params, struct randval *);
+ /* read all mimetypes in a hash */
+ this->mime_types = new(Hash);
+ if (0 == access("./config/mime.types", O_RDONLY)) {
+ FILE * handle = fopen("./config/mime.types", "r");
+
+ if (NULL != handle) {
+ char buffer[512];
+
+ while (NULL != fgets(buffer, 512, handle)) {
+ char * tmp;
+ char * key = buffer;
+ char * value;
+ size_t nkey;
+ size_t nvalue;
+
+ buffer[511] = '\0';
+ tmp = memchr(key, ' ', 512);
+
+ if (NULL != tmp) {
+ *tmp = '\0';
+ }
+ nkey = tmp - buffer;
+
+ value = tmp + 1;
+ for (; *value == ' ' && value < buffer+512; value++);
+
+ nvalue = strlen(value);
+
+ hashAdd(this->mime_types,
+ new(HashValue, key, nkey, value, nvalue));
+ }
+
+ fclose(handle);
+ }
+ }
+
sprintf(cbuf_id, "%s_%s", "parser", id);
this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF);
@@ -84,6 +123,7 @@ httpWorkerDtor(void * _this)
delete(this->writer);
if (NULL != this->pbuf) {
+ delete(this->mime_types);
delete(this->pbuf); //!< cloned workers have NULL, so delete won't do anything
delete(this->wbuf); //!< cloned workers have NULL, so delete won't do anything
tdestroy(*(this->sroot), tDelete);
@@ -99,6 +139,8 @@ httpWorkerClone(void * _this, void * _base)
this->val = base->val;
+ this->mime_types = base->mime_types;
+
this->parser = new(HttpParser, base->pbuf);
this->writer = new(HttpWriter, base->wbuf);
diff --git a/src/http/worker/get_mime_type.c b/src/http/worker/get_mime_type.c
new file mode 100644
index 0000000..cc407b6
--- /dev/null
+++ b/src/http/worker/get_mime_type.c
@@ -0,0 +1,42 @@
+/**
+ * \file
+ *
+ * \author Georg Hopp
+ *
+ * \copyright
+ * Copyright © 2012 Georg Hopp
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+
+#include "http/worker.h"
+
+#include "utils/memory.h"
+#include "hash.h"
+
+char *
+httpWorkerGetMimeType(
+ HttpWorker this,
+ const char * extension)
+{
+ HashValue mime_type;
+
+ mime_type = hashGet(this->mime_types, extension, strlen(extension));
+
+ return (char *)mime_type->value;
+}
+
+// vim: set ts=4 sw=4:
diff --git a/src/http/worker/process.c b/src/http/worker/process.c
index 551a669..078ac93 100644
--- a/src/http/worker/process.c
+++ b/src/http/worker/process.c
@@ -47,6 +47,7 @@
HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t);
void httpWorkerAddCommonHeader(HttpMessage, HttpMessage);
+char * httpWorkerGetMimeType(HttpWorker, const char * extension);
ssize_t
@@ -68,11 +69,11 @@ httpWorkerProcess(HttpWorker this, Stream st)
if (NULL == this->session) {
HashValue sidstr = hashGet(request->cookies, CSTRA("sid"));
- if (NULL != sidstr) {
- unsigned long sid;
+ if (NULL != sidstr) {
+ unsigned long sid;
sid = strtoul((char*)(sidstr->value), NULL, 10);
- this->session = sessionGet(this->sroot, sid);
+ this->session = sessionGet(this->sroot, sid);
}
}
@@ -225,12 +226,25 @@ httpWorkerProcess(HttpWorker this, Stream st)
else {
char asset[2048] = "./assets/html";
+ char * extension = strrchr(request->path, '.');
+ char * mime_type = NULL;
+ char default_mime[] = "application/octet-stream";
+
+ if (NULL != extension) {
+ extension++;
+ mime_type = httpWorkerGetMimeType(this, extension);
+ }
+
+ if (NULL == mime_type) {
+ mime_type = default_mime;
+ }
strcat(asset, request->path);
response = httpWorkerGetAsset(
request,
asset,
- CSTRA("text/html"));
+ mime_type,
+ strlen(mime_type));
}
}