UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef LOCATION_H 30 #define LOCATION_H 31 32 #include <cx/string.h> 33 #include <cx/hash_map.h> 34 #include <regex.h> 35 #include "../public/nsapi.h" 36 #include "../config/serverconfig.h" 37 #include "config.h" 38 #include "../util/strreplace.h" 39 #include "../webdav/webdav.h" 40 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 #define WS_LOCATION_NMATCH 16 47 48 typedef struct WSLocation WSLocation; 49 typedef struct WSLocationConfig WSLocationConfig; 50 51 typedef enum WSLocationMatch { 52 WS_LOCATION_MATCH_EXACT = 0, 53 WS_LOCATION_MATCH_PREFIX, 54 WS_LOCATION_MATCH_CS_REGEX, 55 WS_LOCATION_MATCH_CI_REGEX 56 } WSLocationMatch; 57 58 struct WSLocationConfig { 59 /* 60 * location regex match results 61 */ 62 regmatch_t match[WS_LOCATION_NMATCH]; 63 64 /* 65 * Activate dirindex setting 66 */ 67 WSBool set_dirindex; 68 69 /* 70 * Enable/Disable directory index 71 */ 72 WSBool dirindex; 73 74 /* 75 * Activate forcetls setting 76 */ 77 WSBool set_forcetls; 78 79 /* 80 * force TLS 81 */ 82 WSBool forcetls; 83 84 /* 85 * obj.conf object name 86 */ 87 cxmutstr name; 88 89 /* 90 * VFS type 91 */ 92 cxmutstr vfs; 93 94 /* 95 * Document root path 96 */ 97 cxmutstr docroot; 98 99 /* 100 * Redirect URL 101 * Can contain references to regex capture groups (redirect match regex, 102 * if available, or the location regex) 103 */ 104 StringTemplate *redirect; 105 106 /* 107 * Redirect match regex 108 */ 109 regex_t redirect_match; 110 111 /* 112 * Status code for redirect response 113 */ 114 int redirect_status; 115 116 /* 117 * WebDAV settings 118 * If dav is set, "webdav" is added to the obj.conf object list, 119 * unless name is also set 120 */ 121 WebdavRepository *dav; 122 123 /* 124 * List of ACL names, that should be added to the request ACL list 125 * value: char* 126 */ 127 CxList *acls; 128 }; 129 130 struct WSLocation { 131 WSLocationMatch match; 132 cxmutstr match_string; 133 regex_t regex; 134 135 /* 136 * standard location settings 137 */ 138 WSLocationConfig config; 139 140 /* 141 * list of rewrite rules 142 */ 143 CxList *rewrite; 144 145 146 /* 147 * extended settings 148 * key: directive name 149 * value: char* 150 */ 151 CxMap *ext_config; 152 153 WSLocation *children_begin; 154 WSLocation *children_end; 155 WSLocation *prev; 156 WSLocation *next; 157 }; 158 159 WSLocation* cfg_location_get(ServerConfiguration *cfg, ConfigNode *obj); 160 161 int location_apply_config(WSLocationConfig *target, WSLocation *loc); 162 int location_match(WSLocation *loc, cxstring uri, regmatch_t *match); 163 WSLocationConfig* location_match_and_get_config(pool_handle_t *pool, Request *rq, cxstring uri, WSLocation *loc); 164 WSLocationConfig* cfg_location_match(Session *sn, Request *rq); 165 166 #ifdef __cplusplus 167 } 168 #endif 169 170 #endif /* LOCATION_H */ 171 172