src/server/httprequest.c

Tue, 27 Dec 2011 20:12:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 27 Dec 2011 20:12:21 +0100
changeset 6
ce8fecc9847d
parent 5
dbc01588686e
child 7
3c2ed7a7a5fd
permissions
-rw-r--r--

improved request processing

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 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 <stdio.h>
#include <stdlib.h>

#include "nsapi.h"
#include "pool.h"
#include "pblock.h"
#include "io.h"
#include "util.h"
#include "httprequest.h"
#include "conf.h"
#include "vserver.h"

HTTPRequest *http_request_new() {
    HTTPRequest *req = malloc(sizeof(HTTPRequest));
    req->connection = NULL;
    req->uri.ptr = NULL;

    HeaderArray *hd = malloc(sizeof(HeaderArray));
    hd->next = NULL;
    hd->len = 0;
    hd->headers = calloc(16, sizeof(Header));
    hd->alloc = 16;

    req->headers = hd;

    return req;
}

int handle_request(HTTPRequest *request) {
    // handle nsapi request

    // create pool
    request->pool = pool_create();

    // create nsapi data structures
    NSAPISession *sn = malloc(sizeof(NSAPISession));
    NSAPIRequest *rq = malloc(sizeof(NSAPIRequest));
    request->rq = rq;
    rq->phase = NSAPIAuthTrans;

    // fill session structure
    sn->sys_fd = request->connection->fd;
    sn->sn.pool = pool_create();
    sn->sn.csd = stream_new_from_fd(request->connection->fd);
    sn->sn.client = NULL;
    sn->sn.next = NULL;
    sn->sn.fill = 1;
    sn->sn.subject = NULL;

    // init NSAPI request structure
    if(request_initialize(request->pool, request, rq) != 0) {
        printf("Cannot initialize request structure\n");
        return 1;
    }

    // set default virtual server
    rq->vs = conf_get_default_vs();


    /* Pass request line as "clf-request" */
    pblock_kvinsert(
            pb_key_clf_request,
            request->request_line.ptr,
            request->request_line.length,
            rq->rq.reqpb);

    /* Pass method as "method" in reqpb, and also as method_num */
    pblock_kvinsert(
            pb_key_method,
            request->method.ptr,
            request->method.length,
            rq->rq.reqpb);
    // TODO: method num
    //rqRq.rq.method_num = rqHdr->GetMethodNumber();
    //PR_ASSERT(rqRq.rq.method_num != -1 || iStatus);
    
    /* Pass protocol as "protocol" in reqpb, and also in protv_num */
    pblock_kvinsert(
            pb_key_protocol,
            request->httpv.ptr,
            request->httpv.length,
            rq->rq.reqpb);
    // TODO: protocol num

    /* Pass any query as "query" in reqpb */
    // TODO: query

    /* Get abs_path part of request URI, and canonicalize the path */
    sstr_t absPath = request->uri;
    // TODO: get abs_path
    absPath.ptr = util_canonicalize_uri(
            request->pool,
            absPath.ptr,
            absPath.length,
            (int*)&absPath.length);

    /* Decode the abs_path */
    // TODO: decode abs_path

    /* Pass the abs_path as "uri" in reqpb */
    // TODO: pass abs_path to reqpb
    // TODO: replace this code
    pblock_kvinsert(
            pb_key_uri,
            absPath.ptr,
            absPath.length,
            rq->rq.reqpb);

    // pass http header to the NSAPI request structure


    // Send the request to the NSAPI system
    nsapi_handle_request(sn, rq);

    return 0;
}



void header_add(HeaderArray *hd, char *name, char *value) {
    while(hd->len >= hd->alloc) {
        if(hd->next == NULL) {
            HeaderArray *block = malloc(sizeof(HeaderArray));
            block->next = NULL;
            block->len = 0;
            block->headers = calloc(16, sizeof(Header));
            block->alloc = 16;
            hd->next = block;
        }
        hd = hd->next;
    }
    hd->headers[hd->len].name = name;
    hd->headers[hd->len].value = value;
    hd->len++;
}


/*
 * NSAPI Processing
 * TODO: add this to new file
 */

