|
Server 0.0.1
HTTP/REST server implementation
|
00001 00023 #include <stdio.h> 00024 #include <time.h> 00025 #include <sys/stat.h> 00026 #include <fcntl.h> 00027 #include <string.h> 00028 #include <sys/types.h> 00029 00030 #include "class.h" 00031 #include "stream.h" 00032 00033 #include "http/response.h" 00034 #include "http/message.h" 00035 #include "http/header.h" 00036 00037 #include "utils/memory.h" 00038 #include "hash.h" 00039 00040 HttpResponse 00041 httpResponseAsset( 00042 const char * fname, 00043 const char * mime, 00044 size_t nmime, 00045 const char * match, 00046 size_t nmatch) 00047 { 00048 struct tm * tmp; 00049 char etag[200]; 00050 size_t netag; 00051 char mtime[200]; 00052 size_t nmtime; 00053 struct stat st; 00054 HttpResponse response; 00055 HttpMessage message; 00056 int handle; 00057 00058 handle = open(fname, O_RDONLY); 00059 fstat(handle, &st); 00060 00061 tmp = localtime(&(st.st_mtime)); 00062 netag = strftime(etag, sizeof(etag), "%s", tmp); 00063 nmtime = strftime(mtime, sizeof(mtime), "%a, %d %b %Y %T %Z", tmp); 00064 00065 if (netag == nmatch && 0 == memcmp(etag, match, netag)) { 00066 return httpResponse304(mime, nmime, etag, netag, mtime, nmtime); 00067 } 00068 00069 response = new(HttpResponse, "HTTP/1.1", 200, "OK"); 00070 message = (HttpMessage)response; 00071 00072 message->type = HTTP_MESSAGE_PIPED; 00073 message->handle = new(Stream, STREAM_FD, handle); 00074 message->nbody = st.st_size; 00075 00076 hashAdd(message->header, 00077 new(HttpHeader, CSTRA("Content-Type"), mime, nmime)); 00078 hashAdd(message->header, 00079 new(HttpHeader, CSTRA("ETag"), etag, netag)); 00080 hashAdd(message->header, 00081 new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime)); 00082 00083 return response; 00084 } 00085 00086 // vim: set ts=4 sw=4: