-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod_aclr2.c
executable file
·327 lines (261 loc) · 8.95 KB
/
mod_aclr2.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
/*
* Original mod_aclr module for Apache 1.3.x
* Copyright (c) Dmitry MikSir (http://miksir.maker.ru)
*
* Porting to Apache 2.x
* Copyright (c) 2011-2012 Andrey Belov
* Copyright (c) 2011-2012 Nginx, Inc. (http://nginx.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_request.h"
#include "util_filter.h"
#include "apr_file_info.h"
#include "apr_general.h"
#include "apr_lib.h"
#include "apr_strings.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
module AP_MODULE_DECLARE_DATA aclr_module;
#define ACLR_VERSION "0.01"
#define ACLR_ENABLED 1
#define ACLR_DISABLED 0
#define UNSET -1
typedef struct {
int state;
int redirect_outside_of_docroot;
apr_off_t fsize;
} aclr_dir_config;
const char *xa_int_name = "X-Accel-Internal";
const char *xa_ver_name = "X-Accel-Version";
const char *xa_redir_name = "X-Accel-Redirect";
/* utilities */
#ifdef DEBUG
static int debuglevel = 0;
#define aclr_debug(level, s, fmt, args...) \
if (level <= debuglevel) \
_aclr_debug(s, fmt, args)
static void
_aclr_debug(server_rec *s, const char *fmt, ...)
{
char errstr[MAX_STRING_LEN];
va_list args;
va_start(args, fmt);
apr_vsnprintf(errstr, sizeof(errstr), fmt, args);
va_end(args);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "[%" APR_PID_T_FMT "] %s",
getpid(), errstr);
return;
}
#else
#define aclr_debug(level, s, fmt, ...)
#endif
/* runtime */
static int
aclr_handler(request_rec *r)
{
int rc;
const char *idhead;
char *real_uri;
const char *docroot;
size_t docroot_len;
ap_filter_t *f, *nextf;
char iredirect[MAX_STRING_LEN];
aclr_dir_config *cfg = (aclr_dir_config *)ap_get_module_config
(r->per_dir_config, &aclr_module);
if (cfg->state != ACLR_ENABLED) {
return DECLINED;
}
if (!ap_is_initial_req(r)) {
return DECLINED;
}
idhead = apr_table_get(r->headers_in, xa_int_name);
if (idhead == NULL) {
return DECLINED;
}
#ifdef DEBUG
const char *server_name = ap_get_server_name(r);
#endif
docroot = ap_document_root(r);
docroot_len = strlen(docroot);
/* obtain real URI from filename - for rewrited URIs */
if (strncmp(r->filename, docroot, docroot_len) != 0) {
if (cfg->redirect_outside_of_docroot != ACLR_ENABLED) {
aclr_debug(2, r->server, "file \"%s\" is outside of "
"DocumentRoot \"%s\": %s%s",
r->filename, docroot, server_name, r->uri);
return DECLINED;
}
real_uri = r->uri;
}
else {
real_uri = r->filename;
real_uri += docroot_len;
}
snprintf(iredirect, sizeof(iredirect), "%s%s", idhead, real_uri);
aclr_debug(3, r->server, "trying to process request: %s%s -> %s",
server_name, r->uri, iredirect);
aclr_debug(3, r->server, "r->filename: %s", r->filename);
aclr_debug(3, r->server, "r->uri: %s", r->uri);
aclr_debug(3, r->server, "docroot: %s", docroot);
aclr_debug(3, r->server, "real_uri: %s", real_uri);
if ((rc = ap_discard_request_body(r)) != OK) {
return rc;
}
apr_table_set(r->headers_out, xa_ver_name, ACLR_VERSION);
if ((r->method_number != M_GET) || (r->header_only)) {
aclr_debug(2, r->server, "request method is not GET: %s%s",
server_name, r->uri);
return DECLINED;
}
if (r->finfo.filetype != APR_REG) {
aclr_debug(2, r->server, "request file not found "
"or is not regular file: %s%s",
server_name, r->uri);
return DECLINED;
}
if (cfg->fsize != UNSET && r->finfo.size < cfg->fsize) {
aclr_debug(2, r->server, "file size %lu < minsize %lu: %s%s",
r->finfo.size, cfg->fsize, server_name, r->uri);
return DECLINED;
}
f = r->output_filters;
do {
nextf = f->next;
aclr_debug(3, r->server, "output filter: %s", f->frec->name);
if (strcmp(f->frec->name, "includes") == 0) {
aclr_debug(2, r->server, "request uses INCLUDES filter: %s%s",
server_name, r->uri);
return DECLINED;
}
f = nextf;
} while (f && f != r->proto_output_filters);
apr_table_set(r->headers_out, xa_redir_name, iredirect);
r->header_only = 0;
ap_update_mtime(r, r->finfo.mtime);
aclr_debug(1, r->server, "request %s%s redirected to %s",
server_name, r->uri, iredirect);
return OK;
}
/* configs */
static void *
aclr_create_dir_config(apr_pool_t *p, char *dummy)
{
aclr_dir_config *cfg;
cfg = (aclr_dir_config *)apr_palloc(p, sizeof(aclr_dir_config));
cfg->state = UNSET;
cfg->redirect_outside_of_docroot = UNSET;
cfg->fsize = UNSET;
return (void *)cfg;
}
/* commands */
static const char *
set_aclr_state(cmd_parms *cmd, void *config, int flag)
{
aclr_dir_config *cfg = (aclr_dir_config *)config;
cfg->state = (flag ? ACLR_ENABLED : ACLR_DISABLED);
return NULL;
}
static const char *
set_aclr_outside_of_docroot(cmd_parms *cmd, void *config, int flag)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) return err;
aclr_dir_config *cfg = (aclr_dir_config *)config;
cfg->redirect_outside_of_docroot = (flag ? ACLR_ENABLED : ACLR_DISABLED);
return NULL;
}
static const char *
set_redirect_min_size(cmd_parms *cmd, void *config, const char *size_str)
{
aclr_dir_config *cfg = (aclr_dir_config *)config;
char *endptr;
long size;
size = strtol(size_str, &endptr, 10);
while (apr_isspace(*endptr)) endptr++;
if (*endptr == '\0' || *endptr == 'b' || *endptr == 'B') {
;
}
else if (*endptr == 'k' || *endptr == 'K') {
size *= 1024;
}
else if (*endptr == 'm' || *endptr == 'M') {
size *= 1048576;
}
else {
return apr_pstrcat(cmd->pool, "Invalid size in AccelRedirectSize: ",
size_str, NULL);
}
cfg->fsize = (size >= 0) ? size : UNSET;
return NULL;
}
#ifdef DEBUG
static const char *
set_debug_level(cmd_parms *cmd, void *config, const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) return err;
debuglevel = strtol(arg, NULL, 10);
return NULL;
}
#endif
/* module info */
static const command_rec aclr_cmds[] =
{
AP_INIT_FLAG("AccelRedirectSet", set_aclr_state,
NULL, ACCESS_CONF|RSRC_CONF,
"Turn X-Accel-Redirect support On or Off (default Off)"),
AP_INIT_TAKE1("AccelRedirectSize", set_redirect_min_size,
NULL, ACCESS_CONF|RSRC_CONF,
"Minimum size of file for redirect"),
AP_INIT_FLAG("AccelRedirectOutsideDocRoot", set_aclr_outside_of_docroot,
NULL, RSRC_CONF,
"Allow redirect outside of DocumentRoot (default Off)"),
#ifdef DEBUG
AP_INIT_TAKE1("AccelRedirectDebug", set_debug_level,
NULL, RSRC_CONF,
"Debug level (0=off, 1=min, 2=mid, 3=max)"),
#endif
{ NULL }
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_handler(aclr_handler, NULL, NULL, APR_HOOK_REALLY_LAST - 1);
}
module AP_MODULE_DECLARE_DATA aclr_module =
{
STANDARD20_MODULE_STUFF,
aclr_create_dir_config, /* create per-directory config structures */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
aclr_cmds, /* command handlers */
register_hooks /* register hooks */
};