src/server/daemon/sessionhandler.c

Wed, 25 Jan 2017 11:50:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 25 Jan 2017 11:50:19 +0100
branch
srvctrl
changeset 158
77f4f0079428
parent 152
8b85c5face66
child 159
9ba9f8befa80
child 161
aadda87bad1b
permissions
-rw-r--r--

adds reconfig and shutdown handler to srvctrl

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
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
42 typedef struct _event_http_io {
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
43 HTTPRequest *request;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
44 HttpParser *parser;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
45 int error;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
46 } EventHttpIO;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
47
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
48
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
49 int connection_read(Connection *conn, void *buf, int len) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
50 return (int)read(conn->fd, buf, len);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
51 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
52
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
53 int connection_write(Connection *conn, const void *buf, int len) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
54 return (int)write(conn->fd, buf, len);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
55 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
56
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
57 void connection_close(Connection *conn) {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
58 close(conn->fd);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
59 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
60
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
61 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
62 int ret = SSL_read(conn->ssl, buf, len);
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
63 if(ret <= 0) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
64 conn->ssl_error = SSL_get_error(conn->ssl, ret);
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
65 }
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
66 return ret;
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_write(Connection *conn, const void *buf, int len) {
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
70 int ret = SSL_write(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 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
78 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
79 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
80 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
81 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
82 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
83 }
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
84 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
85 close(conn->fd);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
86 }
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
87
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
88 void connection_destroy(Connection *conn) {
128
288fd9b9a739 fixes ssl shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 114
diff changeset
89 conn->close(conn);
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
90 if(conn->ssl) {
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
91 SSL_free(conn->ssl);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
92 }
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
93 free(conn);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
94 }
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
95
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 SessionHandler* create_basic_session_handler() {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 BasicSessionHandler *handler = malloc(sizeof(BasicSessionHandler));
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
98 handler->threadpool = threadpool_new(4, 8);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 handler->sh.enqueue_connection = basic_enq_conn;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
100 handler->sh.keep_alive = basic_keep_alive;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return (SessionHandler*)handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 void basic_enq_conn(SessionHandler *handler, Connection *conn) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 BasicSessionHandler *sh = (BasicSessionHandler*)handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 conn->session_handler = handler;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 threadpool_run(sh->threadpool, basic_run_session, conn);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 void* basic_run_session(void *data) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 Connection *conn = (Connection*)data;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
114 HTTPRequest request;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
115 http_request_init(&request);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
116 request.connection = conn;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 // read request
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 netbuf *buf = malloc(sizeof(netbuf));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 buf->rdtimeout = 120;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 buf->pos = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 buf->cursize = 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 buf->maxsize = 2048;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 buf->sd = &conn->fd;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 buf->inbuf = malloc(2048);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 buf->errmsg = NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
128 request.netbuf = buf;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
130 HttpParser *parser = http_parser_new(&request);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 int state;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 int r;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
133 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
134 if(r <= 0) {
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 // TODO: error handling
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 fprintf(stderr, "%s\n", "Error: Cannot read from socket");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 buf->cursize += r;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 while((state = http_parser_process(parser)) != 0) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 if(state == 2) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 // TODO: error handling
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 fprintf(stderr, "%s\n", "Error: Cannot parse http request");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
146 r = conn->read(conn, buf->inbuf + buf->pos, buf->maxsize - buf->pos);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 if(r == -1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 // TODO: error handling
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 fprintf(stderr, "%s\n", "Error: Cannot read from socket");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 buf->cursize += r;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
154
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 // process request
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
156 r = handle_request(&request, NULL); // TODO: use correct thread pool
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
157
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
158 // TODO: free, see evt_request_finish
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
163 void basic_keep_alive(SessionHandler *handler, Connection *conn) {
135
471e28cca288 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
164 connection_destroy(conn);
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
165 }
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
166
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
167
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
168 /* ----- event session handler ----- */
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
170 SessionHandler* create_event_session_handler() {
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
171 EventSessionHandler *handler = malloc(sizeof(EventSessionHandler));
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
172 handler->eventhandler = get_default_event_handler();
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
173 handler->sh.enqueue_connection = evt_enq_conn;
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
174 handler->sh.keep_alive = evt_keep_alive;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
175 return (SessionHandler*)handler;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
176 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
177
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
178 void evt_enq_conn(SessionHandler *handler, Connection *conn) {
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
179 HTTPRequest *request = malloc(sizeof(HTTPRequest));
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
180 http_request_init(request);
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
181 request->connection = conn;
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
182 conn->session_handler = handler;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
183
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
184 // set socket non blocking
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
185 int flags;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
186 if (-1 == (flags = fcntl(conn->fd, F_GETFL, 0))) {
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
187 flags = 0;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
188 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
189 if (fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK) != 0) {
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
190 perror("Error: start_event_session: fcntl");
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
191 // TODO: error
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
192 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
193
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
194 // TODO: remove code redundancy (basic_run_session)
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
195
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
196 // read request
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
197 netbuf *buf = malloc(sizeof(netbuf));
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
198 buf->rdtimeout = 120;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
199 buf->pos = 0;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
200 buf->cursize = 0;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
201 buf->maxsize = 2048;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
202 buf->sd = &conn->fd;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
203 buf->inbuf = malloc(2048);
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
204 buf->errmsg = NULL;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
205
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
206 request->netbuf = buf;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
207
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
208 HttpParser *parser = http_parser_new(request);
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
209
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
210 EventHttpIO *io = malloc(sizeof(EventHttpIO));
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
211 if(io == NULL) {
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
212 // TODO: error handling
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
213 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
214 io->request = request;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
215 io->parser = parser;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
216 io->error = 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
217
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
218 /*
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
219 * to start the request handling, we begin with a poll on the socket,
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
220 *
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
221 * evt_enq_conn() --> event handler --> handle_request()
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
222 */
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
223
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
224 event_handler_t *ev = ((EventSessionHandler*)handler)->eventhandler;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
225
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
226 event_t *event = malloc(sizeof(event_t));
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
227 ZERO(event, sizeof(event_t));
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
228 event->fn = evt_request_input;
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
229 event->finish = evt_request_finish;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
230 event->cookie = io;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
231
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
232 if(ev_pollin(ev, conn->fd, event) != 0) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
233 // TODO: ev_pollin should log, intercept some errors here
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
234 log_ereport(LOG_FAILURE, "Cannot enqueue connection");
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
235 connection_destroy(conn);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
236 // TODO: free stuff
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
237 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
238 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
239
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
240 int evt_request_input(event_handler_t *handler, event_t *event) {
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
241 EventHttpIO *io = event->cookie;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
242 HttpParser *parser = io->parser;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
243 HTTPRequest *request = io->request;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
244 Connection *conn = io->request->connection;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
245 netbuf *buf = request->netbuf;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
246
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
247 int state;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
248 int r;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
249 r = conn->read(
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
250 conn,
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
251 buf->inbuf + buf->pos,
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
252 buf->maxsize - buf->pos);
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
253 if(r <= 0) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
254 if(conn->ssl) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
255 // SSL specific error handling
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
256 switch(conn->ssl_error) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
257 case SSL_ERROR_WANT_READ: {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
258 event->poll = EVENT_POLLIN;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
259 return 1;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
260 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
261 case SSL_ERROR_WANT_WRITE: {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
262 event->poll = EVENT_POLLOUT;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
263 return 1;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
264 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
265 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
266 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
267
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
268 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
269 io->error = 1;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
270 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
271 }
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
272 //fwrite(buf->inbuf + buf->pos, 1, r, stdout);
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 47
diff changeset
273 //printf("\n");
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
274
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
275 buf->cursize += r;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
276 state = http_parser_process(parser);
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
277 if(state == 2) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
278 // parse error
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
279 fatal_error(request, 400);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
280 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
281 io->error = 2;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
282 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
283 } else if(state == 1) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
284 /*
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
285 * 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
286 * continue polling
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
287 */
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 129
diff changeset
288 event->poll = EVENT_POLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
289 return 1;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
290 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
291
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
292 // we are done with reading
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
293
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
294 // set socket blocking
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
295 int flags;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
296 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
297 flags = 0;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
298 }
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
299 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
300 // just close the connection if fcntl fails
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
301 event->finish = evt_request_error;
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
302 io->error = 3;
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
303 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
304 }
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
305
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
306 /*
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
307 * process request
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
308 *
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
309 * 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
310 * 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
311 */
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
312 return 0;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
313 }
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
314
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
315 int evt_request_finish(event_handler_t *h, event_t *event) {
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
316 EventHttpIO *io = event->cookie;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
317 HttpParser *parser = io->parser;
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
318 HTTPRequest *request = io->request;
67
50505dc3f8a6 dynamic thread pool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
319
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
320 int r = handle_request(request, NULL);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
321 if(r != 0) {
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
322 // TODO: error message
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
323 close(request->connection->fd);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
324 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
325
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
326 /*
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
327 * 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
328 * 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
329 *
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
330 * don't free request->netbuf and request->connection
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
331 */
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
332 http_request_cleanup(request);
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
333 http_parser_free(parser);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
334
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
335 free(io);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
336 free(event);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
337
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
338 return 0;
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
339 }
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
340
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
341 int evt_request_error(event_handler_t *h, event_t *event) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
342 EventHttpIO *io = event->cookie;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
343 HttpParser *parser = io->parser;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
344 HTTPRequest *request = io->request;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
345
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
346 if(event->error) {
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
347 log_ereport(LOG_VERBOSE, "sessionhandler http io error: %d", io->error);
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
348 }
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
349
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
350 free(request->netbuf->inbuf);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
351 free(request->netbuf);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
352
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
353 cfg_unref(request->connection->listener->cfg);
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
354 connection_destroy(request->connection);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
355
114
c3a0f1275d71 fixed keep alive bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
356 http_request_cleanup(request);
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
357 http_parser_free(parser);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
358
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
359 free(io);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
360 free(event);
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
361
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
362 return 0;
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
363 }
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
364
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
365 void evt_keep_alive(SessionHandler *handler, Connection *conn) {
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
366 // TODO: set timeout
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
367
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
368 /* TODO:
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
369 * Don't just re-enqueue the connection
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
370 * create a evt_req_init function which does most of the evt_enq_conn stuff
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
371 * but don't poll.
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
372 * evt_keep_alive should poll and if an event occurs:
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
373 * evt_req_init
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
374 * evt_request_input
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
375 * evt_enq_conn should do:
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
376 * evt_req_init
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
377 * ev_pollin
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
378 */
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
379 evt_enq_conn(handler, conn);
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 67
diff changeset
380 }

mercurial