2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
2018-04-22 00:21:54 +00:00
|
|
|
#include "keyvalue.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
2018-07-01 00:11:53 +00:00
|
|
|
#include "burl.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
2009-10-12 21:49:09 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2019-11-10 16:27:59 +00:00
|
|
|
pcre_keyvalue_buffer *rewrite;
|
|
|
|
pcre_keyvalue_buffer *rewrite_NF;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
enum { REWRITE_STATE_REWRITTEN = 1024, REWRITE_STATE_FINISHED = 2048}; /*flags*/
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2019-11-10 16:27:59 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_rewrite_init) {
|
2019-11-10 16:27:59 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 08:39:40 +00:00
|
|
|
FREE_FUNC(mod_rewrite_free) {
|
|
|
|
plugin_data * const p = p_d;
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == p->cvlist) return;
|
|
|
|
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
/* kvb value might be copied in multiple directives; free only once */
|
|
|
|
pcre_keyvalue_buffer *kvb = NULL, *kvb_NF = NULL;
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* url.rewrite-once */
|
|
|
|
case 1: /* url.rewrite-final */
|
|
|
|
case 2: /* url.rewrite */
|
|
|
|
case 3: /* url.rewrite-repeat */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
kvb = cpv->v.v;
|
|
|
|
break;
|
|
|
|
case 4: /* url.rewrite-if-not-file */
|
|
|
|
case 5: /* url.rewrite-repeat-if-not-file */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
kvb_NF = cpv->v.v;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kvb) pcre_keyvalue_buffer_free(kvb);
|
|
|
|
if (kvb_NF) pcre_keyvalue_buffer_free(kvb_NF);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
static void mod_rewrite_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* url.rewrite-once */
|
|
|
|
case 1: /* url.rewrite-final */
|
|
|
|
case 2: /* url.rewrite */
|
|
|
|
case 3: /* url.rewrite-repeat */
|
|
|
|
/*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here in mod_rewrite*/
|
|
|
|
pconf->rewrite = cpv->v.v;
|
|
|
|
break;
|
|
|
|
case 4: /* url.rewrite-if-not-file */
|
|
|
|
case 5: /* url.rewrite-repeat-if-not-file */
|
|
|
|
/*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here in mod_rewrite*/
|
|
|
|
pconf->rewrite_NF = cpv->v.v;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
static void mod_rewrite_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_rewrite_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_rewrite_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-11-10 16:27:59 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-11-10 16:27:59 +00:00
|
|
|
mod_rewrite_merge_config(&p->conf, p->cvlist+p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2009-10-26 18:48:26 +00:00
|
|
|
|
2019-12-08 00:15:55 +00:00
|
|
|
static pcre_keyvalue_buffer * mod_rewrite_parse_list(server *srv, const array *a, pcre_keyvalue_buffer *kvb, const int condidx) {
|
2021-09-22 15:24:06 +00:00
|
|
|
const int pcre_jit = config_feature_bool(srv, "server.pcre_jit", 1);
|
2019-11-10 16:27:59 +00:00
|
|
|
int allocated = 0;
|
|
|
|
if (NULL == kvb) {
|
|
|
|
allocated = 1;
|
|
|
|
kvb = pcre_keyvalue_buffer_init();
|
|
|
|
kvb->x0 = (unsigned short)condidx;
|
|
|
|
}
|
|
|
|
|
2019-12-08 05:28:21 +00:00
|
|
|
buffer * const tb = srv->tmp_buf;
|
2019-11-10 16:27:59 +00:00
|
|
|
for (uint32_t j = 0; j < a->used; ++j) {
|
|
|
|
data_string *ds = (data_string *)a->data[j];
|
|
|
|
if (srv->srvconf.http_url_normalize) {
|
2019-12-08 05:28:21 +00:00
|
|
|
pcre_keyvalue_burl_normalize_key(&ds->key, tb);
|
|
|
|
pcre_keyvalue_burl_normalize_value(&ds->value, tb);
|
2019-11-10 16:27:59 +00:00
|
|
|
}
|
2021-03-14 12:09:58 +00:00
|
|
|
if (!pcre_keyvalue_buffer_append(srv->errh, kvb, &ds->key, &ds->value,
|
|
|
|
pcre_jit)) {
|
2019-11-10 16:27:59 +00:00
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"pcre-compile failed for %s", ds->key.ptr);
|
|
|
|
if (allocated) pcre_keyvalue_buffer_free(kvb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvb;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("url.rewrite-once"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("url.rewrite-final"), /* old name => url.rewrite-once */
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("url.rewrite"), /* old name => url.rewrite-once */
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("url.rewrite-repeat"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("url.rewrite-if-not-file"), /* rewrite-once if ENOENT */
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("url.rewrite-repeat-if-not-file"), /* repeat if ENOENT */
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_KVSTRING,
|
2019-11-10 16:27:59 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_rewrite"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* process and validate config directives
|
|
|
|
* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
/* parse directives in specific order to encode repeat_idx in kvb->x1 */
|
|
|
|
config_plugin_value_t *rewrite_once = NULL, *rewrite_repeat = NULL,
|
|
|
|
*rewrite_NF = NULL, *rewrite_repeat_NF = NULL,
|
|
|
|
*rewrite = NULL, *rewrite_final = NULL;
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* url.rewrite-once */
|
|
|
|
rewrite_once = cpv;
|
|
|
|
break;
|
|
|
|
case 1: /* url.rewrite-final */
|
|
|
|
rewrite_final = cpv;
|
|
|
|
break;
|
|
|
|
case 2: /* url.rewrite */
|
|
|
|
rewrite = cpv;
|
|
|
|
break;
|
|
|
|
case 3: /* url.rewrite-repeat */
|
|
|
|
rewrite_repeat = cpv;
|
|
|
|
break;
|
|
|
|
case 4: /* url.rewrite-if-not-file */
|
|
|
|
rewrite_NF = cpv;
|
|
|
|
break;
|
|
|
|
case 5: /* url.rewrite-repeat-if-not-file */
|
|
|
|
rewrite_repeat_NF = cpv;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int condidx = p->cvlist[i].k_id;
|
|
|
|
pcre_keyvalue_buffer *kvb = NULL, *kvb_NF = NULL;
|
|
|
|
|
|
|
|
if ((cpv = rewrite_once)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
kvb = cpv->v.v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cpv = rewrite_final)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
kvb = cpv->v.v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cpv = rewrite)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
kvb = cpv->v.v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kvb) kvb->x1 = (unsigned short)kvb->used; /* repeat_idx */
|
|
|
|
|
|
|
|
if ((cpv = rewrite_repeat)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
/*kvb = cpv->v.v;*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cpv = rewrite_NF)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb_NF, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
kvb_NF = cpv->v.v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kvb_NF) kvb_NF->x1 = (unsigned short)kvb_NF->used; /* repeat_idx */
|
|
|
|
|
|
|
|
if ((cpv = rewrite_repeat_NF)) {
|
2019-12-08 00:15:55 +00:00
|
|
|
cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb_NF, condidx);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (NULL == cpv->v.v) return HANDLER_ERROR;
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
/*kvb_NF = cpv->v.v;*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_rewrite_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2009-10-26 18:48:26 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
URIHANDLER_FUNC(mod_rewrite_con_reset) {
|
2020-01-13 02:51:12 +00:00
|
|
|
r->plugin_ctx[((plugin_data *)p_d)->id] = NULL;
|
2019-11-10 16:27:59 +00:00
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static handler_t process_rewrite_rules(request_st * const r, plugin_data *p, const pcre_keyvalue_buffer *kvb) {
|
2018-07-01 00:11:53 +00:00
|
|
|
struct burl_parts_t burl;
|
2018-04-17 04:53:40 +00:00
|
|
|
pcre_keyvalue_ctx ctx;
|
|
|
|
handler_t rc;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->plugin_ctx[p->id]) {
|
|
|
|
uintptr_t * const hctx = (uintptr_t *)(r->plugin_ctx + p->id);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
if (((++*hctx) & 0x1FF) > 100) {
|
2019-11-17 17:53:16 +00:00
|
|
|
if (0 != kvb->x0) {
|
|
|
|
config_cond_info cfginfo;
|
2020-01-13 02:51:12 +00:00
|
|
|
config_get_config_cond_info(&cfginfo, kvb->x0);
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-17 17:53:16 +00:00
|
|
|
"ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request, "
|
|
|
|
"perhaps you want to use url.rewrite-once instead of "
|
2021-01-28 03:39:39 +00:00
|
|
|
"url.rewrite-repeat (%s)", cfginfo.comp_key);
|
2019-01-21 22:59:44 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-17 17:53:16 +00:00
|
|
|
"ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request");
|
2005-07-23 20:58:46 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
if (*hctx & REWRITE_STATE_FINISHED) return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-21 05:15:51 +00:00
|
|
|
ctx.cache = NULL;
|
|
|
|
if (kvb->x0) { /*(kvb->x0 is context_idx)*/
|
2020-01-09 04:35:39 +00:00
|
|
|
ctx.cond_match_count =
|
2020-01-13 02:51:12 +00:00
|
|
|
r->cond_cache[kvb->x0].patterncount;
|
|
|
|
ctx.cache = r->cond_match + kvb->x0;
|
2019-11-21 05:15:51 +00:00
|
|
|
}
|
2018-07-01 00:11:53 +00:00
|
|
|
ctx.burl = &burl;
|
2020-01-13 02:51:12 +00:00
|
|
|
burl.scheme = &r->uri.scheme;
|
|
|
|
burl.authority = &r->uri.authority;
|
|
|
|
burl.port = sock_addr_get_port(&r->con->srv_socket->addr);
|
2020-01-20 15:56:54 +00:00
|
|
|
burl.path = &r->target; /*(uri-encoded and includes query-part)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
burl.query = &r->uri.query;
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(burl.authority))
|
2020-01-13 02:51:12 +00:00
|
|
|
burl.authority = r->server_name;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer * const tb = r->tmp_buf;
|
|
|
|
rc = pcre_keyvalue_buffer_process(kvb, &ctx, &r->target, tb);
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (HANDLER_FINISHED == rc && !buffer_is_blank(tb) && tb->ptr[0] == '/') {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->target, tb);
|
|
|
|
uintptr_t * const hctx = (uintptr_t *)(r->plugin_ctx + p->id);
|
2019-11-10 16:27:59 +00:00
|
|
|
*hctx |= REWRITE_STATE_REWRITTEN;
|
|
|
|
/*(kvb->x1 is repeat_idx)*/
|
|
|
|
if (ctx.m < kvb->x1) *hctx |= REWRITE_STATE_FINISHED;
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_reset(&r->physical.path);
|
2018-04-17 04:53:40 +00:00
|
|
|
rc = HANDLER_COMEBACK;
|
2009-10-12 21:49:09 +00:00
|
|
|
}
|
2018-07-22 22:34:20 +00:00
|
|
|
else if (HANDLER_FINISHED == rc) {
|
|
|
|
rc = HANDLER_ERROR;
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"mod_rewrite invalid result (not beginning with '/') "
|
2020-01-13 02:51:12 +00:00
|
|
|
"while processing uri: %s", r->target.ptr);
|
2018-07-22 22:34:20 +00:00
|
|
|
}
|
2018-04-17 04:53:40 +00:00
|
|
|
else if (HANDLER_ERROR == rc) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"pcre_exec() error "
|
2020-01-13 02:51:12 +00:00
|
|
|
"while processing uri: %s", r->target.ptr);
|
2018-04-17 04:53:40 +00:00
|
|
|
}
|
|
|
|
return rc;
|
2009-10-12 21:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_rewrite_physical) {
|
2019-11-10 16:27:59 +00:00
|
|
|
plugin_data * const p = p_d;
|
2009-10-12 21:49:09 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (NULL != r->handler_module) return HANDLER_GO_ON;
|
2009-10-12 21:49:09 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_rewrite_patch_config(r, p);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (!p->conf.rewrite_NF || !p->conf.rewrite_NF->used) return HANDLER_GO_ON;
|
2009-10-12 21:49:09 +00:00
|
|
|
|
2019-11-10 16:27:59 +00:00
|
|
|
/* skip if physical.path is a regular file */
|
2020-10-13 17:57:37 +00:00
|
|
|
const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path);
|
|
|
|
if (st && S_ISREG(st->st_mode)) return HANDLER_GO_ON;
|
2009-10-12 21:49:09 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
return process_rewrite_rules(r, p, p->conf.rewrite_NF);
|
2009-10-12 21:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
URIHANDLER_FUNC(mod_rewrite_uri_handler) {
|
2019-11-10 16:27:59 +00:00
|
|
|
plugin_data *p = p_d;
|
2009-10-12 21:49:09 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_rewrite_patch_config(r, p);
|
2019-11-10 16:27:59 +00:00
|
|
|
if (!p->conf.rewrite || !p->conf.rewrite->used) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
return process_rewrite_rules(r, p, p->conf.rewrite);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_rewrite_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_rewrite_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "rewrite";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_rewrite_init;
|
2005-07-26 08:24:32 +00:00
|
|
|
/* it has to stay _raw as we are matching on uri + querystring
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-07-26 08:24:32 +00:00
|
|
|
p->handle_uri_raw = mod_rewrite_uri_handler;
|
2009-10-12 21:49:09 +00:00
|
|
|
p->handle_physical = mod_rewrite_physical;
|
2005-02-20 14:27:00 +00:00
|
|
|
p->cleanup = mod_rewrite_free;
|
2020-07-25 12:23:58 +00:00
|
|
|
p->handle_request_reset = mod_rewrite_con_reset;
|
2009-10-26 18:48:26 +00:00
|
|
|
p->set_defaults = mod_rewrite_set_defaults;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|