# HG changeset patch # User Olaf Wintermann # Date 1763898299 -3600 # Node ID b85d45fd3b017639dab977a232860e113c312c38 # Parent 9728d3a2ac97d9c76fd3af0c29995d116a200a58 add location config parser diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/config/serverconfig.c --- a/src/server/config/serverconfig.c Sat Nov 22 16:44:42 2025 +0100 +++ b/src/server/config/serverconfig.c Sun Nov 23 12:44:59 2025 +0100 @@ -500,6 +500,15 @@ return ret; } +int serverconfig_directive_count_args(ConfigNode *dir) { + int count = 0; + ConfigParam *arg = dir->args; + while(arg) { + count++; + arg = arg->next; + } + return count; +} diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/config/serverconfig.h --- a/src/server/config/serverconfig.h Sat Nov 22 16:44:42 2025 +0100 +++ b/src/server/config/serverconfig.h Sun Nov 23 12:44:59 2025 +0100 @@ -180,6 +180,11 @@ */ cxstring serverconfig_directive_get_arg(ConfigNode *directive, cxstring param_name); +/* + * Returns the number of directive arguments + */ +int serverconfig_directive_count_args(ConfigNode *dir); + /* * Checks if the directive name matches any string in the names array. diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/config.c --- a/src/server/daemon/config.c Sat Nov 22 16:44:42 2025 +0100 +++ b/src/server/daemon/config.c Sun Nov 23 12:44:59 2025 +0100 @@ -51,6 +51,7 @@ #include "ldap_auth.h" #include "configmanager.h" #include "resourcepool.h" +#include "location.h" #include "vserver.h" #include "../util/pblock.h" @@ -746,6 +747,18 @@ } vs->acls = acldata; + CxList *locations = serverconfig_get_node_list(obj, CONFIG_NODE_OBJECT, CX_STR("Location")); + CxIterator i = cxListIterator(locations); + cx_foreach(ConfigNode *, node, i) { + WSLocation *location = cfg_location_get(cfg->a, node); + if(location) { + vs_add_location(vs, location); + } else { + log_ereport(LOG_FAILURE, "VirtualServer %s error: abort", vs->name.ptr); + return 1; + } + } + cxListFree(locations); // set the access log for the virtual server // TODO: don't always use the default diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/location.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/daemon/location.c Sun Nov 23 12:44:59 2025 +0100 @@ -0,0 +1,124 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "location.h" +#include "config.h" +#include "../util/util.h" +#include + +#define DIR_CHECK_ARGC(n) if(argc != n) { \ + log_ereport(LOG_FAILURE, "%s directive argc != %d", name.ptr, n); \ + return 1; \ + } + +static void location_list_add(WSLocation *location, WSLocation *sub) { + void **begin = (void**)&location->children_begin; + void **end = (void**)&location->children_end; + cx_linked_list_add(begin, end, offsetof(WSLocation, prev), offsetof(WSLocation, next), sub); +} + +static int add_location_config(const CxAllocator *a, WSLocation *location, ConfigNode *dir) { + cxmutstr name = dir->name; + int argc = serverconfig_directive_count_args(dir); + if(!cx_strcasecmp(name, "DirectoryIndex")) { + DIR_CHECK_ARGC(1); + location->config.set_dirindex = TRUE; + location->config.dirindex = util_getboolean_s(cx_strcast(dir->args->value), FALSE); + } else if(!cx_strcasecmp(name, "DocumentRoot")) { + DIR_CHECK_ARGC(1); + location->config.docroot = cx_strdup_a(a, dir->args->value); + } else if(!cx_strcasecmp(name, "Location")) { + WSLocation *sub_location = cfg_location_get(a, dir); + if(!sub_location) { + return 1; + } + location_list_add(location, sub_location); + } + + return 0; +} + + +WSLocation* cfg_location_get(const CxAllocator *a, ConfigNode *obj) { + WSLocationMatch match = WS_LOCATION_MATCH_EXACT; + cxmutstr match_str; + + int regex_flags = REG_EXTENDED; + int argc = serverconfig_directive_count_args(obj); + if(argc == 2) { + // arg0: match type + cxmutstr type_str = obj->args->value; + if(!cx_strcmp(type_str, "=")) { + // noop + } else if(!cx_strcmp(type_str, "^~")) { + match = WS_LOCATION_MATCH_PREFIX; + } else if(!cx_strcmp(type_str, "~")) { + match = WS_LOCATION_MATCH_CS_REGEX; + } else if(!cx_strcmp(type_str, "~*")) { + match = WS_LOCATION_MATCH_CI_REGEX; + regex_flags |= REG_ICASE; + } else { + log_ereport(LOG_FAILURE, "Location: unknown operator %.*s", (int)type_str.length, type_str.ptr); + return NULL; + } + + // arg1: match_string + match_str = obj->args->next->value; + } else if(argc == 1) { + // arg0: match_string + match_str = obj->args->value; + } else if(argc > 2) { + log_ereport(LOG_FAILURE, "Location directive must have 0-2 args"); + return NULL; + } + + WSLocation *location = cxZalloc(a, sizeof(WSLocation)); + location->match_string = cx_strdup_a((a), match_str); + + if(match == WS_LOCATION_MATCH_CS_REGEX || match == WS_LOCATION_MATCH_CI_REGEX) { + if(regcomp(&location->regex, match_str.ptr, regex_flags)) { + log_ereport(LOG_FAILURE, "Location: cannot compile regex pattern %s", match_str.ptr); + return NULL; + } + } + + ConfigNode *dir = obj->children_begin; + while(dir) { + if(dir->type == CONFIG_NODE_OBJECT || dir->type == CONFIG_NODE_DIRECTIVE) { + if(add_location_config(a, location, dir)) { + log_ereport(LOG_FAILURE, "Location %s: abort", match_str.ptr); + return NULL; + } + } + dir = dir->next; + } + + + + return location; +} diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/location.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/daemon/location.h Sun Nov 23 12:44:59 2025 +0100 @@ -0,0 +1,152 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LOCATION_H +#define LOCATION_H + +#include +#include +#include +#include "../public/nsapi.h" +#include "../config/serverconfig.h" +#include "../util/strreplace.h" +#include "../webdav/webdav.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct WSLocation WSLocation; +typedef struct WSLocationConfig WSLocationConfig; + +typedef enum WSLocationMatch { + WS_LOCATION_MATCH_EXACT = 0, + WS_LOCATION_MATCH_PREFIX, + WS_LOCATION_MATCH_CS_REGEX, + WS_LOCATION_MATCH_CI_REGEX +} WSLocationMatch; + +struct WSLocationConfig { + /* + * Activate dirindex setting + */ + WSBool set_dirindex; + + /* + * Enable/Disable directory index + */ + WSBool dirindex; + + /* + * Activate forcetls setting + */ + WSBool set_forcetls; + + /* + * force TLS + */ + WSBool forcetls; + + /* + * obj.conf object name + */ + cxmutstr name; + + /* + * VFS type + */ + cxmutstr vfs; + + /* + * Document root path + */ + cxmutstr docroot; + + /* + * Redirect URL + * Can contain references to regex capture groups (redirect match regex, + * if available, or the location regex) + */ + StringTemplate *redirect; + + /* + * Redirect match regex + */ + regex_t redirect_match; + + /* + * Status code for redirect response + */ + int redirect_status; + + /* + * WebDAV settings + * If dav is set, "webdav" is added to the obj.conf object list, + * unless name is also set + */ + WebdavRepository *dav; + + /* + * List of ACL names, that should be added to the request ACL list + * value: char* + */ + CxList *acls; +}; + +struct WSLocation { + WSLocationMatch match; + cxmutstr match_string; + regex_t regex; + + /* + * standard location settings + */ + WSLocationConfig config; + + /* + * extended settings + * key: directive name + * value: char* + */ + CxMap *ext_config; + + WSLocation *children_begin; + WSLocation *children_end; + WSLocation *prev; + WSLocation *next; +}; + +WSLocation* cfg_location_get(const CxAllocator *a, ConfigNode *obj); + +#ifdef __cplusplus +} +#endif + +#endif /* LOCATION_H */ + diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/rewrite.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/daemon/rewrite.c Sun Nov 23 12:44:59 2025 +0100 @@ -0,0 +1,29 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "rewrite.h" \ No newline at end of file diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/rewrite.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/daemon/rewrite.h Sun Nov 23 12:44:59 2025 +0100 @@ -0,0 +1,66 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#ifndef WS_REWRITE_H +#define WS_REWRITE_H + +#include "../public/nsapi.h" +#include "../util/strreplace.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct RewriteRule { + /* + * + */ + cxmutstr match; + + /* + * Compiled match regex + */ + regex_t regex; + + /* + * Rewrite to this url + */ + StringTemplate *url; +} RewriteRule; + + +#ifdef __cplusplus +} +#endif + +#endif /* WS_REWRITE_H */ + diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/vserver.c --- a/src/server/daemon/vserver.c Sat Nov 22 16:44:42 2025 +0100 +++ b/src/server/daemon/vserver.c Sun Nov 23 12:44:59 2025 +0100 @@ -29,6 +29,7 @@ #include "vserver.h" #include +#include VirtualServer* vs_new(pool_handle_t *pool) { VirtualServer *vs = pool_malloc(pool, sizeof(VirtualServer)); @@ -52,6 +53,12 @@ return 0; } +int vs_add_location(VirtualServer *vs, WSLocation *location) { + void **begin = (void**)&vs->locations_begin; + void **end = (void**)&vs->locations_end; + cx_linked_list_add(begin, end, offsetof(WSLocation, prev), offsetof(WSLocation, next), location); + return 0; +} // public API diff -r 9728d3a2ac97 -r b85d45fd3b01 src/server/daemon/vserver.h --- a/src/server/daemon/vserver.h Sat Nov 22 16:44:42 2025 +0100 +++ b/src/server/daemon/vserver.h Sun Nov 23 12:44:59 2025 +0100 @@ -35,27 +35,21 @@ #include "../public/nsapi.h" #include "../daemon/acldata.h" +#include "location.h" + #include "log.h" #ifdef __cplusplus extern "C" { #endif -typedef struct VSLocation VSLocation; - -struct VSLocation { - VSLocation *child_begin; - VSLocation *child_end; - - VSLocation *next; -}; struct VirtualServer { pool_handle_t *pool; cxmutstr name; cxmutstr host; - CxMap hosts; + CxMap *hosts; // TODO: list of listeners, check listener of vs cxmutstr objectfile; @@ -66,17 +60,19 @@ ACLData *acls; AccessLog *log; - VSLocation *locations_begin; - VSLocation *locations_end; + WSLocation *locations_begin; + WSLocation *locations_end; - uint32_t ref; // reference counter + uint32_t ref; // reference counter }; VirtualServer* vs_new(pool_handle_t *pool); int vs_add_host(VirtualServer *vs, cxstring host); +int vs_add_location(VirtualServer *vs, WSLocation *location); + #ifdef __cplusplus }