src/server/daemon/sessionhandler.c

Sun, 15 Sep 2024 09:47:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Sep 2024 09:47:36 +0200
changeset 558
0e79e17c70e2
parent 557
e35829a3a6d8
child 559
3966dc878a28
permissions
-rw-r--r--

improve sessionhandler trace logging

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
33 #include "../public/nsapi.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "sessionhandler.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "httprequest.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
42 struct EventHttpIO {
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;
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
45 EVWatchList watch;
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
46 Event *io_event;
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
47 int error;
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
48 };
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
49
106
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 int connection_read(Connection *conn, void *buf, int len) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
52 return (int)read(conn->fd, buf, len);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
53 }
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 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
56 return (int)write(conn->fd, buf, len);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
57 }
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 void connection_close(Connection *conn) {
169
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
60 while(close(conn->fd)) {
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
61 if(errno != EINTR) {
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
62 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
63 break;
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
64 }
171
af7e2d80dee6 adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 169
diff changeset
65 log_ereport(LOG_VERBOSE, "connection close: EINTR");
169
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
66 }
106
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
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
69 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
70 int ret = SSL_read(conn->ssl, buf, len);
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
71 if(ret <= 0) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
72 conn->ssl_error = SSL_get_error(conn->ssl, ret);
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
73 }
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
74 return ret;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
75 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
76
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
77 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
78 int ret = SSL_write(conn->ssl, buf, len);
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
79 if(ret <= 0) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
80 conn->ssl_error = SSL_get_error(conn->ssl, ret);
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
81 }
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
82 return ret;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
83 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
84
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
85 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
86 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
87 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
88 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
89 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
90 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
91 }
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
92 }
169
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
93 while(close(conn->fd)) {
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
94 if(errno != EINTR) {
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
95 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
96 break;
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
97 }
171
af7e2d80dee6 adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 169
diff changeset
98 log_ereport(LOG_VERBOSE, "connection close: EINTR");
169
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
99 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
100 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
101
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
102 void connection_destroy(Connection *conn) {
558
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
103 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection_destroy", (unsigned long long int)conn->id);
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
104 cfg_unref(conn->listener->cfg);
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
105 conn->close(conn);
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
106 if(conn->ssl) {
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
107 SSL_free(conn->ssl);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
108 }
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
109 free(conn);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
110 }
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
111
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
112 IOStream* create_connection_iostream(
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
113 SessionHandler *sh,
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
114 Connection *conn,
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
115 pool_handle_t *pool,
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
116 WSBool *ssl)
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 191
diff changeset
117 {
191
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
118 IOStream *io = NULL;
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
119 if(conn->ssl) {
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
120 io = sslstream_new(pool, conn->ssl);
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
121 *ssl = 1;
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
122 } else {
430
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
123 io = Sysstream_new(pool, conn->fd);
191
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
124 *ssl = 0;
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
125 }
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
126 return io;
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
127 }
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
128
391ccd490d97 moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 188
diff changeset
129
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
130 SessionHandler* create_basic_session_handler(pool_handle_t *pool) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
131 BasicSessionHandler *handler = pool_malloc(pool, sizeof(BasicSessionHandler));
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
132 handler->threadpool = threadpool_new(4, 8);
357
f45e962edf45 add separate threadpool_start function for creating initial threadpool threads
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
133 threadpool_start(handler->threadpool); // TODO: handle error
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 handler->sh.enqueue_connection = basic_enq_conn;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
135 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
136 handler->sh.create_iostream = create_connection_iostream;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 return (SessionHandler*)handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 void basic_enq_conn(SessionHandler *handler, Connection *conn) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 BasicSessionHandler *sh = (BasicSessionHandler*)handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 conn->session_handler = handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 threadpool_run(sh->threadpool, basic_run_session, conn);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 void* basic_run_session(void *data) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 Connection *conn = (Connection*)data;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
150 HTTPRequest *request = malloc(sizeof(HTTPRequest));
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
151 http_request_init(request);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
152 request->connection = conn;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 // read request
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 netbuf *buf = malloc(sizeof(netbuf));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 buf->rdtimeout = 120;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 buf->pos = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 buf->cursize = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 buf->maxsize = 2048;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 buf->sd = &conn->fd;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 buf->inbuf = malloc(2048);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 buf->errmsg = NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
164 request->netbuf = buf;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
166 HttpParser *parser = http_parser_new(request);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 int state;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 int r;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
169 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
170
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
171 if(r > 0) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
172 int err = 0;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
173 buf->cursize += r;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
174 while((state = http_parser_process(parser)) != 0) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
175 if(state == 2) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
176 log_ereport(LOG_FAILURE, "basic_run_session: invalid http request");
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
177 err = 1;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
178 break;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
179 }
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
180 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
181 if(r == -1) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
182 log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno));
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
183 err = 1;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
184 break;
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
185 }
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
186 buf->cursize += r;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
188
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
189 if(!err) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
190 if(http_parser_validate(parser)) {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
191 // process request
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
192 r = handle_request(request, NULL, NULL); // TODO: use correct thread pool
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
193 } else {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
194 log_ereport(LOG_FAILURE, "basic_run_session: http parser validation failed");
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
195 fatal_error(request, 400);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
196 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
198 } else {
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
199 log_ereport(LOG_FAILURE, "basic_run_session: IO error: %s", strerror(errno));
180
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
200 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
201
410
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
202 free(buf->inbuf);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
203 free(buf);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
204 connection_destroy(conn);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
205 http_parser_free(parser);
8f4d28ac6ae2 improve error handling in basic_run_session
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 357
diff changeset
206 http_request_cleanup(request);
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
207
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
211 void basic_keep_alive(SessionHandler *handler, Connection *conn) {
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
212 connection_destroy(conn);
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
213 }
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
214
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
215
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
216 /* ----- event session handler ----- */
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
218 SessionHandler* create_event_session_handler(pool_handle_t *pool) {
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
219 EventSessionHandler *handler = pool_malloc(pool, sizeof(EventSessionHandler));
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
220 handler->eventhandler = get_default_event_handler();
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
221 handler->sh.enqueue_connection = evt_enq_conn;
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
222 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
223 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
224 return (SessionHandler*)handler;
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
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
227 void evt_request_timeout(EventHandler *h, EVWatchList *item) {
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
228 log_ereport(LOG_VERBOSE, "sessionhandler: request timeout");
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
229 item->intdata = 0;
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
230
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
231 EventHttpIO *io = item->data1;
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
232 io->error = 4;
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
233
558
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
234 log_ereport(LOG_DEBUG, "trace reqid: %016llx request timeout", (unsigned long long int)io->request->connection->id);
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
235
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
236 if(ev_remove_poll(h, io->request->connection->fd)) {
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
237 log_ereport(LOG_FAILURE, "sessionhandler: request timeout: cannot remove poll");
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
238 }
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
239 evt_request_error(h, io->io_event);
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
240 }
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
241
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
242 int evt_add_request(EventHandler *h, Event *event) {
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
243 EventHttpIO *io = event->cookie;
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
244 Connection *conn = io->request->connection;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
245
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
246 if(ev_pollin(h, conn->fd, io->io_event) != 0) {
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
247 // TODO: ev_pollin should log, intercept some errors here
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
248 log_ereport(LOG_FAILURE, "Cannot enqueue connection");
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
249 evt_request_error(h, event);
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
250 } else {
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
251 // add request timeout
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
252 io->watch.intdata = 1;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
253 io->watch.created = time(NULL);
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
254 io->watch.expire = io->watch.created + 240; // TODO: config
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
255 io->watch.destroy = evt_request_timeout;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
256 io->watch.data1 = io;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
257 ev_watchlist_add(h, &io->watch);
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
258 }
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
259
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
260 return 0;
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
261 }
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
262
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
263 void evt_enq_conn(SessionHandler *handler, Connection *conn) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
264 log_ereport(LOG_DEBUG, "trace reqid: %016llx enqueue connection", (unsigned long long int)conn->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
265
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
266 Event *start_request = malloc(sizeof(Event));
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
267 if(!start_request) {
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
268 connection_destroy(conn);
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
269 return;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
270 }
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
271
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
272 Event *event = malloc(sizeof(Event));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
273 if(!event) {
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
274 connection_destroy(conn);
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
275 free(start_request);
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
276 return;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
277 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
278
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
279 EventHttpIO *io = evt_req_init(handler, conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
280 if(!io) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
281 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | evt_req_init failed", (unsigned long long int)conn->id);
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
282 connection_destroy(conn);
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
283 free(start_request);
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
284 free(event);
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
285 return;
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
286 }
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
287
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
288 ZERO(event, sizeof(Event));
194
6345f50208d5 fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
289 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
290 event->finish = evt_request_finish;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
291 event->cookie = io;
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
292 io->io_event = event;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
293
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
294 /*
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
295 * to start the request handling, switch to the event handler
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
296 *
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
297 * evt_enq_conn() --> event handler --> handle_request()
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
298 */
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
299
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
300 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler);
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
301
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
302 ZERO(start_request, sizeof(Event));
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
303 start_request->cookie = io;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
304 start_request->fn = evt_add_request;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
305 start_request->finish = ev_free_event;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
306 start_request->error = 0;
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
307 if(event_send(ev, start_request)) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
308 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | event_send failed", (unsigned long long int)conn->id);
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
309 log_ereport(LOG_FAILURE, "Cannot start request timeout: event_send failed");
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
310 evt_request_error(ev, event);
555
66b0accda0a8 move first request poll to the event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 554
diff changeset
311 free(start_request);
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
312 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
313 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
314
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
315 EventHttpIO* evt_req_init(SessionHandler *handler, Connection *conn) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
316 // set socket non blocking
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
317 int flags;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
318 if ((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
319 flags = 0;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
320 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
321 if (fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK) != 0) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
322 log_ereport(LOG_FAILURE, "sessionhandler: fcntl failed: %s", strerror(errno));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
323 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
324 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
325
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
326 HTTPRequest *request = malloc(sizeof(HTTPRequest));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
327 if(!request) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
328 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
329 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
330 http_request_init(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
331 request->connection = conn;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
332 conn->session_handler = handler;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
333
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
334 // TODO: remove code redundancy (basic_run_session)
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
335
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
336 // read request
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
337 netbuf *buf = malloc(sizeof(netbuf));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
338 if(!buf) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
339 http_request_cleanup(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
340 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
341 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
342 buf->rdtimeout = 120;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
343 buf->pos = 0;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
344 buf->cursize = 0;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
345 buf->maxsize = 2048;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
346 buf->sd = &conn->fd;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
347 buf->errmsg = NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
348 buf->inbuf = malloc(2048);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
349 if(!buf->inbuf) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
350 http_request_cleanup(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
351 free(buf);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
352 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
353 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
354
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
355 request->netbuf = buf;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
356
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
357 HttpParser *parser = http_parser_new(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
358 if(!parser) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
359 http_request_cleanup(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
360 free(buf->inbuf);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
361 free(buf);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
362 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
363 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
364
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
365 EventHttpIO *io = malloc(sizeof(EventHttpIO));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
366 if(io == NULL) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
367 http_request_cleanup(request);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
368 free(buf->inbuf);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
369 free(buf);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
370 http_parser_free(parser);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
371 return NULL;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
372 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
373 io->request = request;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
374 io->parser = parser;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
375 io->error = 0;
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
376 ZERO(&io->watch, sizeof(EVWatchList));
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
377
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
378 return io;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
379 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
380
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
381 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
382 EventHttpIO *io = event->cookie;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
383 Connection *conn = io->request->connection;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
384
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
385 log_ereport(LOG_DEBUG, "trace reqid: %016llx ssl accept", (unsigned long long int)conn->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
386
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
387 int ret = SSL_accept(conn->ssl);
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
388 if(ret <= 0) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
389 log_ereport(LOG_DEBUG, "trace reqid: %016llx ssl accept | error", (unsigned long long int)conn->id);
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
390 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
391 char *errstr;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
392 switch(error) {
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
393 default: errstr = "unknown"; break;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
394 case SSL_ERROR_WANT_READ: {
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
395 event->events = EVENT_POLLIN;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
396 return 1;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
397 }
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
398 case SSL_ERROR_WANT_WRITE: {
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
399 event->events = EVENT_POLLOUT;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
400 return 1;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
401 }
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
402 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
403 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
404 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
405 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
406 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
407 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
408 }
197
23fa058ae54b fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 195
diff changeset
409 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
410 event->finish = evt_request_error;
23fa058ae54b fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 195
diff changeset
411 io->error = 1;
23fa058ae54b fixes ssl accept error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 195
diff changeset
412 return 0;
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
413 }
194
6345f50208d5 fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
414 conn->ssl_accepted = WS_TRUE;
188
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
415
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
416 // 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
417 event->fn = evt_request_input;
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
418 return evt_request_input(handler, event);
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
419 }
0e6a05c779e0 using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 159
diff changeset
420
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
421 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
422 EventHttpIO *io = event->cookie;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
423 HttpParser *parser = io->parser;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
424 HTTPRequest *request = io->request;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
425 Connection *conn = io->request->connection;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
426 netbuf *buf = request->netbuf;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
427
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
428 int state;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
429 int r;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
430 r = conn->read(
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
431 conn,
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
432 buf->inbuf + buf->pos,
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
433 buf->maxsize - buf->pos);
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
434
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
435 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | r=%d", (unsigned long long int)conn->id, r);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
436
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
437 if(r <= 0) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
438 if(conn->ssl) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
439 // SSL specific error handling
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
440 switch(conn->ssl_error) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
441 case SSL_ERROR_WANT_READ: {
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
442 event->events = EVENT_POLLIN;
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
443 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | ssl want read", (unsigned long long int)conn->id);
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
444 return 1;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
445 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
446 case SSL_ERROR_WANT_WRITE: {
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
447 event->events = EVENT_POLLOUT;
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
448 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | ssl want write", (unsigned long long int)conn->id);
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
449 return 1;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
450 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
451 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
452 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
453
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
454 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | error", (unsigned long long int)conn->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
455
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
456 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
457 io->error = 1;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
458 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
459 }
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
460 //fwrite(buf->inbuf + buf->pos, 1, r, stdout);
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
461 //printf("\n");
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
462
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
463 buf->cursize += r;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
464 state = http_parser_process(parser);
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
465 if(state == 2) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
466 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | http parser error", (unsigned long long int)conn->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
467
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
468 // parse error
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
469 fatal_error(request, 400);
354
017eda1be105 add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 197
diff changeset
470 log_ereport(LOG_VERBOSE, "http parser: bad request");
017eda1be105 add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 197
diff changeset
471 //printf("\n\n%.*s\n\n", parser->request->netbuf->cursize, parser->request->netbuf->inbuf);
017eda1be105 add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 197
diff changeset
472 //fflush(stdout);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
473 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
474 io->error = 2;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
475 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
476 } else if(state == 1) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
477 /*
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
478 * 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
479 * continue polling
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
480 */
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
481 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | event_pollin", (unsigned long long int)conn->id);
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
482 event->events = EVENT_POLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
483 return 1;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
484 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
485
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
486 // we are done with reading
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
487
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
488 // set socket blocking
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
489 int flags;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
490 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
491 flags = 0;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
492 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
493 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
494 // just close the connection if fcntl fails
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
495 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | fcntl error", (unsigned long long int)conn->id);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
496 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
497 io->error = 3;
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
498 return 0;
180
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
499 }
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
500
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
501 if(!http_parser_validate(parser)) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
502 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | http parser validate error", (unsigned long long int)conn->id);
180
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
503 log_ereport(LOG_FAILURE, "http_parser_validate failed");
411
bbd82eee568e improve sessionhandler error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
504 fatal_error(request, 400);
181
12828065f120 abort on http_parser_validate error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
505 event->finish = evt_request_error;
12828065f120 abort on http_parser_validate error
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 180
diff changeset
506 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
507 }
180
98462e878ca7 fixes crash with broken http requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 171
diff changeset
508
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
509 /*
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
510 * process request
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
511 *
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
512 * 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
513 * 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
514 */
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
515 return 0;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
516 }
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
517
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
518 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
519 EventHttpIO *io = event->cookie;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
520 HttpParser *parser = io->parser;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
521 HTTPRequest *request = io->request;
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
522
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
523 log_ereport(LOG_DEBUG, "trace reqid: %016llx request finish", (unsigned long long int)request->connection->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
524
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
525 // remove timeout
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
526 if(io->watch.intdata) {
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
527 ev_watchlist_remove(h, &io->watch);
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
528 }
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
529
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
530 int r = handle_request(request, NULL, h);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
531 if(r != 0) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
532 log_ereport(LOG_DEBUG, "trace reqid: %016llx request finish | handle_request failed", (unsigned long long int)request->connection->id);
169
76c96ee18221 check close return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 162
diff changeset
533 connection_destroy(request->connection);
544
27684460629f fix memory leak in case handle_request fails (some illegal requests)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
534 free(request->netbuf->inbuf);
27684460629f fix memory leak in case handle_request fails (some illegal requests)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
535 free(request->netbuf);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
536 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
537
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
538 /*
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
539 * 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
540 * 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
541 *
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
542 * don't free request->netbuf and request->connection
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
543 */
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
544 http_request_cleanup(request);
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
545 http_parser_free(parser);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
546
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
547 free(io);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
548 free(event);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
549
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
550 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
551 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
552
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
553 int evt_request_error(EventHandler *h, Event *event) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
554 EventHttpIO *io = event->cookie;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
555 HttpParser *parser = io->parser;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
556 HTTPRequest *request = io->request;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
557
558
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
558 log_ereport(LOG_DEBUG, "trace reqid: %016llx request error", (unsigned long long int)request->connection->id);
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
559
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
560 if(event->error) {
195
d367b687bc74 fixes double close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 194
diff changeset
561 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
562 }
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
563
554
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
564 // remove timeout
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
565 if(io->watch.intdata) {
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
566 ev_watchlist_remove(h, &io->watch);
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
567 }
e0a6b761ddbc add request timeout handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 546
diff changeset
568
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
569 free(request->netbuf->inbuf);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
570 free(request->netbuf);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
571
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
572 connection_destroy(request->connection);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
573
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
574 http_request_cleanup(request);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
575 http_parser_free(parser);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
576
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
577 free(io);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
578 free(event);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
579
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
580 return 0;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
581 }
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
582
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
583 void evt_keep_alive(SessionHandler *handler, Connection *conn) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
584 log_ereport(LOG_DEBUG, "trace reqid: %016llx keep alive", (unsigned long long int)conn->id);
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
585
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
586 conn->id++;
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
587 Event *event = malloc(sizeof(Event));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
588 if(!event) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
589 connection_destroy(conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
590 return;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
591 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
592
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
593 ZERO(event, sizeof(Event));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
594 event->fn = evt_keep_alive_enqueue;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
595 event->finish = ev_free_event; // this will free the event obj at the end
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
596 event->cookie = conn;
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
597
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
598 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
599 if(event_send(ev, event)) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
600 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep_alive event error", (unsigned long long int)conn->id);
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
601 log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed");
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
602 connection_destroy(conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
603 free(event);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
604 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
605 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
606
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
607 int evt_keep_alive_enqueue(EventHandler *h, Event *event) {
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
608 Connection *conn = event->cookie;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
609
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
610 EVWatchList *keepalive = malloc(sizeof(EVWatchList));
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
611 if(!keepalive) {
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
612 connection_destroy(conn);
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
613 return 0;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
614 }
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
615
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
616 Event *ioevent = malloc(sizeof(Event));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
617 if(!ioevent) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
618 connection_destroy(conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
619 free(keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
620 return 0;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
621 }
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
622
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
623 // add keepalive object to the eventhandler watchlist
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
624 // the watchlist will check the timeout
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
625 ZERO(keepalive, sizeof(EVWatchList));
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
626 keepalive->data1 = conn;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
627 keepalive->data2 = ioevent;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
628 keepalive->destroy = evt_keep_alive_destroy;
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
629 keepalive->created = time(NULL);
546
5494c28db896 increase keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
630 keepalive->expire = keepalive->created + 120; // TODO: config
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
631 ev_watchlist_add(h, keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
632
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
633 // wait for input
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
634 ZERO(ioevent, sizeof(Event));
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
635 ioevent->fn = evt_keep_alive_input_event;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
636 ioevent->finish = ev_free_event;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
637 ioevent->cookie = keepalive;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
638 if(ev_pollin(h, conn->fd, ioevent) != 0) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
639 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep_alive_enqueue", (unsigned long long int)conn->id);
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
640 log_ereport(LOG_FAILURE, "Cannot enqueue connection");
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
641 ev_watchlist_remove(h, keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
642 connection_destroy(conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
643 free(keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
644 free(ioevent);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
645 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
646
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
647 return 0;
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
648 }
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
649
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
650 int evt_keep_alive_input_event(EventHandler *h, Event *event) {
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
651 EVWatchList *keepalive = event->cookie;
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
652 Connection *conn = keepalive->data1;
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
653
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
654 // remove connection from the keep-alive list
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
655 ev_watchlist_remove(h, keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
656 free(keepalive);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
657
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
658 // prepare http io
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
659 EventHttpIO *io = evt_req_init(conn->session_handler, conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
660 if(!io) {
557
e35829a3a6d8 add sessionhandler debug logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 555
diff changeset
661 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep alive input", (unsigned long long int)conn->id);
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
662 connection_destroy(conn);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
663 return 0;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
664 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
665
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
666 // pass this event to the request input function
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
667 // the event object needs some adjustments for this (see evt_enq_conn)
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
668 event->cookie = io;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
669 event->fn = conn->ssl && !conn->ssl_accepted ? evt_request_ssl_accept : evt_request_input;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
670 event->finish = evt_request_finish;
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
671 return event->fn(h, event);
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
672 }
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
673
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
674 void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item) {
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
675 Connection *conn = item->data1;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
676 Event *ioevent = item->data2;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
677 log_ereport(LOG_DEBUG, "sessionhandler: keep-alive timeout: close connection");
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
678
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
679 if(ev_remove_poll(h, conn->fd)) {
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
680 log_ereport(LOG_FAILURE, "sessionhandler: keep-alive timeout: cannot remove poll");
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
681 }
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
682
558
0e79e17c70e2 improve sessionhandler trace logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 557
diff changeset
683 log_ereport(LOG_DEBUG, "trace reqid: %016llx keep_alive timeout", (unsigned long long int)conn->id);
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
684 connection_destroy(conn);
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
685 free(ioevent);
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
686 free(item);
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
687 }

mercurial