Tue, 19 Apr 2022 19:14:49 +0200
add function for registering webdav backends
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
14
b8bf95b39952
New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
9
diff
changeset
|
29 | #include "../public/nsapi.h" |
1 | 30 | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <fcntl.h> | |
34 | #include <sys/shm.h> | |
35 | #include <sys/types.h> | |
36 | #include <sys/ipc.h> | |
37 | #include <sys/socket.h> | |
38 | #include <sys/file.h> | |
39 | #include <netinet/in.h> | |
40 | #include <netdb.h> | |
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include <fcntl.h> | |
44 | #include <unistd.h> | |
45 | #include <strings.h> | |
46 | #include <stdbool.h> | |
47 | #include <pthread.h> | |
48 | ||
92
382bff43c6eb
fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
87
diff
changeset
|
49 | #include <ucx/map.h> |
382bff43c6eb
fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
87
diff
changeset
|
50 | |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
38
diff
changeset
|
51 | #include "../util/atomic.h" |
1 | 52 | #include "httplistener.h" |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
169
diff
changeset
|
53 | #include "netsite.h" |
1 | 54 | |
55 | #include "session.h" | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
56 | #include "configmanager.h" |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
57 | #include "log.h" |
1 | 58 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
59 | UcxMap *listener_map = NULL; |
1 | 60 | |
61 | int start_all_listener() { | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
62 | ServerConfiguration *conf = cfgmgr_get_server_config(); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
63 | UcxList *ls = conf->listeners; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
64 | while(ls) { |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
65 | HttpListener *listener = ls->data; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
66 | http_listener_start(listener); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
67 | ls = ls->next; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
68 | } |
1 | 69 | |
70 | return 0; | |
71 | } | |
72 | ||
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
73 | HttpListener* http_listener_create(ListenerConfig *conf) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
74 | if(listener_map == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
75 | listener_map = ucx_map_new(16); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
76 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
77 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
78 | HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
79 | if(fl == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
80 | return http_listener_new(conf); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
81 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
82 | |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
83 | HttpListener* newls = calloc(1, sizeof(HttpListener)); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
84 | if(newls == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
85 | // TODO: error |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
86 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
87 | |
61
c858850f3d3a
improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
88 | newls->name = conf->name; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
89 | newls->cfg = conf->cfg; |
61
c858850f3d3a
improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
90 | newls->nacceptors = conf->nacceptors; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
91 | newls->default_vs.vs_name = conf->vs.ptr; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
92 | newls->port = fl->port; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
93 | newls->server_socket = fl->server_socket; |
74
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
94 | newls->running = 1; |
81
d25825f37967
preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
95 | newls->threadpool = NULL; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
96 | newls->ref = 2; // 1 reference is fl->next |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
97 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
98 | newls->session_handler = fl->session_handler; // TODO |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
99 | |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
100 | // the listener threadpool might be changed |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
101 | if(conf->threadpool.ptr != NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
102 | newls->threadpool = get_threadpool(conf->threadpool); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
103 | } |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
104 | if(newls->threadpool == NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
105 | newls->threadpool = get_default_threadpool(); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
106 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
107 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
108 | // create acceptor threads |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
109 | newls->acceptors = calloc(newls->nacceptors, sizeof(void*)); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
110 | for (int i=0;i<newls->nacceptors;i++) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
111 | newls->acceptors[i] = acceptor_new(newls); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
112 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
113 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
114 | // fl hold one reference of newls |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
115 | fl->next = newls; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
116 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
117 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
118 | ucx_map_sstr_put(listener_map, newls->name, newls); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
119 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
120 | for (int i=0;i<newls->nacceptors;i++) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
121 | acceptor_start(newls->acceptors[i]); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
122 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
123 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
124 | // check if a restart is required to apply all changes |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
125 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
126 | if(newls->port != conf->port) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
127 | // TODO: log |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
128 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
129 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
130 | return newls; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
131 | } |
1 | 132 | |
133 | HttpListener* http_listener_new(ListenerConfig *conf) { | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
134 | // TODO: remove |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
135 | if(listener_map == NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
136 | listener_map = ucx_map_new(16); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
137 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
138 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
139 | HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
140 | if(fl != NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
141 | return fl; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
142 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
143 | // end remove |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
144 | |
1 | 145 | HttpListener *listener = malloc(sizeof(HttpListener)); |
74
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
146 | listener->running = 0; |
38
d07810b02147
added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
147 | listener->cfg = conf->cfg; |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
148 | listener->name = conf->name; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
149 | listener->default_vs.vs_name = conf->vs.ptr; |
81
d25825f37967
preparation for admin interface
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
78
diff
changeset
|
150 | listener->threadpool = NULL; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
151 | if(conf->threadpool.ptr != NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
152 | listener->threadpool = get_threadpool(conf->threadpool); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
153 | } |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
154 | if(listener->threadpool == NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
155 | listener->threadpool = get_default_threadpool(); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
156 | } |
134
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
157 | if(conf->blockingio) { |
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
158 | listener->session_handler = create_basic_session_handler(); |
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
159 | } else { |
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
160 | listener->session_handler = create_event_session_handler(); |
44415e4399ce
adds blockingio config
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
161 | } |
1 | 162 | listener->nacceptors = conf->nacceptors; |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
163 | listener->port = conf->port; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
164 | listener->ref = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
165 | listener->next = NULL; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
166 | listener->ssl = NULL; |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
167 | |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
168 | int error = 0; |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
169 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
170 | if(conf->ssl) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
171 | listener->ssl = malloc(sizeof(HttpSSL)); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
172 | |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
173 | SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method()); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
174 | SSL_CTX_set_options( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
175 | ctx, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
176 | SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv3); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
177 | if(conf->disable_proto.ptr) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
178 | ssize_t n = 0; |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
179 | sstr_t *plist = sstrsplit(conf->disable_proto, S(","), &n); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
180 | if(plist) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
181 | for(int i=0;i<n;i++) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
182 | sstr_t proto = plist[i]; |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
183 | log_ereport( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
184 | LOG_VERBOSE, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
185 | "Listener %s: Disable protocol %s", |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
186 | listener->name.ptr, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
187 | proto.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
188 | if(!sstrcasecmp(sstrtrim(proto), S("SSLv2"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
189 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
190 | } else if(!sstrcasecmp(sstrtrim(proto), S("SSLv3"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
191 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
192 | } else if(!sstrcasecmp(sstrtrim(proto), S("TLSv1"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
193 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
194 | } else if(!sstrcasecmp(sstrtrim(proto), S("TLSv1.1"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
195 | #ifdef SSL_OP_NO_TLSv1_1 |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
196 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
197 | #else |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
198 | log_ereport( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
199 | LOG_WARN, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
200 | "Listener: %s: TLSv1.1 already not supported", |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
201 | listener->name.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
202 | #endif |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
203 | } else if(sstrcasecmp(sstrtrim(proto), S("TLSv1.2"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
204 | #ifdef SSL_OP_NO_TLSv1_2 |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
205 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
206 | #else |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
207 | log_ereport( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
208 | LOG_WARN, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
209 | "Listener: %s: TLSv1.2 already not supported", |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
210 | listener->name.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
211 | #endif |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
212 | } else if(sstrcasecmp(sstrtrim(proto), S("TLSv1.3"))) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
213 | #ifdef SSL_OP_NO_TLSv1_3 |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
214 | SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
215 | #else |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
216 | log_ereport( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
217 | LOG_WARN, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
218 | "Listener: %s: TLSv1.3 already not supported", |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
219 | listener->name.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
220 | #endif |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
221 | } else { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
222 | error = 1; |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
223 | log_ereport( |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
224 | LOG_MISCONFIG, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
225 | "Listener: %s: Unknown protocol %s", |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
226 | listener->name.ptr, |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
227 | proto.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
228 | } |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
229 | free(proto.ptr); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
230 | } |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
231 | free(plist); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
232 | } |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
233 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
234 | |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
235 | if(error) { |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
236 | return NULL; |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
129
diff
changeset
|
237 | } |
129
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
238 | // TODO: cleanup on error |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
239 | |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
240 | int ret; |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
241 | char errbuf[512]; |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
242 | |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
243 | if(!conf->chainfile.ptr) { |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
194
diff
changeset
|
244 | ret = SSL_CTX_use_certificate_file(ctx, conf->certfile.ptr, SSL_FILETYPE_PEM); |
129
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
245 | if(!ret) { |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
246 | ERR_error_string(ERR_get_error(), errbuf); |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
247 | log_ereport(LOG_MISCONFIG, "Cannot load ssl chain file: %s", errbuf); |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
248 | return NULL; |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
249 | } |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
250 | } else { |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
194
diff
changeset
|
251 | ret = SSL_CTX_use_certificate_chain_file(ctx, conf->chainfile.ptr); |
129
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
252 | if(!ret) { |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
253 | ERR_error_string(ERR_get_error(), errbuf); |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
254 | log_ereport(LOG_MISCONFIG, "Cannot load ssl cert file: %s", errbuf); |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
255 | return NULL; |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
256 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
257 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
258 | |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
194
diff
changeset
|
259 | ret = SSL_CTX_use_PrivateKey_file(ctx, conf->privkeyfile.ptr, SSL_FILETYPE_PEM); |
129
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
260 | if(!ret) { |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
261 | ERR_error_string(ERR_get_error(), errbuf); |
fd324464f56f
adds support for ssl cert chain files and improves ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
262 | log_ereport(LOG_MISCONFIG, "Cannot load ssl key file: %s", errbuf); |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
263 | return NULL; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
264 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
265 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
266 | // TODO: chain |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
267 | listener->ssl->sslctx = ctx; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
268 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
269 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
270 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
271 | ucx_map_sstr_put(listener_map, listener->name, listener); |
1 | 272 | |
273 | struct sockaddr_in servaddr; /* server address */ | |
274 | ||
275 | /* init address structure */ | |
276 | memset(&servaddr, 0, sizeof(servaddr)); | |
277 | servaddr.sin_family = AF_INET; | |
278 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
279 | servaddr.sin_port = htons(conf->port); | |
280 | ||
281 | /* create socket */ | |
282 | if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
283 | perror("Error: http_listener_new: socket"); | |
284 | return NULL; | |
285 | } | |
286 | ||
287 | int o = 1; | |
288 | setsockopt( | |
289 | listener->server_socket, | |
290 | SOL_SOCKET, SO_REUSEADDR, | |
291 | &o, | |
292 | sizeof(int)); | |
293 | ||
294 | /* bind server socket to address */ | |
295 | if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){ | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
111
diff
changeset
|
296 | log_ereport(LOG_FAILURE, "http_listener_new: bind failed. Port: %d", conf->port); |
1 | 297 | return NULL; |
298 | } | |
299 | ||
300 | /* create acceptors */ | |
301 | listener->acceptors = calloc(listener->nacceptors, sizeof(void*)); | |
302 | for (int i=0;i<listener->nacceptors;i++) { | |
303 | listener->acceptors[i] = acceptor_new(listener); | |
304 | } | |
305 | ||
306 | return listener; | |
307 | } | |
308 | ||
309 | int http_listener_start(HttpListener *listener) { | |
74
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
310 | if(listener->running) { |
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
311 | return 0; |
5bc6d078fb2c
listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
312 | } |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
46
diff
changeset
|
313 | log_ereport(LOG_INFORM, "start listener on port %d", listener->port); |
1 | 314 | |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
315 | if (listen(listener->server_socket, 256) == -1) { |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
111
diff
changeset
|
316 | log_ereport(LOG_FAILURE, "http_listener_start: listen failed"); |
1 | 317 | return -1; |
318 | } | |
319 | ||
320 | /* start acceptor threads */ | |
321 | for (int i=0;i<listener->nacceptors;i++) { | |
322 | acceptor_start(listener->acceptors[i]); | |
323 | } | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
324 | |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
325 | return 0; |
1 | 326 | } |
327 | ||
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
328 | void http_listener_ref(HttpListener *listener) { |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
38
diff
changeset
|
329 | ws_atomic_inc32(&listener->ref); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
330 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
331 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
332 | void http_listener_unref(HttpListener *listener) { |
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
38
diff
changeset
|
333 | uint32_t ref = ws_atomic_dec32(&listener->ref); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
334 | if(ref == 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
335 | free(listener->acceptors); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
336 | // TODO: unref cfg |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
337 | // TODO: unref session handler |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
338 | free(listener); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
339 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
340 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
341 | |
1 | 342 | |
343 | ||
344 | Acceptor* acceptor_new(HttpListener *listener) { | |
345 | Acceptor *acceptor = malloc(sizeof(Acceptor)); | |
346 | acceptor->listener = listener; | |
347 | return acceptor; | |
348 | } | |
349 | ||
350 | void acceptor_start(Acceptor *a) { | |
351 | if(pthread_create( | |
352 | &a->tid, | |
353 | NULL, | |
354 | (void*(*)(void*))acceptor_thread, | |
355 | a) != 0) | |
356 | { | |
357 | perror("Error: acceptor_start: pthread_create"); | |
358 | } | |
359 | } | |
360 | ||
361 | void* acceptor_thread(Acceptor *acceptor) { | |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
362 | WS_ASSERT(acceptor); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
363 | WS_ASSERT(acceptor->listener); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
364 | WS_ASSERT(acceptor->listener->session_handler); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
365 | WS_ASSERT(acceptor->listener->session_handler->enqueue_connection); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
366 | |
1 | 367 | HttpListener *listener = acceptor->listener; |
368 | ||
369 | for (;;) { | |
370 | /* accept connections */ | |
371 | struct sockaddr_in ca; | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
372 | socklen_t length = sizeof(ca); |
1 | 373 | int clientfd; |
374 | ||
375 | /* accept a connection */ | |
376 | clientfd = accept( | |
377 | listener->server_socket, | |
378 | (struct sockaddr*)&ca, | |
379 | &length); | |
380 | if (clientfd == -1) { | |
381 | perror("Error: acceptor_thread: accept"); | |
382 | continue; | |
383 | } | |
145
1c93281ca4bf
fixes memory leaks in request_stat_path and send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
134
diff
changeset
|
384 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
385 | // check listener |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
386 | HttpListener *ls = listener; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
387 | int acceptor_exit = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
388 | while(ls->next) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
389 | ls = ls->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
390 | acceptor_exit = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
391 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
392 | |
1 | 393 | /* create Connection object */ |
394 | Connection *conn = malloc(sizeof(Connection)); | |
395 | conn->address = ca; | |
396 | conn->fd = clientfd; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
397 | conn->listener = ls; |
194
6345f50208d5
fixes keep-alive with ssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
398 | conn->ssl_accepted = 0; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
399 | if(ls->ssl) { |
191
391ccd490d97
moves IOStream creation to the sessionhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
188
diff
changeset
|
400 | // SSL connections are always non-blocking |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
401 | // set socket non blocking |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
402 | int flags; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
403 | if((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
404 | flags = 0; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
405 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
406 | if(fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK)) { |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
407 | perror("Error: acceptor_thread: fcntl"); |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
408 | // TODO: error |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
409 | } |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
410 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
411 | SSL *ssl = SSL_new(ls->ssl->sslctx); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
412 | SSL_set_fd(ssl, clientfd); |
188
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
413 | |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
414 | conn->ssl = ssl; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
415 | conn->read = connection_ssl_read; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
416 | conn->write = connection_ssl_write; |
0e6a05c779e0
using non-blocking IO for SSL_accept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
152
diff
changeset
|
417 | conn->close = connection_ssl_close; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
418 | } else { |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
419 | conn->ssl = NULL; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
420 | conn->read = connection_read; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
421 | conn->write = connection_write; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
422 | conn->close = connection_close; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
423 | } |
61
c858850f3d3a
improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
424 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
425 | if(conn) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
426 | cfg_ref(ls->cfg); |
1 | 427 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
428 | /* enqueue the connection */ |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
429 | ls->session_handler->enqueue_connection( |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
430 | ls->session_handler, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
431 | conn); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
432 | } |
1 | 433 | |
434 | /* ready for new connection */ | |
38
d07810b02147
added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
37
diff
changeset
|
435 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
436 | if(acceptor_exit) { |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
437 | // this acceptor is outdated |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
438 | break; |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
439 | } |
1 | 440 | } |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
441 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
442 | http_listener_unref(listener->next); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
443 | http_listener_unref(listener); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
444 | |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
445 | return NULL; |
1 | 446 | } |