blob: 1438814b6510936b814356f53d57bc13fc6c55e6 [file] [log] [blame]
Andy Greena1ce6be2013-01-18 11:43:21 +08001/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
Andy Green8c0d3c02015-11-02 20:34:12 +08004 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
Andy Greena1ce6be2013-01-18 11:43:21 +08005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22
23#include "private-libwebsockets.h"
24
Andy Greene38031a2014-04-03 08:24:29 +080025int lws_context_init_server(struct lws_context_creation_info *info,
Andy Green4b85c1d2015-12-04 11:08:32 +080026 struct lws_context *context)
Andy Greene38031a2014-04-03 08:24:29 +080027{
Andy Green3b193862015-11-02 13:13:44 +080028 lws_sockfd_type sockfd;
Andy Green2cd30742015-11-02 13:10:33 +080029#if LWS_POSIX
Andy Green8c0d3c02015-11-02 20:34:12 +080030 int n;
Andy Greene38031a2014-04-03 08:24:29 +080031 struct sockaddr_in sin;
32 socklen_t len = sizeof(sin);
Andy Greene38031a2014-04-03 08:24:29 +080033#ifdef LWS_USE_IPV6
34 struct sockaddr_in6 serv_addr6;
35#endif
36 struct sockaddr_in serv_addr4;
37 struct sockaddr *v;
Andy Green2cd30742015-11-02 13:10:33 +080038 int opt = 1;
Andy Green8c0d3c02015-11-02 20:34:12 +080039#endif
Andy Green4b85c1d2015-12-04 11:08:32 +080040 struct lws *wsi;
Andy Greene38031a2014-04-03 08:24:29 +080041
42 /* set up our external listening socket we serve on */
43
44 if (info->port == CONTEXT_PORT_NO_LISTEN)
45 return 0;
46
Andy Green8c0d3c02015-11-02 20:34:12 +080047#if LWS_POSIX
Andy Greene38031a2014-04-03 08:24:29 +080048#ifdef LWS_USE_IPV6
49 if (LWS_IPV6_ENABLED(context))
50 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
51 else
52#endif
53 sockfd = socket(AF_INET, SOCK_STREAM, 0);
Andy Green8c0d3c02015-11-02 20:34:12 +080054
Andy Greenfc772cc2015-11-14 13:48:58 +080055 if (sockfd == -1) {
Andy Green8c0d3c02015-11-02 20:34:12 +080056#else
57 sockfd = mbed3_create_tcp_stream_socket();
58 if (!lws_sockfd_valid(sockfd)) {
59#endif
60
Andy Greene38031a2014-04-03 08:24:29 +080061 lwsl_err("ERROR opening socket\n");
62 return 1;
63 }
64
Andy Green8c0d3c02015-11-02 20:34:12 +080065#if LWS_POSIX
Andy Greene38031a2014-04-03 08:24:29 +080066 /*
67 * allow us to restart even if old sockets in TIME_WAIT
68 */
Andy Greenf38e7862014-11-30 13:07:11 +080069 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
Andy Green9c8d5902014-11-30 13:30:57 +080070 (const void *)&opt, sizeof(opt)) < 0) {
71 compatible_close(sockfd);
Andy Greenf38e7862014-11-30 13:07:11 +080072 return 1;
Andy Green9c8d5902014-11-30 13:30:57 +080073 }
Andy Green8c0d3c02015-11-02 20:34:12 +080074#endif
Andy Greene38031a2014-04-03 08:24:29 +080075 lws_plat_set_socket_options(context, sockfd);
76
Andy Green8c0d3c02015-11-02 20:34:12 +080077#if LWS_POSIX
Andy Greene38031a2014-04-03 08:24:29 +080078#ifdef LWS_USE_IPV6
79 if (LWS_IPV6_ENABLED(context)) {
80 v = (struct sockaddr *)&serv_addr6;
81 n = sizeof(struct sockaddr_in6);
82 bzero((char *) &serv_addr6, sizeof(serv_addr6));
83 serv_addr6.sin6_addr = in6addr_any;
84 serv_addr6.sin6_family = AF_INET6;
85 serv_addr6.sin6_port = htons(info->port);
86 } else
87#endif
88 {
89 v = (struct sockaddr *)&serv_addr4;
90 n = sizeof(serv_addr4);
91 bzero((char *) &serv_addr4, sizeof(serv_addr4));
92 serv_addr4.sin_addr.s_addr = INADDR_ANY;
93 serv_addr4.sin_family = AF_INET;
Andy Greene38031a2014-04-03 08:24:29 +080094
95 if (info->iface) {
96 if (interface_to_sa(context, info->iface,
97 (struct sockaddr_in *)v, n) < 0) {
98 lwsl_err("Unable to find interface %s\n",
99 info->iface);
100 compatible_close(sockfd);
101 return 1;
102 }
103 }
vpeter47cc7ae42014-04-27 12:32:15 +0200104
105 serv_addr4.sin_port = htons(info->port);
Andy Greene38031a2014-04-03 08:24:29 +0800106 } /* ipv4 */
107
108 n = bind(sockfd, v, n);
109 if (n < 0) {
110 lwsl_err("ERROR on binding to port %d (%d %d)\n",
111 info->port, n, LWS_ERRNO);
112 compatible_close(sockfd);
113 return 1;
114 }
Alejandro Mery6ff28242014-12-04 23:59:35 +0100115
Andy Greene38031a2014-04-03 08:24:29 +0800116 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
117 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
118 else
119 info->port = ntohs(sin.sin_port);
Andy Green8c0d3c02015-11-02 20:34:12 +0800120#endif
Andy Greene38031a2014-04-03 08:24:29 +0800121
122 context->listen_port = info->port;
123
Andy Green4b85c1d2015-12-04 11:08:32 +0800124 wsi = lws_zalloc(sizeof(struct lws));
Andy Greene38031a2014-04-03 08:24:29 +0800125 if (wsi == NULL) {
126 lwsl_err("Out of mem\n");
127 compatible_close(sockfd);
128 return 1;
129 }
Andy Greene38031a2014-04-03 08:24:29 +0800130 wsi->sock = sockfd;
131 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Green8c0d3c02015-11-02 20:34:12 +0800132 wsi->protocol = context->protocols;
Andy Greene38031a2014-04-03 08:24:29 +0800133
Andy Green1963c9a2015-10-15 07:39:33 +0800134 if (insert_wsi_socket_into_fds(context, wsi)) {
135 compatible_close(sockfd);
136 return 1;
137 }
Andy Greene38031a2014-04-03 08:24:29 +0800138
139 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
140 context->listen_service_count = 0;
141 context->listen_service_fd = sockfd;
142
Andy Green8c0d3c02015-11-02 20:34:12 +0800143#if LWS_POSIX
Andy Greene38031a2014-04-03 08:24:29 +0800144 listen(sockfd, LWS_SOMAXCONN);
Andy Green8c0d3c02015-11-02 20:34:12 +0800145#else
146 mbed3_tcp_stream_bind(sockfd, info->port, wsi);
147#endif
Andy Greene38031a2014-04-03 08:24:29 +0800148 lwsl_notice(" Listening on port %d\n", info->port);
Alejandro Mery6ff28242014-12-04 23:59:35 +0100149
Andy Greene38031a2014-04-03 08:24:29 +0800150 return 0;
151}
152
Andy Greend99476b2014-04-03 08:40:05 +0800153int
Andy Green4b85c1d2015-12-04 11:08:32 +0800154_lws_rx_flow_control(struct lws *wsi)
Andy Greend99476b2014-04-03 08:40:05 +0800155{
Andy Green4b85c1d2015-12-04 11:08:32 +0800156 struct lws_context *context = wsi->protocol->owning_server;
Andy Greend99476b2014-04-03 08:40:05 +0800157
158 /* there is no pending change */
Andy Green024eb6c2014-10-08 12:00:53 +0800159 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
Andy Greend99476b2014-04-03 08:40:05 +0800160 return 0;
161
162 /* stuff is still buffered, not ready to really accept new input */
Andy Green024eb6c2014-10-08 12:00:53 +0800163 if (wsi->rxflow_buffer) {
Andy Greend99476b2014-04-03 08:40:05 +0800164 /* get ourselves called back to deal with stashed buffer */
Andy Green62304762015-12-04 08:43:54 +0800165 lws_callback_on_writable(context, wsi);
Andy Greend99476b2014-04-03 08:40:05 +0800166 return 0;
167 }
168
169 /* pending is cleared, we can change rxflow state */
170
Andy Green024eb6c2014-10-08 12:00:53 +0800171 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
Andy Greend99476b2014-04-03 08:40:05 +0800172
173 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
Andy Green024eb6c2014-10-08 12:00:53 +0800174 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
Andy Greend99476b2014-04-03 08:40:05 +0800175
176 /* adjust the pollfd for this wsi */
177
Andy Green024eb6c2014-10-08 12:00:53 +0800178 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
Andy Greena717df22014-04-11 13:14:37 +0800179 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
180 lwsl_info("%s: fail\n", __func__);
Andy Greend99476b2014-04-03 08:40:05 +0800181 return -1;
Andy Greena717df22014-04-11 13:14:37 +0800182 }
Andy Greend99476b2014-04-03 08:40:05 +0800183 } else
184 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
185 return -1;
186
Andy Greenf004ec52014-04-12 11:47:25 +0800187 return 0;
Andy Greend99476b2014-04-03 08:40:05 +0800188}
189
Andy Green4b85c1d2015-12-04 11:08:32 +0800190int lws_http_action(struct lws_context *context,
191 struct lws *wsi)
Andy Green024eb6c2014-10-08 12:00:53 +0800192{
193 char *uri_ptr = NULL;
194 int uri_len = 0;
195 enum http_version request_version;
196 enum http_connection_type connection_type;
197 int http_version_len;
198 char content_length_str[32];
199 char http_version_str[10];
200 char http_conn_str[20];
Andy Green2cd30742015-11-02 13:10:33 +0800201 unsigned int n, count = 0;
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800202 static const unsigned char methods[] = {
203 WSI_TOKEN_GET_URI,
204 WSI_TOKEN_POST_URI,
205 WSI_TOKEN_OPTIONS_URI,
206 WSI_TOKEN_PUT_URI,
207 WSI_TOKEN_PATCH_URI,
208 WSI_TOKEN_DELETE_URI,
209#ifdef LWS_USE_HTTP2
210 WSI_TOKEN_HTTP_COLON_PATH,
211#endif
212 };
Andy Green93d947c2015-02-20 07:37:20 +0800213#ifdef _DEBUG
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800214 static const char * const method_names[] = {
215 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
216#ifdef LWS_USE_HTTP2
217 ":path",
218#endif
219 };
Andy Green93d947c2015-02-20 07:37:20 +0800220#endif
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800221
Andy Green024eb6c2014-10-08 12:00:53 +0800222 /* it's not websocket.... shall we accept it as http? */
223
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800224 for (n = 0; n < ARRAY_SIZE(methods); n++)
225 if (lws_hdr_total_length(wsi, methods[n]))
226 count++;
227 if (!count) {
Andy Green024eb6c2014-10-08 12:00:53 +0800228 lwsl_warn("Missing URI in HTTP request\n");
229 goto bail_nuke_ah;
230 }
231
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800232 if (count != 1) {
233 lwsl_warn("multiple methods?\n");
Andy Green024eb6c2014-10-08 12:00:53 +0800234 goto bail_nuke_ah;
235 }
236
Andy Green3ef579b2015-12-04 09:23:56 +0800237 if (lws_ensure_user_space(wsi))
Andy Green024eb6c2014-10-08 12:00:53 +0800238 goto bail_nuke_ah;
239
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800240 for (n = 0; n < ARRAY_SIZE(methods); n++)
241 if (lws_hdr_total_length(wsi, methods[n])) {
242 uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
243 uri_len = lws_hdr_total_length(wsi, methods[n]);
244 lwsl_info("Method: %s request for '%s'\n",
245 method_names[n], uri_ptr);
246 break;
247 }
Andy Green024eb6c2014-10-08 12:00:53 +0800248
249 /* HTTP header had a content length? */
250
251 wsi->u.http.content_length = 0;
Quinlan Pfiffer49f72aa2015-01-10 19:01:52 -0800252 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
253 lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
254 lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
Andy Green024eb6c2014-10-08 12:00:53 +0800255 wsi->u.http.content_length = 100 * 1024 * 1024;
256
257 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
258 lws_hdr_copy(wsi, content_length_str,
259 sizeof(content_length_str) - 1,
260 WSI_TOKEN_HTTP_CONTENT_LENGTH);
261 wsi->u.http.content_length = atoi(content_length_str);
262 }
263
264 /* http_version? Default to 1.0, override with token: */
265 request_version = HTTP_VERSION_1_0;
266
267 /* Works for single digit HTTP versions. : */
268 http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
269 if (http_version_len > 7) {
270 lws_hdr_copy(wsi, http_version_str,
271 sizeof(http_version_str) - 1, WSI_TOKEN_HTTP);
272 if (http_version_str[5] == '1' && http_version_str[7] == '1')
273 request_version = HTTP_VERSION_1_1;
274 }
275 wsi->u.http.request_version = request_version;
276
277 /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
278 if (request_version == HTTP_VERSION_1_1)
279 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
280 else
281 connection_type = HTTP_CONNECTION_CLOSE;
282
283 /* Override default if http "Connection:" header: */
284 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
285 lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
286 WSI_TOKEN_CONNECTION);
287 http_conn_str[sizeof(http_conn_str) - 1] = '\0';
288 if (!strcasecmp(http_conn_str, "keep-alive"))
289 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
290 else
Ash 200010d89f3c2015-10-20 12:12:12 +0800291 if (!strcasecmp(http_conn_str, "close"))
Andy Green024eb6c2014-10-08 12:00:53 +0800292 connection_type = HTTP_CONNECTION_CLOSE;
293 }
294 wsi->u.http.connection_type = connection_type;
295
296 n = 0;
297 if (wsi->protocol->callback)
298 n = wsi->protocol->callback(context, wsi,
299 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
300 wsi->user_space, uri_ptr, uri_len);
301
302 if (!n) {
303 /*
304 * if there is content supposed to be coming,
305 * put a timeout on it having arrived
306 */
Andy Green62304762015-12-04 08:43:54 +0800307 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
Andy Green024eb6c2014-10-08 12:00:53 +0800308 AWAITING_TIMEOUT);
309
310 if (wsi->protocol->callback)
311 n = wsi->protocol->callback(context, wsi,
312 LWS_CALLBACK_HTTP,
313 wsi->user_space, uri_ptr, uri_len);
314 }
315
316 /* now drop the header info we kept a pointer to */
Alejandro Meryac3ec392014-12-05 00:09:20 +0100317 lws_free2(wsi->u.http.ah);
Andy Green024eb6c2014-10-08 12:00:53 +0800318
319 if (n) {
320 lwsl_info("LWS_CALLBACK_HTTP closing\n");
321 return 1; /* struct ah ptr already nuked */ }
322
323 /*
324 * If we're not issuing a file, check for content_length or
325 * HTTP keep-alive. No keep-alive header allocation for
326 * ISSUING_FILE, as this uses HTTP/1.0.
327 *
Andy Green62304762015-12-04 08:43:54 +0800328 * In any case, return 0 and let lws_read decide how to
Andy Green024eb6c2014-10-08 12:00:53 +0800329 * proceed based on state
330 */
331 if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE)
332 /* Prepare to read body if we have a content length: */
333 if (wsi->u.http.content_length > 0)
334 wsi->state = WSI_STATE_HTTP_BODY;
335
336 return 0;
337
338bail_nuke_ah:
339 /* drop the header info */
Alejandro Meryac3ec392014-12-05 00:09:20 +0100340 lws_free2(wsi->u.hdr.ah);
341
Andy Green024eb6c2014-10-08 12:00:53 +0800342 return 1;
343}
344
Andy Greenaad2eac2014-04-03 09:03:37 +0800345
Andy Green4b85c1d2015-12-04 11:08:32 +0800346int lws_handshake_server(struct lws_context *context,
347 struct lws *wsi, unsigned char **buf, size_t len)
Andy Greenaad2eac2014-04-03 09:03:37 +0800348{
349 struct allocated_headers *ah;
Andy Green024eb6c2014-10-08 12:00:53 +0800350 int protocol_len;
Andy Green7a8d86e2014-07-19 06:52:39 +0800351 char protocol_list[128];
352 char protocol_name[32];
Andy Green7a8d86e2014-07-19 06:52:39 +0800353 char *p;
354 int n, hit;
Andy Greenaad2eac2014-04-03 09:03:37 +0800355
356 /* LWS_CONNMODE_WS_SERVING */
357
358 while (len--) {
Andy Green3ef579b2015-12-04 09:23:56 +0800359 if (lws_parse(context, wsi, *(*buf)++)) {
360 lwsl_info("lws_parse failed\n");
Andy Greenaad2eac2014-04-03 09:03:37 +0800361 goto bail_nuke_ah;
362 }
363
364 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
365 continue;
366
Andy Green3ef579b2015-12-04 09:23:56 +0800367 lwsl_parser("lws_parse sees parsing complete\n");
Andy Greenaad2eac2014-04-03 09:03:37 +0800368
369 wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT;
Andy Green62304762015-12-04 08:43:54 +0800370 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
Andy Greenaad2eac2014-04-03 09:03:37 +0800371
372 /* is this websocket protocol or normal http 1.0? */
373
374 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
375 !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
Andy Green024eb6c2014-10-08 12:00:53 +0800376
Andy Greenaad2eac2014-04-03 09:03:37 +0800377 ah = wsi->u.hdr.ah;
Andy Green024eb6c2014-10-08 12:00:53 +0800378
Andy Green44c11612014-11-08 11:18:47 +0800379 lws_union_transition(wsi, LWS_CONNMODE_HTTP_SERVING_ACCEPTED);
Andy Greenaad2eac2014-04-03 09:03:37 +0800380 wsi->state = WSI_STATE_HTTP;
381 wsi->u.http.fd = LWS_INVALID_FILE;
382
383 /* expose it at the same offset as u.hdr */
384 wsi->u.http.ah = ah;
Andy Green024eb6c2014-10-08 12:00:53 +0800385
386 n = lws_http_action(context, wsi);
Andy Greenaad2eac2014-04-03 09:03:37 +0800387
Andy Green024eb6c2014-10-08 12:00:53 +0800388 return n;
Andy Greenaad2eac2014-04-03 09:03:37 +0800389 }
390
Andy Green2af58562014-09-30 08:15:49 +0800391 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
392 "websocket"))
393 goto upgrade_ws;
Andy Green024eb6c2014-10-08 12:00:53 +0800394#ifdef LWS_USE_HTTP2
Andy Greena54f2322014-09-30 09:43:14 +0800395 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
Andy Greenb08cb502014-09-30 16:33:56 +0800396 "h2c-14"))
Andy Greena54f2322014-09-30 09:43:14 +0800397 goto upgrade_h2c;
Andy Green024eb6c2014-10-08 12:00:53 +0800398#endif
Andy Green2af58562014-09-30 08:15:49 +0800399 /* dunno what he wanted to upgrade to */
400 goto bail_nuke_ah;
Andy Greena54f2322014-09-30 09:43:14 +0800401
Andy Green024eb6c2014-10-08 12:00:53 +0800402#ifdef LWS_USE_HTTP2
403upgrade_h2c:
404 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
405 lwsl_err("missing http2_settings\n");
Andy Greena54f2322014-09-30 09:43:14 +0800406 goto bail_nuke_ah;
407 }
408
Andy Green024eb6c2014-10-08 12:00:53 +0800409 lwsl_err("h2c upgrade...\n");
Andy Greena54f2322014-09-30 09:43:14 +0800410
Andy Green024eb6c2014-10-08 12:00:53 +0800411 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
412 /* convert the peer's HTTP-Settings */
413 n = lws_b64_decode_string(p, protocol_list, sizeof(protocol_list));
414 if (n < 0) {
415 lwsl_parser("HTTP2_SETTINGS too long\n");
416 return 1;
417 }
418
419 /* adopt the header info */
420
421 ah = wsi->u.hdr.ah;
Andy Greena54f2322014-09-30 09:43:14 +0800422
Andy Green44c11612014-11-08 11:18:47 +0800423 lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING);
Andy Green2af58562014-09-30 08:15:49 +0800424
Andy Green024eb6c2014-10-08 12:00:53 +0800425 /* http2 union member has http union struct at start */
426 wsi->u.http.ah = ah;
427
428 lws_http2_init(&wsi->u.http2.peer_settings);
429 lws_http2_init(&wsi->u.http2.my_settings);
430
431 /* HTTP2 union */
432
433 lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings, (unsigned char *)protocol_list, n);
434
435 strcpy(protocol_list,
436 "HTTP/1.1 101 Switching Protocols\x0d\x0a"
437 "Connection: Upgrade\x0d\x0a"
438 "Upgrade: h2c\x0d\x0a\x0d\x0a");
Andy Green97ee57f2014-10-29 09:39:08 +0800439 n = lws_issue_raw(wsi, (unsigned char *)protocol_list,
440 strlen(protocol_list));
441 if (n != strlen(protocol_list)) {
Andy Green024eb6c2014-10-08 12:00:53 +0800442 lwsl_debug("http2 switch: ERROR writing to socket\n");
443 return 1;
444 }
445
446 wsi->state = WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE;
447
Andy Greena54f2322014-09-30 09:43:14 +0800448 return 0;
Andy Green024eb6c2014-10-08 12:00:53 +0800449#endif
Andy Greena54f2322014-09-30 09:43:14 +0800450
Andy Green2af58562014-09-30 08:15:49 +0800451upgrade_ws:
Andy Greenaad2eac2014-04-03 09:03:37 +0800452 if (!wsi->protocol)
Andy Green62304762015-12-04 08:43:54 +0800453 lwsl_err("NULL protocol at lws_read\n");
Andy Greenaad2eac2014-04-03 09:03:37 +0800454
455 /*
456 * It's websocket
457 *
Andy Green7a8d86e2014-07-19 06:52:39 +0800458 * Select the first protocol we support from the list
459 * the client sent us.
460 *
461 * Copy it to remove header fragmentation
Andy Greenaad2eac2014-04-03 09:03:37 +0800462 */
463
Andy Green7a8d86e2014-07-19 06:52:39 +0800464 if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
465 WSI_TOKEN_PROTOCOL) < 0) {
466 lwsl_err("protocol list too long");
467 goto bail_nuke_ah;
468 }
Andy Greenaad2eac2014-04-03 09:03:37 +0800469
Andy Green7a8d86e2014-07-19 06:52:39 +0800470 protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
471 protocol_list[protocol_len] = '\0';
472 p = protocol_list;
473 hit = 0;
Andy Greenaad2eac2014-04-03 09:03:37 +0800474
Andy Green7a8d86e2014-07-19 06:52:39 +0800475 while (*p && !hit) {
Andy Green2cd30742015-11-02 13:10:33 +0800476 unsigned int n = 0;
Andy Green7a8d86e2014-07-19 06:52:39 +0800477 while (n < sizeof(protocol_name) - 1 && *p && *p !=',')
478 protocol_name[n++] = *p++;
479 protocol_name[n] = '\0';
480 if (*p)
481 p++;
482
483 lwsl_info("checking %s\n", protocol_name);
484
485 n = 0;
Andy Greenf14ea7a2014-11-30 12:45:39 +0800486 while (wsi->protocol && context->protocols[n].callback) {
Andy Greenafa10d52014-08-11 09:11:57 +0800487 if (!wsi->protocol->name) {
488 n++;
Andy Green7a8d86e2014-07-19 06:52:39 +0800489 continue;
Andy Greenafa10d52014-08-11 09:11:57 +0800490 }
Andy Green7a8d86e2014-07-19 06:52:39 +0800491 if (!strcmp(context->protocols[n].name,
492 protocol_name)) {
493 lwsl_info("prot match %d\n", n);
494 wsi->protocol = &context->protocols[n];
495 hit = 1;
496 break;
497 }
498
499 n++;
500 }
Andy Greenaad2eac2014-04-03 09:03:37 +0800501 }
502
503 /* we didn't find a protocol he wanted? */
504
Andy Green7a8d86e2014-07-19 06:52:39 +0800505 if (!hit) {
Andy Greenaad2eac2014-04-03 09:03:37 +0800506 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
507 NULL) {
Andy Green7a8d86e2014-07-19 06:52:39 +0800508 /*
509 * some clients only have one protocol and
510 * do not sent the protocol list header...
511 * allow it and match to protocol 0
512 */
513 lwsl_info("defaulting to prot 0 handler\n");
Andy Greenaad2eac2014-04-03 09:03:37 +0800514 wsi->protocol = &context->protocols[0];
515 } else {
Andy Green7a8d86e2014-07-19 06:52:39 +0800516 lwsl_err("No protocol from list \"%s\" supported\n",
517 protocol_list);
Andy Greenaad2eac2014-04-03 09:03:37 +0800518 goto bail_nuke_ah;
519 }
520 }
521
522 /* allocate wsi->user storage */
Andy Green3ef579b2015-12-04 09:23:56 +0800523 if (lws_ensure_user_space(wsi))
Andy Greenaad2eac2014-04-03 09:03:37 +0800524 goto bail_nuke_ah;
525
526 /*
527 * Give the user code a chance to study the request and
528 * have the opportunity to deny it
529 */
530
531 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
532 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
533 wsi->user_space,
534 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
535 lwsl_warn("User code denied connection\n");
536 goto bail_nuke_ah;
537 }
538
539
540 /*
541 * Perform the handshake according to the protocol version the
542 * client announced
543 */
544
545 switch (wsi->ietf_spec_revision) {
546 case 13:
547 lwsl_parser("lws_parse calling handshake_04\n");
548 if (handshake_0405(context, wsi)) {
549 lwsl_info("hs0405 has failed the connection\n");
550 goto bail_nuke_ah;
551 }
552 break;
553
554 default:
555 lwsl_warn("Unknown client spec version %d\n",
556 wsi->ietf_spec_revision);
557 goto bail_nuke_ah;
558 }
559
560 /* drop the header info -- no bail_nuke_ah after this */
Andrew Canadaya19d4852014-11-07 11:21:09 +0800561 lws_free_header_table(wsi);
Andy Greenaad2eac2014-04-03 09:03:37 +0800562
Andy Green44c11612014-11-08 11:18:47 +0800563 lws_union_transition(wsi, LWS_CONNMODE_WS_SERVING);
Andy Greenaad2eac2014-04-03 09:03:37 +0800564
565 /*
566 * create the frame buffer for this connection according to the
567 * size mentioned in the protocol definition. If 0 there, use
568 * a big default for compatibility
569 */
570
571 n = wsi->protocol->rx_buffer_size;
572 if (!n)
573 n = LWS_MAX_SOCKET_IO_BUF;
574 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
Alejandro Mery6ff28242014-12-04 23:59:35 +0100575 wsi->u.ws.rx_user_buffer = lws_malloc(n);
Andy Greenaad2eac2014-04-03 09:03:37 +0800576 if (!wsi->u.ws.rx_user_buffer) {
577 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
578 return 1;
579 }
580 lwsl_info("Allocating RX buffer %d\n", n);
Andy Green8c0d3c02015-11-02 20:34:12 +0800581#if LWS_POSIX
Andy Greenaad2eac2014-04-03 09:03:37 +0800582 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof n)) {
583 lwsl_warn("Failed to set SNDBUF to %d", n);
584 return 1;
585 }
Andy Green8c0d3c02015-11-02 20:34:12 +0800586#endif
Andy Greenaad2eac2014-04-03 09:03:37 +0800587 lwsl_parser("accepted v%02d connection\n",
588 wsi->ietf_spec_revision);
589 } /* while all chars are handled */
590
591 return 0;
592
593bail_nuke_ah:
594 /* drop the header info */
Andrew Canadaya19d4852014-11-07 11:21:09 +0800595 lws_free_header_table(wsi);
Andy Greenaad2eac2014-04-03 09:03:37 +0800596 return 1;
597}
598
Andy Green4b85c1d2015-12-04 11:08:32 +0800599struct lws *
600lws_create_new_server_wsi(struct lws_context *context)
Andy Greena1ce6be2013-01-18 11:43:21 +0800601{
Andy Green4b85c1d2015-12-04 11:08:32 +0800602 struct lws *new_wsi;
Andy Greena1ce6be2013-01-18 11:43:21 +0800603
Andy Green4b85c1d2015-12-04 11:08:32 +0800604 new_wsi = lws_zalloc(sizeof(struct lws));
Andy Greena1ce6be2013-01-18 11:43:21 +0800605 if (new_wsi == NULL) {
606 lwsl_err("Out of memory for new connection\n");
607 return NULL;
608 }
609
Andy Greena1ce6be2013-01-18 11:43:21 +0800610 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
Andy Green024eb6c2014-10-08 12:00:53 +0800611 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
Andy Greena1ce6be2013-01-18 11:43:21 +0800612
613 /* intialize the instance struct */
614
615 new_wsi->state = WSI_STATE_HTTP;
Andy Greena1ce6be2013-01-18 11:43:21 +0800616 new_wsi->mode = LWS_CONNMODE_HTTP_SERVING;
Andy Green224149a2013-02-11 21:43:41 +0800617 new_wsi->hdr_parsing_completed = 0;
Andy Greena1ce6be2013-01-18 11:43:21 +0800618
Jose Luis Millan45a04b62015-03-28 10:20:50 +0800619#ifdef LWS_OPENSSL_SUPPORT
620 new_wsi->use_ssl = LWS_SSL_ENABLED(context);
621#endif
622
Andy Green16ab3182013-02-10 18:02:31 +0800623 if (lws_allocate_header_table(new_wsi)) {
Alejandro Mery6ff28242014-12-04 23:59:35 +0100624 lws_free(new_wsi);
Andy Green16ab3182013-02-10 18:02:31 +0800625 return NULL;
Andy Greena1ce6be2013-01-18 11:43:21 +0800626 }
627
628 /*
629 * these can only be set once the protocol is known
630 * we set an unestablished connection's protocol pointer
631 * to the start of the supported list, so it can look
632 * for matching ones during the handshake
633 */
634 new_wsi->protocol = context->protocols;
635 new_wsi->user_space = NULL;
Andy Greena1ce6be2013-01-18 11:43:21 +0800636 new_wsi->ietf_spec_revision = 0;
Andy Greenc53f7ca2015-11-14 07:35:27 +0800637 new_wsi->sock = LWS_SOCK_INVALID;
638
Andy Green76b6ea12014-02-15 19:25:50 +0800639 /*
640 * outermost create notification for wsi
641 * no user_space because no protocol selection
642 */
643 context->protocols[0].callback(context, new_wsi,
644 LWS_CALLBACK_WSI_CREATE, NULL, NULL, 0);
645
Andy Greena1ce6be2013-01-18 11:43:21 +0800646 return new_wsi;
647}
648
Andy Green91b05892014-10-17 08:38:44 +0800649/**
650 * lws_http_transaction_completed() - wait for new http transaction or close
651 * @wsi: websocket connection
652 *
653 * Returns 1 if the HTTP connection must close now
654 * Returns 0 and resets connection to wait for new HTTP header /
655 * transaction if possible
656 */
657
658LWS_VISIBLE
Andy Green4b85c1d2015-12-04 11:08:32 +0800659int lws_http_transaction_completed(struct lws *wsi)
Andy Green91b05892014-10-17 08:38:44 +0800660{
661 /* if we can't go back to accept new headers, drop the connection */
662 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
663 lwsl_info("%s: close connection\n", __func__);
664 return 1;
665 }
666
667 /* otherwise set ourselves up ready to go again */
668 wsi->state = WSI_STATE_HTTP;
Andy Green26271482015-10-21 08:16:34 +0800669 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
670 wsi->u.http.content_length = 0;
671
672 /* He asked for it to stay alive indefinitely */
Andy Green62304762015-12-04 08:43:54 +0800673 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
Andy Green26271482015-10-21 08:16:34 +0800674
675 if (lws_allocate_header_table(wsi))
676 return 1;
677
678 /* If we're (re)starting on headers, need other implied init */
679 wsi->u.hdr.ues = URIES_IDLE;
Andy Green91b05892014-10-17 08:38:44 +0800680
Andy Green26271482015-10-21 08:16:34 +0800681 lwsl_info("%s: keep-alive await new transaction\n", __func__);
Andy Green91b05892014-10-17 08:38:44 +0800682
683 return 0;
684}
685
Andy Green8c0d3c02015-11-02 20:34:12 +0800686LWS_VISIBLE
Andy Green4b85c1d2015-12-04 11:08:32 +0800687int lws_server_socket_service(struct lws_context *context,
688 struct lws *wsi, struct lws_pollfd *pollfd)
Andy Greena1ce6be2013-01-18 11:43:21 +0800689{
Andy Green4b85c1d2015-12-04 11:08:32 +0800690 struct lws *new_wsi = NULL;
Andy Green8c0d3c02015-11-02 20:34:12 +0800691 lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
692#if LWS_POSIX
Bob Robertsac049112013-04-25 09:16:30 +0800693 socklen_t clilen;
Andy Greena1ce6be2013-01-18 11:43:21 +0800694 struct sockaddr_in cli_addr;
Andy Green8c0d3c02015-11-02 20:34:12 +0800695#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800696 int n;
Patrick Ganstererac49f1e2014-03-30 10:18:51 +0200697 int len;
Andy Greena1ce6be2013-01-18 11:43:21 +0800698
699 switch (wsi->mode) {
700
701 case LWS_CONNMODE_HTTP_SERVING:
Andy Green7cf6cb02013-05-19 14:04:10 +0800702 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
Andy Green024eb6c2014-10-08 12:00:53 +0800703 case LWS_CONNMODE_HTTP2_SERVING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800704
705 /* handle http headers coming in */
706
Andy Green2764eba2013-12-09 14:16:17 +0800707 /* pending truncated sends have uber priority */
708
Andrew Canadayaf8db352014-08-23 21:45:12 -0400709 if (wsi->truncated_send_len) {
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200710 if (pollfd->revents & LWS_POLLOUT)
Andy Greena1a24d22014-04-10 14:25:24 +0800711 if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
Andy Green2764eba2013-12-09 14:16:17 +0800712 wsi->truncated_send_offset,
Andy Greena1a24d22014-04-10 14:25:24 +0800713 wsi->truncated_send_len) < 0) {
714 lwsl_info("closing from socket service\n");
715 return -1;
716 }
Andy Green2764eba2013-12-09 14:16:17 +0800717 /*
718 * we can't afford to allow input processing send
719 * something new, so spin around he event loop until
720 * he doesn't have any partials
721 */
722 break;
723 }
724
Andy Greena1ce6be2013-01-18 11:43:21 +0800725 /* any incoming data ready? */
726
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200727 if (pollfd->revents & LWS_POLLIN) {
Andy Green1f5c9f02014-10-09 08:14:30 +0800728 len = lws_ssl_capable_read(context, wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800729 context->service_buffer,
Andy Green78f266a2014-04-05 16:48:48 +0100730 sizeof(context->service_buffer));
Andy Green11f27342015-11-08 12:10:26 +0800731 lwsl_debug("%s: read %d\r\n", __func__, len);
Andy Green78f266a2014-04-05 16:48:48 +0100732 switch (len) {
733 case 0:
Andy Green224149a2013-02-11 21:43:41 +0800734 lwsl_info("lws_server_skt_srv: read 0 len\n");
735 /* lwsl_info(" state=%d\n", wsi->state); */
736 if (!wsi->hdr_parsing_completed)
Andrew Canadaya19d4852014-11-07 11:21:09 +0800737 lws_free_header_table(wsi);
Andy Green78f266a2014-04-05 16:48:48 +0100738 /* fallthru */
739 case LWS_SSL_CAPABLE_ERROR:
Andy Green3ef579b2015-12-04 09:23:56 +0800740 lws_close_and_free_session(
Andy Green78f266a2014-04-05 16:48:48 +0100741 context, wsi,
742 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greena1ce6be2013-01-18 11:43:21 +0800743 return 0;
Andy Green78f266a2014-04-05 16:48:48 +0100744 case LWS_SSL_CAPABLE_MORE_SERVICE:
Christian Schüldtfde93792014-10-09 08:37:12 +0800745 goto try_pollout;
Andy Greena1ce6be2013-01-18 11:43:21 +0800746 }
747
Andy Greena1a24d22014-04-10 14:25:24 +0800748 /* just ignore incoming if waiting for close */
749 if (wsi->state != WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
750
751 /* hm this may want to send (via HTTP callback for example) */
Andy Green62304762015-12-04 08:43:54 +0800752 n = lws_read(context, wsi,
Andy Greena1a24d22014-04-10 14:25:24 +0800753 context->service_buffer, len);
754 if (n < 0)
755 /* we closed wsi */
756 return 0;
Andy Green2764eba2013-12-09 14:16:17 +0800757
Andy Greena1a24d22014-04-10 14:25:24 +0800758 /* hum he may have used up the writability above */
759 break;
760 }
Andy Greena1ce6be2013-01-18 11:43:21 +0800761 }
762
Christian Schüldtfde93792014-10-09 08:37:12 +0800763try_pollout:
Andy Greena1ce6be2013-01-18 11:43:21 +0800764 /* this handles POLLOUT for http serving fragments */
765
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200766 if (!(pollfd->revents & LWS_POLLOUT))
Andy Greena1ce6be2013-01-18 11:43:21 +0800767 break;
768
769 /* one shot */
Andy Green158e8042014-04-02 14:25:10 +0800770 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
771 goto fail;
Andy Greena717df22014-04-11 13:14:37 +0800772
773 lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE);
Andy Green7a132792013-12-18 09:48:26 +0800774
Andy Green54cb3462013-02-14 22:23:54 +0800775 if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) {
776 n = user_callback_handle_rxflow(
777 wsi->protocol->callback,
778 wsi->protocol->owning_server,
779 wsi, LWS_CALLBACK_HTTP_WRITEABLE,
780 wsi->user_space,
781 NULL,
782 0);
783 if (n < 0)
Andy Green28e2ab62014-11-30 13:35:24 +0800784 goto fail;
Andy Greena1ce6be2013-01-18 11:43:21 +0800785 break;
Andy Green54cb3462013-02-14 22:23:54 +0800786 }
Andy Greena1ce6be2013-01-18 11:43:21 +0800787
Andy Green91b05892014-10-17 08:38:44 +0800788 /* >0 == completion, <0 == error */
Andy Green62304762015-12-04 08:43:54 +0800789 n = lws_serve_http_file_fragment(context, wsi);
Andy Green91b05892014-10-17 08:38:44 +0800790 if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi)))
Andy Green28e2ab62014-11-30 13:35:24 +0800791 goto fail;
Andy Greena1ce6be2013-01-18 11:43:21 +0800792 break;
793
794 case LWS_CONNMODE_SERVER_LISTENER:
795
Andy Green8c0d3c02015-11-02 20:34:12 +0800796#if LWS_POSIX
Andy Greena1ce6be2013-01-18 11:43:21 +0800797 /* pollin means a client has connected to us then */
798
Patrick Ganstererb47f87b2014-03-30 09:18:05 +0200799 if (!(pollfd->revents & LWS_POLLIN))
Andy Greena1ce6be2013-01-18 11:43:21 +0800800 break;
801
802 /* listen socket got an unencrypted connection... */
Andy Green8c0d3c02015-11-02 20:34:12 +0800803
Andy Greena1ce6be2013-01-18 11:43:21 +0800804 clilen = sizeof(cli_addr);
Andy Greene000a702013-01-29 12:37:35 +0800805 lws_latency_pre(context, wsi);
Andy Greena1ce6be2013-01-18 11:43:21 +0800806 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
807 &clilen);
Andy Greenb5b23192013-02-11 17:13:32 +0800808 lws_latency(context, wsi,
809 "unencrypted accept LWS_CONNMODE_SERVER_LISTENER",
810 accept_fd, accept_fd >= 0);
Andy Greena1ce6be2013-01-18 11:43:21 +0800811 if (accept_fd < 0) {
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100812 if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EWOULDBLOCK) {
Andy Greene2160712013-01-28 12:19:10 +0800813 lwsl_debug("accept asks to try again\n");
814 break;
815 }
Patrick Gansterer2dbd8372014-02-28 12:37:52 +0100816 lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO));
Andy Greena1ce6be2013-01-18 11:43:21 +0800817 break;
818 }
819
Andy Green158e8042014-04-02 14:25:10 +0800820 lws_plat_set_socket_options(context, accept_fd);
Andy Green8c0d3c02015-11-02 20:34:12 +0800821#else
822 /* not very beautiful... */
823 accept_fd = (lws_sockfd_type)pollfd;
824#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800825 /*
826 * look at who we connected to and give user code a chance
827 * to reject based on client IP. There's no protocol selected
828 * yet so we issue this to protocols[0]
829 */
830
831 if ((context->protocols[0].callback)(context, wsi,
832 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
Edwin van den Oetelaar8c8a8e12013-02-20 20:56:59 +0800833 NULL, (void *)(long)accept_fd, 0)) {
Andy Greena1ce6be2013-01-18 11:43:21 +0800834 lwsl_debug("Callback denied network connection\n");
835 compatible_close(accept_fd);
836 break;
837 }
838
Andy Green3ef579b2015-12-04 09:23:56 +0800839 new_wsi = lws_create_new_server_wsi(context);
Andy Greena1ce6be2013-01-18 11:43:21 +0800840 if (new_wsi == NULL) {
841 compatible_close(accept_fd);
842 break;
843 }
844
845 new_wsi->sock = accept_fd;
846
Andy Green176de272014-02-15 14:36:02 +0800847 /* the transport is accepted... give him time to negotiate */
Andy Green62304762015-12-04 08:43:54 +0800848 lws_set_timeout(new_wsi,
Andy Green176de272014-02-15 14:36:02 +0800849 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
850 AWAITING_TIMEOUT);
851
Andy Green8c0d3c02015-11-02 20:34:12 +0800852#if LWS_POSIX == 0
853 mbed3_tcp_stream_accept(accept_fd, new_wsi);
854#endif
855
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200856 /*
857 * A new connection was accepted. Give the user a chance to
858 * set properties of the newly created wsi. There's no protocol
859 * selected yet so we issue this to protocols[0]
860 */
861
862 (context->protocols[0].callback)(context, new_wsi,
863 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED, NULL, NULL, 0);
864
Andy Greena717df22014-04-11 13:14:37 +0800865 lws_libev_accept(context, new_wsi, accept_fd);
Alexandre Erwin Ittnerd578f572014-02-06 23:15:51 -0200866
Andy Greencdb9bf92014-04-12 10:07:02 +0800867 if (!LWS_SSL_ENABLED(context)) {
Andy Green11f27342015-11-08 12:10:26 +0800868#if LWS_POSIX
Andy Greena1ce6be2013-01-18 11:43:21 +0800869 lwsl_debug("accepted new conn port %u on fd=%d\n",
870 ntohs(cli_addr.sin_port), accept_fd);
Andy Green11f27342015-11-08 12:10:26 +0800871#endif
Andy Green1963c9a2015-10-15 07:39:33 +0800872 if (insert_wsi_socket_into_fds(context, new_wsi))
873 goto fail;
Andy Greene2160712013-01-28 12:19:10 +0800874 }
Andy Greena1ce6be2013-01-18 11:43:21 +0800875 break;
Andy Greene2160712013-01-28 12:19:10 +0800876
Andy Greena1ce6be2013-01-18 11:43:21 +0800877 default:
878 break;
879 }
Andy Greencdb9bf92014-04-12 10:07:02 +0800880
Andy Green62824f92014-08-10 09:50:42 +0800881 if (lws_server_socket_service_ssl(context, &wsi, new_wsi,
882 accept_fd, pollfd))
883 goto fail;
Andy Greencdb9bf92014-04-12 10:07:02 +0800884
Andy Greena1ce6be2013-01-18 11:43:21 +0800885 return 0;
wonder-micedd32c242015-04-23 05:37:46 +0800886
Andy Green158e8042014-04-02 14:25:10 +0800887fail:
Andy Green3ef579b2015-12-04 09:23:56 +0800888 lws_close_and_free_session(context, wsi,
Andy Green158e8042014-04-02 14:25:10 +0800889 LWS_CLOSE_STATUS_NOSTATUS);
890 return 1;
Andy Greena1ce6be2013-01-18 11:43:21 +0800891}
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100892
Andy Green4e7a1332013-11-11 07:30:33 +0800893/**
Andy Green62304762015-12-04 08:43:54 +0800894 * lws_serve_http_file() - Send a file back to the client using http
Andy Green4e7a1332013-11-11 07:30:33 +0800895 * @context: libwebsockets context
896 * @wsi: Websocket instance (available from user callback)
897 * @file: The file to issue over http
898 * @content_type: The http content type, eg, text/html
899 * @other_headers: NULL or pointer to \0-terminated other header string
900 *
901 * This function is intended to be called from the callback in response
902 * to http requests from the client. It allows the callback to issue
903 * local files down the http link in a single step.
904 *
905 * Returning <0 indicates error and the wsi should be closed. Returning
Andy Green91b05892014-10-17 08:38:44 +0800906 * >0 indicates the file was completely sent and
907 * lws_http_transaction_completed() called on the wsi (and close if != 0)
Andy Green4e7a1332013-11-11 07:30:33 +0800908 * ==0 indicates the file transfer is started and needs more service later,
909 * the wsi should be left alone.
910 */
911
Andy Green62304762015-12-04 08:43:54 +0800912LWS_VISIBLE int lws_serve_http_file(
Andy Green4b85c1d2015-12-04 11:08:32 +0800913 struct lws_context *context,
914 struct lws *wsi, const char *file,
Andy Green917f43a2014-10-12 14:31:47 +0800915 const char *content_type, const char *other_headers,
916 int other_headers_len)
Andy Green4e7a1332013-11-11 07:30:33 +0800917{
Andy Green024eb6c2014-10-08 12:00:53 +0800918 unsigned char *response = context->service_buffer + LWS_SEND_BUFFER_PRE_PADDING;
919 unsigned char *p = response;
Andy Green917f43a2014-10-12 14:31:47 +0800920 unsigned char *end = p + sizeof(context->service_buffer) -
921 LWS_SEND_BUFFER_PRE_PADDING;
Andy Green4e7a1332013-11-11 07:30:33 +0800922 int ret = 0;
Andy Green4e7a1332013-11-11 07:30:33 +0800923
Andy Green158e8042014-04-02 14:25:10 +0800924 wsi->u.http.fd = lws_plat_open_file(file, &wsi->u.http.filelen);
Andy Green4e7a1332013-11-11 07:30:33 +0800925
Patrick Gansterer81338aa2014-02-27 03:21:50 +0100926 if (wsi->u.http.fd == LWS_INVALID_FILE) {
Andy Green4e7a1332013-11-11 07:30:33 +0800927 lwsl_err("Unable to open '%s'\n", file);
Andy Green62304762015-12-04 08:43:54 +0800928 lws_return_http_status(context, wsi,
Andy Green4e7a1332013-11-11 07:30:33 +0800929 HTTP_STATUS_NOT_FOUND, NULL);
Andy Green4e7a1332013-11-11 07:30:33 +0800930 return -1;
931 }
932
Andy Green917f43a2014-10-12 14:31:47 +0800933 if (lws_add_http_header_status(context, wsi, 200, &p, end))
Andy Green91b05892014-10-17 08:38:44 +0800934 return -1;
Andy Green917f43a2014-10-12 14:31:47 +0800935 if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER, (unsigned char *)"libwebsockets", 13, &p, end))
Andy Green91b05892014-10-17 08:38:44 +0800936 return -1;
Andy Green917f43a2014-10-12 14:31:47 +0800937 if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (unsigned char *)content_type, strlen(content_type), &p, end))
Andy Green91b05892014-10-17 08:38:44 +0800938 return -1;
Andy Green200f3852014-10-18 12:23:05 +0800939 if (lws_add_http_header_content_length(context, wsi, wsi->u.http.filelen, &p, end))
Andy Green91b05892014-10-17 08:38:44 +0800940 return -1;
Andy Green4e7a1332013-11-11 07:30:33 +0800941
Andy Green917f43a2014-10-12 14:31:47 +0800942 if (other_headers) {
943 if ((end - p) < other_headers_len)
944 return -1;
945 memcpy(p, other_headers, other_headers_len);
946 p += other_headers_len;
947 }
948
949 if (lws_finalize_http_header(context, wsi, &p, end))
Andy Green91b05892014-10-17 08:38:44 +0800950 return -1;
Andy Green917f43a2014-10-12 14:31:47 +0800951
Andy Green62304762015-12-04 08:43:54 +0800952 ret = lws_write(wsi, response,
Andy Green024eb6c2014-10-08 12:00:53 +0800953 p - response, LWS_WRITE_HTTP_HEADERS);
954 if (ret != (p - response)) {
955 lwsl_err("_write returned %d from %d\n", ret, (p - response));
Andy Green4e7a1332013-11-11 07:30:33 +0800956 return -1;
957 }
958
959 wsi->u.http.filepos = 0;
960 wsi->state = WSI_STATE_HTTP_ISSUING_FILE;
961
Andy Green62304762015-12-04 08:43:54 +0800962 return lws_serve_http_file_fragment(context, wsi);
Andy Green4e7a1332013-11-11 07:30:33 +0800963}
964
Andy Greend7340c12014-04-10 14:08:10 +0800965
Andy Green4b85c1d2015-12-04 11:08:32 +0800966int lws_interpret_incoming_packet(struct lws *wsi,
Andy Greend7340c12014-04-10 14:08:10 +0800967 unsigned char *buf, size_t len)
968{
969 size_t n = 0;
970 int m;
971
972#if 0
973 lwsl_parser("received %d byte packet\n", (int)len);
974 lwsl_hexdump(buf, len);
975#endif
976
977 /* let the rx protocol state machine have as much as it needs */
978
979 while (n < len) {
980 /*
981 * we were accepting input but now we stopped doing so
982 */
Andy Green024eb6c2014-10-08 12:00:53 +0800983 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
984 lws_rxflow_cache(wsi, buf, n, len);
Andy Greend7340c12014-04-10 14:08:10 +0800985
986 return 1;
987 }
988
989 /* account for what we're using in rxflow buffer */
Andy Green024eb6c2014-10-08 12:00:53 +0800990 if (wsi->rxflow_buffer)
991 wsi->rxflow_pos++;
Andy Greend7340c12014-04-10 14:08:10 +0800992
993 /* process the byte */
Andy Green3ef579b2015-12-04 09:23:56 +0800994 m = lws_rx_sm(wsi, buf[n++]);
Andy Greend7340c12014-04-10 14:08:10 +0800995 if (m < 0)
996 return -1;
997 }
998
999 return 0;
Andy Greencdb9bf92014-04-12 10:07:02 +08001000}
1001
1002LWS_VISIBLE void
Andy Green4b85c1d2015-12-04 11:08:32 +08001003lws_server_get_canonical_hostname(struct lws_context *context,
Andy Greencdb9bf92014-04-12 10:07:02 +08001004 struct lws_context_creation_info *info)
1005{
1006 if (info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)
1007 return;
Andy Green8c0d3c02015-11-02 20:34:12 +08001008#if LWS_POSIX
Andy Greencdb9bf92014-04-12 10:07:02 +08001009 /* find canonical hostname */
1010 gethostname((char *)context->canonical_hostname,
1011 sizeof(context->canonical_hostname) - 1);
1012
1013 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Green8c0d3c02015-11-02 20:34:12 +08001014#else
1015 (void)context;
1016#endif
vpeter44dd8ada2014-04-27 13:28:22 +02001017}