diff --git a/src/http/message.c b/src/http/message.c index 2ee7db4..51499ff 100644 --- a/src/http/message.c +++ b/src/http/message.c @@ -67,7 +67,7 @@ httpMessageDtor(void * _this) break; case HTTP_MESSAGE_PIPED: - if (2 < (this->handle->handle).fd) { + if (NULL != this->handle && 2 < (this->handle->handle).fd) { close((this->handle->handle).fd); } delete(this->handle); diff --git a/src/http/response/asset.c b/src/http/response/asset.c index b0c58f2..e09bd26 100644 --- a/src/http/response/asset.c +++ b/src/http/response/asset.c @@ -21,6 +21,7 @@ */ #include +#include #include #include #include @@ -55,8 +56,12 @@ httpResponseAsset( HttpMessage message; int handle; - handle = open(fname, O_RDONLY); - fstat(handle, &st); + if (-1 == access(fname, O_RDONLY)) { + handle = -1; + } else { + handle = open(fname, O_RDONLY); + fstat(handle, &st); + } tmp = localtime(&(st.st_mtime)); netag = strftime(etag, sizeof(etag), "%s", tmp); @@ -70,8 +75,10 @@ httpResponseAsset( message = (HttpMessage)response; message->type = HTTP_MESSAGE_PIPED; - message->handle = new(Stream, STREAM_FD, handle); - message->nbody = st.st_size; + if (-1 != handle) { + message->handle = new(Stream, STREAM_FD, handle); + message->nbody = st.st_size; + } hashAdd(message->header, new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); diff --git a/src/http/worker/get_asset.c b/src/http/worker/get_asset.c index 36f69ce..3ec9aef 100644 --- a/src/http/worker/get_asset.c +++ b/src/http/worker/get_asset.c @@ -37,9 +37,10 @@ httpWorkerGetAsset( const char * mime, size_t nmime) { - char * match; - size_t nmatch; - HttpHeader header; + char * match; + size_t nmatch; + HttpHeader header; + HttpMessage message; header = hashGet( ((HttpMessage)request)->header, @@ -54,8 +55,14 @@ httpWorkerGetAsset( nmatch = (header->nvalue)[0]; } - return (HttpMessage)httpResponseAsset( + message = (HttpMessage)httpResponseAsset( fname, mime, nmime, match, nmatch); + + if (message->type == HTTP_MESSAGE_PIPED && message->handle == NULL) { + delete(message); + } + + return message; } // vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index f495f2d..551a669 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -158,11 +158,11 @@ httpWorkerProcess(HttpWorker this, Stream st) CSTRA("text/html")); } - if (0 == strcmp("/sessinfo/", request->path)) { + else if (0 == strcmp("/sessinfo/", request->path)) { response = (HttpMessage)httpResponseSession(this->session); } - if (0 == strcmp("/sess/", request->path)) { + else if (0 == strcmp("/sess/", request->path)) { if (NULL == this->session) { this->session = sessionAdd( this->sroot, @@ -171,7 +171,7 @@ httpWorkerProcess(HttpWorker this, Stream st) response = (HttpMessage)httpResponseSession(this->session); } - if (0 == strcmp("/randval/", request->path)) { + else if (0 == strcmp("/randval/", request->path)) { if (NULL != this->session) { response = (HttpMessage)httpResponseRandval( this->val->timestamp, @@ -181,47 +181,58 @@ httpWorkerProcess(HttpWorker this, Stream st) } } - if (0 == strcmp("/image/me", request->path)) { + else if (0 == strcmp("/image/me", request->path)) { response = httpWorkerGetAsset( request, "./assets/image/waldschrat.jpg", CSTRA("image/jpeg")); } - if (0 == strcmp("/assets/js/jquery", request->path)) { + else if (0 == strcmp("/assets/js/jquery", request->path)) { response = httpWorkerGetAsset( request, "./assets/js/jquery-1.7.1.min.js", CSTRA("text/javascript")); } - if (0 == strcmp("/assets/js/serverval", request->path)) { + else if (0 == strcmp("/assets/js/serverval", request->path)) { response = httpWorkerGetAsset( request, "./assets/js/serverval.js", CSTRA("text/javascript")); } - if (0 == strcmp("/assets/js/session", request->path)) { + else if (0 == strcmp("/assets/js/session", request->path)) { response = httpWorkerGetAsset( request, "./assets/js/session.js", CSTRA("text/javascript")); } - if (0 == strcmp("/assets/js/init", request->path)) { + else if (0 == strcmp("/assets/js/init", request->path)) { response = httpWorkerGetAsset( request, "./assets/js/init.js", CSTRA("text/javascript")); } - if (0 == strcmp("/assets/style/common", request->path)) { + else if (0 == strcmp("/assets/style/common", request->path)) { response = httpWorkerGetAsset( request, "./assets/style/common.css", CSTRA("text/css")); } + + else { + char asset[2048] = "./assets/html"; + + strcat(asset, request->path); + response = httpWorkerGetAsset( + request, + asset, + CSTRA("text/html")); + } + } if (NULL == response) {