|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
#include "base.h"
|
|
#include "log.h"
|
|
#include "buffer.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
/*
|
|
* Copyright (c) 2007 John Brooks (special@inspircd.org)
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
/* plugin config for all request/connections */
|
|
|
|
typedef struct {
|
|
array *suffixes;
|
|
} plugin_config;
|
|
|
|
typedef struct {
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config **config_storage;
|
|
plugin_config conf;
|
|
} plugin_data;
|
|
|
|
/* init the plugin data */
|
|
INIT_FUNC(mod_autoext_init)
|
|
{
|
|
plugin_data *p;
|
|
p = calloc(1, sizeof(*p));
|
|
return p;
|
|
}
|
|
|
|
/* detroy the plugin data */
|
|
FREE_FUNC(mod_autoext_free)
|
|
{
|
|
plugin_data *p = p_d;
|
|
|
|
UNUSED(srv);
|
|
|
|
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 (!s)
|
|
continue;
|
|
|
|
array_free(s->suffixes);
|
|
free(s);
|
|
}
|
|
free(p->config_storage);
|
|
}
|
|
|
|
free(p);
|
|
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
/* handle plugin config and check values */
|
|
|
|
SETDEFAULTS_FUNC(mod_autoext_set_defaults)
|
|
{
|
|
plugin_data *p = p_d;
|
|
size_t i = 0;
|
|
|
|
config_values_t cv[] = {
|
|
{ "autoext.suffixes", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
};
|
|
|
|
if (!p)
|
|
return HANDLER_ERROR;
|
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config*));
|
|
|
|
for (i = 0; i < srv->config_context->used; i++)
|
|
{
|
|
plugin_config *s;
|
|
|
|
s = calloc(1, sizeof(plugin_config));
|
|
s->suffixes = array_init();
|
|
|
|
cv[0].destination = s->suffixes;
|
|
|
|
p->config_storage[i] = s;
|
|
|
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv))
|
|
return HANDLER_ERROR;
|
|
}
|
|
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
#define PATCH(x) p->conf.x = s->x;
|
|
|
|
static int mod_autoext_patch_connection(server *srv, connection *con, plugin_data *p)
|
|
{
|
|
size_t i, j;
|
|
plugin_config *s = p->config_storage[0];
|
|
|
|
PATCH(suffixes);
|
|
|
|
/* 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];
|
|
s = p->config_storage[i];
|
|
|
|
/* condition didn't match */
|
|
if (!config_check_cond(srv, con, dc))
|
|
continue;
|
|
|
|
/* merge config */
|
|
for (j = 0; j < dc->value->used; j++)
|
|
{
|
|
data_unset *du = dc->value->data[j];
|
|
|
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("autoext.suffixes")))
|
|
{
|
|
PATCH(suffixes);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#undef PATCH
|
|
|
|
PHYSICALPATH_FUNC(mod_autoext_handle_physical)
|
|
{
|
|
struct stat st;
|
|
size_t i, k;
|
|
plugin_data *p = (plugin_data *) p_d;
|
|
|
|
if (stat(con->physical.path->ptr, &st) == 0)
|
|
return HANDLER_GO_ON;
|
|
|
|
buffer_copy_string_buffer(srv->tmp_buf, con->physical.path);
|
|
|
|
mod_autoext_patch_connection(srv, con, p);
|
|
|
|
for (i = 1; i < srv->tmp_buf->used; i++)
|
|
{
|
|
if (srv->tmp_buf->ptr[i] != '/' && srv->tmp_buf->ptr[i] != '\0')
|
|
continue;
|
|
|
|
srv->tmp_buf->ptr[i] = '\0';
|
|
|
|
if (stat(srv->tmp_buf->ptr, &st) != 0 && errno == ENOENT)
|
|
{
|
|
// We have our file that doesn't exist! Now, find out which suffix, if any, this has
|
|
buffer *apbuf = buffer_init();
|
|
buffer_copy_string(apbuf, srv->tmp_buf->ptr);
|
|
int apbaselen = apbuf->used;
|
|
|
|
for (k = 0; k < p->conf.suffixes->used; k++)
|
|
{
|
|
apbuf->used = apbaselen;
|
|
buffer_append_string(apbuf, ((data_string*) p->conf.suffixes->data[k])->value->ptr);
|
|
|
|
if (stat(apbuf->ptr, &st) == 0)
|
|
{
|
|
// Match!
|
|
buffer_append_string(apbuf, con->physical.path->ptr + i);
|
|
buffer_reset(con->physical.path);
|
|
buffer_copy_string(con->physical.path, apbuf->ptr);
|
|
return HANDLER_GO_ON;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
srv->tmp_buf->ptr[i] = '/';
|
|
}
|
|
|
|
return HANDLER_GO_ON;
|
|
}
|
|
|
|
/* this function is called at dlopen() time and inits the callbacks */
|
|
|
|
int mod_autoext_plugin_init(plugin *p) {
|
|
p->version = LIGHTTPD_VERSION_ID;
|
|
p->name = buffer_init_string("autoext");
|
|
|
|
p->init = mod_autoext_init;
|
|
p->handle_physical = mod_autoext_handle_physical;
|
|
p->set_defaults = mod_autoext_set_defaults;
|
|
p->cleanup = mod_autoext_free;
|
|
|
|
p->data = NULL;
|
|
|
|
return 0;
|
|
}
|