[core] merge redirect/rewrite pattern substitution
merge redirect/rewrite pattern substitution function (share code)personal/stbuehler/fix-fdevent
parent
a5a2654bd4
commit
cb371557e5
|
@ -304,7 +304,7 @@ typedef enum {
|
|||
COND_RESULT_TRUE /* active */
|
||||
} cond_result_t;
|
||||
|
||||
typedef struct {
|
||||
typedef struct cond_cache_t {
|
||||
/* current result (with preconditions) */
|
||||
cond_result_t result;
|
||||
/* result without preconditions (must never be "skip") */
|
||||
|
|
|
@ -615,17 +615,3 @@ int config_check_cond(server *srv, connection *con, data_config *dc) {
|
|||
}
|
||||
return (config_check_cond_cached(srv, con, dc) == COND_RESULT_TRUE);
|
||||
}
|
||||
|
||||
int config_append_cond_match_buffer(connection *con, data_config *dc, buffer *buf, int n)
|
||||
{
|
||||
cond_cache_t *cache = &con->cond_cache[dc->context_ndx];
|
||||
if (n >= cache->patterncount) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
n <<= 1; /* n *= 2 */
|
||||
buffer_append_string_len(buf,
|
||||
cache->comp_value->ptr + cache->matches[n],
|
||||
cache->matches[n + 1] - cache->matches[n]);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ static const keyvalue http_status_body[] = {
|
|||
};
|
||||
|
||||
|
||||
const char *keyvalue_get_value(const keyvalue *kv, int k) {
|
||||
static const char *keyvalue_get_value(const keyvalue *kv, int k) {
|
||||
int i;
|
||||
for (i = 0; kv[i].value; i++) {
|
||||
if (kv[i].key == k) return kv[i].value;
|
||||
|
@ -134,7 +134,7 @@ const char *keyvalue_get_value(const keyvalue *kv, int k) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int keyvalue_get_key(const keyvalue *kv, const char *s) {
|
||||
static int keyvalue_get_key(const keyvalue *kv, const char *s) {
|
||||
int i;
|
||||
for (i = 0; kv[i].value; i++) {
|
||||
if (0 == strcmp(kv[i].value, s)) return kv[i].key;
|
||||
|
@ -258,3 +258,50 @@ 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) {
|
||||
const char *pattern = patternb->ptr;
|
||||
const size_t pattern_len = buffer_string_length(patternb);
|
||||
size_t start = 0;
|
||||
|
||||
/* search for $... or %... pattern substitutions */
|
||||
|
||||
buffer_reset(b);
|
||||
|
||||
for (size_t k = 0; k + 1 < pattern_len; ++k) {
|
||||
if (pattern[k] == '$' || pattern[k] == '%') {
|
||||
size_t num = pattern[k + 1] - '0';
|
||||
|
||||
buffer_append_string_len(b, pattern + start, k - start);
|
||||
|
||||
if (!light_isdigit((unsigned char)pattern[k + 1])) {
|
||||
/* enable escape: "%%" => "%", "%a" => "%a", "$$" => "$" */
|
||||
buffer_append_string_len(b, pattern+k, pattern[k] == pattern[k+1] ? 1 : 2);
|
||||
} else if (pattern[k] == '$') {
|
||||
/* n is always > 0 */
|
||||
if (num < (size_t)n) {
|
||||
buffer_append_string(b, list[num]);
|
||||
}
|
||||
} else if (cache) {
|
||||
if (num < (size_t)cache->patterncount) {
|
||||
num <<= 1; /* n *= 2 */
|
||||
buffer_append_string_len(b,
|
||||
cache->comp_value->ptr + cache->matches[num],
|
||||
cache->matches[num + 1] - cache->matches[num]);
|
||||
}
|
||||
} else {
|
||||
#if 0
|
||||
/* we have no context, we are global */
|
||||
log_error_write(srv, __FILE__, __LINE__, "ss",
|
||||
"used a redirect/rewrite containing a %[0-9]+ in the global scope, ignored:",
|
||||
pattern);
|
||||
#endif
|
||||
}
|
||||
|
||||
k++;
|
||||
start = k + 1;
|
||||
}
|
||||
}
|
||||
|
||||
buffer_append_string_len(b, pattern + start, pattern_len - start);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#endif
|
||||
|
||||
struct server;
|
||||
struct cond_cache_t;
|
||||
|
||||
/* sources:
|
||||
* - [RFC2616], Section 9
|
||||
|
@ -90,11 +91,9 @@ const char *get_http_status_body_name(int i);
|
|||
int get_http_version_key(const char *s);
|
||||
http_method_t get_http_method_key(const char *s);
|
||||
|
||||
const char *keyvalue_get_value(const keyvalue *kv, int k);
|
||||
int keyvalue_get_key(const keyvalue *kv, 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);
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -20,7 +20,6 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
PLUGIN_DATA;
|
||||
buffer *match_buf;
|
||||
buffer *location;
|
||||
|
||||
plugin_config **config_storage;
|
||||
|
@ -33,7 +32,6 @@ INIT_FUNC(mod_redirect_init) {
|
|||
|
||||
p = calloc(1, sizeof(*p));
|
||||
|
||||
p->match_buf = buffer_init();
|
||||
p->location = buffer_init();
|
||||
|
||||
return p;
|
||||
|
@ -59,7 +57,6 @@ FREE_FUNC(mod_redirect_free) {
|
|||
}
|
||||
|
||||
|
||||
buffer_free(p->match_buf);
|
||||
buffer_free(p->location);
|
||||
|
||||
free(p);
|
||||
|
@ -165,6 +162,7 @@ static int mod_redirect_patch_connection(server *srv, connection *con, plugin_da
|
|||
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;
|
||||
|
||||
/*
|
||||
|
@ -175,78 +173,31 @@ static handler_t mod_redirect_uri_handler(server *srv, connection *con, void *p_
|
|||
*/
|
||||
|
||||
mod_redirect_patch_connection(srv, con, p);
|
||||
|
||||
buffer_copy_buffer(p->match_buf, con->request.uri);
|
||||
cache = p->conf.context ? &con->cond_cache[p->conf.context->context_ndx] : NULL;
|
||||
|
||||
for (i = 0; i < p->conf.redirect->used; i++) {
|
||||
pcre *match;
|
||||
pcre_extra *extra;
|
||||
const char *pattern;
|
||||
size_t pattern_len;
|
||||
int n;
|
||||
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);
|
||||
|
||||
match = kv->key;
|
||||
extra = kv->key_extra;
|
||||
pattern = kv->value->ptr;
|
||||
pattern_len = buffer_string_length(kv->value);
|
||||
|
||||
if ((n = pcre_exec(match, extra, CONST_BUF_LEN(p->match_buf), 0, 0, ovec, 3 * N)) < 0) {
|
||||
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 == pattern_len) {
|
||||
} 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;
|
||||
size_t start;
|
||||
size_t k;
|
||||
|
||||
/* it matched */
|
||||
pcre_get_substring_list(p->match_buf->ptr, ovec, n, &list);
|
||||
pcre_get_substring_list(con->request.uri->ptr, ovec, n, &list);
|
||||
|
||||
/* search for $[0-9] */
|
||||
|
||||
buffer_reset(p->location);
|
||||
|
||||
start = 0;
|
||||
for (k = 0; k + 1 < pattern_len; k++) {
|
||||
if (pattern[k] == '$' || pattern[k] == '%') {
|
||||
/* got one */
|
||||
|
||||
size_t num = pattern[k + 1] - '0';
|
||||
|
||||
buffer_append_string_len(p->location, pattern + start, k - start);
|
||||
|
||||
if (!isdigit((unsigned char)pattern[k + 1])) {
|
||||
/* enable escape: "%%" => "%", "%a" => "%a", "$$" => "$" */
|
||||
buffer_append_string_len(p->location, pattern+k, pattern[k] == pattern[k+1] ? 1 : 2);
|
||||
} else if (pattern[k] == '$') {
|
||||
/* n is always > 0 */
|
||||
if (num < (size_t)n) {
|
||||
buffer_append_string(p->location, list[num]);
|
||||
}
|
||||
} else if (p->conf.context == NULL) {
|
||||
/* we have no context, we are global */
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb",
|
||||
"used a rewrite containing a %[0-9]+ in the global scope, ignored:",
|
||||
kv->value);
|
||||
} else {
|
||||
config_append_cond_match_buffer(con, p->conf.context, p->location, num);
|
||||
}
|
||||
|
||||
k++;
|
||||
start = k + 1;
|
||||
}
|
||||
}
|
||||
|
||||
buffer_append_string_len(p->location, pattern + start, pattern_len - start);
|
||||
pcre_keyvalue_buffer_subst(p->location, kv->value, list, n, cache);
|
||||
|
||||
pcre_free(list);
|
||||
|
||||
|
|
|
@ -337,6 +337,7 @@ URIHANDLER_FUNC(mod_rewrite_con_reset) {
|
|||
|
||||
static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data *p, rewrite_rule_buffer *kvb) {
|
||||
size_t i;
|
||||
cond_cache_t *cache;
|
||||
handler_ctx *hctx;
|
||||
|
||||
if (con->plugin_ctx[p->id]) {
|
||||
|
@ -353,11 +354,11 @@ 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;
|
||||
const char *pattern;
|
||||
size_t pattern_len;
|
||||
int n;
|
||||
rewrite_rule *rule = kvb->ptr[i];
|
||||
|
@ -365,7 +366,6 @@ static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data
|
|||
int ovec[N * 3];
|
||||
|
||||
match = rule->key;
|
||||
pattern = rule->value->ptr;
|
||||
pattern_len = buffer_string_length(rule->value);
|
||||
|
||||
if ((n = pcre_exec(match, NULL, CONST_BUF_LEN(p->match_buf), 0, 0, ovec, 3 * N)) < 0) {
|
||||
|
@ -380,49 +380,11 @@ static handler_t process_rewrite_rules(server *srv, connection *con, plugin_data
|
|||
return HANDLER_GO_ON;
|
||||
} else {
|
||||
const char **list;
|
||||
size_t start;
|
||||
size_t k;
|
||||
|
||||
/* it matched */
|
||||
pcre_get_substring_list(p->match_buf->ptr, ovec, n, &list);
|
||||
|
||||
/* search for $[0-9] */
|
||||
|
||||
buffer_reset(con->request.uri);
|
||||
|
||||
start = 0;
|
||||
for (k = 0; k+1 < pattern_len; k++) {
|
||||
if (pattern[k] == '$' || pattern[k] == '%') {
|
||||
/* got one */
|
||||
|
||||
size_t num = pattern[k + 1] - '0';
|
||||
|
||||
buffer_append_string_len(con->request.uri, pattern + start, k - start);
|
||||
|
||||
if (!isdigit((unsigned char)pattern[k + 1])) {
|
||||
/* enable escape: "%%" => "%", "%a" => "%a", "$$" => "$" */
|
||||
buffer_append_string_len(con->request.uri, pattern+k, pattern[k] == pattern[k+1] ? 1 : 2);
|
||||
} else if (pattern[k] == '$') {
|
||||
/* n is always > 0 */
|
||||
if (num < (size_t)n) {
|
||||
buffer_append_string(con->request.uri, list[num]);
|
||||
}
|
||||
} else if (p->conf.context == NULL) {
|
||||
/* we have no context, we are global */
|
||||
log_error_write(srv, __FILE__, __LINE__, "sb",
|
||||
"used a redirect containing a %[0-9]+ in the global scope, ignored:",
|
||||
rule->value);
|
||||
|
||||
} else {
|
||||
config_append_cond_match_buffer(con, p->conf.context, con->request.uri, num);
|
||||
}
|
||||
|
||||
k++;
|
||||
start = k + 1;
|
||||
}
|
||||
}
|
||||
|
||||
buffer_append_string_len(con->request.uri, pattern + start, pattern_len - start);
|
||||
pcre_keyvalue_buffer_subst(con->request.uri, rule->value, list, n, cache);
|
||||
|
||||
pcre_free(list);
|
||||
|
||||
|
|
|
@ -94,6 +94,5 @@ handler_t plugins_call_cleanup(server *srv);
|
|||
int config_insert_values_global(server *srv, array *ca, const config_values_t *cv, config_scope_type_t scope);
|
||||
int config_insert_values_internal(server *srv, array *ca, const config_values_t *cv, config_scope_type_t scope);
|
||||
int config_check_cond(server *srv, connection *con, data_config *dc);
|
||||
int config_append_cond_match_buffer(connection *con, data_config *dc, buffer *buf, int n);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue