diff options
Diffstat (limited to 'src/net/util.c')
| -rw-r--r-- | src/net/util.c | 51 |
1 files changed, 50 insertions, 1 deletions
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); |
