src/server/daemon/sessionhandler.h

Sun, 12 Mar 2023 11:42:17 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 12 Mar 2023 11:42:17 +0100
changeset 466
019c22775f7c
parent 438
22eca559aded
child 542
1327febf99c4
permissions
-rw-r--r--

add force-type SAF

/*
 * 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 SESSIONHANDLER_H
#define	SESSIONHANDLER_H

#include "../util/thrpool.h"
#include "../public/nsapi.h"
#include "../util/io.h"
#include "event.h"

#include <openssl/bio.h> 
#include <openssl/ssl.h> 
#include <openssl/err.h> 

#ifdef	__cplusplus
extern "C" {
#endif

typedef struct _session_handler   SessionHandler;
typedef struct _connection        Connection;

typedef union ConnectionAddr      ConnectionAddr;

union ConnectionAddr {
    struct sockaddr_in   address_v4;
    struct sockaddr_in6  address_v6;
};

enum ConnectionAddrType {
    CONN_ADDR_IPV4 = 0,
    CONN_ADDR_IPV6
};

typedef enum ConnectionAddrType ConnectionAddrType;

struct _connection {
    int                  fd;
    ConnectionAddr       address;
    ConnectionAddrType   addr_type;
    HttpListener         *listener;
    SessionHandler       *session_handler;
    SSL                  *ssl;
    WSBool               ssl_accepted;
    int                  ssl_error;
    int (*read)(Connection *conn, void *buf, int len);
    int (*write)(Connection *conn, const void *buf, int len);
    void (*close)(Connection *conn);
};

typedef void(*enqueue_connection_f)(SessionHandler*, Connection*);
typedef void(*keep_alive_f)(SessionHandler*, Connection*);
struct _session_handler {
    /*
     * Adds a connection. The session handler starts reading and parsing the
     * http request. After that its pass the request to the request handler
     * (handle_request).
     */
    void(*enqueue_connection)(SessionHandler *sh, Connection *conn);
    
    /*
     * Adds a connection to the keep-alive handler. The session handler
     * waits for new data and re-enqueues the connection, if new data is
     * available
     */
    void(*keep_alive)(SessionHandler*, Connection *conn);
    
    /*
     * Creates an IOStream object for the connection
     */
    IOStream*(*create_iostream)(SessionHandler *sh, Connection *conn, pool_handle_t *pool, WSBool *ssl);
};

/*
 * BasicSessionHandler
 *
 * The BasicSessionHandler enqueues the connections to a threadpool. IO is
 * handled by the threadpool.
 */
typedef struct _basic_session_handler {
    SessionHandler sh;
    threadpool_t   *threadpool;
    
} BasicSessionHandler;

/*
 * EventSessionHandler
 * 
 * The EventSessionHandler uses a event handler to handle request inputs.
 */
typedef struct _event_session_handler {
    SessionHandler  sh;
    EVHandler       *eventhandler;
} EventSessionHandler;

/*
 * EventHttpIO
 * 
 * defined in sesionhandler.c
 */

int connection_read(Connection *conn, void *buf, int len);
int connection_write(Connection *conn, const void *buf, int len);
void connection_close(Connection *conn);
int connection_ssl_read(Connection *conn, void *buf, int len);
int connection_ssl_write(Connection *conn, const void *buf, int len);
void connection_ssl_close(Connection *conn);

void connection_destroy(Connection *conn);

/*
 * generic create_iostream function for BasicSessionHandler
 * and EventSessionHandler
 */
IOStream* create_connection_iostream(
        SessionHandler *sh,
        Connection *conn,
        pool_handle_t *pool,
        WSBool *ssl);


SessionHandler* create_basic_session_handler(pool_handle_t *pool);

void basic_enq_conn(SessionHandler *handler, Connection *conn);

void* basic_run_session(void *data);

void basic_keep_alive(SessionHandler *handler, Connection *conn);


SessionHandler* create_event_session_handler(pool_handle_t *pool);

void evt_enq_conn(SessionHandler *handler, Connection *conn);

int evt_request_ssl_accept(EventHandler *handler, Event *event);
int evt_request_input(EventHandler *h, Event *event);
int evt_request_finish(EventHandler *h, Event *event);
int evt_request_error(EventHandler *h, Event *event);

void evt_keep_alive(SessionHandler *handler, Connection *conn);


#ifdef	__cplusplus
}
#endif

#endif	/* SESSIONHANDLER_H */

mercurial