42 typedef struct _event_http_io { |
42 typedef struct _event_http_io { |
43 HTTPRequest *request; |
43 HTTPRequest *request; |
44 HttpParser *parser; |
44 HttpParser *parser; |
45 } EventHttpIO; |
45 } EventHttpIO; |
46 |
46 |
|
47 |
|
48 int connection_read(Connection *conn, void *buf, int len) { |
|
49 return (int)read(conn->fd, buf, len); |
|
50 } |
|
51 |
|
52 int connection_write(Connection *conn, const void *buf, int len) { |
|
53 return (int)write(conn->fd, buf, len); |
|
54 } |
|
55 |
|
56 void connection_close(Connection *conn) { |
|
57 close(conn->fd); |
|
58 } |
|
59 |
|
60 int connection_ssl_read(Connection *conn, void *buf, int len) { |
|
61 return SSL_read(conn->ssl, buf, len); |
|
62 } |
|
63 |
|
64 int connection_ssl_write(Connection *conn, const void *buf, int len) { |
|
65 return SSL_write(conn->ssl, buf, len); |
|
66 } |
|
67 |
|
68 void connection_ssl_close(Connection *conn) { |
|
69 SSL_shutdown(conn->ssl); |
|
70 close(conn->fd); |
|
71 } |
|
72 |
47 SessionHandler* create_basic_session_handler() { |
73 SessionHandler* create_basic_session_handler() { |
48 BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler)); |
74 BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler)); |
49 handler->threadpool = threadpool_new(4, 8); |
75 handler->threadpool = threadpool_new(4, 8); |
50 handler->sh.enqueue_connection = basic_enq_conn; |
76 handler->sh.enqueue_connection = basic_enq_conn; |
51 |
77 handler->sh.keep_alive = basic_keep_alive; |
52 |
78 |
53 return (SessionHandler*)handler; |
79 return (SessionHandler*)handler; |
54 } |
80 } |
55 |
81 |
56 void basic_enq_conn(SessionHandler *handler, Connection *conn) { |
82 void basic_enq_conn(SessionHandler *handler, Connection *conn) { |
79 request.netbuf = buf; |
105 request.netbuf = buf; |
80 |
106 |
81 HttpParser *parser = http_parser_new(&request); |
107 HttpParser *parser = http_parser_new(&request); |
82 int state; |
108 int state; |
83 int r; |
109 int r; |
84 r = read(conn->fd, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
110 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
85 if(r == -1) { |
111 if(r == -1) { |
86 // TODO: error handling |
112 // TODO: error handling |
87 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
113 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
88 return NULL; |
114 return NULL; |
89 } |
115 } |
92 if(state == 2) { |
118 if(state == 2) { |
93 // TODO: error handling |
119 // TODO: error handling |
94 fprintf(stderr, "%s\n", "Error: Cannot parse http request"); |
120 fprintf(stderr, "%s\n", "Error: Cannot parse http request"); |
95 return NULL; |
121 return NULL; |
96 } |
122 } |
97 r = read(conn->fd, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
123 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
98 if(r == -1) { |
124 if(r == -1) { |
99 // TODO: error handling |
125 // TODO: error handling |
100 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
126 fprintf(stderr, "%s\n", "Error: Cannot read from socket"); |
101 return NULL; |
127 return NULL; |
102 } |
128 } |
103 buf->cursize += r; |
129 buf->cursize += r; |
104 } |
130 } |
105 |
131 |
106 // process request |
132 // process request |
107 r = handle_request(&request, NULL); // TODO: use correct thread pool |
133 r = handle_request(&request, NULL); // TODO: use correct thread pool |
108 |
134 |
109 // TODO: free, see evt_request_finish |
135 // TODO: free, see evt_request_finish |
110 |
136 |
188 |
214 |
189 int evt_request_input(event_handler_t *handler, event_t *event) { |
215 int evt_request_input(event_handler_t *handler, event_t *event) { |
190 EventHttpIO *io = event->cookie; |
216 EventHttpIO *io = event->cookie; |
191 HttpParser *parser = io->parser; |
217 HttpParser *parser = io->parser; |
192 HTTPRequest *request = io->request; |
218 HTTPRequest *request = io->request; |
|
219 Connection *conn = io->request->connection; |
193 netbuf *buf = request->netbuf; |
220 netbuf *buf = request->netbuf; |
194 |
221 |
195 int state; |
222 int state; |
196 int r; |
223 int r; |
197 r = read( |
224 r = conn->read( |
198 request->connection->fd, |
225 conn, |
199 buf->inbuf + buf->pos, |
226 buf->inbuf + buf->pos, |
200 buf->maxsize - buf->pos); |
227 buf->maxsize - buf->pos); |
201 if(r == -1) { |
228 if(r == -1) { |
202 event->finish = evt_request_error; |
229 event->finish = evt_request_error; |
203 return 0; |
230 return 0; |