summaryrefslogtreecommitdiff
path: root/src/mod_rewrite.c
blob: 94609300005167788f7343862098bb8cfb57ec66 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "base.h"
#include "log.h"
#include "buffer.h"

#include "plugin.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

typedef struct {
	pcre_keyvalue_buffer *rewrite;
	buffer *once;
	data_config *context; /* to which apply me */
} plugin_config;

typedef struct {
	enum { REWRITE_STATE_UNSET, REWRITE_STATE_FINISHED} state;
	int loops;
} handler_ctx;

typedef struct {
	PLUGIN_DATA;
	buffer *match_buf;

	plugin_config **config_storage;

	plugin_config conf;
} plugin_data;

static handler_ctx * handler_ctx_init() {
	handler_ctx * hctx;

	hctx = calloc(1, sizeof(*hctx));

	hctx->state = REWRITE_STATE_UNSET;
	hctx->loops = 0;

	return hctx;
}

static void handler_ctx_free(handler_ctx *hctx) {
	free(hctx);
}


INIT_FUNC(mod_rewrite_init) {
	plugin_data *p;

	UNUSED(srv);

	p = calloc(1, sizeof(*p));

	p->match_buf = buffer_init();

	return p;
}

FREE_FUNC(mod_rewrite_free) {
	plugin_data *p = p_d;

	UNUSED(srv);

	if (!p) return HANDLER_GO_ON;

	buffer_free(p->match_buf);
	if (p->config_storage) {
		size_t i;
		for (i = 0; i < srv->config_context->used; i++) {
			plugin_config *s = p->config_storage[i];

			if (!s) continue;
			
			pcre_keyvalue_buffer_free(s->rewrite);
			buffer_free(s->once);

			free(s);
		}
		free(p->config_storage);
	}

	free(p);

	return HANDLER_GO_ON;
}

static handler_t parse_config_entry(server *UNUSED_PARAM(srv), plugin_config *s, array *ca, const char *option, size_t option_len, int once) {
	data_unset *du;

	if (NULL != (du = array_get_element(ca, option, option_len))) {
		data_array *da = (data_array *)du;
		size_t j;

		if (du->type != TYPE_ARRAY) {
			ERROR("unexpected type (%d) for key %s, expected 'array of strings'", 
					du->type, option);

			return HANDLER_ERROR;
		}

		da = (data_array *)du;

		for (j = 0; j < da->value->used; j++) {
			if (da->value->data[j]->type != TYPE_STRING) {
				ERROR("unexpected type (%d) for value of %s[%s], expected 'string'", 
					da->value->data[j]->type, option, 
					SAFE_BUF_STR(da->value->data[j]->key));

				return HANDLER_ERROR;
			}

			if (0 != pcre_keyvalue_buffer_append(s->rewrite,
							    ((data_string *)(da->value->data[j]))->key->ptr,
							    ((data_string *)(da->value->data[j]))->value->ptr)) {
#ifdef HAVE_PCRE_H
				ERROR("pcre-compile failed for: %s", SAFE_BUF_STR(da->value->data[j]->key));
#else
				ERROR("pcre support is missing, please install libpcre and the headers%s", "");
#endif
			}

			if (once) {
				buffer_append_string_len(s->once, CONST_STR_LEN("1"));
			} else {
				buffer_append_string_len(s->once, CONST_STR_LEN("0"));
			}
		}
	}

	return HANDLER_GO_ON;
}

SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
	plugin_data *p = p_d;
	size_t i = 0;

	config_values_t cv[] = {
		{ "url.rewrite-repeat",        NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
		{ "url.rewrite-once",          NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 1 */

		/* old names, still supported
		 *
		 * url.rewrite remapped to url.rewrite-once
		 * url.rewrite-final    is url.rewrite-once
		 *
		 */
		{ "url.rewrite",               NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
		{ "url.rewrite-final",         NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
		{ NULL,                        NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
	};

	if (!p) return HANDLER_ERROR;

	/* 0 */
	p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));

	for (i = 0; i < srv->config_context->used; i++) {
		plugin_config *s;
		array *ca;

		s = calloc(1, sizeof(plugin_config));
		s->rewrite   = pcre_keyvalue_buffer_init();
		s->once      = buffer_init();

		p->config_storage[i] = s;
		ca = ((data_config *)srv->config_context->data[i])->value;

		if (0 != config_insert_values_global(srv, ca, cv)) {
			return HANDLER_ERROR;
		}

		if (HANDLER_GO_ON != parse_config_entry(srv, s, ca, CONST_STR_LEN("url.rewrite-once"),   1)) return HANDLER_ERROR;
		if (HANDLER_GO_ON != parse_config_entry(srv, s, ca, CONST_STR_LEN("url.rewrite-final"),  1)) return HANDLER_ERROR;
		if (HANDLER_GO_ON != parse_config_entry(srv, s, ca, CONST_STR_LEN("url.rewrite"),        1)) return HANDLER_ERROR;
		if (HANDLER_GO_ON != parse_config_entry(srv, s, ca, CONST_STR_LEN("url.rewrite-repeat"), 0)) return HANDLER_ERROR;
	}

	return HANDLER_GO_ON;
}
#ifdef HAVE_PCRE_H
static int mod_rewrite_patch_connection(server *srv, connection *con, plugin_data *p) {
	size_t i, j;
	plugin_config *s = p->config_storage[0];
	p->conf.rewrite = s->rewrite;
	p->conf.once    = s->once;

	/* skip the first, the global context */
	for (i = 1; i < srv->config_context->used; i++) {
		data_config *dc = (data_config *)srv->config_context->data[i];
		s = p->config_storage[i];

		if (COMP_HTTP_URL == dc->comp) continue;

		/* condition didn't match */
		if (!config_check_cond(srv, con, dc)) continue;

		/* merge config */
		for (j = 0; j < dc->value->used; j++) {
			data_unset *du = dc->value->data[j];

			if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite"))) {
				p->conf.rewrite = s->rewrite;
				p->conf.once    = s->once;
				p->conf.context = dc;
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-once"))) {
				p->conf.rewrite = s->rewrite;
				p->conf.once    = s->once;
				p->conf.context = dc;
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-repeat"))) {
				p->conf.rewrite = s->rewrite;
				p->conf.once    = s->once;
				p->conf.context = dc;
			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-final"))) {
				p->conf.rewrite = s->rewrite;
				p->conf.once    = s->once;
				p->conf.context = dc;
			}
		}
	}

	return 0;
}
#endif
URIHANDLER_FUNC(mod_rewrite_con_reset) {
	plugin_data *p = p_d;

	UNUSED(srv);

	if (con->plugin_ctx[p->id]) {
		handler_ctx_free(con->plugin_ctx[p->id]);
		con->plugin_ctx[p->id] = NULL;
	}

	return HANDLER_GO_ON;
}

URIHANDLER_FUNC(mod_rewrite_uri_handler) {
#ifdef HAVE_PCRE_H
	plugin_data *p = p_d;
	int i;
	handler_ctx *hctx = NULL;

	/*
	 * REWRITE URL
	 *
	 * e.g. rewrite /base/ to /index.php?section=base
	 *
	 */

	if (con->plugin_ctx[p->id]) {
		hctx = con->plugin_ctx[p->id];

		if (hctx->loops++ > 100) {
			ERROR("ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request after %d loops at %s", 
					hctx->loops, SAFE_BUF_STR(con->request.uri));

			con->http_status = 500;

			return HANDLER_FINISHED;
		}

		if (hctx->state == REWRITE_STATE_FINISHED) return HANDLER_GO_ON;
	}

	mod_rewrite_patch_connection(srv, con, p);

	if (!p->conf.rewrite) return HANDLER_GO_ON;

	buffer_copy_string_buffer(p->match_buf, con->request.uri);
	i = config_exec_pcre_keyvalue_buffer(con, p->conf.rewrite, p->conf.context, p->match_buf, con->request.uri);

	if (i >= 0) {
		if (!hctx) {
			hctx = handler_ctx_init();

			con->plugin_ctx[p->id] = hctx;
		}

		if (p->conf.once->ptr[i] == '1')
			hctx->state = REWRITE_STATE_FINISHED;

		/* looks like we finished the rewrite 
		 *
		 * start the uri-splitting again
		 * */

		if (con->request.uri->used == 0 ||
		    con->request.uri->ptr[0] != '/') {
			con->http_status = 500;

			ERROR("url.rewrite contains a regex for '%s' which leads to a URI without a leading slash: %s", 
					SAFE_BUF_STR(p->match_buf), SAFE_BUF_STR(con->request.uri));

			return HANDLER_FINISHED;
		}

		return HANDLER_COMEBACK;
	} else if (i != PCRE_ERROR_NOMATCH) {
		ERROR("execution error while matching '%s' against '%s': %d", 
				SAFE_BUF_STR(p->match_buf), SAFE_BUF_STR(con->request.uri), i);
		con->http_status = 500;

		return HANDLER_FINISHED;
	}
#undef N

#else
	UNUSED(srv);
	UNUSED(con);
	UNUSED(p_d);
#endif

	return HANDLER_GO_ON;
}

LI_EXPORT int mod_rewrite_plugin_init(plugin *p);
LI_EXPORT int mod_rewrite_plugin_init(plugin *p) {
	p->version     = LIGHTTPD_VERSION_ID;
	p->name        = buffer_init_string("rewrite");

	p->init        = mod_rewrite_init;
	/* it has to stay _raw as we are matching on uri + querystring
	 */

	p->handle_uri_raw = mod_rewrite_uri_handler;
	p->set_defaults = mod_rewrite_set_defaults;
	p->cleanup     = mod_rewrite_free;
	p->connection_reset = mod_rewrite_con_reset;

	p->data        = NULL;

	return 0;
}