src/server/httplistener.c

Sun, 08 Jan 2012 15:46:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 08 Jan 2012 15:46:47 +0100
changeset 13
1fdbf4170ef4
parent 9
30e51941a673
permissions
-rw-r--r--

Added webdav code

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

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>

#include "map.h"
#include "httplistener.h"

#include "session.h"

hashmap_t *listener_map = NULL;


int start_all_listener() {
    HttpListener *listener = get_http_listener("default");
    http_listener_start(listener); 

    return 0;
}

HttpListener* get_http_listener(char *name) {
    return hashmap_get(listener_map, sstr(name));
}


HttpListener* http_listener_new(ListenerConfig *conf) {
    HttpListener *listener = malloc(sizeof(HttpListener));
    listener->session_handler = create_basic_session_handler();
    listener->nacceptors = conf->nacceptors;

    struct sockaddr_in servaddr;   /* server address */

    /* init address structure */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(conf->port);

    /* create socket */
    if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Error: http_listener_new: socket");
        return NULL;
    }

    int o = 1;
    setsockopt(
            listener->server_socket,
            SOL_SOCKET, SO_REUSEADDR,
            &o,
            sizeof(int));

    /* bind server socket to address */
    if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){
        perror("Error: http_listener_new: bind");
        return NULL;
    }

    /* create acceptors */
    listener->acceptors = calloc(listener->nacceptors, sizeof(void*));
    for (int i=0;i<listener->nacceptors;i++) {
        listener->acceptors[i] = acceptor_new(listener);
    }

    if(listener_map == NULL) {
        listener_map = hashmap_new(8);
    }
    hashmap_put(listener_map, sstr(conf->name), listener);
    return listener;
}

int http_listener_start(HttpListener *listener) {
    printf("INFO: start listener\n");

    if (listen(listener->server_socket, 16) == -1) {
        perror("Error: http_listener_start: listen");
        return -1;
    }

    /* start acceptor threads */
    for (int i=0;i<listener->nacceptors;i++) {
        acceptor_start(listener->acceptors[i]);
    }
}



Acceptor* acceptor_new(HttpListener *listener) {
    Acceptor *acceptor = malloc(sizeof(Acceptor));
    acceptor->listener = listener;
    return acceptor;
}

void acceptor_start(Acceptor *a) {
    if(pthread_create(
            &a->tid,
            NULL,
            (void*(*)(void*))acceptor_thread,
            a) != 0)
    {
        perror("Error: acceptor_start: pthread_create");
    }
}

void* acceptor_thread(Acceptor *acceptor) {
    HttpListener *listener = acceptor->listener;

    for (;;) {
        /* accept connections */
        struct sockaddr_in ca;
        socklen_t length = sizeof(ca);
        int clientfd;

        /* accept a connection */
        clientfd = accept(
                listener->server_socket,
                (struct sockaddr*)&ca,
                &length);
        if (clientfd == -1) {
            perror("Error: acceptor_thread: accept");
            continue;
        }
        
        /* create Connection object */
        Connection *conn = malloc(sizeof(Connection));
        conn->address = ca;
        conn->fd = clientfd;

        /* enqueue the connection */
        listener->session_handler->enqueue_connection(
                listener->session_handler,
                conn);

        /* ready for new connection */
    }
}

mercurial