src/server/daemon/httplistener.c

Wed, 26 Jun 2013 15:09:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Jun 2013 15:09:54 +0200
changeset 79
f48cea237ec3
parent 78
3578977d29a3
child 81
d25825f37967
permissions
-rw-r--r--

fixed some memory leaks

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
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
29 #include "../public/nsapi.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <fcntl.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <sys/shm.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <sys/types.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <sys/ipc.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <sys/socket.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <sys/file.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <netinet/in.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include <netdb.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <stdlib.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <fcntl.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <unistd.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include <strings.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #include <stdbool.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include <pthread.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
49 #include "../ucx/map.h"
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
50 #include "../util/atomic.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 #include "httplistener.h"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
57 UcxMap *listener_map = NULL;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 return 0;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
86 newls->name = conf->name;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
87 newls->cfg = conf->cfg;
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
88 newls->nacceptors = conf->nacceptors;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
89 newls->default_vs.vs_name = conf->vs.ptr;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
90 newls->port = fl->port;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
91 newls->server_socket = fl->server_socket;
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
92 newls->running = 1;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
93 newls->ref = 2; // 1 reference is fl->next
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
94
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
95 newls->session_handler = fl->session_handler; // TODO
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
96
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
97 // the listener threadpool might be changed
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
98 if(conf->threadpool.ptr != NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
99 newls->threadpool = get_threadpool(conf->threadpool);
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
100 }
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
101 if(newls->threadpool == NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
102 newls->threadpool = get_default_threadpool();
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
103 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
104
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
105 // create acceptor threads
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
106 newls->acceptors = calloc(newls->nacceptors, sizeof(void*));
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
107 for (int i=0;i<newls->nacceptors;i++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
108 newls->acceptors[i] = acceptor_new(newls);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
109 }
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 // fl hold one reference of newls
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
112 fl->next = 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
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
115 ucx_map_sstr_put(listener_map, newls->name, 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 for (int i=0;i<newls->nacceptors;i++) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
118 acceptor_start(newls->acceptors[i]);
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
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
121 // 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
122
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
123 if(newls->port != conf->port) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
124 // TODO: log
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
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
127 return newls;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
128 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 HttpListener* http_listener_new(ListenerConfig *conf) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
131 // TODO: remove
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
132 if(listener_map == NULL) {
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
133 listener_map = ucx_map_new(16);
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
134 }
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
135
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
136 HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name);
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
137 if(fl != NULL) {
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
138 return fl;
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
139 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
140 // end remove
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
141
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 HttpListener *listener = malloc(sizeof(HttpListener));
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 listener->running = 0;
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
144 listener->cfg = conf->cfg;
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
145 listener->name = conf->name;
37
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
146 listener->default_vs.vs_name = conf->vs.ptr;
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
147 if(conf->threadpool.ptr != NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
148 listener->threadpool = get_threadpool(conf->threadpool);
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
149 }
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
150 if(listener->threadpool == NULL) {
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
151 listener->threadpool = get_default_threadpool();
360b9aabe17e added support for asynchronous safs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
152 }
78
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 74
diff changeset
153 //listener->session_handler = create_basic_session_handler();
3578977d29a3 added keep-alive support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 74
diff changeset
154 listener->session_handler = create_event_session_handler();
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 listener->nacceptors = conf->nacceptors;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
156 listener->port = conf->port;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
157 listener->ref = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
158 listener->next = NULL;
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
159 ucx_map_sstr_put(listener_map, listener->name, listener);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 struct sockaddr_in servaddr; /* server address */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 /* init address structure */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 memset(&servaddr, 0, sizeof(servaddr));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 servaddr.sin_family = AF_INET;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 servaddr.sin_port = htons(conf->port);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 /* create socket */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 perror("Error: http_listener_new: socket");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 int o = 1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 setsockopt(
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 listener->server_socket,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 SOL_SOCKET, SO_REUSEADDR,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 &o,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 sizeof(int));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 /* bind server socket to address */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 perror("Error: http_listener_new: bind");
20
7b235fa88008 Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 19
diff changeset
185 printf("port: %d\n", conf->port);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 return NULL;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 /* create acceptors */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 listener->acceptors = calloc(listener->nacceptors, sizeof(void*));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 for (int i=0;i<listener->nacceptors;i++) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 listener->acceptors[i] = acceptor_new(listener);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 return listener;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 int http_listener_start(HttpListener *listener) {
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
199 if(listener->running) {
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
200 return 0;
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
201 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 printf("INFO: start listener\n");
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
203 log_ereport(LOG_INFORM, "start listener on port %d", listener->port);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204
21
627b09ee74e4 New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 20
diff changeset
205 if (listen(listener->server_socket, 256) == -1) {
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 perror("Error: http_listener_start: listen");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 return -1;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 /* start acceptor threads */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 for (int i=0;i<listener->nacceptors;i++) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 acceptor_start(listener->acceptors[i]);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
19
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
214
d680536f8c2f Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
215 return 0;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
218 void http_listener_ref(HttpListener *listener) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
219 ws_atomic_inc32(&listener->ref);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
220 }
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 void http_listener_unref(HttpListener *listener) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 38
diff changeset
223 uint32_t ref = ws_atomic_dec32(&listener->ref);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
224 if(ref == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
225 free(listener->acceptors);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
226 // TODO: unref cfg
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
227 // TODO: unref session handler
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
228 free(listener);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
229 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
230 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
231
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 Acceptor* acceptor_new(HttpListener *listener) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 Acceptor *acceptor = malloc(sizeof(Acceptor));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 acceptor->listener = listener;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 return acceptor;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 void acceptor_start(Acceptor *a) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 if(pthread_create(
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 &a->tid,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 NULL,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 (void*(*)(void*))acceptor_thread,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 a) != 0)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 perror("Error: acceptor_start: pthread_create");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 void* acceptor_thread(Acceptor *acceptor) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 HttpListener *listener = acceptor->listener;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 for (;;) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 /* accept connections */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 struct sockaddr_in ca;
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
257 socklen_t length = sizeof(ca);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 int clientfd;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 /* accept a connection */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 clientfd = accept(
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 listener->server_socket,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 (struct sockaddr*)&ca,
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 &length);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 if (clientfd == -1) {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 perror("Error: acceptor_thread: accept");
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 continue;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 }
9
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
269
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
270 // check listener
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
271 HttpListener *ls = listener;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
272 int acceptor_exit = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
273 while(ls->next) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
274 ls = ls->next;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
275 acceptor_exit = 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
276 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
277
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 /* create Connection object */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 Connection *conn = malloc(sizeof(Connection));
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 conn->address = ca;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 conn->fd = clientfd;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
282 conn->listener = ls;
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
283
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
284 cfg_ref(ls->cfg);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 /* enqueue the connection */
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
287 ls->session_handler->enqueue_connection(
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
288 ls->session_handler,
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 conn);
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 /* ready for new connection */
38
d07810b02147 added ldap authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 37
diff changeset
292
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
293 if(acceptor_exit) {
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
294 // this acceptor is outdated
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
295 break;
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
296 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 }
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
298
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
299 http_listener_unref(listener->next);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
300 http_listener_unref(listener);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
301
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
302 return NULL;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 }

mercurial