-
Notifications
You must be signed in to change notification settings - Fork 104
/
mime.c
208 lines (159 loc) · 4.55 KB
/
mime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2014 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include <ctype.h>
#include "mime.h"
#include "mime-protected.h"
#include "util.h"
#define ENTRIES "mime"
ret_t
cherokee_mime_new (cherokee_mime_t **mime)
{
CHEROKEE_NEW_STRUCT(n, mime);
cherokee_avl_init (&n->ext_table);
INIT_LIST_HEAD(&n->entry_list);
/* Return the object
*/
*mime = n;
return ret_ok;
}
ret_t
cherokee_mime_free (cherokee_mime_t *mime)
{
cherokee_list_t *i, *tmp;
if (mime == NULL)
return ret_ok;
cherokee_avl_mrproper (AVL_GENERIC(&mime->ext_table), NULL);
list_for_each_safe (i, tmp, &mime->entry_list) {
cherokee_list_del (i);
cherokee_mime_entry_free (MIME_ENTRY(i));
}
free (mime);
return ret_ok;
}
static ret_t
get_by_mime (cherokee_mime_t *mime, cherokee_buffer_t *type, cherokee_mime_entry_t **entry)
{
ret_t ret;
cherokee_list_t *i;
list_for_each (i, &mime->entry_list) {
cherokee_buffer_t *itype;
ret = cherokee_mime_entry_get_type (MIME_ENTRY(i), &itype);
if (ret != ret_ok) continue;
if (cherokee_buffer_cmp_buf (type, itype) == 0) {
*entry = MIME_ENTRY(i);
return ret_ok;
}
}
return ret_not_found;
}
static ret_t
get_by_mime_or_new (cherokee_mime_t *mime, cherokee_buffer_t *type, cherokee_mime_entry_t **entry)
{
ret_t ret;
/* Hit
*/
ret = get_by_mime (mime, type, entry);
if (ret == ret_ok)
return ret_ok;
/* Miss
*/
ret = cherokee_mime_entry_new (entry);
if (ret != ret_ok) {
return ret;
}
ret = cherokee_mime_entry_set_type (*entry, type);
if (ret != ret_ok) {
cherokee_mime_entry_free (*entry);
return ret;
}
cherokee_list_add (LIST(*entry), &mime->entry_list);
TRACE(ENTRIES, "Type set '%s'\n", type->buf);
return ret_ok;
}
static ret_t
add_extension (char *val, void *data)
{
ret_t ret;
cherokee_mime_entry_t *entry = NULL;
cherokee_mime_t *mime = ((void **)data)[0];
cherokee_buffer_t *type = ((void **)data)[1];
/* Get the mime entry object
*/
ret = get_by_mime_or_new (mime, type, &entry);
if (ret != ret_ok) return ret;
/* Link to it by the extension
*/
TRACE(ENTRIES, "'%s' adding extension: '%s'\n", type->buf, val);
cherokee_avl_add_ptr (&mime->ext_table, (const char *)val, entry);
return ret_ok;
}
static ret_t
set_maxage (cherokee_mime_t *mime, cherokee_buffer_t *type, cuint_t maxage)
{
ret_t ret;
cherokee_mime_entry_t *entry = NULL;
/* Get the mime entry object
*/
ret = get_by_mime_or_new (mime, type, &entry);
if (ret != ret_ok) return ret;
/* Setting max-age
*/
TRACE(ENTRIES, "'%s' setting max-age: %d\n", type->buf, maxage);
cherokee_mime_entry_set_maxage (entry, maxage);
return ret_ok;
}
static ret_t
configure_mime (cherokee_config_node_t *config, void *data)
{
ret_t ret;
cuint_t maxage;
cherokee_config_node_t *subconf;
cherokee_mime_t *mime = MIME(data);
void *params[] = {mime, &config->key};
ret = cherokee_config_node_get (config, "extensions", &subconf);
if (ret == ret_ok) {
cherokee_config_node_read_list (subconf, NULL, add_extension, params);
}
ret = cherokee_config_node_read_uint (config, "max-age", &maxage);
if (ret == ret_ok) {
ret = set_maxage (mime, &config->key, maxage);
if (ret != ret_ok) return ret;
}
return ret_ok;
}
ret_t
cherokee_mime_configure (cherokee_mime_t *mime, cherokee_config_node_t *config)
{
ret_t ret;
ret = cherokee_config_node_while (config, configure_mime, mime);
if (ret != ret_ok) return ret;
return ret_ok;
}
ret_t
cherokee_mime_get_by_suffix (cherokee_mime_t *mime,
char *suffix,
cherokee_mime_entry_t **entry)
{
return cherokee_avl_get_ptr (&mime->ext_table, suffix, (void **)entry);
}