2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* plugin config for all request/connections */
|
|
|
|
typedef struct {
|
|
|
|
array *alias;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
/* init the plugin data */
|
|
|
|
INIT_FUNC(mod_alias_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* detroy the plugin data */
|
|
|
|
FREE_FUNC(mod_alias_free) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->config_storage) {
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-28 22:39:30 +00:00
|
|
|
if(!s) continue;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(s->alias);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle plugin config and check values */
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_alias_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2005-02-20 14:27:00 +00:00
|
|
|
{ "alias.url", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_ERROR;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-16 13:07:46 +00:00
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-16 13:07:46 +00:00
|
|
|
s = calloc(1, sizeof(plugin_config));
|
2006-10-04 13:26:23 +00:00
|
|
|
s->alias = array_init();
|
2005-02-20 14:27:00 +00:00
|
|
|
cv[0].destination = s->alias;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->config_storage[i] = s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2005-08-09 06:42:33 +00:00
|
|
|
if (s->alias->used >= 2) {
|
|
|
|
const array *a = s->alias;
|
|
|
|
size_t j, k;
|
|
|
|
|
|
|
|
for (j = 0; j < a->used; j ++) {
|
|
|
|
const buffer *prefix = a->data[a->sorted[j]]->key;
|
|
|
|
for (k = j + 1; k < a->used; k ++) {
|
|
|
|
const buffer *key = a->data[a->sorted[k]]->key;
|
|
|
|
|
|
|
|
if (key->used < prefix->used) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (memcmp(key->ptr, prefix->ptr, prefix->used - 1) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* ok, they have same prefix. check position */
|
|
|
|
if (a->sorted[j] < a->sorted[k]) {
|
2008-10-16 12:42:15 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SBSBS",
|
|
|
|
"url.alias: `", key, "' will never match as `", prefix, "' matched first");
|
2005-08-09 06:42:33 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PATCH(x) \
|
|
|
|
p->conf.x = s->x;
|
2005-08-08 10:27:07 +00:00
|
|
|
static int mod_alias_patch_connection(server *srv, connection *con, plugin_data *p) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i, j;
|
2005-08-08 10:27:07 +00:00
|
|
|
plugin_config *s = p->config_storage[0];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
PATCH(alias);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* skip the first, the global context */
|
|
|
|
for (i = 1; i < srv->config_context->used; i++) {
|
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
2005-08-08 10:27:07 +00:00
|
|
|
s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* condition didn't match */
|
|
|
|
if (!config_check_cond(srv, con, dc)) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* merge config */
|
|
|
|
for (j = 0; j < dc->value->used; j++) {
|
|
|
|
data_unset *du = dc->value->data[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("alias.url"))) {
|
|
|
|
PATCH(alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef PATCH
|
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
PHYSICALPATH_FUNC(mod_alias_physical_handler) {
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_data *p = p_d;
|
2005-08-08 09:42:27 +00:00
|
|
|
int uri_len, basedir_len;
|
|
|
|
char *uri_ptr;
|
2005-08-08 10:27:07 +00:00
|
|
|
size_t k;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
if (con->physical.path->used == 0) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
mod_alias_patch_connection(srv, con, p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
/* not to include the tailing slash */
|
|
|
|
basedir_len = (con->physical.basedir->used - 1) - 1;
|
|
|
|
uri_len = con->physical.path->used - 1 - basedir_len;
|
|
|
|
uri_ptr = con->physical.path->ptr + basedir_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (k = 0; k < p->conf.alias->used; k++) {
|
|
|
|
data_string *ds = (data_string *)p->conf.alias->data[k];
|
|
|
|
int alias_len = ds->key->used - 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (alias_len > uri_len) continue;
|
|
|
|
if (ds->key->used == 0) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-02-24 11:57:26 +00:00
|
|
|
if (0 == (con->conf.force_lowercase_filenames ?
|
2006-03-01 21:20:24 +00:00
|
|
|
strncasecmp(uri_ptr, ds->key->ptr, alias_len) :
|
2006-02-24 11:57:26 +00:00
|
|
|
strncmp(uri_ptr, ds->key->ptr, alias_len))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* matched */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 09:42:27 +00:00
|
|
|
buffer_copy_string_buffer(con->physical.basedir, ds->value);
|
|
|
|
buffer_copy_string_buffer(srv->tmp_buf, ds->value);
|
|
|
|
buffer_append_string(srv->tmp_buf, uri_ptr + alias_len);
|
|
|
|
buffer_copy_string_buffer(con->physical.path, srv->tmp_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not found */
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this function is called at dlopen() time and inits the callbacks */
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_alias_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_alias_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
|
|
|
p->name = buffer_init_string("alias");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_alias_init;
|
2005-08-08 09:42:27 +00:00
|
|
|
p->handle_physical= mod_alias_physical_handler;
|
2005-02-20 14:27:00 +00:00
|
|
|
p->set_defaults = mod_alias_set_defaults;
|
|
|
|
p->cleanup = mod_alias_free;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->data = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|