Fix directory validation after handler
[MonkeyD.git] / src / mimetype.c
blob93f79674990ce0e0f3d297eda60997596e529b3d
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2008, Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "mimetype.h"
32 #include "memory.h"
33 #include "str.h"
34 #include "config.h"
35 #include "monkey.h"
37 /* Carga en estructura los mimetypes */
38 void mk_mimetype_read_config()
40 char buffer[255], path[MAX_PATH];
41 char *name=0,*type=0, *last=0;
42 FILE *mime_file;
44 snprintf(path,MAX_PATH,"%s/monkey.mime", config->serverconf);
46 if((mime_file=fopen(path,"r"))==NULL ) {
47 puts("Error: I can't open monkey.mime file");
48 exit(1);
51 /* Rutina que carga en memoria los mime types */
52 while( fgets(buffer,255,mime_file) ) {
53 int len;
54 len = strlen(buffer);
55 if(buffer[len-1] == '\n') {
56 buffer[--len] = 0;
57 if(len && buffer[len-1] == '\r')
58 buffer[--len] = 0;
61 name = strtok_r(buffer, "\"\t ", &last);
62 type = strtok_r(NULL, "\"\t ", &last);
64 if (!name || !type) continue;
65 if (buffer[0] == '#') continue;
67 if(mk_mimetype_add(name,type,NULL)!=0)
68 puts("Error loading Mime Types");
70 fclose(mime_file);
72 /* Set default mime type */
73 mimetype_default = mk_mem_malloc_z(sizeof(struct mimetype));
74 mimetype_default->name = MIMETYPE_DEFAULT_NAME;
75 mk_pointer_set(&mimetype_default->type, MIMETYPE_DEFAULT_TYPE);
76 mimetype_default->script_bin_path = NULL;
77 mimetype_default->next = NULL;
80 int mk_mimetype_add(char *name, char *type, char *bin_path)
83 struct mimetype *new_mime, *aux_mime;
85 new_mime = mk_mem_malloc_z(sizeof(struct mimetype));
87 new_mime->name = mk_string_dup(name);
88 mk_pointer_set(&new_mime->type, mk_string_dup(type));
89 new_mime->script_bin_path = mk_string_dup(bin_path);
91 new_mime->next=NULL;
93 if(first_mime==NULL)
94 first_mime=new_mime;
95 else {
96 aux_mime=first_mime;
97 while(aux_mime->next!=NULL)
98 aux_mime=aux_mime->next;
99 aux_mime->next = new_mime;
101 return 0;
104 struct mimetype *mk_mimetype_find(mk_pointer *filename)
106 int j, len;
108 j = len = filename->len;
110 /* looking for extension */
111 while(filename->data[j]!='.' && j>=0)
112 j--;
114 if(j==0){
115 return NULL;
118 return mk_mimetype_cmp(filename->data+j+1);
121 /* Busca mime type segun Request */
122 struct mimetype *mk_mimetype_cmp(char *name)
124 struct mimetype *aux_mime;
126 aux_mime=first_mime;
127 while(aux_mime!=NULL) {
128 if(strcasecmp(aux_mime->name,name)==0) {
129 return aux_mime;
131 else
132 aux_mime=aux_mime->next;
135 return NULL;
138 int mk_mimetype_free(char **arr)
140 mk_mem_free(arr);
141 return 0;