src/server/daemon/sessionhandler.c

Sat, 14 Jan 2012 13:53:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 14 Jan 2012 13:53:44 +0100
changeset 14
b8bf95b39952
parent 1
src/server/sessionhandler.c@3c066d52342d
child 35
4417619a9bbd
permissions
-rw-r--r--

New source folder layout

/*
 * 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 "../public/nsapi.h"

#include "sessionhandler.h"
#include "httprequest.h"
#include "httpparser.h"

SessionHandler* create_basic_session_handler() {
    BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler));
    handler->threadpool = threadpool_new(8);
    handler->sh.enqueue_connection = basic_enq_conn;


    return (SessionHandler*)handler;
}

void basic_enq_conn(SessionHandler *handler, Connection *conn) {
    BasicSessionHandler *sh = (BasicSessionHandler*)handler;
    conn->session_handler = handler;
    threadpool_run(sh->threadpool, basic_run_session, conn);
}

void* basic_run_session(void *data) {
    Connection *conn = (Connection*)data;

    HTTPRequest *request = http_request_new();
    request->connection = conn;

    // read request
    netbuf *buf = malloc(sizeof(netbuf));
    buf->rdtimeout = 120;
    buf->pos = 0;
    buf->cursize = 0;
    buf->maxsize = 2048;
    buf->sd = &conn->fd;
    buf->inbuf = malloc(2048);
    buf->errmsg = NULL;

    request->netbuf = buf;

    HttpParser *parser = http_parser_new(request);
    int state;
    int r;
    r = read(conn->fd, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
    if(r == -1) {
         // TODO: error handling
        fprintf(stderr, "%s\n", "Error: Cannot read from socket");
        return NULL;
    }
    buf->cursize += r;
    while((state = http_parser_process(parser)) != 0) {
        if(state == 2) {
            // TODO: error handling
            fprintf(stderr, "%s\n", "Error: Cannot parse http request");
            return NULL;
        }
        r = read(conn->fd, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
        if(r == -1) {
            // TODO: error handling
            fprintf(stderr, "%s\n", "Error: Cannot read from socket");
            return NULL;
        }
        buf->cursize += r;
    }

    // process request
    r = handle_request(request);


    return NULL;
}

mercurial