diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua.c | 3 | ||||
| -rw-r--r-- | src/net.c | 1 | ||||
| -rw-r--r-- | src/net/common.h | 3 | ||||
| -rw-r--r-- | src/net/lua.c | 8 | ||||
| -rw-r--r-- | src/net/util.c | 62 | ||||
| -rw-r--r-- | src/net/util.h | 3 | ||||
| -rw-r--r-- | src/thread.c | 15 | ||||
| -rw-r--r-- | src/types/str.c | 2 |
8 files changed, 92 insertions, 5 deletions
@@ -139,6 +139,9 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ modi = 1;
lua_pushlightuserdata(dest, lua_touserdata(src, -1));
break;
+ case LUA_TTHREAD:
+ lua_pushnil(dest);
+ break;
default:
printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type);
abort();
@@ -265,6 +265,7 @@ net_end: }
int start_serv(lua_State* L, int port){
+ parse_mimetypes();
//need these on windows for sockets (stupid)
#ifdef _WIN32
WSADATA Data;
diff --git a/src/net/common.h b/src/net/common.h index 4120734..6b1a25a 100644 --- a/src/net/common.h +++ b/src/net/common.h @@ -19,6 +19,7 @@ #include "../table.h" #include "../types/str.h" #include "../types/parray.h" +#include "../types/map.h" #define max_con 200 //2^42 @@ -62,4 +63,6 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t con_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; +extern map_t* mime_type; + #endif diff --git a/src/net/lua.c b/src/net/lua.c index dd73db2..df2213b 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -216,6 +216,14 @@ int l_sendfile(lua_State* L){ p_fatal("missing permissions"); } + char* ext = strrchr(path, '.'); + if(ext){ + char* content_type = map_get(mime_type, ext + 1); + + if(content_type) + {luaI_tsets(L, header, "Content-Type", content_type);} + } + str* r; i_write_header(L, header, &r, "", 0); send(client_fd, r->c, r->len, 0); diff --git a/src/net/util.c b/src/net/util.c index 2dd9a7f..44a525b 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -356,3 +356,65 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){ *_params = params; return out; } + +map_t* mime_type = NULL; +void parse_mimetypes(){ + mime_type = map_init(); + FILE* fp = fopen(MIMETYPES, "r"); + char* buffer = calloc(1024, sizeof * buffer); + + for(;fgets(buffer, 1024, fp); memset(buffer, 0, 1024)){ + int i; + for(i = 0; buffer[i] == ' '; i++); + if(buffer[i] == '#') continue; + + //printf("s: '%s'\n",buffer + i); + char* type = calloc(512, sizeof * type); + int type_len = 0; + for(; buffer[i + type_len] != ' ' && buffer[i + type_len] != '\t'; type_len++){ + //printf("%c", buffer[i + type_len]); + type[type_len] = buffer[i + type_len]; + if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n') break; + } + type[type_len] = '\0'; + + //check if the type has an associated file type + if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n'){ + free(type); + continue; + } + type = realloc(type, (type_len + 1) * sizeof * type); + + + int file_type_len = 0; + char* file_type = calloc(512, sizeof * file_type); + i += type_len; + + for(;buffer[i] == ' ' || buffer[i] == '\t'; i++); + + int used = 0; + for(;;){ + if(buffer[i + file_type_len] == ' ' || buffer[i + file_type_len] == '\n' || buffer[i + file_type_len] == '\0'){ + used = 1; + file_type[file_type_len] = '\0'; + file_type = realloc(file_type, (file_type_len + 1) * sizeof * file_type); + //printf("set %s to %s\n",file_type, type); + map_set(&mime_type, file_type, type); + file_type = calloc(512, sizeof * file_type); + if(buffer[i + file_type_len] != ' ') break; + i += file_type_len + 1; + file_type_len = 0; + //printf("finish\n"); + } else { + file_type[file_type_len] = buffer[i + file_type_len]; + file_type_len++; + } + } + free(file_type); + + //printf("e: '%s'\n", type); + if(!used)free(type); + } + //printf("done\n"); + +} diff --git a/src/net/util.h b/src/net/util.h index fbe73f8..e592420 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -1,5 +1,7 @@ #include "common.h" #include "../types/larray.h" + +#define MIMETYPES "/etc/mime.types" /** * @brief calls recv into buffer until everything is read * @@ -50,3 +52,4 @@ parray_t* route_match(parray_t* paths, char* path, larray_t** params); int match_param(char* path, char* match, parray_t* arr); +void parse_mimetypes(); diff --git a/src/thread.c b/src/thread.c index 033da33..db8e54e 100644 --- a/src/thread.c +++ b/src/thread.c @@ -269,6 +269,7 @@ int _buffer_mod(lua_State* L){ int l_buffer_index(lua_State* L){
uint64_t len, hash;
+ struct thread_buffer *buffer = lua_touserdata(L, 1);
const char* str = luaL_tolstring(L, 2, &len);
hash = fnv_1((uint8_t*)str, len, v_1);
@@ -284,7 +285,15 @@ int l_buffer_index(lua_State* L){ lua_pushcfunction(L, _buffer_mod);
break;
default:
- lua_pushnil(L);
+ lua_pushstring(buffer->L, str);
+ lua_gettable(buffer->L, 1);
+ if(lua_isnil(buffer->L, -1)){
+ lua_pushnil(L);
+ return 1;
+ }
+
+ luaI_deepcopy(buffer->L, L, SKIP_GC);
+ lua_pop(buffer->L, 1);
break;
}
return 1;
@@ -349,7 +358,6 @@ return __proxy_call(__this_obj,'%s',table.unpack({_a,_b,_c}));end", key); }
int l_buffer_gc(lua_State* L){
- printf("gc\n");
struct thread_buffer *buffer = lua_touserdata(L, 1);
pthread_mutex_lock(&*buffer->lock);
pthread_mutex_destroy(&*buffer->lock);
@@ -387,9 +395,8 @@ int l_buffer(lua_State* L){ void _lua_getfenv(lua_State* L){
}
-int l_testcopy(lua_State* L){
-
+int l_testcopy(lua_State* L){
lua_State* temp = luaL_newstate();
luaI_deepcopy(L, temp, SKIP_GC);
lua_close(temp);
diff --git a/src/types/str.c b/src/types/str.c index 0378285..4b257cb 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -12,7 +12,7 @@ str* str_initl(const char* init, size_t len){ if(s->c == NULL) p_fatal("failed to allocate string\n");
s->len = len ;
- memcpy(s->c, init, len + 1);
+ memcpy(s->c, init, (len + 1) * sizeof * init);
return s;
}
|
