UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef SESSIONHANDLER_H 30 #define SESSIONHANDLER_H 31 32 #include "../util/thrpool.h" 33 #include "../public/nsapi.h" 34 #include "../util/io.h" 35 #include "event.h" 36 37 #include <openssl/bio.h> 38 #include <openssl/ssl.h> 39 #include <openssl/err.h> 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 typedef struct _session_handler SessionHandler; 46 typedef struct _connection Connection; 47 48 typedef union ConnectionAddr ConnectionAddr; 49 50 union ConnectionAddr { 51 struct sockaddr_in address_v4; 52 struct sockaddr_in6 address_v6; 53 }; 54 55 enum ConnectionAddrType { 56 CONN_ADDR_IPV4 = 0, 57 CONN_ADDR_IPV6 58 }; 59 60 typedef enum ConnectionAddrType ConnectionAddrType; 61 62 struct _connection { 63 uint64_t id; 64 int fd; 65 ConnectionAddr address; 66 ConnectionAddrType addr_type; 67 HttpListener *listener; 68 SessionHandler *session_handler; 69 SSL *ssl; 70 WSBool ssl_accepted; 71 int ssl_error; 72 int (*read)(Connection *conn, void *buf, int len); 73 int (*write)(Connection *conn, const void *buf, int len); 74 void (*close)(Connection *conn); 75 }; 76 77 typedef void(*enqueue_connection_f)(SessionHandler*, Connection*); 78 typedef void(*keep_alive_f)(SessionHandler*, Connection*); 79 struct _session_handler { 80 /* 81 * Adds a connection. The session handler starts reading and parsing the 82 * http request. After that its pass the request to the request handler 83 * (handle_request). 84 */ 85 void(*enqueue_connection)(SessionHandler *sh, Connection *conn); 86 87 /* 88 * Adds a connection to the keep-alive handler. The session handler 89 * waits for new data and re-enqueues the connection, if new data is 90 * available 91 */ 92 void(*keep_alive)(SessionHandler*, Connection *conn); 93 94 /* 95 * Creates an IOStream object for the connection 96 */ 97 IOStream*(*create_iostream)(SessionHandler *sh, Connection *conn, pool_handle_t *pool, WSBool *ssl); 98 }; 99 100 /* 101 * BasicSessionHandler 102 * 103 * The BasicSessionHandler enqueues the connections to a threadpool. IO is 104 * handled by the threadpool. 105 */ 106 typedef struct _basic_session_handler { 107 SessionHandler sh; 108 threadpool_t *threadpool; 109 110 } BasicSessionHandler; 111 112 /* 113 * EventSessionHandler 114 * 115 * The EventSessionHandler uses a event handler to handle request inputs. 116 */ 117 typedef struct _event_session_handler { 118 SessionHandler sh; 119 EVHandler *eventhandler; 120 } EventSessionHandler; 121 122 /* 123 * EventHttpIO 124 * 125 * defined in sesionhandler.c 126 */ 127 typedef struct EventHttpIO EventHttpIO; 128 129 int connection_read(Connection *conn, void *buf, int len); 130 int connection_write(Connection *conn, const void *buf, int len); 131 void connection_close(Connection *conn); 132 int connection_ssl_read(Connection *conn, void *buf, int len); 133 int connection_ssl_write(Connection *conn, const void *buf, int len); 134 void connection_ssl_close(Connection *conn); 135 136 void connection_destroy(Connection *conn); 137 138 /* 139 * generic create_iostream function for BasicSessionHandler 140 * and EventSessionHandler 141 */ 142 IOStream* create_connection_iostream( 143 SessionHandler *sh, 144 Connection *conn, 145 pool_handle_t *pool, 146 WSBool *ssl); 147 148 149 SessionHandler* create_basic_session_handler(pool_handle_t *pool); 150 151 void basic_enq_conn(SessionHandler *handler, Connection *conn); 152 153 void* basic_run_session(void *data); 154 155 void basic_keep_alive(SessionHandler *handler, Connection *conn); 156 157 158 SessionHandler* create_event_session_handler(pool_handle_t *pool); 159 160 void evt_request_timeout(EventHandler *h, EVWatchList *item); 161 162 int evt_add_request(EventHandler *h, Event *event); 163 164 void evt_enq_conn(SessionHandler *handler, Connection *conn); 165 166 EventHttpIO* evt_req_init(SessionHandler *handler, Connection *conn); 167 168 int evt_request_ssl_accept(EventHandler *handler, Event *event); 169 int evt_request_input(EventHandler *h, Event *event); 170 int evt_request_finish(EventHandler *h, Event *event); 171 int evt_request_error(EventHandler *h, Event *event); 172 173 void evt_keep_alive(SessionHandler *handler, Connection *conn); 174 175 int evt_keep_alive_enqueue(EventHandler *h, Event *event); 176 int evt_keep_alive_input_event(EventHandler *h, Event *event); 177 178 void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item); 179 180 #ifdef __cplusplus 181 } 182 #endif 183 184 #endif /* SESSIONHANDLER_H */ 185 186