Sat, 18 Aug 2012 11:39:34 +0200
using non blocking IO for request input
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" |
1 | 50 | #include "httplistener.h" |
51 | ||
52 | #include "session.h" | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
53 | #include "configmanager.h" |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
54 | #include "log.h" |
1 | 55 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
56 | UcxMap *listener_map = NULL; |
1 | 57 | |
58 | int start_all_listener() { | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
59 | ServerConfiguration *conf = cfgmgr_get_server_config(); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
60 | UcxList *ls = conf->listeners; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
61 | while(ls) { |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
62 | HttpListener *listener = ls->data; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
63 | http_listener_start(listener); |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
64 | ls = ls->next; |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
65 | } |
1 | 66 | |
67 | return 0; | |
68 | } | |
69 | ||
70 | ||
71 | HttpListener* http_listener_new(ListenerConfig *conf) { | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
72 | if(listener_map == NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
73 | listener_map = ucx_map_new(16); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
74 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
75 | |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
76 | HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
77 | if(fl != NULL) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
78 | return fl; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
79 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
80 | |
1 | 81 | HttpListener *listener = malloc(sizeof(HttpListener)); |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
82 | listener->name = conf->name; |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
29
diff
changeset
|
83 | //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
|
84 | listener->session_handler = create_event_session_handler(); |
1 | 85 | listener->nacceptors = conf->nacceptors; |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
86 | listener->port = conf->port; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
21
diff
changeset
|
87 | ucx_map_sstr_put(listener_map, listener->name, listener); |
1 | 88 | |
89 | struct sockaddr_in servaddr; /* server address */ | |
90 | ||
91 | /* init address structure */ | |
92 | memset(&servaddr, 0, sizeof(servaddr)); | |
93 | servaddr.sin_family = AF_INET; | |
94 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
95 | servaddr.sin_port = htons(conf->port); | |
96 | ||
97 | /* create socket */ | |
98 | if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
99 | perror("Error: http_listener_new: socket"); | |
100 | return NULL; | |
101 | } | |
102 | ||
103 | int o = 1; | |
104 | setsockopt( | |
105 | listener->server_socket, | |
106 | SOL_SOCKET, SO_REUSEADDR, | |
107 | &o, | |
108 | sizeof(int)); | |
109 | ||
110 | /* bind server socket to address */ | |
111 | if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){ | |
112 | perror("Error: http_listener_new: bind"); | |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
19
diff
changeset
|
113 | printf("port: %d\n", conf->port); |
1 | 114 | return NULL; |
115 | } | |
116 | ||
117 | /* create acceptors */ | |
118 | listener->acceptors = calloc(listener->nacceptors, sizeof(void*)); | |
119 | for (int i=0;i<listener->nacceptors;i++) { | |
120 | listener->acceptors[i] = acceptor_new(listener); | |
121 | } | |
122 | ||
123 | return listener; | |
124 | } | |
125 | ||
126 | int http_listener_start(HttpListener *listener) { | |
127 | printf("INFO: start listener\n"); | |
29
e8619defde14
added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
128 | log_ereport(LOG_LEVEL_INFO, "start listener on port %d", listener->port); |
1 | 129 | |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
130 | if (listen(listener->server_socket, 256) == -1) { |
1 | 131 | perror("Error: http_listener_start: listen"); |
132 | return -1; | |
133 | } | |
134 | ||
135 | /* start acceptor threads */ | |
136 | for (int i=0;i<listener->nacceptors;i++) { | |
137 | acceptor_start(listener->acceptors[i]); | |
138 | } | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
139 | |
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
140 | return 0; |
1 | 141 | } |
142 | ||
143 | ||
144 | ||
145 | Acceptor* acceptor_new(HttpListener *listener) { | |
146 | Acceptor *acceptor = malloc(sizeof(Acceptor)); | |
147 | acceptor->listener = listener; | |
148 | return acceptor; | |
149 | } | |
150 | ||
151 | void acceptor_start(Acceptor *a) { | |
152 | if(pthread_create( | |
153 | &a->tid, | |
154 | NULL, | |
155 | (void*(*)(void*))acceptor_thread, | |
156 | a) != 0) | |
157 | { | |
158 | perror("Error: acceptor_start: pthread_create"); | |
159 | } | |
160 | } | |
161 | ||
162 | void* acceptor_thread(Acceptor *acceptor) { | |
163 | HttpListener *listener = acceptor->listener; | |
164 | ||
165 | for (;;) { | |
166 | /* accept connections */ | |
167 | struct sockaddr_in ca; | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
168 | socklen_t length = sizeof(ca); |
1 | 169 | int clientfd; |
170 | ||
171 | /* accept a connection */ | |
172 | clientfd = accept( | |
173 | listener->server_socket, | |
174 | (struct sockaddr*)&ca, | |
175 | &length); | |
176 | if (clientfd == -1) { | |
177 | perror("Error: acceptor_thread: accept"); | |
178 | continue; | |
179 | } | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
180 | |
1 | 181 | /* create Connection object */ |
182 | Connection *conn = malloc(sizeof(Connection)); | |
183 | conn->address = ca; | |
184 | conn->fd = clientfd; | |
19
d680536f8c2f
Added configuration manager
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
185 | conn->listener = listener; |
1 | 186 | |
187 | /* enqueue the connection */ | |
188 | listener->session_handler->enqueue_connection( | |
189 | listener->session_handler, | |
190 | conn); | |
191 | ||
192 | /* ready for new connection */ | |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
193 | if(0) { |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
194 | break; |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
195 | } |
1 | 196 | } |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
197 | |
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
198 | return NULL; |
1 | 199 | } |