src/server/daemon/httplistener.c

changeset 415
d938228c382e
parent 398
83234bc3bee9
child 438
22eca559aded
equal deleted inserted replaced
414:99a34860c105 415:d938228c382e
52 #include <stdio.h> 52 #include <stdio.h>
53 #include <stdlib.h> 53 #include <stdlib.h>
54 #include <sys/socket.h> 54 #include <sys/socket.h>
55 #include <unistd.h> 55 #include <unistd.h>
56 56
57 #include <ucx/map.h> 57
58 #include <cx/hash_map.h>
58 59
59 #include "../util/atomic.h" 60 #include "../util/atomic.h"
60 #include "httplistener.h" 61 #include "httplistener.h"
61 #include "netsite.h" 62 #include "netsite.h"
62 63
63 #include "session.h" 64 #include "session.h"
64 #include "configmanager.h" 65 #include "configmanager.h"
65 #include "log.h" 66 #include "log.h"
66 67
67 UcxMap *listener_map = NULL; 68 #define LISTENER_MAX_PROTOCOL_TOKENS 1024
69
70 CxMap *listener_map = NULL;
68 71
69 int start_all_listener() { 72 int start_all_listener() {
70 ServerConfiguration *conf = cfgmgr_get_server_config(); 73 ServerConfiguration *conf = cfgmgr_get_server_config();
71 UcxList *ls = conf->listeners; 74 CxList *ls = conf->listeners;
72 while(ls) { 75 CxIterator iter = cxListIterator(ls, 0);
73 HttpListener *listener = ls->data; 76 cx_foreach(HttpListener *, listener, iter) {
74 http_listener_start(listener); 77 http_listener_start(listener);
75 ls = ls->next;
76 } 78 }
77 79
78 return 0; 80 return 0;
79 } 81 }
80 82
81 HttpListener* http_listener_create(ListenerConfig *conf) { 83 HttpListener* http_listener_create(ListenerConfig *conf) {
82 if(listener_map == NULL) { 84 if(listener_map == NULL) {
83 listener_map = ucx_map_new(16); 85 listener_map = cxHashMapCreate(cxDefaultAllocator, 16);
84 } 86 }
85 87
86 HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); 88 HttpListener *fl = cxMapGet(listener_map, cx_hash_key(conf->name.ptr, conf->name.length));
87 if(fl == NULL) { 89 if(fl == NULL) {
88 return http_listener_new(conf); 90 return http_listener_new(conf);
89 } 91 }
90 92
91 HttpListener* newls = calloc(1, sizeof(HttpListener)); 93 HttpListener* newls = calloc(1, sizeof(HttpListener));
106 108
107 newls->session_handler = fl->session_handler; // TODO 109 newls->session_handler = fl->session_handler; // TODO
108 110
109 // the listener threadpool might be changed 111 // the listener threadpool might be changed
110 if(conf->threadpool.ptr != NULL) { 112 if(conf->threadpool.ptr != NULL) {
111 newls->threadpool = get_threadpool(conf->threadpool); 113 newls->threadpool = get_threadpool(cx_strcast(conf->threadpool));
112 } 114 }
113 if(newls->threadpool == NULL) { 115 if(newls->threadpool == NULL) {
114 newls->threadpool = get_default_threadpool(); 116 newls->threadpool = get_default_threadpool();
115 } 117 }
116 118
128 130
129 // fl hold one reference of newls 131 // fl hold one reference of newls
130 fl->next = newls; 132 fl->next = newls;
131 133
132 134
133 ucx_map_sstr_put(listener_map, newls->name, newls); 135 cxMapPut(listener_map, cx_hash_key(newls->name.ptr, newls->name.length), newls);
134 136
135 for (int i=0;i<newls->nacceptors;i++) { 137 for (int i=0;i<newls->nacceptors;i++) {
136 //acceptor_start(newls->acceptors[i]); 138 //acceptor_start(newls->acceptors[i]);
137 acceptor_start(newls->acceptors6[i]); 139 acceptor_start(newls->acceptors6[i]);
138 } 140 }
147 } 149 }
148 150
149 HttpListener* http_listener_new(ListenerConfig *conf) { 151 HttpListener* http_listener_new(ListenerConfig *conf) {
150 // TODO: remove 152 // TODO: remove
151 if(listener_map == NULL) { 153 if(listener_map == NULL) {
152 listener_map = ucx_map_new(16); 154 listener_map = cxHashMapCreate(cxDefaultAllocator, 16);
153 } 155 }
154 156
155 HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); 157 HttpListener *fl = cxMapGet(listener_map, cx_hash_key(conf->name.ptr, conf->name.length));
156 if(fl != NULL) { 158 if(fl != NULL) {
157 return fl; 159 return fl;
158 } 160 }
159 // end remove 161 // end remove
160 162
163 listener->cfg = conf->cfg; 165 listener->cfg = conf->cfg;
164 listener->name = conf->name; 166 listener->name = conf->name;
165 listener->default_vs.vs_name = conf->vs.ptr; 167 listener->default_vs.vs_name = conf->vs.ptr;
166 listener->threadpool = NULL; 168 listener->threadpool = NULL;
167 if(conf->threadpool.ptr != NULL) { 169 if(conf->threadpool.ptr != NULL) {
168 listener->threadpool = get_threadpool(conf->threadpool); 170 listener->threadpool = get_threadpool(cx_strcast(conf->threadpool));
169 } 171 }
170 if(listener->threadpool == NULL) { 172 if(listener->threadpool == NULL) {
171 listener->threadpool = get_default_threadpool(); 173 listener->threadpool = get_default_threadpool();
172 } 174 }
173 if(conf->blockingio) { 175 if(conf->blockingio) {
189 SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method()); 191 SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method());
190 SSL_CTX_set_options( 192 SSL_CTX_set_options(
191 ctx, 193 ctx,
192 SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv3); 194 SSL_OP_SINGLE_DH_USE | SSL_OP_NO_SSLv3);
193 if(conf->disable_proto.ptr) { 195 if(conf->disable_proto.ptr) {
194 ssize_t n = 0; 196 cxstring *plist = NULL;
195 sstr_t *plist = sstrsplit(conf->disable_proto, S(","), &n); 197 ssize_t n = cx_strsplit_a(cxDefaultAllocator, conf->disable_proto, cx_str(","), LISTENER_MAX_PROTOCOL_TOKENS, &plist);
196 if(plist) { 198 if(plist) {
197 for(int i=0;i<n;i++) { 199 for(int i=0;i<n;i++) {
198 sstr_t proto = plist[i]; 200 cxstring proto = plist[i];
199 log_ereport( 201 log_ereport(
200 LOG_VERBOSE, 202 LOG_VERBOSE,
201 "Listener %s: Disable protocol %s", 203 "Listener %s: Disable protocol %s",
202 listener->name.ptr, 204 listener->name.ptr,
203 proto.ptr); 205 proto.ptr);
204 if(!sstrcasecmp(sstrtrim(proto), S("SSLv2"))) { 206 if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv2"))) {
205 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2); 207 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
206 } else if(!sstrcasecmp(sstrtrim(proto), S("SSLv3"))) { 208 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("SSLv3"))) {
207 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3); 209 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
208 } else if(!sstrcasecmp(sstrtrim(proto), S("TLSv1"))) { 210 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1"))) {
209 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1); 211 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
210 } else if(!sstrcasecmp(sstrtrim(proto), S("TLSv1.1"))) { 212 } else if(!cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.1"))) {
211 #ifdef SSL_OP_NO_TLSv1_1 213 #ifdef SSL_OP_NO_TLSv1_1
212 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1); 214 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);
213 #else 215 #else
214 log_ereport( 216 log_ereport(
215 LOG_WARN, 217 LOG_WARN,
216 "Listener: %s: TLSv1.1 already not supported", 218 "Listener: %s: TLSv1.1 already not supported",
217 listener->name.ptr); 219 listener->name.ptr);
218 #endif 220 #endif
219 } else if(sstrcasecmp(sstrtrim(proto), S("TLSv1.2"))) { 221 } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.2"))) {
220 #ifdef SSL_OP_NO_TLSv1_2 222 #ifdef SSL_OP_NO_TLSv1_2
221 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2); 223 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);
222 #else 224 #else
223 log_ereport( 225 log_ereport(
224 LOG_WARN, 226 LOG_WARN,
225 "Listener: %s: TLSv1.2 already not supported", 227 "Listener: %s: TLSv1.2 already not supported",
226 listener->name.ptr); 228 listener->name.ptr);
227 #endif 229 #endif
228 } else if(sstrcasecmp(sstrtrim(proto), S("TLSv1.3"))) { 230 } else if(cx_strcasecmp(cx_strtrim(proto), cx_str("TLSv1.3"))) {
229 #ifdef SSL_OP_NO_TLSv1_3 231 #ifdef SSL_OP_NO_TLSv1_3
230 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3); 232 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3);
231 #else 233 #else
232 log_ereport( 234 log_ereport(
233 LOG_WARN, 235 LOG_WARN,
240 LOG_MISCONFIG, 242 LOG_MISCONFIG,
241 "Listener: %s: Unknown protocol %s", 243 "Listener: %s: Unknown protocol %s",
242 listener->name.ptr, 244 listener->name.ptr,
243 proto.ptr); 245 proto.ptr);
244 } 246 }
245 free(proto.ptr);
246 } 247 }
247 free(plist); 248 free(plist);
248 } 249 }
249 } 250 }
250 251
282 // TODO: chain 283 // TODO: chain
283 listener->ssl->sslctx = ctx; 284 listener->ssl->sslctx = ctx;
284 } 285 }
285 286
286 287
287 ucx_map_sstr_put(listener_map, listener->name, listener); 288 cxMapPut(listener_map, cx_hash_key(listener->name.ptr, listener->name.length), listener);
288 289
289 struct sockaddr_in servaddr; /* server address */ 290 struct sockaddr_in servaddr; /* server address */
290 struct sockaddr_in6 servaddr6; 291 struct sockaddr_in6 servaddr6;
291 292
292 /* init address structure */ 293 /* init address structure */

mercurial