[mod_indexfile] section into subroutines

separate out logic to choose whether or not to try to handle request
from logic to try to find target from list of potential indexfiles
personal/stbuehler/tests-path
Glenn Strauss 2021-08-02 05:31:34 -04:00
parent 67c0b1498a
commit 24d103c5c5
1 changed files with 33 additions and 30 deletions

View File

@ -1,18 +1,16 @@
#include "first.h"
#include "log.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "http_header.h"
#include "log.h"
#include "plugin.h"
#include "request.h"
#include "stat_cache.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* plugin config for all request/connections */
typedef struct {
const array *indexfiles;
} plugin_config;
@ -23,7 +21,6 @@ typedef struct {
plugin_config conf;
} plugin_data;
/* init the plugin data */
INIT_FUNC(mod_indexfile_init) {
return calloc(1, sizeof(plugin_data));
}
@ -81,23 +78,11 @@ SETDEFAULTS_FUNC(mod_indexfile_set_defaults) {
return HANDLER_GO_ON;
}
URIHANDLER_FUNC(mod_indexfile_subrequest) {
plugin_data *p = p_d;
if (NULL != r->handler_module) return HANDLER_GO_ON;
if (!buffer_has_slash_suffix(&r->uri.path)) return HANDLER_GO_ON;
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);
}
for (uint32_t k = 0; k < p->conf.indexfiles->used; ++k) {
const data_string * const ds = (data_string *)p->conf.indexfiles->data[k];
buffer * const b = (ds->value.ptr[0] != '/')
__attribute_nonnull__
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] != '/')
? &r->physical.path
: &r->physical.doc_root; /* index file relative to doc_root */
/* if the index-file starts with a prefix as use this file as
@ -105,7 +90,7 @@ URIHANDLER_FUNC(mod_indexfile_subrequest) {
/* temporarily append to base-path buffer to check existence */
const uint32_t len = buffer_clen(b);
buffer_append_path_len(b, BUF_PTR_LEN(&ds->value));
buffer_append_path_len(b, BUF_PTR_LEN(v));
const stat_cache_st * const st = stat_cache_path_stat(b);
@ -130,17 +115,17 @@ URIHANDLER_FUNC(mod_indexfile_subrequest) {
}
/* found */
if (ds->value.ptr[0] == '/') {
if (v->ptr[0] == '/') {
/* replace uri.path */
buffer_copy_buffer(&r->uri.path, &ds->value);
buffer_copy_buffer(&r->uri.path, v);
http_header_env_set(r, CONST_STR_LEN("PATH_TRANSLATED_DIRINDEX"),
BUF_PTR_LEN(&r->physical.path));
buffer_copy_buffer(&r->physical.path, &r->physical.doc_root);
} else {
/* append to uri.path the relative path to index file (/ -> /index.php) */
buffer_append_string_buffer(&r->uri.path, &ds->value);
buffer_append_string_buffer(&r->uri.path, v);
}
buffer_append_string_buffer(&r->physical.path, &ds->value);
buffer_append_string_buffer(&r->physical.path, v);
return HANDLER_GO_ON;
}
@ -148,6 +133,24 @@ URIHANDLER_FUNC(mod_indexfile_subrequest) {
return HANDLER_GO_ON;
}
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);
}
int mod_indexfile_plugin_init(plugin *p);
int mod_indexfile_plugin_init(plugin *p) {