src/server/util/object.h

Mon, 01 Jul 2013 18:05:13 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 01 Jul 2013 18:05:13 +0200
changeset 83
28433f06d5ee
parent 61
c858850f3d3a
child 95
74a81d9e19d0
permissions
-rw-r--r--

added minimal nsapi conditions

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 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 OBJECT_H
#define	OBJECT_H

#include "../public/nsapi.h"
#include "pool.h"

#ifdef	__cplusplus
extern "C" {
#endif

// TODO: Enum auslagern in andere Datei?
enum RequestPhase {
    NSAPIAuthTrans = 0,
    NSAPINameTrans,
    NSAPIPathCheck,
    NSAPIObjectType,
    NSAPIService,
    NSAPIAddLog,
    REQ_FINISH,
    NUM_NSAPI_TYPES
};
typedef enum RequestPhase RequestPhase;

typedef struct Condition          Condition;
typedef int8_t                    ConditionResult;
typedef struct Expression         Expression;
typedef enum OperandType          OperandType;
typedef enum Operator             Operator;
typedef enum VarType              VarType;

typedef struct NSAPIContext       NSAPIContext;
typedef struct HTTPObjectConfig   HTTPObjectConfig;

struct directive {
    FuncStruct *func;
    pblock     *param;
    Condition  *cond;
};

struct dtable {
    directive **dirs;
    int       ndir;
};

struct httpd_object {
    pool_handle_t *pool;
    char          *name;
    char          *path;
    dtable        *dt;
    int           nd;
};

struct httpd_objset {
    httpd_object **obj;
    int pos;
};

struct Condition {
    Condition  *parent;
    Expression *expression;
    int        index; // used by NSAPIContext to link expression with result
};

enum OperandType {
    EXPR_OP_NULL = 0,   // no operand
    EXPR_OP_STRING,     // string literal
    EXPR_OP_INTEGER,    // integer literal
    EXPR_OP_VAR,        // variable
    EXPR_OP_FUNC,       // function,
    EXPR_OP_EXPRESSION  // operand is an expression
};

enum Operator {
    OP_NOOP = 0,
    OP_NOT,          // not, !
    OP_AND,          // and, &&
    OP_OR,           // or, ||
    OP_XOR,          // xor, ^
    OP_WILDCARD,     // =
    OP_REGEX,        // =~
    OP_NREGEX,       // !~
    OP_ADD,          // +
    OP_SUB,          // -
    OP_CAT,          // .
    OP_DEF,          // defined
    OP_DEXISTS,      // -d
    OP_FDEXISTS,     // -e
    OP_FEXISTS,      // -f
    OP_LEXISTS,      // -l
    OP_READABLE,     // -r
    OP_FSIZE,        // -s
    OP_UMAP,         // -U
    OP_LESS,         // <
    OP_LESSEQ,       // <=
    OP_GREATER,      // >
    OP_GREATEREQ,    // >=
    OP_STRLESS,      // lt
    OP_STRLESSEQ,    // le
    OP_STRGREATER,   // gt
    OP_STRGREATEREQ, // ge
    OP_EQUAL,        // ==
    OP_NOTEQUAL,     // !=
    OP_STREQUAL,     // eq
    OP_STRNOTEQUAL,  // ne
};

enum VarType {
    VAR_NULL = 0,
    VAR_STRING,
    VAR_INTEGER,
    VAR_BOOL
};

struct Expression {
    // type of the operands
    OperandType optype[2];
    // operand data
    void *opdata[2];
    // operator
    Operator expr_operator;
    // logical connective to next expression
    Operator next_operator;
    // next expression
    Expression *next;
};


struct NSAPIContext{
    HTTPObjectConfig  *conf;

    ConditionResult   **results;
    int               nres;
    int               nmaxres;

    //httpd_objset      *objset;
    int last_req_code;

    int objset_index;
    int dtable_index;
};

struct HTTPObjectConfig {
    httpd_object  **objects;
    int           nobj;
    pool_handle_t *pool;
    uint32_t      ref; // reference counter
};

/*
 * creates a new httpd_object
 */
httpd_object* object_new(pool_handle_t *pool, char *name);

/*
 * frees an httpd_object
 */
void object_free(httpd_object *obj);

/*
 * adds a directive to the object with the type dt (enum RequestPhase)
 */
void object_add_directive(httpd_object *obj, directive *dir, int dt);

/*
 * get a list of all directives with a specific type
 */
#define object_get_dtable(obj,type) &obj->dt[type];



/*
 * creates a new httpd_objset
 */
httpd_objset* objset_create(pool_handle_t *pool);

/*
 * adds a object to the objset
 */
void objset_add_object(pool_handle_t *p, httpd_objset *os, httpd_object *obj);


/*
 * creates a new HTTPObjectConfig
 */
// TODO

/*
 * adds an object to the object configuration
 */
void httpobjconf_add_object(HTTPObjectConfig *conf, httpd_object *obj);



/*
 * prepares the NSAPI context for the next request stage
 */
void nsapi_context_next_stage(NSAPIContext *context);

Condition* condition_from_str(pool_handle_t *pool, char *expr, size_t len);
Expression* expression_from_str(pool_handle_t *pool, char *expr, size_t len);
Operator expr_operator(char *token, size_t len);

/*
 * get the type of the token
 * 
 * returns
 *   0: operand
 *   1: operator
 */
int expr_token_type(char *token, size_t len);
void expr_set_op(pool_handle_t *pool, OperandType *type, void **val, char *token, size_t len);


int condition_evaluate(Condition *condition, Session *sn, Request *rq);
int expression_evaluate(Expression *ex, Session *sn, Request *rq);
int expr_get_var(char *var, Session *sn, Request *rq, void **val, VarType *t);

#ifdef	__cplusplus
}
#endif

#endif	/* OBJECT_H */

mercurial