aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorame <[email protected]>2025-06-12 13:57:51 -0500
committerame <[email protected]>2025-06-12 13:57:51 -0500
commitfe09d3576ada9918f55431298c5d9a099e690761 (patch)
tree32fc93c956154a0cdc6a760f3acca1263d4feb2a /src/net
parent8e7257aac8b30aaa57577770fd636e784361e35d (diff)
rewrite mimetype parsing
Diffstat (limited to 'src/net')
-rw-r--r--src/net/lua.c2
-rw-r--r--src/net/util.c51
2 files changed, 51 insertions, 2 deletions
diff --git a/src/net/lua.c b/src/net/lua.c
index d60c018..ba9a204 100644
--- a/src/net/lua.c
+++ b/src/net/lua.c
@@ -229,7 +229,7 @@ int l_sendfile(lua_State* L){
}
char* ext = strrchr(path, '.');
- if(ext){
+ if(ext && mime_type != NULL){
char* content_type = map_get(mime_type, ext + 1);
if(content_type)
diff --git a/src/net/util.c b/src/net/util.c
index 92d3bce..e8e54d0 100644
--- a/src/net/util.c
+++ b/src/net/util.c
@@ -407,15 +407,64 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){
}
map_t* mime_type = NULL;
+char* _mimetypes = "/etc/mime.types";
+
void parse_mimetypes(){
mime_type = map_init();
+
+ FILE* fp = fopen(_mimetypes, "r");
+ if(fp == NULL) return (void)printf("unable to load mimetypes, set llby.net.mimetypes to a proper location, or nil to skip this\n");
+
+ char* line = NULL;
+ size_t len = 0;
+ ssize_t read;
+
+ for(;(read = getline(&line, &len, fp)) != -1;){
+ if(line[0] == '#' || line[0] == '\n') continue;
+ int used = 0;
+ char* mtype = calloc(1024, sizeof * mtype);
+ int mtype_len = 0;
+ int i = 0;
+ for(; line[i] != ' ' && line[i] != '\t' && i < read; i++){
+ mtype[mtype_len] = line[i];
+ mtype_len++;
+ }
+
+ char* type = calloc(512, sizeof * type);
+ int type_len = 0;
+ for(; i < read; i++){
+ if(line[i] == ' ' || line[i] == '\t' || line[i] == '\n'){
+ if(type_len == 0) continue;
+ char* mtype_c = calloc(1024, sizeof * mtype);
+ strcpy(mtype_c, mtype);
+ map_set(&mime_type, type, mtype_c);
+ used = 1;
+ type_len = 0;
+ type = calloc(512, sizeof * type);
+ } else {
+ type[type_len] = line[i];
+ type_len++;
+ }
+ }
+ free(mtype);
+ free(type);
+ }
+
+ fclose(fp);
+}
+
+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;
+ if(buffer[i] == '#') {
+ for(; buffer[i] != '\n' && buffer[i] != '\0'; i++);
+ continue;
+ }
//printf("s: '%s'\n",buffer + i);
char* type = calloc(512, sizeof * type);