Browse Source

[mod_redirect, mod_rewrite] code reuse (sharing)

personal/stbuehler/fix-fdevent
Glenn Strauss 4 years ago
parent
commit
cf9474aa2c
  1. 74
      src/keyvalue.c
  2. 24
      src/keyvalue.h
  3. 126
      src/mod_redirect.c
  4. 245
      src/mod_rewrite.c

74
src/keyvalue.c

@ -174,6 +174,14 @@ http_method_t get_http_method_key(const char *s) {
#include <pcre.h>
#endif
typedef struct pcre_keyvalue {
#ifdef HAVE_PCRE_H
pcre *key;
pcre_extra *key_extra;
#endif
buffer *value;
} pcre_keyvalue;
pcre_keyvalue_buffer *pcre_keyvalue_buffer_init(void) {
pcre_keyvalue_buffer *kvb;
@ -183,17 +191,15 @@ pcre_keyvalue_buffer *pcre_keyvalue_buffer_init(void) {
return kvb;
}
int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, const char *key, const char *value) {
int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, buffer *key, buffer *value) {
#ifdef HAVE_PCRE_H
size_t i;
const char *errptr;
int erroff;
pcre_keyvalue *kv;
#endif
if (!key) return -1;
#ifdef HAVE_PCRE_H
if (kvb->size == 0) {
kvb->size = 4;
kvb->used = 0;
@ -218,7 +224,7 @@ int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, const ch
}
kv = kvb->kv[kvb->used];
if (NULL == (kv->key = pcre_compile(key,
if (NULL == (kv->key = pcre_compile(key->ptr,
0, &errptr, &erroff, NULL))) {
log_error_write(srv, __FILE__, __LINE__, "SS",
@ -231,17 +237,22 @@ int pcre_keyvalue_buffer_append(server *srv, pcre_keyvalue_buffer *kvb, const ch
return -1;
}
kv->value = buffer_init_string(value);
kv->value = buffer_init_buffer(value);
kvb->used++;
return 0;
#else
static int logged_message = 0;
if (logged_message) return 0;
logged_message = 1;
log_error_write(srv, __FILE__, __LINE__, "s",
"pcre support is missing, please install libpcre and the headers");
UNUSED(kvb);
UNUSED(key);
UNUSED(value);
return -1;
#endif
return 0;
}
void pcre_keyvalue_buffer_free(pcre_keyvalue_buffer *kvb) {
@ -263,7 +274,8 @@ void pcre_keyvalue_buffer_free(pcre_keyvalue_buffer *kvb) {
free(kvb);
}
void pcre_keyvalue_buffer_subst(buffer *b, const buffer *patternb, const char **list, int n, struct cond_cache_t *cache) {
#ifdef HAVE_PCRE_H
static void pcre_keyvalue_buffer_subst(buffer *b, const buffer *patternb, const char **list, int n, pcre_keyvalue_ctx *ctx) {
const char *pattern = patternb->ptr;
const size_t pattern_len = buffer_string_length(patternb);
size_t start = 0;
@ -286,7 +298,8 @@ void pcre_keyvalue_buffer_subst(buffer *b, const buffer *patternb, const char **
if (num < (size_t)n) {
buffer_append_string(b, list[num]);
}
} else if (cache) {
} else if (ctx->cache) {
const struct cond_cache_t * const cache = ctx->cache;
if (num < (size_t)cache->patterncount) {
num <<= 1; /* n *= 2 */
buffer_append_string_len(b,
@ -309,3 +322,44 @@ void pcre_keyvalue_buffer_subst(buffer *b, const buffer *patternb, const char **
buffer_append_string_len(b, pattern + start, pattern_len - start);
}
handler_t pcre_keyvalue_buffer_process(pcre_keyvalue_buffer *kvb, pcre_keyvalue_ctx *ctx, buffer *input, buffer *result) {
for (int i = 0, used = (int)kvb->used; i < used; ++i) {
pcre_keyvalue * const kv = kvb->kv[i];
#define N 10
int ovec[N * 3];
#undef N
int n = pcre_exec(kv->key, kv->key_extra, CONST_BUF_LEN(input),
0, 0, ovec, sizeof(ovec)/sizeof(int));
if (n < 0) {
if (n != PCRE_ERROR_NOMATCH) {
return HANDLER_ERROR;
}
}
else if (buffer_string_is_empty(kv->value)) {
/* short-circuit if blank replacement pattern
* (do not attempt to match against remaining kvb rules) */
ctx->m = i;
return HANDLER_GO_ON;
}
else { /* it matched */
const char **list;
ctx->m = i;
pcre_get_substring_list(input->ptr, ovec, n, &list);
pcre_keyvalue_buffer_subst(result, kv->value, list, n, ctx);
pcre_free(list);
return HANDLER_FINISHED;
}
}
return HANDLER_GO_ON;
}
#else
handler_t pcre_keyvalue_buffer_process(pcre_keyvalue_buffer *kvb, pcre_keyvalue_ctx *ctx, buffer *input, buffer *result) {
UNUSED(kvb);
UNUSED(ctx);
UNUSED(input);
UNUSED(result);
return HANDLER_GO_ON;
}
#endif

24
src/keyvalue.h

@ -63,26 +63,18 @@ typedef enum { HTTP_VERSION_UNSET = -1, HTTP_VERSION_1_0, HTTP_VERSION_1_1 } htt
typedef struct {
int key;
const char *value;
} keyvalue;
struct pcre_keyvalue; /* declaration */
#ifdef HAVE_PCRE_H
struct pcre_extra; /* declaration */
#endif
typedef struct {
#ifdef HAVE_PCRE_H
void *key;
struct pcre_extra *key_extra;
#endif
buffer *value;
} pcre_keyvalue;
typedef struct pcre_keyvalue_ctx {
struct cond_cache_t *cache;
int m;
} pcre_keyvalue_ctx;
typedef struct {
pcre_keyvalue **kv;
struct pcre_keyvalue **kv;
size_t used;
size_t size;
} pcre_keyvalue_buffer;
@ -95,8 +87,8 @@ int get_http_version_key(const char *s);
http_method_t get_http_method_key(const char *s);
pcre_keyvalue_buffer *pcre_keyvalue_buffer_init(void);
int pcre_keyvalue_buffer_append(struct server *srv, pcre_keyvalue_buffer *kvb, const char *key, const char *value);
int pcre_keyvalue_buffer_append(struct server *srv, pcre_keyvalue_buffer *kvb, buffer *key, buffer *value);
void pcre_keyvalue_buffer_free(pcre_keyvalue_buffer *kvb);
void pcre_keyvalue_buffer_subst(buffer *b, const buffer *patternb, const char **list, int n, struct cond_cache_t *cache);
handler_t pcre_keyvalue_buffer_process(pcre_keyvalue_buffer *kvb, pcre_keyvalue_ctx *ctx, buffer *input, buffer *result);
#endif

126
src/mod_redirect.c

@ -7,64 +7,41 @@
#include "plugin.h"
#include "response.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_PCRE_H
#include <pcre.h>
#endif
typedef struct {
pcre_keyvalue_buffer *redirect;
data_config *context; /* to which apply me */
unsigned short redirect_code;
} plugin_config;
typedef struct {
PLUGIN_DATA;
buffer *location;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
INIT_FUNC(mod_redirect_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->location = buffer_init();
return p;
return calloc(1, sizeof(plugin_data));
}
FREE_FUNC(mod_redirect_free) {
plugin_data *p = p_d;
if (!p) return HANDLER_GO_ON;
if (p->config_storage) {
size_t i;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
if (NULL == s) continue;
pcre_keyvalue_buffer_free(s->redirect);
free(s);
}
free(p->config_storage);
}
buffer_free(p->location);
free(p);
return HANDLER_GO_ON;
}
@ -103,6 +80,8 @@ SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
return HANDLER_ERROR;
}
if (s->redirect_code < 100 || s->redirect_code >= 1000) s->redirect_code = 301;
if (NULL == (du = array_get_element(config->value, "url.redirect"))) {
/* no url.redirect defined */
continue;
@ -117,12 +96,10 @@ SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
}
for (j = 0; j < da->value->used; j++) {
if (0 != pcre_keyvalue_buffer_append(srv, s->redirect,
((data_string *)(da->value->data[j]))->key->ptr,
((data_string *)(da->value->data[j]))->value->ptr)) {
data_string *ds = (data_string *)da->value->data[j];
if (0 != pcre_keyvalue_buffer_append(srv, s->redirect, ds->key, ds->value)) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"pcre-compile failed for", da->value->data[j]->key);
"pcre-compile failed for", ds->key);
return HANDLER_ERROR;
}
}
@ -130,7 +107,7 @@ SETDEFAULTS_FUNC(mod_redirect_set_defaults) {
return HANDLER_GO_ON;
}
#ifdef HAVE_PCRE_H
static int mod_redirect_patch_connection(server *srv, connection *con, plugin_data *p) {
size_t i, j;
plugin_config *s = p->config_storage[0];
@ -162,70 +139,37 @@ static int mod_redirect_patch_connection(server *srv, connection *con, plugin_da
return 0;
}
#endif
static handler_t mod_redirect_uri_handler(server *srv, connection *con, void *p_data) {
#ifdef HAVE_PCRE_H
plugin_data *p = p_data;
cond_cache_t *cache;
size_t i;
/*
* REWRITE URL
*
* e.g. redirect /base/ to /index.php?section=base
*
*/
mod_redirect_patch_connection(srv, con, p);
cache = p->conf.context ? &con->cond_cache[p->conf.context->context_ndx] : NULL;
for (i = 0; i < p->conf.redirect->used; i++) {
pcre_keyvalue *kv = p->conf.redirect->kv[i];
# define N 10
int ovec[N * 3];
int n = pcre_exec(kv->key, kv->key_extra, CONST_BUF_LEN(con->request.uri), 0, 0, ovec, 3 * N);
if (n < 0) {
if (n != PCRE_ERROR_NOMATCH) {
log_error_write(srv, __FILE__, __LINE__, "sd",
"execution error while matching: ", n);
return HANDLER_ERROR;
}
} else if (0 == buffer_string_length(kv->value)) {
/* short-circuit if blank replacement pattern
* (do not attempt to match against remaining redirect rules) */
return HANDLER_GO_ON;
} else {
const char **list;
/* it matched */
pcre_get_substring_list(con->request.uri->ptr, ovec, n, &list);
pcre_keyvalue_buffer_subst(p->location, kv->value, list, n, cache);
pcre_free(list);
response_header_insert(srv, con, CONST_STR_LEN("Location"), CONST_BUF_LEN(p->location));
con->http_status = p->conf.redirect_code > 99 && p->conf.redirect_code < 1000 ? p->conf.redirect_code : 301;
con->mode = DIRECT;
con->file_finished = 1;
return HANDLER_FINISHED;
}
}
#undef N
#else
UNUSED(srv);
UNUSED(con);
UNUSED(p_data);
#endif
return HANDLER_GO_ON;
URIHANDLER_FUNC(mod_redirect_uri_handler) {
plugin_data *p = p_d;
pcre_keyvalue_ctx ctx;
handler_t rc;
mod_redirect_patch_connection(srv, con, p);
ctx.cache = p->conf.context
? &con->cond_cache[p->conf.context->context_ndx]
: NULL;
/* redirect URL on match
* e.g. redirect /base/ to /index.php?section=base
*/
rc = pcre_keyvalue_buffer_process(p->conf.redirect, &ctx,
con->request.uri, srv->tmp_buf);
if (HANDLER_FINISHED == rc) {
response_header_insert(srv, con, CONST_STR_LEN("Location"),
CONST_BUF_LEN(srv->tmp_buf));
con->http_status = p->conf.redirect_code;
con->mode = DIRECT;
con->file_finished = 1;
}
else if (HANDLER_ERROR == rc) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"pcre_exec() error while processing uri:",
con->request.uri);
}
return rc;
}
int mod_redirect_plugin_init(plugin *p);
int mod_redirect_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;

245
src/mod_rewrite.c

@ -7,33 +7,14 @@
#include "plugin.h"
#include "stat_cache.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_PCRE_H
#include <pcre.h>
typedef struct {
pcre *key;
buffer *value;
int once;
} rewrite_rule;
typedef struct {
rewrite_rule **ptr;
size_t used;
size_t size;
} rewrite_rule_buffer;
typedef struct {
rewrite_rule_buffer *rewrite;
rewrite_rule_buffer *rewrite_NF;
pcre_keyvalue_buffer *rewrite;
pcre_keyvalue_buffer *rewrite_NF;
data_config *context, *context_NF; /* to which apply me */
int rewrite_repeat_idx, rewrite_NF_repeat_idx;
} plugin_config;
typedef struct {
@ -43,10 +24,7 @@ typedef struct {
typedef struct {
PLUGIN_DATA;
buffer *match_buf;
plugin_config **config_storage;
plugin_config conf;
} plugin_data;
@ -65,109 +43,31 @@ static void handler_ctx_free(handler_ctx *hctx) {
free(hctx);
}
static rewrite_rule_buffer *rewrite_rule_buffer_init(void) {
rewrite_rule_buffer *kvb;
kvb = calloc(1, sizeof(*kvb));
return kvb;
}
static int rewrite_rule_buffer_append(rewrite_rule_buffer *kvb, buffer *key, buffer *value, int once) {
size_t i;
const char *errptr;
int erroff;
if (!key) return -1;
if (kvb->size == 0) {
kvb->size = 4;
kvb->used = 0;
kvb->ptr = malloc(kvb->size * sizeof(*kvb->ptr));
for(i = 0; i < kvb->size; i++) {
kvb->ptr[i] = calloc(1, sizeof(**kvb->ptr));
}
} else if (kvb->used == kvb->size) {
kvb->size += 4;
kvb->ptr = realloc(kvb->ptr, kvb->size * sizeof(*kvb->ptr));
for(i = kvb->used; i < kvb->size; i++) {
kvb->ptr[i] = calloc(1, sizeof(**kvb->ptr));
}
}
if (NULL == (kvb->ptr[kvb->used]->key = pcre_compile(key->ptr,
0, &errptr, &erroff, NULL))) {
return -1;
}
kvb->ptr[kvb->used]->value = buffer_init();
buffer_copy_buffer(kvb->ptr[kvb->used]->value, value);
kvb->ptr[kvb->used]->once = once;
kvb->used++;
return 0;
}
static void rewrite_rule_buffer_free(rewrite_rule_buffer *kvb) {
size_t i;
for (i = 0; i < kvb->size; i++) {
if (kvb->ptr[i]->key) pcre_free(kvb->ptr[i]->key);
if (kvb->ptr[i]->value) buffer_free(kvb->ptr[i]->value);
free(kvb->ptr[i]);
}
if (kvb->ptr) free(kvb->ptr);
free(kvb);
}
INIT_FUNC(mod_rewrite_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->match_buf = buffer_init();
return p;
return calloc(1, sizeof(plugin_data));
}
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 (NULL == s) continue;
rewrite_rule_buffer_free(s->rewrite);
rewrite_rule_buffer_free(s->rewrite_NF);
pcre_keyvalue_buffer_free(s->rewrite);
pcre_keyvalue_buffer_free(s->rewrite_NF);
free(s);
}
free(p->config_storage);
}
free(p);
return HANDLER_GO_ON;
}
static int parse_config_entry(server *srv, array *ca, rewrite_rule_buffer *kvb, const char *option, size_t olen, int once) {
static int parse_config_entry(server *srv, array *ca, pcre_keyvalue_buffer *kvb, const char *option, size_t olen) {
data_unset *du;
if (NULL != (du = array_get_element_klen(ca, option, olen))) {
@ -183,12 +83,10 @@ static int parse_config_entry(server *srv, array *ca, rewrite_rule_buffer *kvb,
}
for (j = 0; j < da->value->used; j++) {
if (0 != rewrite_rule_buffer_append(kvb,
((data_string *)(da->value->data[j]))->key,
((data_string *)(da->value->data[j]))->value,
once)) {
data_string *ds = (data_string *)da->value->data[j];
if (0 != pcre_keyvalue_buffer_append(srv, kvb, ds->key, ds->value)) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"pcre-compile failed for", da->value->data[j]->key);
"pcre-compile failed for", ds->key);
return HANDLER_ERROR;
}
}
@ -196,18 +94,6 @@ static int parse_config_entry(server *srv, array *ca, rewrite_rule_buffer *kvb,
return 0;
}
#else
static int parse_config_entry(server *srv, array *ca, const char *option, size_t olen) {
static int logged_message = 0;
if (logged_message) return 0;
if (NULL != array_get_element_klen(ca, option, olen)) {
logged_message = 1;
log_error_write(srv, __FILE__, __LINE__, "s",
"pcre support is missing, please install libpcre and the headers");
}
return 0;
}
#endif
SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
size_t i = 0;
@ -235,48 +121,39 @@ SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
};
#ifdef HAVE_PCRE_H
plugin_data *p = p_d;
if (!p) return HANDLER_ERROR;
/* 0 */
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
#else
UNUSED(p_d);
#endif
for (i = 0; i < srv->config_context->used; i++) {
data_config const* config = (data_config const*)srv->config_context->data[i];
#ifdef HAVE_PCRE_H
plugin_config *s;
s = calloc(1, sizeof(plugin_config));
s->rewrite = rewrite_rule_buffer_init();
s->rewrite_NF = rewrite_rule_buffer_init();
s->rewrite = pcre_keyvalue_buffer_init();
s->rewrite_NF = pcre_keyvalue_buffer_init();
p->config_storage[i] = s;
#endif
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
return HANDLER_ERROR;
}
#ifndef HAVE_PCRE_H
# define parse_config_entry(srv, ca, x, option, y) parse_config_entry(srv, ca, option)
#endif
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-once"), 1);
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-final"), 1);
parse_config_entry(srv, config->value, s->rewrite_NF, CONST_STR_LEN("url.rewrite-if-not-file"), 1);
parse_config_entry(srv, config->value, s->rewrite_NF, CONST_STR_LEN("url.rewrite-repeat-if-not-file"), 0);
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite"), 1);
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-repeat"), 0);
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-once"));
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-final"));
parse_config_entry(srv, config->value, s->rewrite_NF, CONST_STR_LEN("url.rewrite-if-not-file"));
s->rewrite_NF_repeat_idx = (int)s->rewrite_NF->used;
parse_config_entry(srv, config->value, s->rewrite_NF, CONST_STR_LEN("url.rewrite-repeat-if-not-file"));
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite"));
s->rewrite_repeat_idx = (int)s->rewrite->used;
parse_config_entry(srv, config->value, s->rewrite, CONST_STR_LEN("url.rewrite-repeat"));
}
return HANDLER_GO_ON;
}
#ifdef HAVE_PCRE_H
#define PATCH(x) \
p->conf.x = s->x;
static int mod_rewrite_patch_connection(server *srv, connection *con, plugin_data *p) {
@ -287,6 +164,8 @@ static int mod_rewrite_patch_connection(server *srv, connection *con, plugin_dat
PATCH(rewrite_NF);
p->conf.context = NULL;
p->conf.context_NF = NULL;
PATCH(rewrite_repeat_idx);
PATCH(rewrite_NF_repeat_idx);
/* skip the first, the global context */
for (i = 1; i < srv->config_context->used; i++) {
@ -303,21 +182,27 @@ static int mod_rewrite_patch_connection(server *srv, connection *con, plugin_dat
if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite"))) {
PATCH(rewrite);
p->conf.context = dc;
PATCH(rewrite_repeat_idx);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-once"))) {
PATCH(rewrite);
p->conf.context = dc;
PATCH(rewrite_repeat_idx);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-repeat"))) {
PATCH(rewrite);
p->conf.context = dc;
PATCH(rewrite_NF_repeat_idx);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-if-not-file"))) {
PATCH(rewrite_NF);
p->conf.context_NF = dc;
PATCH(rewrite_repeat_idx);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-repeat-if-not-file"))) {
PATCH(rewrite_NF);
p->conf.context_NF = dc;
PATCH(rewrite_NF_repeat_idx);
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("url.rewrite-final"))) {
PATCH(rewrite);
p->conf.context = dc;
PATCH(rewrite_repeat_idx);
}
}
}
@ -338,10 +223,10 @@ URIHANDLER_FUNC(mod_rewrite_con_reset) {
return HANDLER_GO_ON;
}
static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data *p, rewrite_rule_buffer *kvb) {
size_t i;
cond_cache_t *cache;
static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data *p, pcre_keyvalue_buffer *kvb, int repeat_idx) {
handler_ctx *hctx;
pcre_keyvalue_ctx ctx;
handler_t rc;
if (con->plugin_ctx[p->id]) {
hctx = con->plugin_ctx[p->id];
@ -357,55 +242,26 @@ static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data
if (hctx->state == REWRITE_STATE_FINISHED) return HANDLER_GO_ON;
}
cache = p->conf.context ? &con->cond_cache[p->conf.context->context_ndx] : NULL;
buffer_copy_buffer(p->match_buf, con->request.uri);
for (i = 0; i < kvb->used; i++) {
pcre *match;
size_t pattern_len;
int n;
rewrite_rule *rule = kvb->ptr[i];
# define N 10
int ovec[N * 3];
match = rule->key;
pattern_len = buffer_string_length(rule->value);
ctx.cache = p->conf.context ? &con->cond_cache[p->conf.context->context_ndx] : NULL;
if ((n = pcre_exec(match, NULL, CONST_BUF_LEN(p->match_buf), 0, 0, ovec, 3 * N)) < 0) {
if (n != PCRE_ERROR_NOMATCH) {
log_error_write(srv, __FILE__, __LINE__, "sd",
"execution error while matching: ", n);
return HANDLER_ERROR;
}
} else if (0 == pattern_len) {
/* short-circuit if blank replacement pattern
* (do not attempt to match against remaining rewrite rules) */
return HANDLER_GO_ON;
rc = pcre_keyvalue_buffer_process(kvb, &ctx, con->request.uri, srv->tmp_buf);
if (HANDLER_FINISHED == rc) {
buffer_copy_buffer(con->request.uri, srv->tmp_buf);
if (con->plugin_ctx[p->id] == NULL) {
hctx = handler_ctx_init();
con->plugin_ctx[p->id] = hctx;
} else {
const char **list;
/* it matched */
pcre_get_substring_list(p->match_buf->ptr, ovec, n, &list);
pcre_keyvalue_buffer_subst(con->request.uri, rule->value, list, n, cache);
pcre_free(list);
if (con->plugin_ctx[p->id] == NULL) {
hctx = handler_ctx_init();
con->plugin_ctx[p->id] = hctx;
} else {
hctx = con->plugin_ctx[p->id];
}
if (rule->once) hctx->state = REWRITE_STATE_FINISHED;
return HANDLER_COMEBACK;
hctx = con->plugin_ctx[p->id];
}
#undef N
if (ctx.m < repeat_idx) hctx->state = REWRITE_STATE_FINISHED;
rc = HANDLER_COMEBACK;
}
return HANDLER_GO_ON;
else if (HANDLER_ERROR == rc) {
log_error_write(srv, __FILE__, __LINE__, "sb",
"pcre_exec() error while processing uri:",
con->request.uri);
}
return rc;
}
URIHANDLER_FUNC(mod_rewrite_physical) {
@ -426,7 +282,7 @@ URIHANDLER_FUNC(mod_rewrite_physical) {
if (S_ISREG(sce->st.st_mode)) return HANDLER_GO_ON;
}
switch(r = process_rewrite_rules(srv, con, p, p->conf.rewrite_NF)) {
switch(r = process_rewrite_rules(srv, con, p, p->conf.rewrite_NF, p->conf.rewrite_NF_repeat_idx)) {
case HANDLER_COMEBACK:
buffer_reset(con->physical.path);
/* fall through */
@ -444,16 +300,14 @@ URIHANDLER_FUNC(mod_rewrite_uri_handler) {
if (!p->conf.rewrite) return HANDLER_GO_ON;
return process_rewrite_rules(srv, con, p, p->conf.rewrite);
return process_rewrite_rules(srv, con, p, p->conf.rewrite, p->conf.rewrite_repeat_idx);
}
#endif
int mod_rewrite_plugin_init(plugin *p);
int mod_rewrite_plugin_init(plugin *p) {
p->version = LIGHTTPD_VERSION_ID;
p->name = buffer_init_string("rewrite");
#ifdef HAVE_PCRE_H
p->init = mod_rewrite_init;
/* it has to stay _raw as we are matching on uri + querystring
*/
@ -462,7 +316,6 @@ int mod_rewrite_plugin_init(plugin *p) {
p->handle_physical = mod_rewrite_physical;
p->cleanup = mod_rewrite_free;
p->connection_reset = mod_rewrite_con_reset;
#endif
p->set_defaults = mod_rewrite_set_defaults;
p->data = NULL;

Loading…
Cancel
Save