1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
82
83
84
85 void(*enqueue_connection)(SessionHandler *sh, Connection *conn);
86
87
88
89
90
91
92 void(*keep_alive)(SessionHandler*, Connection *conn);
93
94
95
96
97 IOStream*(*create_iostream)(SessionHandler *sh, Connection *conn,
pool_handle_t *pool, WSBool *ssl);
98 };
99
100
101
102
103
104
105
106 typedef struct _basic_session_handler {
107 SessionHandler sh;
108 threadpool_t *threadpool;
109
110 } BasicSessionHandler;
111
112
113
114
115
116
117 typedef struct _event_session_handler {
118 SessionHandler sh;
119 EVHandler *eventhandler;
120 } EventSessionHandler;
121
122
123
124
125
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
140
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
185
186