This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_vhs_alias.c
348 lines (310 loc) · 9.54 KB
/
mod_vhs_alias.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
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
338
339
340
341
342
343
344
345
346
347
348
/**
* @version $Id$
* @brief Loosely based on mod_alias.c
*/
#include "include/mod_vhs.h"
extern module AP_MODULE_DECLARE_DATA vhs_module;
void * create_alias_dir_config(apr_pool_t * p, char *d)
{
alias_dir_conf *a = (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
return a;
}
void * merge_alias_dir_config(apr_pool_t * p, void *basev, void *overridesv)
{
alias_dir_conf *a = (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
alias_dir_conf *base = (alias_dir_conf *) basev;
alias_dir_conf *overrides = (alias_dir_conf *) overridesv;
a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
return a;
}
const char * add_alias_internal(cmd_parms * cmd, void *dummy, const char *f, const char *r, int use_regex)
{
server_rec *s = cmd->server;
vhs_config_rec *conf = ap_get_module_config(s->module_config,
&vhs_module);
alias_entry *new = apr_array_push(conf->aliases);
alias_entry *entries = (alias_entry *) conf->aliases->elts;
int i;
/* XX r can NOT be relative to DocumentRoot here... compat bug. */
if (use_regex) {
#ifdef DEBIAN
new->regexp = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
#else
new->regexp = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
#endif /* DEBIAN */
if (new->regexp == NULL)
return "Regular expression could not be compiled.";
new->real = r;
} else {
/*
* XXX This may be optimized, but we must know that new->real
* exists. If so, we can dir merge later, trusing new->real
* and just canonicalizing the remainder. Not till I finish
* cleaning out the old ap_canonical stuff first.
*/
new->real = r;
}
new->fake = f;
new->handler = cmd->info;
/*
* check for overlapping (Script)Alias directives and throw a warning
* if found one
*/
if (!use_regex) {
for (i = 0; i < conf->aliases->nelts - 1; ++i) {
alias_entry *p = &entries[i];
if ((!p->regexp && alias_matches(f, p->fake) > 0)
|| (p->regexp && !ap_regexec(p->regexp, f, 0, NULL, 0))) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
"The %s directive in %s at line %d will probably "
"never match because it overlaps an earlier "
"%sAlias%s.",
cmd->cmd->name, cmd->directive->filename,
cmd->directive->line_num,
p->handler ? "Script" : "",
p->regexp ? "Match" : "");
break; /* one warning per alias should be
* sufficient */
}
}
}
return NULL;
}
const char * add_alias(cmd_parms * cmd, void *dummy, const char *f, const char *r)
{
return add_alias_internal(cmd, dummy, f, r, 0);
}
const char * add_alias_regex(cmd_parms * cmd, void *dummy, const char *f, const char *r)
{
return add_alias_internal(cmd, dummy, f, r, 1);
}
const char * add_redirect_internal(cmd_parms * cmd, alias_dir_conf * dirconf,
const char *arg1, const char *arg2,
const char *arg3, int use_regex)
{
alias_entry *new;
server_rec *s = cmd->server;
vhs_config_rec *serverconf = ap_get_module_config(s->module_config,
&vhs_module);
int status = (int)(long)cmd->info;
#if APR_MAJOR_VERSION > 0
ap_regex_t *r = NULL;
#else
#ifdef DEBIAN
ap_regex_t *r = NULL;
#else
regex_t *r = NULL;
#endif /* DEBIAN */
#endif
const char *f = arg2;
const char *url = arg3;
if (!strcasecmp(arg1, "gone"))
status = HTTP_GONE;
else if (!strcasecmp(arg1, "permanent"))
status = HTTP_MOVED_PERMANENTLY;
else if (!strcasecmp(arg1, "temp"))
status = HTTP_MOVED_TEMPORARILY;
else if (!strcasecmp(arg1, "seeother"))
status = HTTP_SEE_OTHER;
else if (apr_isdigit(*arg1))
status = atoi(arg1);
else {
f = arg1;
url = arg2;
}
if (use_regex) {
#ifdef DEBIAN
r = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
#else
r = ap_pregcomp(cmd->pool, f, REG_EXTENDED);
#endif /* DEBIAN */
if (r == NULL)
return "Regular expression could not be compiled.";
}
if (ap_is_HTTP_REDIRECT(status)) {
if (!url)
return "URL to redirect to is missing";
if (!use_regex && !ap_is_url(url))
return "Redirect to non-URL";
} else {
if (url)
return "Redirect URL not valid for this status";
}
if (cmd->path)
new = apr_array_push(dirconf->redirects);
else
new = apr_array_push(serverconf->redirects);
new->fake = f;
new->real = url;
new->regexp = r;
new->redir_status = status;
return NULL;
}
const char * add_redirect(cmd_parms * cmd, void *dirconf,
const char *arg1, const char *arg2,
const char *arg3)
{
return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
}
const char * add_redirect2(cmd_parms * cmd, void *dirconf,
const char *arg1, const char *arg2)
{
return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0);
}
const char * add_redirect_regex(cmd_parms * cmd, void *dirconf,
const char *arg1, const char *arg2,
const char *arg3)
{
return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
}
int alias_matches(const char *uri, const char *alias_fakename)
{
const char *aliasp = alias_fakename, *urip = uri;
while (*aliasp) {
if (*aliasp == '/') {
/*
* any number of '/' in the alias matches any number
* in the supplied URI, but there must be at least
* one...
*/
if (*urip != '/')
return 0;
do {
++aliasp;
} while (*aliasp == '/');
do {
++urip;
} while (*urip == '/');
} else {
/* Other characters are compared literally */
if (*urip++ != *aliasp++)
return 0;
}
}
/* Check last alias path component matched all the way */
if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
return 0;
/*
* Return number of characters from URI which matched (may be greater
* than length of alias, since we may have matched doubled slashes)
*/
return urip - uri;
}
char * try_alias_list(request_rec * r, apr_array_header_t * aliases,
int doesc, int *status)
{
alias_entry *entries = (alias_entry *) aliases->elts;
#ifdef DEBIAN
ap_regmatch_t regm [AP_MAX_REG_MATCH];
#else
regmatch_t regm [AP_MAX_REG_MATCH];
#endif /* DEBIAN */
char *found = NULL;
int i;
for (i = 0; i < aliases->nelts; ++i) {
alias_entry *p = &entries[i];
int l;
if (p->regexp) {
if (!ap_regexec(p->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
if (p->real) {
found = ap_pregsub(r->pool, p->real, r->uri,
AP_MAX_REG_MATCH, regm);
if (found && doesc) {
apr_uri_t uri;
apr_uri_parse(r->pool, found, &uri);
/*
* Do not escape the query
* string or fragment.
*/
found = apr_uri_unparse(r->pool, &uri,
APR_URI_UNP_OMITQUERY);
found = ap_escape_uri(r->pool, found);
if (uri.query) {
found = apr_pstrcat(r->pool, found, "?",
uri.query, NULL);
}
if (uri.fragment) {
found = apr_pstrcat(r->pool, found, "#",
uri.fragment, NULL);
}
}
} else {
/* need something non-null */
found = apr_pstrdup(r->pool, "");
}
}
} else {
l = alias_matches(r->uri, p->fake);
if (l > 0) {
if (doesc) {
char *escurl;
escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
found = apr_pstrcat(r->pool, p->real, escurl, NULL);
} else
found = apr_pstrcat(r->pool, p->real, r->uri + l, NULL);
}
}
if (found) {
if (p->handler) { /* Set handler, and leave a
* note for mod_cgi */
r->handler = p->handler;
apr_table_setn(r->notes, "alias-forced-type", r->handler);
}
/*
* XXX This is as SLOW as can be, next step, we
* optimize and merge to whatever part of the found
* path was already canonicalized. After I finish
* eliminating os canonical. Better fail test for
* ap_server_root_relative needed here.
*/
if (!doesc) {
found = ap_server_root_relative(r->pool, found);
}
if (found) {
*status = p->redir_status;
}
return found;
}
}
return NULL;
}
int fixup_redir(request_rec * r)
{
void *dconf = r->per_dir_config;
alias_dir_conf *dirconf =
(alias_dir_conf *) ap_get_module_config(dconf, &vhs_module);
char *ret;
int status;
/* It may have changed since last time, so try again */
if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
if (ap_is_HTTP_REDIRECT(status)) {
if (ret[0] == '/') {
char *orig_target = ret;
ret = ap_construct_url(r->pool, ret, r);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"incomplete redirection target of '%s' for "
"URI '%s' modified to '%s'",
orig_target, r->uri, ret);
}
if (!ap_is_url(ret)) {
status = HTTP_INTERNAL_SERVER_ERROR;
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"cannot redirect '%s' to '%s'; "
"target is not a valid absoluteURI or abs_path",
r->uri, ret);
} else {
/*
* append requested query only, if the config
* didn't supply its own.
*/
if (r->args && !ap_strchr(ret, '?')) {
ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
}
apr_table_setn(r->headers_out, "Location", ret);
}
}
return status;
}
return DECLINED;
}