2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2021-08-02 09:31:34 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
#include "buffer.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2021-08-02 09:31:34 +00:00
|
|
|
#include "log.h"
|
2005-08-15 09:55:23 +00:00
|
|
|
#include "plugin.h"
|
2021-08-02 09:11:36 +00:00
|
|
|
#include "request.h"
|
2005-08-15 09:55:23 +00:00
|
|
|
#include "stat_cache.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 03:49:39 +00:00
|
|
|
const array *indexfiles;
|
2005-08-15 09:55:23 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 03:49:39 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2005-08-15 09:55:23 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_indexfile_init) {
|
2019-10-26 03:49:39 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 03:49:39 +00:00
|
|
|
static void mod_indexfile_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: /* index-file.names */
|
|
|
|
case 1: /* server.indexfiles */
|
|
|
|
pconf->indexfiles = cpv->v.a;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 03:49:39 +00:00
|
|
|
static void mod_indexfile_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_indexfile_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_indexfile_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-10-26 03:49:39 +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-10-26 03:49:39 +00:00
|
|
|
mod_indexfile_merge_config(&p->conf,p->cvlist+p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 03:49:39 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_indexfile_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("index-file.names"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 03:49:39 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("server.indexfiles"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 03:49:39 +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_indexfile"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* 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_indexfile_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 23:12:23 +00:00
|
|
|
__attribute_nonnull__()
|
2021-08-02 09:31:34 +00:00
|
|
|
static handler_t mod_indexfile_tryfiles(request_st * const r, const array * const indexfiles) {
|
|
|
|
for (uint32_t k = 0; k < indexfiles->used; ++k) {
|
|
|
|
const buffer * const v = &((data_string *)indexfiles->data[k])->value;
|
|
|
|
buffer * const b = (v->ptr[0] != '/')
|
2021-03-22 10:55:02 +00:00
|
|
|
? &r->physical.path
|
|
|
|
: &r->physical.doc_root; /* index file relative to doc_root */
|
2006-10-04 13:26:23 +00:00
|
|
|
/* if the index-file starts with a prefix as use this file as
|
2005-09-29 22:14:00 +00:00
|
|
|
* index-generator */
|
2021-03-22 10:55:02 +00:00
|
|
|
|
|
|
|
/* temporarily append to base-path buffer to check existence */
|
[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
|
|
|
const uint32_t len = buffer_clen(b);
|
2021-08-02 09:31:34 +00:00
|
|
|
buffer_append_path_len(b, BUF_PTR_LEN(v));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-03-22 10:55:02 +00:00
|
|
|
const stat_cache_st * const st = stat_cache_path_stat(b);
|
|
|
|
|
[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
|
|
|
buffer_truncate(b, len);
|
2021-03-22 10:55:02 +00:00
|
|
|
|
|
|
|
if (NULL == st) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
|
|
|
case ENOTDIR:
|
|
|
|
continue;
|
|
|
|
case EACCES:
|
2020-01-13 02:51:12 +00:00
|
|
|
r->http_status = 403;
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_FINISHED;
|
2021-03-22 10:55:02 +00:00
|
|
|
default:
|
2020-02-22 22:24:12 +00:00
|
|
|
/* we have no idea what happened. let's tell the user so. */
|
2020-01-13 02:51:12 +00:00
|
|
|
r->http_status = 500;
|
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__,
|
2021-03-22 10:55:02 +00:00
|
|
|
"index file error for request: %s -> %s",
|
2020-01-13 02:51:12 +00:00
|
|
|
r->uri.path.ptr, r->physical.path.ptr);
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-03-22 10:55:02 +00:00
|
|
|
/* found */
|
2021-08-02 09:31:34 +00:00
|
|
|
if (v->ptr[0] == '/') {
|
2016-04-14 12:14:24 +00:00
|
|
|
/* replace uri.path */
|
2021-08-02 09:31:34 +00:00
|
|
|
buffer_copy_buffer(&r->uri.path, v);
|
[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
|
|
|
http_header_env_set(r, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"),
|
|
|
|
BUF_PTR_LEN(&r->physical.path));
|
2021-08-26 05:11:19 +00:00
|
|
|
buffer_copy_path_len2(&r->physical.path,
|
|
|
|
BUF_PTR_LEN(&r->physical.doc_root),
|
|
|
|
BUF_PTR_LEN(v));
|
|
|
|
/*(XXX: not done historical, but rel_path probably should be updated)*/
|
|
|
|
/*buffer_copy_buffer(&r->physical.rel_path, v);*/
|
2016-04-14 12:14:24 +00:00
|
|
|
} else {
|
|
|
|
/* append to uri.path the relative path to index file (/ -> /index.php) */
|
2021-08-02 09:31:34 +00:00
|
|
|
buffer_append_string_buffer(&r->uri.path, v);
|
2021-08-26 05:11:19 +00:00
|
|
|
buffer_append_path_len(&r->physical.path, BUF_PTR_LEN(v));
|
|
|
|
/*(XXX: not done historical, but rel_path probably should be updated)*/
|
|
|
|
/*buffer_append_path_len(&r->physical.rel_path, BUF_PTR_LEN(v));*/
|
2016-04-14 12:14:24 +00:00
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* not found */
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2021-08-02 09:31:34 +00:00
|
|
|
URIHANDLER_FUNC(mod_indexfile_subrequest) {
|
|
|
|
if (NULL != r->handler_module) return HANDLER_GO_ON;
|
|
|
|
if (!buffer_has_slash_suffix(&r->uri.path)) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
mod_indexfile_patch_config(r, p);
|
|
|
|
if (NULL == p->conf.indexfiles) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
if (r->conf.log_request_handling) {
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"-- handling the request as Indexfile");
|
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
|
|
|
"URI : %s", r->uri.path.ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod_indexfile_tryfiles(r, p->conf.indexfiles);
|
|
|
|
}
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_indexfile_plugin_init(plugin *p);
|
2005-08-15 09:55:23 +00:00
|
|
|
int mod_indexfile_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "indexfile";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
p->init = mod_indexfile_init;
|
|
|
|
p->handle_subrequest_start = mod_indexfile_subrequest;
|
|
|
|
p->set_defaults = mod_indexfile_set_defaults;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|