/
handler_error_redir.c
248 lines (199 loc) · 5.85 KB
/
handler_error_redir.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 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 "handler_redir.h"
#include "handler_error_redir.h"
#include "connection.h"
#include "plugin_loader.h"
#include "connection-protected.h"
#include "util.h"
#define ENTRIES "handler,error_handler,redir"
typedef struct {
cherokee_list_t entry;
cuint_t error;
cherokee_buffer_t url;
cherokee_boolean_t show;
} error_entry_t;
static ret_t
props_free (cherokee_handler_error_redir_props_t *props)
{
cherokee_list_t *i, *j;
list_for_each_safe (i, j, &props->errors) {
error_entry_t *entry = (error_entry_t *)i;
cherokee_buffer_mrproper (&entry->url);
free (entry);
}
return cherokee_module_props_free_base (MODULE_PROPS(props));
}
ret_t
cherokee_handler_error_redir_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_list_t *i;
cherokee_handler_error_redir_props_t *props;
UNUSED(srv);
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_error_redir_props);
cherokee_module_props_init_base (MODULE_PROPS(n),
MODULE_PROPS_FREE(props_free));
INIT_LIST_HEAD (&n->errors);
n->error_default = NULL;
*_props = MODULE_PROPS(n);
}
props = PROP_ERREDIR(*_props);
cherokee_config_node_foreach (i, conf) {
error_entry_t *entry;
cint_t error = 0;
cherokee_config_node_t *subconf = CONFIG_NODE(i);
cherokee_boolean_t is_default = false;
/* Special 'Default' case
*/
if (equal_buf_str (&subconf->key, "default")) {
is_default = true;
/* Check the error number
*/
} else {
ret = cherokee_atoi (subconf->key.buf, &error);
if (ret != ret_ok) return ret;
if (!http_type_300 (error) &&
!http_type_400 (error) &&
!http_type_500 (error))
{
LOG_ERROR (CHEROKEE_ERROR_HANDLER_ERROR_REDIR_CODE, subconf->key.buf);
continue;
}
}
/* New object
*/
entry = (error_entry_t *) malloc (sizeof(error_entry_t));
if (entry == NULL)
return ret_nomem;
if (error) {
entry->error = error;
}
entry->show = false;
INIT_LIST_HEAD (&entry->entry);
cherokee_buffer_init (&entry->url);
/* Read child values
*/
ret = cherokee_config_node_copy (subconf, "url", &entry->url);
if (ret != ret_ok) {
LOG_CRITICAL (CHEROKEE_ERROR_HANDLER_ERROR_REDIR_URL, error);
return ret_error;
}
cherokee_config_node_read_bool (subconf, "show", &entry->show);
TRACE(ENTRIES, "Error %d redir to '%s', show=%d\n",
entry->error, entry->url.buf, entry->show);
/* Add it to the list
*/
if (is_default) {
props->error_default = entry;
} else {
cherokee_list_add (&entry->entry, &props->errors);
}
}
return ret_ok;
}
static ret_t
do_redir_external (cherokee_handler_t **hdl,
cherokee_connection_t *conn,
cherokee_module_props_t *props,
error_entry_t *entry)
{
cherokee_buffer_clean (&conn->redirect);
cherokee_buffer_add_buffer (&conn->redirect, &entry->url);
conn->error_code = http_moved_permanently;
return cherokee_handler_redir_new (hdl, conn, props);
}
static ret_t
do_redir_internal (cherokee_connection_t *conn,
error_entry_t *entry)
{
/* Set REDIRECT_URL
*/
cherokee_buffer_clean (&conn->error_internal_url);
cherokee_buffer_add_buffer (&conn->error_internal_url, &conn->request);
/* Set REDIRECT_QUERY_STRING
*/
cherokee_buffer_clean (&conn->error_internal_qs);
cherokee_buffer_add_buffer (&conn->error_internal_qs, &conn->query_string);
/* Clean up the connection
*/
cherokee_buffer_clean (&conn->pathinfo);
cherokee_buffer_clean (&conn->request);
cherokee_buffer_clean (&conn->local_directory);
/* Set the new request
*/
cherokee_buffer_add_buffer (&conn->request, &entry->url);
/* Store the previous error code
*/
conn->error_internal_code = conn->error_code;
return ret_eagain;
}
ret_t
cherokee_handler_error_redir_new (cherokee_handler_t **hdl,
cherokee_connection_t *conn,
cherokee_module_props_t *props)
{
cherokee_list_t *i;
error_entry_t *default_error;
/* Error list
*/
list_for_each (i, &PROP_ERREDIR(props)->errors) {
error_entry_t *entry = (error_entry_t *)i;
if (entry->error != conn->error_code) {
continue;
}
if (entry->show) {
return do_redir_external (hdl, conn, props, entry);
} else {
return do_redir_internal (conn, entry);
}
}
/* Default error
*/
default_error = (error_entry_t *) PROP_ERREDIR(props)->error_default;
if (default_error) {
if (default_error->show) {
return do_redir_external (hdl, conn, props, default_error);
} else {
return do_redir_internal (conn, default_error);
}
}
return ret_error;
}
/* Library init function
*/
static cherokee_boolean_t _error_redir_is_init = false;
void
PLUGIN_INIT_NAME(error_redir) (cherokee_plugin_loader_t *loader)
{
/* Is init?
*/
if (_error_redir_is_init) return;
_error_redir_is_init = true;
/* Load the dependences
*/
cherokee_plugin_loader_load (loader, "redir");
}
PLUGIN_INFO_HANDLER_EASY_INIT (error_redir, http_all_methods);