Sat, 15 Dec 2012 16:05:03 +0100
added support for asynchronous safs
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2011 Olaf Wintermann. All rights reserved. | |
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 | ||
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
49 | #include "../ucx/map.h" |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
50 | #include "../ucx/atomic.h" |
1 | 51 | #include "httplistener.h" |
52 | ||
53 | #include "session.h" | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
54 | #include "configmanager.h" |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
55 | #include "log.h" |
1 | 56 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
57 | UcxMap *listener_map = NULL; |
1 | 58 | |
59 | int start_all_listener() { | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
60 | ServerConfiguration *conf = cfgmgr_get_server_config(); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
61 | UcxList *ls = conf->listeners; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
62 | while(ls) { |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
63 | HttpListener *listener = ls->data; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
64 | http_listener_start(listener); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
65 | ls = ls->next; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
66 | } |
1 | 67 | |
68 | return 0; | |
69 | } | |
70 | ||
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
71 | HttpListener* http_listener_create(ListenerConfig *conf) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
72 | if(listener_map == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
73 | listener_map = ucx_map_new(16); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
74 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
75 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
76 | 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
|
77 | if(fl == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
78 | return http_listener_new(conf); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
79 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
80 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
81 | HttpListener* newls = malloc(sizeof(HttpListener)); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
82 | if(newls == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
83 | // TODO: error |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
84 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
85 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
86 | newls->cfg = conf->cfg; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
87 | newls->default_vs.vs_name = conf->vs.ptr; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
88 | newls->port = fl->port; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
89 | newls->server_socket = fl->server_socket; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
90 | newls->ref = 2; // 1 reference is fl->next |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
91 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
92 | newls->session_handler = fl->session_handler; // TODO |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
93 | |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
94 | // the listener threadpool might be changed |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
95 | if(conf->threadpool.ptr != NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
96 | newls->threadpool = get_threadpool(conf->threadpool); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
97 | } |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
98 | if(newls->threadpool == NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
99 | newls->threadpool = get_default_threadpool(); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
100 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
101 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
102 | // create acceptor threads |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
103 | newls->acceptors = calloc(newls->nacceptors, sizeof(void*)); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
104 | for (int i=0;i<newls->nacceptors;i++) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
105 | newls->acceptors[i] = acceptor_new(newls); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
106 | } |
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 | // fl hold one reference of newls |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
109 | fl->next = newls; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
110 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
111 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
112 | ucx_map_sstr_put(listener_map, newls->name, newls); |
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 | for (int i=0;i<newls->nacceptors;i++) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
115 | acceptor_start(newls->acceptors[i]); |
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 | // 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
|
119 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
120 | if(newls->port != conf->port) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
121 | // TODO: log |
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 | return newls; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
125 | } |
1 | 126 | |
127 | HttpListener* http_listener_new(ListenerConfig *conf) { | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
128 | // TODO: remove |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
129 | if(listener_map == NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
130 | listener_map = ucx_map_new(16); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
131 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
132 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
133 | HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
134 | if(fl != NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
135 | return fl; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
136 | } |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
137 | // end remove |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
138 | |
1 | 139 | HttpListener *listener = malloc(sizeof(HttpListener)); |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
140 | listener->name = conf->name; |
37
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
141 | listener->default_vs.vs_name = conf->vs.ptr; |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
142 | if(conf->threadpool.ptr != NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
143 | listener->threadpool = get_threadpool(conf->threadpool); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
144 | } |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
145 | if(listener->threadpool == NULL) { |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
146 | listener->threadpool = get_default_threadpool(); |
360b9aabe17e
added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
147 | } |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
29
diff
changeset
|
148 | //listener->session_handler = create_basic_session_handler(); |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
29
diff
changeset
|
149 | listener->session_handler = create_event_session_handler(); |
1 | 150 | listener->nacceptors = conf->nacceptors; |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
151 | listener->port = conf->port; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
152 | listener->ref = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
153 | listener->next = NULL; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
154 | ucx_map_sstr_put(listener_map, listener->name, listener); |
1 | 155 | |
156 | struct sockaddr_in servaddr; /* server address */ | |
157 | ||
158 | /* init address structure */ | |
159 | memset(&servaddr, 0, sizeof(servaddr)); | |
160 | servaddr.sin_family = AF_INET; | |
161 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
162 | servaddr.sin_port = htons(conf->port); | |
163 | ||
164 | /* create socket */ | |
165 | if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
166 | perror("Error: http_listener_new: socket"); | |
167 | return NULL; | |
168 | } | |
169 | ||
170 | int o = 1; | |
171 | setsockopt( | |
172 | listener->server_socket, | |
173 | SOL_SOCKET, SO_REUSEADDR, | |
174 | &o, | |
175 | sizeof(int)); | |
176 | ||
177 | /* bind server socket to address */ | |
178 | if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){ | |
179 | perror("Error: http_listener_new: bind"); | |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
180 | printf("port: %d\n", conf->port); |
1 | 181 | return NULL; |
182 | } | |
183 | ||
184 | /* create acceptors */ | |
185 | listener->acceptors = calloc(listener->nacceptors, sizeof(void*)); | |
186 | for (int i=0;i<listener->nacceptors;i++) { | |
187 | listener->acceptors[i] = acceptor_new(listener); | |
188 | } | |
189 | ||
190 | return listener; | |
191 | } | |
192 | ||
193 | int http_listener_start(HttpListener *listener) { | |
194 | printf("INFO: start listener\n"); | |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
195 | log_ereport(LOG_LEVEL_INFO, "start listener on port %d", listener->port); |
1 | 196 | |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
197 | if (listen(listener->server_socket, 256) == -1) { |
1 | 198 | perror("Error: http_listener_start: listen"); |
199 | return -1; | |
200 | } | |
201 | ||
202 | /* start acceptor threads */ | |
203 | for (int i=0;i<listener->nacceptors;i++) { | |
204 | acceptor_start(listener->acceptors[i]); | |
205 | } | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
206 | |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
207 | return 0; |
1 | 208 | } |
209 | ||
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
210 | void http_listener_ref(HttpListener *listener) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
211 | ucx_atomic_inc_32(&listener->ref); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
212 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
213 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
214 | void http_listener_unref(HttpListener *listener) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
215 | uint32_t ref = ucx_atomic_dec_32_nv(&listener->ref); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
216 | if(ref == 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
217 | free(listener->acceptors); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
218 | // TODO: unref cfg |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
219 | // TODO: unref session handler |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
220 | free(listener); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
221 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
222 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
223 | |
1 | 224 | |
225 | ||
226 | Acceptor* acceptor_new(HttpListener *listener) { | |
227 | Acceptor *acceptor = malloc(sizeof(Acceptor)); | |
228 | acceptor->listener = listener; | |
229 | return acceptor; | |
230 | } | |
231 | ||
232 | void acceptor_start(Acceptor *a) { | |
233 | if(pthread_create( | |
234 | &a->tid, | |
235 | NULL, | |
236 | (void*(*)(void*))acceptor_thread, | |
237 | a) != 0) | |
238 | { | |
239 | perror("Error: acceptor_start: pthread_create"); | |
240 | } | |
241 | } | |
242 | ||
243 | void* acceptor_thread(Acceptor *acceptor) { | |
244 | HttpListener *listener = acceptor->listener; | |
245 | ||
246 | for (;;) { | |
247 | /* accept connections */ | |
248 | struct sockaddr_in ca; | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
249 | socklen_t length = sizeof(ca); |
1 | 250 | int clientfd; |
251 | ||
252 | /* accept a connection */ | |
253 | clientfd = accept( | |
254 | listener->server_socket, | |
255 | (struct sockaddr*)&ca, | |
256 | &length); | |
257 | if (clientfd == -1) { | |
258 | perror("Error: acceptor_thread: accept"); | |
259 | continue; | |
260 | } | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
261 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
262 | // check listener |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
263 | HttpListener *ls = listener; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
264 | int acceptor_exit = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
265 | while(ls->next) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
266 | ls = ls->next; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
267 | acceptor_exit = 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
268 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
269 | |
1 | 270 | /* create Connection object */ |
271 | Connection *conn = malloc(sizeof(Connection)); | |
272 | conn->address = ca; | |
273 | conn->fd = clientfd; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
274 | conn->listener = ls; |
1 | 275 | |
276 | /* enqueue the connection */ | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
277 | ls->session_handler->enqueue_connection( |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
278 | ls->session_handler, |
1 | 279 | conn); |
280 | ||
281 | /* ready for new connection */ | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
282 | if(acceptor_exit) { |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
283 | break; |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
284 | } |
1 | 285 | } |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
286 | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
287 | http_listener_unref(listener->next); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
288 | http_listener_unref(listener); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
289 | |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
290 | return NULL; |
1 | 291 | } |