Project

General

Profile

Feature #1259 ยป mod_autoext.c

Special, 2007-07-06 23:08

 
1
#include <ctype.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <errno.h>
5

    
6
#include "base.h"
7
#include "log.h"
8
#include "buffer.h"
9

    
10
#include "plugin.h"
11

    
12
#ifdef HAVE_CONFIG_H
13
#include "config.h"
14
#endif
15

    
16
/*
17
 * Copyright (c) 2007 John Brooks (special@inspircd.org)
18
 *
19
 * Permission is hereby granted, free of charge, to any person
20
 * obtaining a copy of this software and associated documentation
21
 * files (the "Software"), to deal in the Software without
22
 * restriction, including without limitation the rights to use,
23
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
24
 * copies of the Software, and to permit persons to whom the
25
 * Software is furnished to do so, subject to the following
26
 * conditions:
27
 * 
28
 * The above copyright notice and this permission notice shall be
29
 * included in all copies or substantial portions of the Software.
30
 * 
31
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
33
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
35
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
36
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
38
 * OTHER DEALINGS IN THE SOFTWARE.
39
 */
40

    
41
/* plugin config for all request/connections */
42

    
43
typedef struct {
44
	array *suffixes;
45
} plugin_config;
46

    
47
typedef struct {
48
	PLUGIN_DATA;
49

    
50
	plugin_config **config_storage;
51
	plugin_config conf;
52
} plugin_data;
53

    
54
/* init the plugin data */
55
INIT_FUNC(mod_autoext_init)
56
{
57
	plugin_data *p;
58
	p = calloc(1, sizeof(*p));
59
	return p;
60
}
61

    
62
/* detroy the plugin data */
63
FREE_FUNC(mod_autoext_free)
64
{
65
	plugin_data *p = p_d;
66
	
67
	UNUSED(srv);
68
	
69
	if (!p)
70
		return HANDLER_GO_ON;
71
	
72
	if (p->config_storage)
73
	{
74
		size_t i;
75

    
76
		for (i = 0; i < srv->config_context->used; i++)
77
		{
78
			plugin_config *s = p->config_storage[i];
79
			
80
			if (!s)
81
				continue;
82

    
83
			array_free(s->suffixes);
84
			free(s);
85
		}
86
		free(p->config_storage);
87
	}
88

    
89
	free(p);
90

    
91
	return HANDLER_GO_ON;
92
}
93

    
94
/* handle plugin config and check values */
95

    
96
SETDEFAULTS_FUNC(mod_autoext_set_defaults)
97
{
98
	plugin_data *p = p_d;
99
	size_t i = 0;
100

    
101
	config_values_t cv[] = {
102
		{ "autoext.suffixes",           NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
103
		{ NULL,                         NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
104
	};
105

    
106
	if (!p)
107
		return HANDLER_ERROR;
108

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

    
111
	for (i = 0; i < srv->config_context->used; i++)
112
	{
113
		plugin_config *s;
114

    
115
		s = calloc(1, sizeof(plugin_config));
116
		s->suffixes    = array_init();
117

    
118
		cv[0].destination = s->suffixes;
119

    
120
		p->config_storage[i] = s;
121

    
122
		if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv))
123
			return HANDLER_ERROR;
124
	}
125

    
126
	return HANDLER_GO_ON;
127
}
128

    
129
#define PATCH(x) p->conf.x = s->x;
130

    
131
static int mod_autoext_patch_connection(server *srv, connection *con, plugin_data *p)
132
{
133
	size_t i, j;
134
	plugin_config *s = p->config_storage[0];
135

    
136
	PATCH(suffixes);
137

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

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

    
148
		/* merge config */
149
		for (j = 0; j < dc->value->used; j++)
150
		{
151
			data_unset *du = dc->value->data[j];
152
			
153
			if (buffer_is_equal_string(du->key, CONST_STR_LEN("autoext.suffixes")))
154
			{
155
				PATCH(suffixes);
156
			}
157
		}
158
	}
159

    
160
	return 0;
161
}
162
#undef PATCH
163

    
164
PHYSICALPATH_FUNC(mod_autoext_handle_physical)
165
{
166
	struct stat st;
167
	size_t i, k;
168
	plugin_data *p = (plugin_data *) p_d;
169
	
170
	if (stat(con->physical.path->ptr, &st) == 0)
171
		return HANDLER_GO_ON;
172
	
173
	buffer_copy_string_buffer(srv->tmp_buf, con->physical.path);
174
	
175
	mod_autoext_patch_connection(srv, con, p);
176
	
177
	for (i = 1; i < srv->tmp_buf->used; i++)
178
	{
179
		if (srv->tmp_buf->ptr[i] != '/' && srv->tmp_buf->ptr[i] != '\0')
180
			continue;
181
		
182
		srv->tmp_buf->ptr[i] = '\0';
183
		
184
		if (stat(srv->tmp_buf->ptr, &st) != 0 && errno == ENOENT)
185
		{
186
			// We have our file that doesn't exist! Now, find out which suffix, if any, this has
187
			buffer *apbuf = buffer_init();
188
			buffer_copy_string(apbuf, srv->tmp_buf->ptr);
189
			int apbaselen = apbuf->used;
190
			
191
			for (k = 0; k < p->conf.suffixes->used; k++)
192
			{
193
				apbuf->used = apbaselen;
194
				buffer_append_string(apbuf, ((data_string*) p->conf.suffixes->data[k])->value->ptr);
195
				
196
				if (stat(apbuf->ptr, &st) == 0)
197
				{
198
					// Match!
199
					buffer_append_string(apbuf, con->physical.path->ptr + i);
200
					buffer_reset(con->physical.path);
201
					buffer_copy_string(con->physical.path, apbuf->ptr);
202
					return HANDLER_GO_ON;
203
				}
204
			}
205
			
206
			break;
207
		}
208
		
209
		srv->tmp_buf->ptr[i] = '/';
210
	}
211
	
212
	return HANDLER_GO_ON;
213
}
214

    
215
/* this function is called at dlopen() time and inits the callbacks */
216

    
217
int mod_autoext_plugin_init(plugin *p) {
218
	p->version     = LIGHTTPD_VERSION_ID;
219
	p->name        = buffer_init_string("autoext");
220

    
221
	p->init        = mod_autoext_init;
222
	p->handle_physical = mod_autoext_handle_physical;
223
	p->set_defaults  = mod_autoext_set_defaults;
224
	p->cleanup     = mod_autoext_free;
225

    
226
	p->data        = NULL;
227

    
228
	return 0;
229
}
    (1-1/1)