int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) {
    // TODO: threadpool

    int r = REQ_NOACTION;

    do {
        switch(rq->phase) {
            case NSAPIAuthTrans: {
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case NSAPINameTrans: {
                printf(">>> NameTrans\n");
                r = nsapi_nametrans(sn, rq);
                if(r != REQ_PROCEED) {
                    break;
                }
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case NSAPIPathCheck: {
                printf(">>> PathCheck\n");
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case NSAPIObjectType: {
                printf(">>> ObjectType\n");
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case NSAPIService: {
                printf(">>> Service\n");
                r = nsapi_service(sn, rq);
                if(r != REQ_PROCEED) {
                    break;
                }
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case NSAPIAddLog: {
                printf(">>> AddLog\n");
                rq->phase++;
                nsapi_context_next_stage(&rq->context);
            }
            case REQ_FINISH: {
                printf(">>> Finish\n");
                r = nsapi_finish_request(sn, rq);
            }
        }
    } while (r == REQ_RESTART);


    return r;
}

int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
    // TODO: free memory
    close(sn->sys_fd);

    return 0;
}

int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) {
    HTTPObjectConfig *objconf = rq->vs->objects;
    printf("nsapi_nametrans\n");
    httpd_objset *objset = objset_create(sn->sn.pool);
    rq->rq.os = objset;
    /* first object in objconf is the default object  TODO: make sure it is */
    objset_add_object(sn->sn.pool, objset, objconf->objects[0]);

    httpd_object *obj = objset->obj[0]; /* nametrans only in default object */
    dtable *dt = object_get_dtable(obj, NSAPINameTrans);

    /* execute directives */
    int ret = rq->context.last_req_code;
    char *name = NULL;
    char *ppath = NULL;
    for(int i=NCX_DI(rq);i<dt->ndir;i++) {
        directive *d = dt->dirs[i];

        printf("execute [%s]\n", d->func->name);
        ret = d->func->func(d->param, (Session*)sn, (Request*)rq);

        /* check for name or ppath */
        name = pblock_findkeyval(pb_key_name, rq->rq.vars);
        ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars);

        /* add additional objects to the objset */
        if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) {
            fprintf(stderr, "add_objects failed\n");
            return REQ_ABORTED;
        }

        if(ret != REQ_NOACTION) {
            /*
             * if a saf is still processing, we need to save the context, to
             * process this object at a later time
             */
            if(ret == REQ_PROCESSING) {
                /* save nsapi context */
                /* add +1 to start next round with next function */
                rq->context.dtable_index = i + 1;
            }

            return ret;
        }
    }

    /* if no function has set the ppath var, translate it to docroot */
    if(ret == REQ_NOACTION && ppath == NULL) {
        sstr_t docroot = rq->vs->document_root;
        if(docroot.length < 1) {
            printf("docroot too short\n");
            return REQ_ABORTED; /* docroot too short */
        }
        /* if there is a trailing '/', remove it */
        if(docroot.ptr[docroot.length - 1] == '/') {
            docroot.length--;
        }

        sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb));

        sstr_t translated;
        translated.length = docroot.length + uri.length;
        translated.ptr = alloca(translated.length + 1);
        translated = sstrncat(2, translated, docroot, uri);

        pblock_kvinsert(
            pb_key_ppath,
            translated.ptr,
            translated.length,
            rq->rq.vars);
    }

    return REQ_PROCEED;
}

int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
    printf("nsapi_service\n");
    httpd_objset *objset = rq->rq.os;

    if(NCX_OI(rq) == -1) {
        NCX_OI(rq) = objset->pos - 1;
    }

    int ret = rq->context.last_req_code;
    for(int i=NCX_OI(rq);i>=0;i--) {
        httpd_object *obj = objset->obj[i];
        dtable *dt = object_get_dtable(obj, NSAPIService);

        // execute directives
        for(int j=0;j<dt->ndir;j++) {
            directive *d = dt->dirs[j];

            ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
            if(ret != REQ_NOACTION) {
                if(ret == REQ_PROCESSING) {
                    /* save nsapi context */
                    rq->context.objset_index = i;

                    /* add +1 to start next round with next function */
                    rq->context.dtable_index = j + 1;
                }

                return ret;
            }
        }
    }

    return ret;
}

/*
 * adds objects with specific name or path to the httpd_objset
 */
int add_objects(
        HTTPObjectConfig *objs,
        httpd_objset *os,
        NSAPISession *sn,
        NSAPIRequest *rq,
        char *name,
        char *path)
{
    /* first, add all objects with a matching path */


    /* add object with object with matching name */
    

    return REQ_PROCEED;
}

mercurial