Sat, 25 Jan 2020 09:00:27 +0100
fix multistatus response and add propfind tests
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 { |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
43 | HTTPRequest *request; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
44 | HttpParser *parser; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
45 | int error; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
46 | } EventHttpIO; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
47 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
48 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
49 | int connection_read(Connection *conn, void *buf, int len) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
50 | return (int)read(conn->fd, buf, len); |
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 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
53 | 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
|
54 | return (int)write(conn->fd, buf, len); |
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 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
57 | void connection_close(Connection *conn) { |
169
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
58 | while(close(conn->fd)) { |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
59 | if(errno != EINTR) { |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
60 | log_ereport(LOG_VERBOSE, "connection close failed: %s", strerror(errno)); |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
61 | break; |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
62 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
169
diff
changeset
|
63 | log_ereport(LOG_VERBOSE, "connection close: EINTR"); |
169
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
64 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
65 | } |
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 | int connection_ssl_read(Connection *conn, void *buf, int len) { |
128
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
68 | int ret = SSL_read(conn->ssl, buf, len); |
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
69 | if(ret <= 0) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
70 | conn->ssl_error = SSL_get_error(conn->ssl, ret); |
128
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
71 | } |
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
72 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
73 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
74 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
75 | int connection_ssl_write(Connection *conn, const void *buf, int len) { |
128
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
76 | int ret = SSL_write(conn->ssl, buf, len); |
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
77 | if(ret <= 0) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
78 | conn->ssl_error = SSL_get_error(conn->ssl, ret); |
128
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
79 | } |
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
80 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
81 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
82 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
83 | void connection_ssl_close(Connection *conn) { |
142
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
84 | if(!conn->ssl_error) { |
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
85 | int ret = SSL_shutdown(conn->ssl); |
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
86 | if(ret != 1) { |
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
87 | conn->ssl_error = SSL_get_error(conn->ssl, ret); |
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
88 | log_ereport(LOG_VERBOSE, "SSL_shutdown failed: %d", conn->ssl_error); |
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
89 | } |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
90 | } |
169
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
91 | while(close(conn->fd)) { |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
92 | if(errno != EINTR) { |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
93 | log_ereport(LOG_VERBOSE, "connection close failed: %s", strerror(errno)); |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
94 | break; |
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
95 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
169
diff
changeset
|
96 | log_ereport(LOG_VERBOSE, "connection close: EINTR"); |
169
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
97 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
98 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
99 | |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
100 | void connection_destroy(Connection *conn) { |
128
288fd9b9a739
fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
114
diff
changeset
|
101 | conn->close(conn); |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
102 | if(conn->ssl) { |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
103 | SSL_free(conn->ssl); |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
104 | } |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
105 | free(conn); |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
106 | } |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
107 | |
192
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
108 | IOStream* create_connection_iostream( |
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
109 | SessionHandler *sh, |
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
110 | Connection *conn, |
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
111 | pool_handle_t *pool, |
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
112 | WSBool *ssl) |
6a145e13d933
replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
191
diff
changeset
|
113 | { |
191
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
114 | IOStream *io = NULL; |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
115 | if(conn->ssl) { |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
116 | io = sslstream_new(pool, conn->ssl); |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
117 | *ssl = 1; |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
118 | } else { |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
119 | io = sysstream_new(pool, conn->fd); |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
120 | *ssl = 0; |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
121 | } |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
122 | return io; |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
123 | } |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
124 | |
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
125 | |
1 | 126 | SessionHandler* create_basic_session_handler() { |
127 | BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler)); | |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
128 | handler->threadpool = threadpool_new(4, 8); |
1 | 129 | handler->sh.enqueue_connection = basic_enq_conn; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
130 | handler->sh.keep_alive = basic_keep_alive; |
191
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
131 | handler->sh.create_iostream = create_connection_iostream; |
1 | 132 | |
133 | return (SessionHandler*)handler; | |
134 | } | |
135 | ||
136 | void basic_enq_conn(SessionHandler *handler, Connection *conn) { | |
137 | BasicSessionHandler *sh = (BasicSessionHandler*)handler; | |
138 | conn->session_handler = handler; | |
139 | threadpool_run(sh->threadpool, basic_run_session, conn); | |
140 | } | |
141 | ||
142 | void* basic_run_session(void *data) { | |
143 | Connection *conn = (Connection*)data; | |
144 | ||
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
145 | HTTPRequest request; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
146 | http_request_init(&request); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
147 | request.connection = conn; |
1 | 148 | |
149 | // read request | |
150 | netbuf *buf = malloc(sizeof(netbuf)); | |
151 | buf->rdtimeout = 120; | |
152 | buf->pos = 0; | |
153 | buf->cursize = 0; | |
154 | buf->maxsize = 2048; | |
155 | buf->sd = &conn->fd; | |
156 | buf->inbuf = malloc(2048); | |
157 | buf->errmsg = NULL; | |
158 | ||
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
159 | request.netbuf = buf; |
1 | 160 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
161 | HttpParser *parser = http_parser_new(&request); |
1 | 162 | int state; |
163 | int r; | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
164 | r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
165 | if(r <= 0) { |
1 | 166 | // TODO: error handling |
167 | fprintf(stderr, "%s\n", "Error: Cannot read from socket"); | |
168 | return NULL; | |
169 | } | |
170 | buf->cursize += r; | |
171 | while((state = http_parser_process(parser)) != 0) { | |
172 | if(state == 2) { | |
173 | // TODO: error handling | |
174 | fprintf(stderr, "%s\n", "Error: Cannot parse http request"); | |
175 | return NULL; | |
176 | } | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
177 | r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos); |
1 | 178 | if(r == -1) { |
179 | // TODO: error handling | |
180 | fprintf(stderr, "%s\n", "Error: Cannot read from socket"); | |
181 | return NULL; | |
182 | } | |
183 | buf->cursize += r; | |
184 | } | |
180
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
185 | if(!http_parser_validate(parser)) { |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
186 | log_ereport(LOG_FAILURE, "http_parser_validate failed"); |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
187 | // TODO: send error 400 bad request |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
188 | return NULL; |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
189 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
190 | |
1 | 191 | // process request |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
192 | r = handle_request(&request, NULL, NULL); // TODO: use correct thread pool |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
193 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
194 | // TODO: free, see evt_request_finish |
1 | 195 | |
196 | return NULL; | |
197 | } | |
198 | ||
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
199 | void basic_keep_alive(SessionHandler *handler, Connection *conn) { |
135 | 200 | connection_destroy(conn); |
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
201 | } |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
202 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
203 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
204 | /* ----- event session handler ----- */ |
1 | 205 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
206 | SessionHandler* create_event_session_handler() { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
207 | EventSessionHandler *handler = malloc(sizeof(EventSessionHandler)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
208 | handler->eventhandler = get_default_event_handler(); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
209 | handler->sh.enqueue_connection = evt_enq_conn; |
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
210 | handler->sh.keep_alive = evt_keep_alive; |
191
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
211 | handler->sh.create_iostream = create_connection_iostream; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
212 | return (SessionHandler*)handler; |
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 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
215 | 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
|
216 | HTTPRequest *request = malloc(sizeof(HTTPRequest)); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
217 | http_request_init(request); |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
218 | request->connection = conn; |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
219 | conn->session_handler = handler; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
220 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
221 | // set socket non blocking |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
222 | int flags; |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
223 | if ((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
224 | flags = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
225 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
226 | 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
|
227 | perror("Error: start_event_session: fcntl"); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
228 | // TODO: error |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
229 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
230 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
231 | // 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
|
232 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
233 | // read request |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
234 | netbuf *buf = malloc(sizeof(netbuf)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
235 | buf->rdtimeout = 120; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
236 | buf->pos = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
237 | buf->cursize = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
238 | buf->maxsize = 2048; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
239 | buf->sd = &conn->fd; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
240 | buf->inbuf = malloc(2048); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
241 | buf->errmsg = NULL; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
242 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
243 | request->netbuf = buf; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
244 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
245 | HttpParser *parser = http_parser_new(request); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
246 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
247 | EventHttpIO *io = malloc(sizeof(EventHttpIO)); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
248 | if(io == NULL) { |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
249 | // TODO: error handling |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
250 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
251 | io->request = request; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
252 | io->parser = parser; |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
253 | io->error = 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
254 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
255 | /* |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
256 | * 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
|
257 | * |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
258 | * 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
|
259 | */ |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
260 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
261 | Event *event = malloc(sizeof(Event)); |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
262 | ZERO(event, sizeof(Event)); |
194
6345f50208d5
fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
263 | event->fn = conn->ssl && !conn->ssl_accepted ? evt_request_ssl_accept : evt_request_input; |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
264 | event->finish = evt_request_finish; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
265 | event->cookie = io; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
266 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
267 | EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
268 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
269 | if(ev_pollin(ev, conn->fd, event) != 0) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
270 | // TODO: ev_pollin should log, intercept some errors here |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
271 | log_ereport(LOG_FAILURE, "Cannot enqueue connection"); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
272 | connection_destroy(conn); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
273 | // TODO: free stuff |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
274 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
275 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
276 | |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
277 | int evt_request_ssl_accept(EventHandler *handler, Event *event) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
278 | EventHttpIO *io = event->cookie; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
279 | Connection *conn = io->request->connection; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
280 | |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
281 | int ret = SSL_accept(conn->ssl); |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
282 | if(ret <= 0) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
283 | int error = SSL_get_error(conn->ssl, ret); |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
284 | char *errstr; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
285 | switch(error) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
286 | default: errstr = "unknown"; break; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
287 | case SSL_ERROR_WANT_READ: { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
288 | event->events = EVENT_POLLIN; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
289 | return 1; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
290 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
291 | case SSL_ERROR_WANT_WRITE: { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
292 | event->events = EVENT_POLLOUT; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
293 | return 1; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
294 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
295 | case SSL_ERROR_ZERO_RETURN: errstr = "SSL_ERROR_ZERO_RETURN"; break; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
296 | case SSL_ERROR_WANT_CONNECT: errstr = "SSL_ERROR_WANT_CONNECT"; break; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
297 | case SSL_ERROR_WANT_ACCEPT: errstr = "SSL_ERROR_WANT_ACCEPT"; break; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
298 | case SSL_ERROR_WANT_X509_LOOKUP: errstr = "SSL_ERROR_WANT_X509_LOOKUP"; break; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
299 | case SSL_ERROR_SYSCALL: errstr = "SSL_ERROR_SYSCALL"; break; |
197
23fa058ae54b
fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
300 | case SSL_ERROR_SSL: errstr = "SSL_ERROR_SSL"; break; |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
301 | } |
197
23fa058ae54b
fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
302 | log_ereport(LOG_VERBOSE, "SSL accept error[%d]: %s", error, errstr); |
23fa058ae54b
fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
303 | event->finish = evt_request_error; |
23fa058ae54b
fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
304 | io->error = 1; |
23fa058ae54b
fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
195
diff
changeset
|
305 | return 0; |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
306 | } |
194
6345f50208d5
fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
307 | conn->ssl_accepted = WS_TRUE; |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
308 | |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
309 | // SSL_accept successful, start request input now |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
310 | event->fn = evt_request_input; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
311 | return evt_request_input(handler, event); |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
312 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
159
diff
changeset
|
313 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
314 | int evt_request_input(EventHandler *handler, Event *event) { |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
315 | EventHttpIO *io = event->cookie; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
316 | HttpParser *parser = io->parser; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
317 | HTTPRequest *request = io->request; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
318 | Connection *conn = io->request->connection; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
319 | netbuf *buf = request->netbuf; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
320 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
321 | int state; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
322 | int r; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
323 | r = conn->read( |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
324 | conn, |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
325 | buf->inbuf + buf->pos, |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
326 | buf->maxsize - buf->pos); |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
327 | if(r <= 0) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
328 | if(conn->ssl) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
329 | // SSL specific error handling |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
330 | switch(conn->ssl_error) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
331 | case SSL_ERROR_WANT_READ: { |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
332 | event->events = EVENT_POLLIN; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
333 | return 1; |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
334 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
335 | case SSL_ERROR_WANT_WRITE: { |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
336 | event->events = EVENT_POLLOUT; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
337 | return 1; |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
338 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
339 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
340 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
341 | |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
342 | event->finish = evt_request_error; |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
343 | io->error = 1; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
344 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
345 | } |
48
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
346 | //fwrite(buf->inbuf + buf->pos, 1, r, stdout); |
37a512d7b8f6
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
47
diff
changeset
|
347 | //printf("\n"); |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
348 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
349 | buf->cursize += r; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
350 | state = http_parser_process(parser); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
351 | if(state == 2) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
352 | // parse error |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
353 | fatal_error(request, 400); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
354 | event->finish = evt_request_error; |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
355 | io->error = 2; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
356 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
357 | } else if(state == 1) { |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
358 | /* |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
359 | * 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
|
360 | * continue polling |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
361 | */ |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
362 | event->events = EVENT_POLLIN; |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
363 | return 1; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
364 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
365 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
366 | // we are done with reading |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
367 | |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
368 | // set socket blocking |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
369 | int flags; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
370 | 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
|
371 | flags = 0; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
372 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
373 | 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
|
374 | // just close the connection if fcntl fails |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
375 | event->finish = evt_request_error; |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
376 | io->error = 3; |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
377 | return 0; |
180
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
378 | } |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
379 | |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
380 | if(!http_parser_validate(parser)) { |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
381 | log_ereport(LOG_FAILURE, "http_parser_validate failed"); |
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
382 | // TODO: send error 400 bad request |
181
12828065f120
abort on http_parser_validate error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
180
diff
changeset
|
383 | event->finish = evt_request_error; |
12828065f120
abort on http_parser_validate error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
180
diff
changeset
|
384 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
385 | } |
180
98462e878ca7
fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
171
diff
changeset
|
386 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
387 | /* |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
388 | * process request |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
389 | * |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
390 | * 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
|
391 | * 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
|
392 | */ |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
393 | return 0; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
394 | } |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
395 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
396 | int evt_request_finish(EventHandler *h, Event *event) { |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
397 | EventHttpIO *io = event->cookie; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
398 | HttpParser *parser = io->parser; |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
399 | HTTPRequest *request = io->request; |
67
50505dc3f8a6
dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
400 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
401 | int r = handle_request(request, NULL, h); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
402 | if(r != 0) { |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
403 | // TODO: error message |
169
76c96ee18221
check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
162
diff
changeset
|
404 | connection_destroy(request->connection); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
405 | } |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
406 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
407 | /* |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
408 | * 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
|
409 | * 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
|
410 | * |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
411 | * don't free request->netbuf and request->connection |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
412 | */ |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
413 | http_request_cleanup(request); |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
414 | http_parser_free(parser); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
415 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
416 | free(io); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
417 | free(event); |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
418 | |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
419 | return 0; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
420 | } |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
421 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
422 | int evt_request_error(EventHandler *h, Event *event) { |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
423 | EventHttpIO *io = event->cookie; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
424 | HttpParser *parser = io->parser; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
425 | HTTPRequest *request = io->request; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
426 | |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
427 | if(event->error) { |
195
d367b687bc74
fixes double close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
194
diff
changeset
|
428 | log_ereport(LOG_VERBOSE, "sessionhandler http io error: %d fd: %d", io->error, request->connection->fd); |
152
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
429 | } |
8b85c5face66
improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
430 | |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
431 | free(request->netbuf->inbuf); |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
432 | free(request->netbuf); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
433 | |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
434 | cfg_unref(request->connection->listener->cfg); |
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
435 | connection_destroy(request->connection); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
436 | |
114
c3a0f1275d71
fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
437 | http_request_cleanup(request); |
47
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
438 | http_parser_free(parser); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
439 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
440 | free(io); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
441 | free(event); |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
442 | |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
443 | return 0; |
ce9790523346
server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
444 | } |
78
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
445 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
446 | void evt_keep_alive(SessionHandler *handler, Connection *conn) { |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
447 | // TODO: set timeout |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
448 | |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
449 | /* TODO: |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
450 | * Don't just re-enqueue the connection |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
451 | * 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
|
452 | * but don't poll. |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
453 | * 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
|
454 | * evt_req_init |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
455 | * evt_request_input |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
456 | * evt_enq_conn should do: |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
457 | * evt_req_init |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
458 | * ev_pollin |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
459 | */ |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
460 | evt_enq_conn(handler, conn); |
3578977d29a3
added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
67
diff
changeset
|
461 | } |