Little Palm fixes
[MonkeyD.git] / src / mimetype.c
blob5bd42359d76de079f4e3960e199aa815dbd7e8a2
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
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.
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 "request.h"
36 #include "monkey.h"
38 /* Load mimetypes */
39 void mk_mimetype_read_config()
41 char path[MAX_PATH];
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");
52 if (!section) {
53 puts("Error: Invalid mime type file");
54 exit(1);
57 entry = section->entry;
59 while (entry) {
60 if (mk_mimetype_add(entry->key, entry->val, NULL) != 0) {
61 puts("Error loading Mime Types");
63 entry = entry->next;
66 //mk_config_free(c);
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)
78 int len;
79 struct mimetype *new_mime, *aux_mime;
81 new_mime = mk_mem_malloc_z(sizeof(struct mimetype));
83 new_mime->name = name;
85 /* Alloc space */
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
99 mk_mem_free(type);
101 if (first_mime == NULL) {
102 first_mime = new_mime;
104 else {
105 aux_mime = first_mime;
106 while (aux_mime->next != NULL) {
107 aux_mime = aux_mime->next;
109 aux_mime->next = new_mime;
111 return 0;
114 struct mimetype *mk_mimetype_find(mk_pointer * filename)
116 int j, len;
118 j = len = filename->len;
120 /* looking for extension */
121 while (filename->data[j] != '.' && j >= 0)
122 j--;
124 if (j == 0) {
125 return NULL;
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) {
139 return aux_mime;
141 else
142 aux_mime = aux_mime->next;
145 return NULL;
148 int mk_mimetype_free(char **arr)
150 mk_mem_free(arr);
151 return 0;