Sat, 31 Oct 2015 20:10:21 +0100
experimental BSD support
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 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 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
31 | #include <errno.h> |
1 | 32 | |
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
33 | #include "../public/nsapi.h" |
1 | 34 | |
35 | #include "sessionhandler.h" | |
36 | #include "httprequest.h" | |
37 | #include "httpparser.h" | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
38 | #include "log.h" |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
39 | #include "error.h" |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
40 | #include "httplistener.h" |
1 | 41 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
42 | typedef struct _event_http_io { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
43 | HTTPRequest *request; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
44 | HttpParser *parser; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
45 | } EventHttpIO; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
46 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
47 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
48 | int connection_read(Connection *conn, void *buf, int len) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
49 | return (int)read(conn->fd, buf, len); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
50 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
51 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
52 | int connection_write(Connection *conn, const void *buf, int len) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
53 | return (int)write(conn->fd, buf, len); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
54 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
55 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
56 | void connection_close(Connection *conn) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
57 | close(conn->fd); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
58 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
59 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
60 | int connection_ssl_read(Connection *conn, void *buf, int len) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
61 | return SSL_read(conn->ssl, buf, len); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
62 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
63 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
64 | int connection_ssl_write(Connection *conn, const void *buf, int len) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
65 | return SSL_write(conn->ssl, buf, len); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
66 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
67 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
68 | void connection_ssl_close(Connection *conn) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
69 | SSL_shutdown(conn->ssl); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
70 | close(conn->fd); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
71 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
72 | |
1 | 73 | SessionHandler* create_basic_session_handler() { |
74 | BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler)); | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
75 | handler->threadpool = threadpool_new(4, 8); |
1 | 76 | handler->sh.enqueue_connection = basic_enq_conn; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
77 | handler->sh.keep_alive = basic_keep_alive; |
1 | 78 | |
79 | return (SessionHandler*)handler; | |
80 | } | |
81 | ||
82 | void basic_enq_conn(SessionHandler *handler, Connection *conn) { | |
83 | BasicSessionHandler *sh = (BasicSessionHandler*)handler; | |
84 | conn->session_handler = handler; | |
85 | threadpool_run(sh->threadpool, basic_run_session, conn); | |
86 | } | |
87 | ||
88 | void* basic_run_session(void *data) { | |
89 | Connection *conn = (Connection*)data; | |
90 | ||
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
91 | HTTPRequest request; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
92 | http_request_init(&request); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
93 | request.connection = conn; |
1 | 94 | |
95 | // read request | |
96 | netbuf *buf = malloc(sizeof(netbuf)); | |
97 | buf->rdtimeout = 120; | |
98 | buf->pos = 0; | |
99 | buf->cursize = 0; | |
100 | buf->maxsize = 2048; | |
101 | buf->sd = &conn->fd; | |
102 | buf->inbuf = malloc(2048); | |
103 | buf->errmsg = NULL; | |
104 | ||
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
105 | request.netbuf = buf; |
1 | 106 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
107 | HttpParser *parser = http_parser_new(&request); |
1 | 108 | int state; |
109 | int r; | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
110 | r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
1 | 111 | if(r == -1) { |
112 | // TODO: error handling | |
113 | fprintf(stderr, "%s\n", "Error: Cannot read from socket"); | |
114 | return NULL; | |
115 | } | |
116 | buf->cursize += r; | |
117 | while((state = http_parser_process(parser)) != 0) { | |
118 | if(state == 2) { | |
119 | // TODO: error handling | |
120 | fprintf(stderr, "%s\n", "Error: Cannot parse http request"); | |
121 | return NULL; | |
122 | } | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
123 | r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
1 | 124 | if(r == -1) { |
125 | // TODO: error handling | |
126 | fprintf(stderr, "%s\n", "Error: Cannot read from socket"); | |
127 | return NULL; | |
128 | } | |
129 | buf->cursize += r; | |
130 | } | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
131 | |
1 | 132 | // process request |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
133 | r = handle_request(&request, NULL); // TODO: use correct thread pool |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
134 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
135 | // TODO: free, see evt_request_finish |
1 | 136 | |
137 | return NULL; | |
138 | } | |
139 | ||
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
140 | void basic_keep_alive(SessionHandler *handler, Connection *conn) { |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
141 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
142 | } |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
143 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
144 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
145 | /* ----- event session handler ----- */ |
1 | 146 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
147 | SessionHandler* create_event_session_handler() { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
148 | EventSessionHandler *handler = malloc(sizeof(EventSessionHandler)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
149 | handler->eventhandler = get_default_event_handler(); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
150 | handler->sh.enqueue_connection = evt_enq_conn; |
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
151 | handler->sh.keep_alive = evt_keep_alive; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
152 | return (SessionHandler*)handler; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
153 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
154 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
155 | void evt_enq_conn(SessionHandler *handler, Connection *conn) { |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
156 | HTTPRequest *request = malloc(sizeof(HTTPRequest)); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
157 | http_request_init(request); |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
158 | request->connection = conn; |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
159 | conn->session_handler = handler; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
160 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
161 | // set socket non blocking |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
162 | int flags; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
163 | if (-1 == (flags = fcntl(conn->fd, F_GETFL, 0))) { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
164 | flags = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
165 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
166 | if (fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
167 | perror("Error: start_event_session: fcntl"); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
168 | // TODO: error |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
169 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
170 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
171 | // TODO: remove code redundancy (basic_run_session) |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
172 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
173 | // read request |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
174 | netbuf *buf = malloc(sizeof(netbuf)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
175 | buf->rdtimeout = 120; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
176 | buf->pos = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
177 | buf->cursize = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
178 | buf->maxsize = 2048; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
179 | buf->sd = &conn->fd; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
180 | buf->inbuf = malloc(2048); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
181 | buf->errmsg = NULL; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
182 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
183 | request->netbuf = buf; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
184 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
185 | HttpParser *parser = http_parser_new(request); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
186 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
187 | EventHttpIO *io = malloc(sizeof(EventHttpIO)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
188 | if(io == NULL) { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
189 | // TODO: error handling |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
190 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
191 | io->request = request; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
192 | io->parser = parser; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
193 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
194 | /* |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
195 | * to start the request handling, we begin with a poll on the socket, |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
196 | * |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
197 | * evt_enq_conn() --> event handler --> handle_request() |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
198 | */ |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
199 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
200 | event_handler_t *ev = ((EventSessionHandler*)handler)->eventhandler; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
201 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
202 | event_t *event = malloc(sizeof(event_t)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
203 | event->fn = evt_request_input; |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
204 | event->finish = evt_request_finish; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
205 | event->cookie = io; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
206 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
207 | if(ev_pollin(ev, conn->fd, event) != 0) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
208 | // TODO: ev_pollin should log, intercept some errors here |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
48
diff
changeset
|
209 | log_ereport(LOG_WARN, "ev_pollin failed: %s", strerror(errno)); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
210 | close(conn->fd); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
211 | // TODO: free stuff |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
212 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
213 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
214 | |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
215 | int evt_request_input(event_handler_t *handler, event_t *event) { |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
216 | EventHttpIO *io = event->cookie; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
217 | HttpParser *parser = io->parser; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
218 | HTTPRequest *request = io->request; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
219 | Connection *conn = io->request->connection; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
220 | netbuf *buf = request->netbuf; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
221 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
222 | int state; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
223 | int r; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
224 | r = conn->read( |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
225 | conn, |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
226 | buf->inbuf + buf->pos, |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
227 | buf->maxsize - buf->pos); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
228 | if(r == -1) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
229 | event->finish = evt_request_error; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
230 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
231 | } |
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
232 | //fwrite(buf->inbuf + buf->pos, 1, r, stdout); |
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
233 | //printf("\n"); |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
234 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
235 | buf->cursize += r; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
236 | state = http_parser_process(parser); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
237 | if(state == 2) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
238 | // parse error |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
239 | fatal_error(request, 400); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
240 | event->finish = evt_request_error; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
241 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
242 | } else if(state == 1) { |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
243 | /* |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
244 | * we need more data -> return 1 to tell the event handler to |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
245 | * continue polling |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
246 | */ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
247 | return 1; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
248 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
249 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
250 | // we are done with reading |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
251 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
252 | // set socket blocking |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
253 | int flags; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
254 | if (-1 == (flags = fcntl(request->connection->fd, F_GETFL, 0))) { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
255 | flags = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
256 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
257 | if (fcntl(request->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
258 | // just close the connection if fcntl fails |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
259 | event->finish = evt_request_error; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
260 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
261 | } |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
262 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
263 | /* |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
264 | * process request |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
265 | * |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
266 | * We return 0 to finish request input. The event handler than stops |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
267 | * polling and executes event->finish (evt_request_input_finish) |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
268 | */ |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
269 | return 0; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
270 | } |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
271 | |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
272 | int evt_request_finish(event_handler_t *h, event_t *event) { |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
273 | EventHttpIO *io = event->cookie; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
274 | HttpParser *parser = io->parser; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
275 | HTTPRequest *request = io->request; |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
276 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
277 | int r = handle_request(request, NULL); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
278 | if(r != 0) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
279 | // TODO: error message |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
280 | close(request->connection->fd); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
281 | } |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
282 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
283 | /* |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
284 | * handle_request can return before the request is finished, but it copies |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
285 | * all important data. We can free request, parser and event |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
286 | * |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
287 | * don't free request->netbuf and request->connection |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
288 | */ |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
289 | header_array_free(request->headers); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
290 | free(request); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
291 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
292 | http_parser_free(parser); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
293 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
294 | free(io); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
295 | free(event); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
296 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
297 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
298 | } |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
299 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
300 | int evt_request_error(event_handler_t *h, event_t *event) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
301 | EventHttpIO *io = event->cookie; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
302 | HttpParser *parser = io->parser; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
303 | HTTPRequest *request = io->request; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
304 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
305 | close(request->connection->fd); |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
306 | cfg_unref(request->connection->listener->cfg); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
307 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
308 | header_array_free(request->headers); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
309 | free(request); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
310 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
311 | http_parser_free(parser); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
312 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
313 | free(io); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
314 | free(event); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
315 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
316 | return 0; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
317 | } |
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
318 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
319 | void evt_keep_alive(SessionHandler *handler, Connection *conn) { |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
320 | // TODO: set timeout |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
321 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
322 | /* TODO: |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
323 | * Don't just re-enqueue the connection |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
324 | * create a evt_req_init function which does most of the evt_enq_conn stuff |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
325 | * but don't poll. |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
326 | * evt_keep_alive should poll and if an event occurs: |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
327 | * evt_req_init |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
328 | * evt_request_input |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
329 | * evt_enq_conn should do: |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
330 | * evt_req_init |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
331 | * ev_pollin |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
332 | */ |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
333 | evt_enq_conn(handler, conn); |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
334 | } |