UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 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 OBJECT_H 30 #define OBJECT_H 31 32 #include "../public/nsapi.h" 33 #include "pool.h" 34 35 #include <inttypes.h> 36 #include <cx/list.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 // TODO: Enum auslagern in andere Datei? 43 enum RequestStage { 44 NSAPIAuthTrans = 0, 45 NSAPINameTrans, 46 NSAPIPathCheck, 47 NSAPIObjectType, 48 NSAPIService, 49 NSAPIError, 50 NSAPIAddLog, 51 REQ_FINISH, 52 NUM_NSAPI_TYPES 53 }; 54 typedef enum RequestStage RequestStage; 55 56 typedef struct Condition Condition; 57 typedef int8_t ConditionResult; 58 typedef struct Expression Expression; 59 typedef enum OperandType OperandType; 60 typedef enum Operator Operator; 61 typedef enum VarType VarType; 62 63 typedef struct NSAPIContext NSAPIContext; 64 typedef struct HTTPObjectConfig HTTPObjectConfig; 65 66 struct directive { 67 FuncStruct *func; 68 pblock *param; 69 Condition *cond; 70 }; 71 72 struct dtable { 73 directive **dirs; 74 int ndir; 75 int alloc; 76 }; 77 78 struct httpd_object { 79 pool_handle_t *pool; 80 char *name; 81 char *path; 82 dtable *dt; 83 int nd; 84 }; 85 86 struct httpd_objset { 87 httpd_object **obj; 88 int pos; 89 }; 90 91 struct Condition { 92 Condition *parent; 93 Condition *ifnot; 94 Expression *expression; 95 int depth; 96 int index; // used by NSAPIContext to link expression with result 97 }; 98 99 struct Expression { 100 // TODO 101 int n; 102 }; 103 104 105 struct NSAPIContext{ 106 HTTPObjectConfig *conf; 107 108 ConditionResult **results; 109 int nres; 110 int nmaxres; 111 112 //httpd_objset *objset; 113 int last_req_code; 114 115 int objset_index; 116 int dtable_index; 117 }; 118 119 struct HTTPObjectConfig { 120 httpd_object **objects; 121 int nobj; 122 pool_handle_t *pool; 123 uint32_t ref; // reference counter 124 }; 125 126 127 128 typedef struct NSAPIExpression NSAPIExpression; 129 typedef enum NSAPIExpressionType NSAPIExpressionType; 130 typedef enum NSAPIExpressionOperator NSAPIExpressionOperator; 131 132 enum NSAPIExpressionType { 133 NSAPI_EXPRESSION_BOOL = 0, 134 NSAPI_EXPRESSION_INT, 135 NSAPI_EXPRESSION_DOUBLE, 136 NSAPI_EXPRESSION_STRING, 137 NSAPI_EXPRESSION_VARIABLE, 138 NSAPI_EXPRESSION_LOGICAL, 139 NSAPI_EXPRESSION_UNARY, 140 NSAPI_EXPRESSION_BINARY, 141 NSAPI_EXPRESSION_IDENTIFIER 142 }; 143 144 enum NSAPIExpressionOperator { 145 NSAPI_EXPRESSION_NOOP = 0, 146 NSAPI_EXPRESSION_CALL, 147 NSAPI_EXPRESSION_ARG, 148 NSAPI_EXPRESSION_WILDCARD_MATCH, 149 NSAPI_EXPRESSION_REGEX_MATCH, 150 NSAPI_EXPRESSION_REGEX_MISMATCH, 151 NSAPI_EXPRESSION_EQ, 152 NSAPI_EXPRESSION_NEQ, 153 NSAPI_EXPRESSION_GT, 154 NSAPI_EXPRESSION_LT, 155 NSAPI_EXPRESSION_GE, 156 NSAPI_EXPRESSION_LE, 157 NSAPI_EXPRESSION_ADD, 158 NSAPI_EXPRESSION_SUB, 159 NSAPI_EXPRESSION_MUL, 160 NSAPI_EXPRESSION_DIV, 161 NSAPI_EXPRESSION_MOD, 162 NSAPI_EXPRESSION_STRCAT, 163 NSAPI_EXPRESSION_NOT, 164 NSAPI_EXPRESSION_AND, 165 NSAPI_EXPRESSION_OR, 166 NSAPI_EXPRESSION_XOR, 167 NSAPI_EXPRESSION_VALUE_DEFINED, 168 NSAPI_EXPRESSION_DIR_EXISTS, 169 NSAPI_EXPRESSION_FILE_DIR_EXISTS, 170 NSAPI_EXPRESSION_FILE_EXISTS, 171 NSAPI_EXPRESSION_SYMLINK_EXISTS, 172 NSAPI_EXPRESSION_FILE_READABLE, 173 NSAPI_EXPRESSION_FILE_SIZE 174 }; 175 176 union NSAPIExpressionValue { 177 cxstring str; 178 cxstring var; 179 cxstring identifier; 180 int64_t i; 181 double f; 182 int b; 183 }; 184 185 struct NSAPIExpression { 186 // value.ptr is not null if 187 // type is one of (bool, int, string): literal text 188 // type is variable: variable name 189 union NSAPIExpressionValue value; 190 191 NSAPIExpressionType type; 192 193 NSAPIExpressionOperator operator; 194 195 /* 196 * left/single operand 197 */ 198 NSAPIExpression *left; 199 200 /* 201 * right operand or NULL 202 */ 203 NSAPIExpression *right; 204 }; 205 206 207 208 /* 209 * creates a new httpd_object 210 */ 211 httpd_object* object_new(pool_handle_t *pool, char *name); 212 213 /* 214 * frees an httpd_object 215 */ 216 void object_free(httpd_object *obj); 217 218 /* 219 * adds a directive to the object with the type dt (enum RequestPhase) 220 */ 221 int object_add_directive(httpd_object *obj, directive *dir, int dt); 222 223 /* 224 * get a list of all directives with a specific type 225 */ 226 #define object_get_dtable(obj,type) &obj->dt[type]; 227 228 229 230 /* 231 * creates a new httpd_objset 232 */ 233 httpd_objset* objset_create(pool_handle_t *pool); 234 235 /* 236 * adds a object to the objset 237 */ 238 void objset_add_object(pool_handle_t *p, httpd_objset *os, httpd_object *obj); 239 240 241 /* 242 * creates a new HTTPObjectConfig 243 */ 244 // TODO 245 246 /* 247 * adds an object to the object configuration 248 */ 249 void httpobjconf_add_object(HTTPObjectConfig *conf, httpd_object *obj); 250 251 252 253 /* 254 * prepares the NSAPI context for the next request stage 255 */ 256 void nsapi_context_next_stage(NSAPIContext *context); 257 258 259 /* 260 * parses an expression from a list of cxstring tokens and compiles the 261 * expression into an Expression object 262 * 263 * tokens: CxList that contains cxstring 264 */ 265 Expression* condition_create(pool_handle_t *pool, CxList *tokens); 266 267 NSAPIExpression* expr_parse_logical_expr(pool_handle_t *pool, CxList *tokens, size_t *pos); 268 269 int condition_evaluate(Condition *condition, Session *sn, Request *rq); 270 271 272 #ifdef __cplusplus 273 } 274 #endif 275 276 #endif /* OBJECT_H */ 277 278