Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com> |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 5 | * |
| 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 Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 25 | int lws_context_init_server(struct lws_context_creation_info *info, |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 26 | struct lws_context *context) |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 27 | { |
Andy Green | 3b19386 | 2015-11-02 13:13:44 +0800 | [diff] [blame] | 28 | lws_sockfd_type sockfd; |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 29 | #if LWS_POSIX |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 30 | int n; |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 31 | struct sockaddr_in sin; |
| 32 | socklen_t len = sizeof(sin); |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 33 | #ifdef LWS_USE_IPV6 |
| 34 | struct sockaddr_in6 serv_addr6; |
| 35 | #endif |
| 36 | struct sockaddr_in serv_addr4; |
| 37 | struct sockaddr *v; |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 38 | int opt = 1; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 39 | #endif |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 40 | struct lws *wsi; |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 41 | |
| 42 | /* set up our external listening socket we serve on */ |
| 43 | |
| 44 | if (info->port == CONTEXT_PORT_NO_LISTEN) |
| 45 | return 0; |
| 46 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 47 | #if LWS_POSIX |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 48 | #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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 54 | |
Andy Green | fc772cc | 2015-11-14 13:48:58 +0800 | [diff] [blame] | 55 | if (sockfd == -1) { |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 56 | #else |
| 57 | sockfd = mbed3_create_tcp_stream_socket(); |
| 58 | if (!lws_sockfd_valid(sockfd)) { |
| 59 | #endif |
| 60 | |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 61 | lwsl_err("ERROR opening socket\n"); |
| 62 | return 1; |
| 63 | } |
| 64 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 65 | #if LWS_POSIX |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 66 | /* |
| 67 | * allow us to restart even if old sockets in TIME_WAIT |
| 68 | */ |
Andy Green | f38e786 | 2014-11-30 13:07:11 +0800 | [diff] [blame] | 69 | if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
Andy Green | 9c8d590 | 2014-11-30 13:30:57 +0800 | [diff] [blame] | 70 | (const void *)&opt, sizeof(opt)) < 0) { |
| 71 | compatible_close(sockfd); |
Andy Green | f38e786 | 2014-11-30 13:07:11 +0800 | [diff] [blame] | 72 | return 1; |
Andy Green | 9c8d590 | 2014-11-30 13:30:57 +0800 | [diff] [blame] | 73 | } |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 74 | #endif |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 75 | lws_plat_set_socket_options(context, sockfd); |
| 76 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 77 | #if LWS_POSIX |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 78 | #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 Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 94 | |
| 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 | } |
vpeter4 | 7cc7ae4 | 2014-04-27 12:32:15 +0200 | [diff] [blame] | 104 | |
| 105 | serv_addr4.sin_port = htons(info->port); |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 106 | } /* 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 Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 115 | |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 116 | 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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 120 | #endif |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 121 | |
| 122 | context->listen_port = info->port; |
| 123 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 124 | wsi = lws_zalloc(sizeof(struct lws)); |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 125 | if (wsi == NULL) { |
| 126 | lwsl_err("Out of mem\n"); |
| 127 | compatible_close(sockfd); |
| 128 | return 1; |
| 129 | } |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 130 | wsi->sock = sockfd; |
| 131 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 132 | wsi->protocol = context->protocols; |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 133 | |
Andy Green | 1963c9a | 2015-10-15 07:39:33 +0800 | [diff] [blame] | 134 | if (insert_wsi_socket_into_fds(context, wsi)) { |
| 135 | compatible_close(sockfd); |
| 136 | return 1; |
| 137 | } |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 138 | |
| 139 | context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO; |
| 140 | context->listen_service_count = 0; |
| 141 | context->listen_service_fd = sockfd; |
| 142 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 143 | #if LWS_POSIX |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 144 | listen(sockfd, LWS_SOMAXCONN); |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 145 | #else |
| 146 | mbed3_tcp_stream_bind(sockfd, info->port, wsi); |
| 147 | #endif |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 148 | lwsl_notice(" Listening on port %d\n", info->port); |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 149 | |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 153 | int |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 154 | _lws_rx_flow_control(struct lws *wsi) |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 155 | { |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 156 | struct lws_context *context = wsi->protocol->owning_server; |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 157 | |
| 158 | /* there is no pending change */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 159 | if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 160 | return 0; |
| 161 | |
| 162 | /* stuff is still buffered, not ready to really accept new input */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 163 | if (wsi->rxflow_buffer) { |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 164 | /* get ourselves called back to deal with stashed buffer */ |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 165 | lws_callback_on_writable(context, wsi); |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* pending is cleared, we can change rxflow state */ |
| 170 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 171 | wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE; |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 172 | |
| 173 | lwsl_info("rxflow: wsi %p change_to %d\n", wsi, |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 174 | wsi->rxflow_change_to & LWS_RXFLOW_ALLOW); |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 175 | |
| 176 | /* adjust the pollfd for this wsi */ |
| 177 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 178 | if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) { |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 179 | if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) { |
| 180 | lwsl_info("%s: fail\n", __func__); |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 181 | return -1; |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 182 | } |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 183 | } else |
| 184 | if (lws_change_pollfd(wsi, LWS_POLLIN, 0)) |
| 185 | return -1; |
| 186 | |
Andy Green | f004ec5 | 2014-04-12 11:47:25 +0800 | [diff] [blame] | 187 | return 0; |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 190 | int lws_http_action(struct lws_context *context, |
| 191 | struct lws *wsi) |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 192 | { |
| 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 Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 201 | unsigned int n, count = 0; |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 202 | 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 Green | 93d947c | 2015-02-20 07:37:20 +0800 | [diff] [blame] | 213 | #ifdef _DEBUG |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 214 | static const char * const method_names[] = { |
| 215 | "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", |
| 216 | #ifdef LWS_USE_HTTP2 |
| 217 | ":path", |
| 218 | #endif |
| 219 | }; |
Andy Green | 93d947c | 2015-02-20 07:37:20 +0800 | [diff] [blame] | 220 | #endif |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 221 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 222 | /* it's not websocket.... shall we accept it as http? */ |
| 223 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 224 | for (n = 0; n < ARRAY_SIZE(methods); n++) |
| 225 | if (lws_hdr_total_length(wsi, methods[n])) |
| 226 | count++; |
| 227 | if (!count) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 228 | lwsl_warn("Missing URI in HTTP request\n"); |
| 229 | goto bail_nuke_ah; |
| 230 | } |
| 231 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 232 | if (count != 1) { |
| 233 | lwsl_warn("multiple methods?\n"); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 234 | goto bail_nuke_ah; |
| 235 | } |
| 236 | |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 237 | if (lws_ensure_user_space(wsi)) |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 238 | goto bail_nuke_ah; |
| 239 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 240 | 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 Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 248 | |
| 249 | /* HTTP header had a content length? */ |
| 250 | |
| 251 | wsi->u.http.content_length = 0; |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 252 | 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 Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 255 | 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 20001 | 0d89f3c | 2015-10-20 12:12:12 +0800 | [diff] [blame] | 291 | if (!strcasecmp(http_conn_str, "close")) |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 292 | 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 Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 307 | lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 308 | 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 Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 317 | lws_free2(wsi->u.http.ah); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 318 | |
| 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 Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 328 | * In any case, return 0 and let lws_read decide how to |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 329 | * 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 | |
| 338 | bail_nuke_ah: |
| 339 | /* drop the header info */ |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 340 | lws_free2(wsi->u.hdr.ah); |
| 341 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 342 | return 1; |
| 343 | } |
| 344 | |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 345 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 346 | int lws_handshake_server(struct lws_context *context, |
| 347 | struct lws *wsi, unsigned char **buf, size_t len) |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 348 | { |
| 349 | struct allocated_headers *ah; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 350 | int protocol_len; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 351 | char protocol_list[128]; |
| 352 | char protocol_name[32]; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 353 | char *p; |
| 354 | int n, hit; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 355 | |
| 356 | /* LWS_CONNMODE_WS_SERVING */ |
| 357 | |
| 358 | while (len--) { |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 359 | if (lws_parse(context, wsi, *(*buf)++)) { |
| 360 | lwsl_info("lws_parse failed\n"); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 361 | goto bail_nuke_ah; |
| 362 | } |
| 363 | |
| 364 | if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE) |
| 365 | continue; |
| 366 | |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 367 | lwsl_parser("lws_parse sees parsing complete\n"); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 368 | |
| 369 | wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT; |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 370 | lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 371 | |
| 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 Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 376 | |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 377 | ah = wsi->u.hdr.ah; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 378 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 379 | lws_union_transition(wsi, LWS_CONNMODE_HTTP_SERVING_ACCEPTED); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 380 | 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 Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 385 | |
| 386 | n = lws_http_action(context, wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 387 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 388 | return n; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 391 | if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE), |
| 392 | "websocket")) |
| 393 | goto upgrade_ws; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 394 | #ifdef LWS_USE_HTTP2 |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 395 | if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE), |
Andy Green | b08cb50 | 2014-09-30 16:33:56 +0800 | [diff] [blame] | 396 | "h2c-14")) |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 397 | goto upgrade_h2c; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 398 | #endif |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 399 | /* dunno what he wanted to upgrade to */ |
| 400 | goto bail_nuke_ah; |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 401 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 402 | #ifdef LWS_USE_HTTP2 |
| 403 | upgrade_h2c: |
| 404 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) { |
| 405 | lwsl_err("missing http2_settings\n"); |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 406 | goto bail_nuke_ah; |
| 407 | } |
| 408 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 409 | lwsl_err("h2c upgrade...\n"); |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 410 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 411 | 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 Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 422 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 423 | lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING); |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 424 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 425 | /* 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 Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 439 | n = lws_issue_raw(wsi, (unsigned char *)protocol_list, |
| 440 | strlen(protocol_list)); |
| 441 | if (n != strlen(protocol_list)) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 442 | 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 Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 448 | return 0; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 449 | #endif |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 450 | |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 451 | upgrade_ws: |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 452 | if (!wsi->protocol) |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 453 | lwsl_err("NULL protocol at lws_read\n"); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * It's websocket |
| 457 | * |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 458 | * Select the first protocol we support from the list |
| 459 | * the client sent us. |
| 460 | * |
| 461 | * Copy it to remove header fragmentation |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 462 | */ |
| 463 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 464 | 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 Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 469 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 470 | protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL); |
| 471 | protocol_list[protocol_len] = '\0'; |
| 472 | p = protocol_list; |
| 473 | hit = 0; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 474 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 475 | while (*p && !hit) { |
Andy Green | 2cd3074 | 2015-11-02 13:10:33 +0800 | [diff] [blame] | 476 | unsigned int n = 0; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 477 | 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 Green | f14ea7a | 2014-11-30 12:45:39 +0800 | [diff] [blame] | 486 | while (wsi->protocol && context->protocols[n].callback) { |
Andy Green | afa10d5 | 2014-08-11 09:11:57 +0800 | [diff] [blame] | 487 | if (!wsi->protocol->name) { |
| 488 | n++; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 489 | continue; |
Andy Green | afa10d5 | 2014-08-11 09:11:57 +0800 | [diff] [blame] | 490 | } |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 491 | 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 Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* we didn't find a protocol he wanted? */ |
| 504 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 505 | if (!hit) { |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 506 | if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) == |
| 507 | NULL) { |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 508 | /* |
| 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 Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 514 | wsi->protocol = &context->protocols[0]; |
| 515 | } else { |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 516 | lwsl_err("No protocol from list \"%s\" supported\n", |
| 517 | protocol_list); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 518 | goto bail_nuke_ah; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* allocate wsi->user storage */ |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 523 | if (lws_ensure_user_space(wsi)) |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 524 | 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 Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 561 | lws_free_header_table(wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 562 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 563 | lws_union_transition(wsi, LWS_CONNMODE_WS_SERVING); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 564 | |
| 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 Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 575 | wsi->u.ws.rx_user_buffer = lws_malloc(n); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 576 | 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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 581 | #if LWS_POSIX |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 582 | 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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 586 | #endif |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 587 | lwsl_parser("accepted v%02d connection\n", |
| 588 | wsi->ietf_spec_revision); |
| 589 | } /* while all chars are handled */ |
| 590 | |
| 591 | return 0; |
| 592 | |
| 593 | bail_nuke_ah: |
| 594 | /* drop the header info */ |
Andrew Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 595 | lws_free_header_table(wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 596 | return 1; |
| 597 | } |
| 598 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 599 | struct lws * |
| 600 | lws_create_new_server_wsi(struct lws_context *context) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 601 | { |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 602 | struct lws *new_wsi; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 603 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 604 | new_wsi = lws_zalloc(sizeof(struct lws)); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 605 | if (new_wsi == NULL) { |
| 606 | lwsl_err("Out of memory for new connection\n"); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 610 | new_wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 611 | new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 612 | |
| 613 | /* intialize the instance struct */ |
| 614 | |
| 615 | new_wsi->state = WSI_STATE_HTTP; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 616 | new_wsi->mode = LWS_CONNMODE_HTTP_SERVING; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 617 | new_wsi->hdr_parsing_completed = 0; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 618 | |
Jose Luis Millan | 45a04b6 | 2015-03-28 10:20:50 +0800 | [diff] [blame] | 619 | #ifdef LWS_OPENSSL_SUPPORT |
| 620 | new_wsi->use_ssl = LWS_SSL_ENABLED(context); |
| 621 | #endif |
| 622 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 623 | if (lws_allocate_header_table(new_wsi)) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 624 | lws_free(new_wsi); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 625 | return NULL; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 626 | } |
| 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 Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 636 | new_wsi->ietf_spec_revision = 0; |
Andy Green | c53f7ca | 2015-11-14 07:35:27 +0800 | [diff] [blame] | 637 | new_wsi->sock = LWS_SOCK_INVALID; |
| 638 | |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 639 | /* |
| 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 Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 646 | return new_wsi; |
| 647 | } |
| 648 | |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 649 | /** |
| 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 | |
| 658 | LWS_VISIBLE |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 659 | int lws_http_transaction_completed(struct lws *wsi) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 660 | { |
| 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 Green | 2627148 | 2015-10-21 08:16:34 +0800 | [diff] [blame] | 669 | wsi->mode = LWS_CONNMODE_HTTP_SERVING; |
| 670 | wsi->u.http.content_length = 0; |
| 671 | |
| 672 | /* He asked for it to stay alive indefinitely */ |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 673 | lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
Andy Green | 2627148 | 2015-10-21 08:16:34 +0800 | [diff] [blame] | 674 | |
| 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 Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 680 | |
Andy Green | 2627148 | 2015-10-21 08:16:34 +0800 | [diff] [blame] | 681 | lwsl_info("%s: keep-alive await new transaction\n", __func__); |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 686 | LWS_VISIBLE |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 687 | int lws_server_socket_service(struct lws_context *context, |
| 688 | struct lws *wsi, struct lws_pollfd *pollfd) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 689 | { |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 690 | struct lws *new_wsi = NULL; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 691 | lws_sockfd_type accept_fd = LWS_SOCK_INVALID; |
| 692 | #if LWS_POSIX |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 693 | socklen_t clilen; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 694 | struct sockaddr_in cli_addr; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 695 | #endif |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 696 | int n; |
Patrick Gansterer | ac49f1e | 2014-03-30 10:18:51 +0200 | [diff] [blame] | 697 | int len; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 698 | |
| 699 | switch (wsi->mode) { |
| 700 | |
| 701 | case LWS_CONNMODE_HTTP_SERVING: |
Andy Green | 7cf6cb0 | 2013-05-19 14:04:10 +0800 | [diff] [blame] | 702 | case LWS_CONNMODE_HTTP_SERVING_ACCEPTED: |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 703 | case LWS_CONNMODE_HTTP2_SERVING: |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 704 | |
| 705 | /* handle http headers coming in */ |
| 706 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 707 | /* pending truncated sends have uber priority */ |
| 708 | |
Andrew Canaday | af8db35 | 2014-08-23 21:45:12 -0400 | [diff] [blame] | 709 | if (wsi->truncated_send_len) { |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 710 | if (pollfd->revents & LWS_POLLOUT) |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 711 | if (lws_issue_raw(wsi, wsi->truncated_send_malloc + |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 712 | wsi->truncated_send_offset, |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 713 | wsi->truncated_send_len) < 0) { |
| 714 | lwsl_info("closing from socket service\n"); |
| 715 | return -1; |
| 716 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 717 | /* |
| 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 Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 725 | /* any incoming data ready? */ |
| 726 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 727 | if (pollfd->revents & LWS_POLLIN) { |
Andy Green | 1f5c9f0 | 2014-10-09 08:14:30 +0800 | [diff] [blame] | 728 | len = lws_ssl_capable_read(context, wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 729 | context->service_buffer, |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 730 | sizeof(context->service_buffer)); |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 731 | lwsl_debug("%s: read %d\r\n", __func__, len); |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 732 | switch (len) { |
| 733 | case 0: |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 734 | 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 Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 737 | lws_free_header_table(wsi); |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 738 | /* fallthru */ |
| 739 | case LWS_SSL_CAPABLE_ERROR: |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 740 | lws_close_and_free_session( |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 741 | context, wsi, |
| 742 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 743 | return 0; |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 744 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
Christian Schüldt | fde9379 | 2014-10-09 08:37:12 +0800 | [diff] [blame] | 745 | goto try_pollout; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 746 | } |
| 747 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 748 | /* 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 Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 752 | n = lws_read(context, wsi, |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 753 | context->service_buffer, len); |
| 754 | if (n < 0) |
| 755 | /* we closed wsi */ |
| 756 | return 0; |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 757 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 758 | /* hum he may have used up the writability above */ |
| 759 | break; |
| 760 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 761 | } |
| 762 | |
Christian Schüldt | fde9379 | 2014-10-09 08:37:12 +0800 | [diff] [blame] | 763 | try_pollout: |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 764 | /* this handles POLLOUT for http serving fragments */ |
| 765 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 766 | if (!(pollfd->revents & LWS_POLLOUT)) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | /* one shot */ |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 770 | if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) |
| 771 | goto fail; |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 772 | |
| 773 | lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE); |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 774 | |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 775 | 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 Green | 28e2ab6 | 2014-11-30 13:35:24 +0800 | [diff] [blame] | 784 | goto fail; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 785 | break; |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 786 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 787 | |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 788 | /* >0 == completion, <0 == error */ |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 789 | n = lws_serve_http_file_fragment(context, wsi); |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 790 | if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi))) |
Andy Green | 28e2ab6 | 2014-11-30 13:35:24 +0800 | [diff] [blame] | 791 | goto fail; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 792 | break; |
| 793 | |
| 794 | case LWS_CONNMODE_SERVER_LISTENER: |
| 795 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 796 | #if LWS_POSIX |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 797 | /* pollin means a client has connected to us then */ |
| 798 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 799 | if (!(pollfd->revents & LWS_POLLIN)) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 800 | break; |
| 801 | |
| 802 | /* listen socket got an unencrypted connection... */ |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 803 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 804 | clilen = sizeof(cli_addr); |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 805 | lws_latency_pre(context, wsi); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 806 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 807 | &clilen); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 808 | lws_latency(context, wsi, |
| 809 | "unencrypted accept LWS_CONNMODE_SERVER_LISTENER", |
| 810 | accept_fd, accept_fd >= 0); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 811 | if (accept_fd < 0) { |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 812 | if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EWOULDBLOCK) { |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 813 | lwsl_debug("accept asks to try again\n"); |
| 814 | break; |
| 815 | } |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 816 | lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO)); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 817 | break; |
| 818 | } |
| 819 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 820 | lws_plat_set_socket_options(context, accept_fd); |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 821 | #else |
| 822 | /* not very beautiful... */ |
| 823 | accept_fd = (lws_sockfd_type)pollfd; |
| 824 | #endif |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 825 | /* |
| 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 Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 833 | NULL, (void *)(long)accept_fd, 0)) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 834 | lwsl_debug("Callback denied network connection\n"); |
| 835 | compatible_close(accept_fd); |
| 836 | break; |
| 837 | } |
| 838 | |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 839 | new_wsi = lws_create_new_server_wsi(context); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 840 | if (new_wsi == NULL) { |
| 841 | compatible_close(accept_fd); |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | new_wsi->sock = accept_fd; |
| 846 | |
Andy Green | 176de27 | 2014-02-15 14:36:02 +0800 | [diff] [blame] | 847 | /* the transport is accepted... give him time to negotiate */ |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 848 | lws_set_timeout(new_wsi, |
Andy Green | 176de27 | 2014-02-15 14:36:02 +0800 | [diff] [blame] | 849 | PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, |
| 850 | AWAITING_TIMEOUT); |
| 851 | |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 852 | #if LWS_POSIX == 0 |
| 853 | mbed3_tcp_stream_accept(accept_fd, new_wsi); |
| 854 | #endif |
| 855 | |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 856 | /* |
| 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 Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 865 | lws_libev_accept(context, new_wsi, accept_fd); |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 866 | |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 867 | if (!LWS_SSL_ENABLED(context)) { |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 868 | #if LWS_POSIX |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 869 | lwsl_debug("accepted new conn port %u on fd=%d\n", |
| 870 | ntohs(cli_addr.sin_port), accept_fd); |
Andy Green | 11f2734 | 2015-11-08 12:10:26 +0800 | [diff] [blame] | 871 | #endif |
Andy Green | 1963c9a | 2015-10-15 07:39:33 +0800 | [diff] [blame] | 872 | if (insert_wsi_socket_into_fds(context, new_wsi)) |
| 873 | goto fail; |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 874 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 875 | break; |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 876 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 877 | default: |
| 878 | break; |
| 879 | } |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 880 | |
Andy Green | 62824f9 | 2014-08-10 09:50:42 +0800 | [diff] [blame] | 881 | if (lws_server_socket_service_ssl(context, &wsi, new_wsi, |
| 882 | accept_fd, pollfd)) |
| 883 | goto fail; |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 884 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 885 | return 0; |
wonder-mice | dd32c24 | 2015-04-23 05:37:46 +0800 | [diff] [blame] | 886 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 887 | fail: |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 888 | lws_close_and_free_session(context, wsi, |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 889 | LWS_CLOSE_STATUS_NOSTATUS); |
| 890 | return 1; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 891 | } |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 892 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 893 | /** |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 894 | * lws_serve_http_file() - Send a file back to the client using http |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 895 | * @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 Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 906 | * >0 indicates the file was completely sent and |
| 907 | * lws_http_transaction_completed() called on the wsi (and close if != 0) |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 908 | * ==0 indicates the file transfer is started and needs more service later, |
| 909 | * the wsi should be left alone. |
| 910 | */ |
| 911 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 912 | LWS_VISIBLE int lws_serve_http_file( |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 913 | struct lws_context *context, |
| 914 | struct lws *wsi, const char *file, |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 915 | const char *content_type, const char *other_headers, |
| 916 | int other_headers_len) |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 917 | { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 918 | unsigned char *response = context->service_buffer + LWS_SEND_BUFFER_PRE_PADDING; |
| 919 | unsigned char *p = response; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 920 | unsigned char *end = p + sizeof(context->service_buffer) - |
| 921 | LWS_SEND_BUFFER_PRE_PADDING; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 922 | int ret = 0; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 923 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 924 | wsi->u.http.fd = lws_plat_open_file(file, &wsi->u.http.filelen); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 925 | |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 926 | if (wsi->u.http.fd == LWS_INVALID_FILE) { |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 927 | lwsl_err("Unable to open '%s'\n", file); |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 928 | lws_return_http_status(context, wsi, |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 929 | HTTP_STATUS_NOT_FOUND, NULL); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 930 | return -1; |
| 931 | } |
| 932 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 933 | if (lws_add_http_header_status(context, wsi, 200, &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 934 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 935 | if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER, (unsigned char *)"libwebsockets", 13, &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 936 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 937 | if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (unsigned char *)content_type, strlen(content_type), &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 938 | return -1; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 939 | if (lws_add_http_header_content_length(context, wsi, wsi->u.http.filelen, &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 940 | return -1; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 941 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 942 | 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 Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 950 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 951 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 952 | ret = lws_write(wsi, response, |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 953 | p - response, LWS_WRITE_HTTP_HEADERS); |
| 954 | if (ret != (p - response)) { |
| 955 | lwsl_err("_write returned %d from %d\n", ret, (p - response)); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 956 | return -1; |
| 957 | } |
| 958 | |
| 959 | wsi->u.http.filepos = 0; |
| 960 | wsi->state = WSI_STATE_HTTP_ISSUING_FILE; |
| 961 | |
Andy Green | 6230476 | 2015-12-04 08:43:54 +0800 | [diff] [blame] | 962 | return lws_serve_http_file_fragment(context, wsi); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 963 | } |
| 964 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 965 | |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 966 | int lws_interpret_incoming_packet(struct lws *wsi, |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 967 | 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 Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 983 | if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) { |
| 984 | lws_rxflow_cache(wsi, buf, n, len); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 985 | |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | /* account for what we're using in rxflow buffer */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 990 | if (wsi->rxflow_buffer) |
| 991 | wsi->rxflow_pos++; |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 992 | |
| 993 | /* process the byte */ |
Andy Green | 3ef579b | 2015-12-04 09:23:56 +0800 | [diff] [blame] | 994 | m = lws_rx_sm(wsi, buf[n++]); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 995 | if (m < 0) |
| 996 | return -1; |
| 997 | } |
| 998 | |
| 999 | return 0; |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | LWS_VISIBLE void |
Andy Green | 4b85c1d | 2015-12-04 11:08:32 +0800 | [diff] [blame^] | 1003 | lws_server_get_canonical_hostname(struct lws_context *context, |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 1004 | struct lws_context_creation_info *info) |
| 1005 | { |
| 1006 | if (info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME) |
| 1007 | return; |
Andy Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 1008 | #if LWS_POSIX |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 1009 | /* 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 Green | 8c0d3c0 | 2015-11-02 20:34:12 +0800 | [diff] [blame] | 1014 | #else |
| 1015 | (void)context; |
| 1016 | #endif |
vpeter4 | 4dd8ada | 2014-04-27 13:28:22 +0200 | [diff] [blame] | 1017 | } |