diff options
| author | amelia squires <[email protected]> | 2024-09-29 02:49:05 -0500 |
|---|---|---|
| committer | amelia squires <[email protected]> | 2024-09-29 02:49:05 -0500 |
| commit | fe33c0e979ebfc41c3eb9f561589f0d5d8810aaf (patch) | |
| tree | 526d2f71581e2584c9de2b958fd89b6ce0fb0ac1 /src/net | |
| parent | 602818b895fec710b0534b0b8fa7f5e1f57203c2 (diff) | |
docs n stuff
Diffstat (limited to 'src/net')
| -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 |
4 files changed, 76 insertions, 0 deletions
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(); |
