1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
39 void mk_mimetype_read_config()
42 struct mk_config
*cnf
;
43 struct mk_config_section
*section
;
44 struct mk_config_entry
*entry
;
46 /* Read mime types configuration file */
47 snprintf(path
, MAX_PATH
, "%s/monkey.mime", config
->serverconf
);
48 cnf
= mk_config_create(path
);
50 /* Get MimeTypes tag */
51 section
= mk_config_section_get(cnf
, "MIMETYPES");
53 puts("Error: Invalid mime type file");
57 entry
= section
->entry
;
60 if (mk_mimetype_add(entry
->key
, entry
->val
, NULL
) != 0) {
61 puts("Error loading Mime Types");
68 /* Set default mime type */
69 mimetype_default
= mk_mem_malloc_z(sizeof(struct mimetype
));
70 mimetype_default
->name
= MIMETYPE_DEFAULT_NAME
;
71 mk_pointer_set(&mimetype_default
->type
, MIMETYPE_DEFAULT_TYPE
);
72 mimetype_default
->script_bin_path
= NULL
;
73 mimetype_default
->next
= NULL
;
76 int mk_mimetype_add(char *name
, char *type
, char *bin_path
)
79 struct mimetype
*new_mime
, *aux_mime
;
81 new_mime
= mk_mem_malloc_z(sizeof(struct mimetype
));
83 new_mime
->name
= name
;
86 len
= strlen(type
) + 3;
87 new_mime
->type
.data
= mk_mem_malloc(len
);
88 new_mime
->type
.len
= len
- 1;
90 /* Copy mime type and add CRLF */
91 strcpy(new_mime
->type
.data
, type
);
92 strcat(new_mime
->type
.data
, MK_CRLF
);
93 new_mime
->type
.data
[len
-1] = '\0';
94 new_mime
->next
= NULL
;
96 /* Free incoming type, 'name' is not freed as it's used in
97 * the main mimetype list
101 if (first_mime
== NULL
) {
102 first_mime
= new_mime
;
105 aux_mime
= first_mime
;
106 while (aux_mime
->next
!= NULL
) {
107 aux_mime
= aux_mime
->next
;
109 aux_mime
->next
= new_mime
;
114 struct mimetype
*mk_mimetype_find(mk_pointer
* filename
)
118 j
= len
= filename
->len
;
120 /* looking for extension */
121 while (filename
->data
[j
] != '.' && j
>= 0)
128 return mk_mimetype_cmp(filename
->data
+ j
+ 1);
131 /* Match mime type for requested resource */
132 struct mimetype
*mk_mimetype_cmp(char *name
)
134 struct mimetype
*aux_mime
;
136 aux_mime
= first_mime
;
137 while (aux_mime
!= NULL
) {
138 if (strcasecmp(aux_mime
->name
, name
) == 0) {
142 aux_mime
= aux_mime
->next
;
148 int mk_mimetype_free(char **arr
)