Fix content type header for error pages
[MonkeyD.git] / src / mimetype.c
blobd520130b6496f17c89a9584f08f5c378a248cba6
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 /* Carga en estructura los mimetypes */
39 void mk_mimetype_read_config()
41 char path[MAX_PATH];
42 struct mk_config *c;
44 snprintf(path, MAX_PATH, "%s/monkey.mime", config->serverconf);
45 c = mk_config_create(path);
47 while (c) {
48 if (mk_mimetype_add(c->key, c->val, NULL) != 0) {
49 puts("Error loading Mime Types");
51 c = c->next;
54 mk_config_free(c);
56 /* Set default mime type */
57 mimetype_default = mk_mem_malloc_z(sizeof(struct mimetype));
58 mimetype_default->name = MIMETYPE_DEFAULT_NAME;
59 mk_pointer_set(&mimetype_default->type, MIMETYPE_DEFAULT_TYPE);
60 mimetype_default->script_bin_path = NULL;
61 mimetype_default->next = NULL;
64 int mk_mimetype_add(char *name, char *type, char *bin_path)
66 int len;
67 struct mimetype *new_mime, *aux_mime;
69 new_mime = mk_mem_malloc_z(sizeof(struct mimetype));
71 new_mime->name = name;
73 /* Alloc space */
74 len = strlen(type) + 3;
75 new_mime->type.data = mk_mem_malloc(len);
76 new_mime->type.len = len - 1;
78 /* Copy mime type and add CRLF */
79 strcpy(new_mime->type.data, type);
80 strcat(new_mime->type.data, MK_CRLF);
81 new_mime->type.data[len-1] = '\0';
82 new_mime->next = NULL;
84 /* Free incoming type, 'name' is not freed as it's used in
85 * the main mimetype list
87 mk_mem_free(type);
89 if (first_mime == NULL) {
90 first_mime = new_mime;
92 else {
93 aux_mime = first_mime;
94 while (aux_mime->next != NULL) {
95 aux_mime = aux_mime->next;
97 aux_mime->next = new_mime;
99 return 0;
102 struct mimetype *mk_mimetype_find(mk_pointer * filename)
104 int j, len;
106 j = len = filename->len;
108 /* looking for extension */
109 while (filename->data[j] != '.' && j >= 0)
110 j--;
112 if (j == 0) {
113 return NULL;
116 return mk_mimetype_cmp(filename->data + j + 1);
119 /* Busca mime type segun Request */
120 struct mimetype *mk_mimetype_cmp(char *name)
122 struct mimetype *aux_mime;
124 aux_mime = first_mime;
125 while (aux_mime != NULL) {
126 if (strcasecmp(aux_mime->name, name) == 0) {
127 return aux_mime;
129 else
130 aux_mime = aux_mime->next;
133 return NULL;
136 int mk_mimetype_free(char **arr)
138 mk_mem_free(arr);
139 return 0;