Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
| 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
| 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, |
| 26 | struct libwebsocket_context *context) |
| 27 | { |
| 28 | int n; |
| 29 | int sockfd; |
| 30 | struct sockaddr_in sin; |
| 31 | socklen_t len = sizeof(sin); |
| 32 | int opt = 1; |
| 33 | struct libwebsocket *wsi; |
| 34 | #ifdef LWS_USE_IPV6 |
| 35 | struct sockaddr_in6 serv_addr6; |
| 36 | #endif |
| 37 | struct sockaddr_in serv_addr4; |
| 38 | struct sockaddr *v; |
| 39 | |
| 40 | /* set up our external listening socket we serve on */ |
| 41 | |
| 42 | if (info->port == CONTEXT_PORT_NO_LISTEN) |
| 43 | return 0; |
| 44 | |
| 45 | #ifdef LWS_USE_IPV6 |
| 46 | if (LWS_IPV6_ENABLED(context)) |
| 47 | sockfd = socket(AF_INET6, SOCK_STREAM, 0); |
| 48 | else |
| 49 | #endif |
| 50 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 51 | |
| 52 | if (sockfd < 0) { |
| 53 | lwsl_err("ERROR opening socket\n"); |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * allow us to restart even if old sockets in TIME_WAIT |
| 59 | */ |
Andy Green | f38e786 | 2014-11-30 13:07:11 +0800 | [diff] [blame] | 60 | if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
Andy Green | 9c8d590 | 2014-11-30 13:30:57 +0800 | [diff] [blame] | 61 | (const void *)&opt, sizeof(opt)) < 0) { |
| 62 | compatible_close(sockfd); |
Andy Green | f38e786 | 2014-11-30 13:07:11 +0800 | [diff] [blame] | 63 | return 1; |
Andy Green | 9c8d590 | 2014-11-30 13:30:57 +0800 | [diff] [blame] | 64 | } |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 65 | |
| 66 | lws_plat_set_socket_options(context, sockfd); |
| 67 | |
| 68 | #ifdef LWS_USE_IPV6 |
| 69 | if (LWS_IPV6_ENABLED(context)) { |
| 70 | v = (struct sockaddr *)&serv_addr6; |
| 71 | n = sizeof(struct sockaddr_in6); |
| 72 | bzero((char *) &serv_addr6, sizeof(serv_addr6)); |
| 73 | serv_addr6.sin6_addr = in6addr_any; |
| 74 | serv_addr6.sin6_family = AF_INET6; |
| 75 | serv_addr6.sin6_port = htons(info->port); |
| 76 | } else |
| 77 | #endif |
| 78 | { |
| 79 | v = (struct sockaddr *)&serv_addr4; |
| 80 | n = sizeof(serv_addr4); |
| 81 | bzero((char *) &serv_addr4, sizeof(serv_addr4)); |
| 82 | serv_addr4.sin_addr.s_addr = INADDR_ANY; |
| 83 | serv_addr4.sin_family = AF_INET; |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 84 | |
| 85 | if (info->iface) { |
| 86 | if (interface_to_sa(context, info->iface, |
| 87 | (struct sockaddr_in *)v, n) < 0) { |
| 88 | lwsl_err("Unable to find interface %s\n", |
| 89 | info->iface); |
| 90 | compatible_close(sockfd); |
| 91 | return 1; |
| 92 | } |
| 93 | } |
vpeter4 | 7cc7ae4 | 2014-04-27 12:32:15 +0200 | [diff] [blame] | 94 | |
| 95 | serv_addr4.sin_port = htons(info->port); |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 96 | } /* ipv4 */ |
| 97 | |
| 98 | n = bind(sockfd, v, n); |
| 99 | if (n < 0) { |
| 100 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
| 101 | info->port, n, LWS_ERRNO); |
| 102 | compatible_close(sockfd); |
| 103 | return 1; |
| 104 | } |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 105 | |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 106 | if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1) |
| 107 | lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO)); |
| 108 | else |
| 109 | info->port = ntohs(sin.sin_port); |
| 110 | |
| 111 | context->listen_port = info->port; |
| 112 | |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 113 | wsi = lws_zalloc(sizeof(struct libwebsocket)); |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 114 | if (wsi == NULL) { |
| 115 | lwsl_err("Out of mem\n"); |
| 116 | compatible_close(sockfd); |
| 117 | return 1; |
| 118 | } |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 119 | wsi->sock = sockfd; |
| 120 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
| 121 | |
| 122 | insert_wsi_socket_into_fds(context, wsi); |
| 123 | |
| 124 | context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO; |
| 125 | context->listen_service_count = 0; |
| 126 | context->listen_service_fd = sockfd; |
| 127 | |
| 128 | listen(sockfd, LWS_SOMAXCONN); |
| 129 | lwsl_notice(" Listening on port %d\n", info->port); |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 130 | |
Andy Green | e38031a | 2014-04-03 08:24:29 +0800 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 134 | int |
| 135 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
| 136 | { |
| 137 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
| 138 | |
| 139 | /* there is no pending change */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 140 | if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 141 | return 0; |
| 142 | |
| 143 | /* stuff is still buffered, not ready to really accept new input */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 144 | if (wsi->rxflow_buffer) { |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 145 | /* get ourselves called back to deal with stashed buffer */ |
| 146 | libwebsocket_callback_on_writable(context, wsi); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* pending is cleared, we can change rxflow state */ |
| 151 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 152 | wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE; |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 153 | |
| 154 | lwsl_info("rxflow: wsi %p change_to %d\n", wsi, |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 155 | wsi->rxflow_change_to & LWS_RXFLOW_ALLOW); |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 156 | |
| 157 | /* adjust the pollfd for this wsi */ |
| 158 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 159 | if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) { |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 160 | if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) { |
| 161 | lwsl_info("%s: fail\n", __func__); |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 162 | return -1; |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 163 | } |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 164 | } else |
| 165 | if (lws_change_pollfd(wsi, LWS_POLLIN, 0)) |
| 166 | return -1; |
| 167 | |
Andy Green | f004ec5 | 2014-04-12 11:47:25 +0800 | [diff] [blame] | 168 | return 0; |
Andy Green | d99476b | 2014-04-03 08:40:05 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 171 | int lws_http_action(struct libwebsocket_context *context, |
| 172 | struct libwebsocket *wsi) |
| 173 | { |
| 174 | char *uri_ptr = NULL; |
| 175 | int uri_len = 0; |
| 176 | enum http_version request_version; |
| 177 | enum http_connection_type connection_type; |
| 178 | int http_version_len; |
| 179 | char content_length_str[32]; |
| 180 | char http_version_str[10]; |
| 181 | char http_conn_str[20]; |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 182 | int n, count = 0; |
| 183 | static const unsigned char methods[] = { |
| 184 | WSI_TOKEN_GET_URI, |
| 185 | WSI_TOKEN_POST_URI, |
| 186 | WSI_TOKEN_OPTIONS_URI, |
| 187 | WSI_TOKEN_PUT_URI, |
| 188 | WSI_TOKEN_PATCH_URI, |
| 189 | WSI_TOKEN_DELETE_URI, |
| 190 | #ifdef LWS_USE_HTTP2 |
| 191 | WSI_TOKEN_HTTP_COLON_PATH, |
| 192 | #endif |
| 193 | }; |
Andy Green | 93d947c | 2015-02-20 07:37:20 +0800 | [diff] [blame^] | 194 | #ifdef _DEBUG |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 195 | static const char * const method_names[] = { |
| 196 | "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", |
| 197 | #ifdef LWS_USE_HTTP2 |
| 198 | ":path", |
| 199 | #endif |
| 200 | }; |
Andy Green | 93d947c | 2015-02-20 07:37:20 +0800 | [diff] [blame^] | 201 | #endif |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 202 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 203 | /* it's not websocket.... shall we accept it as http? */ |
| 204 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 205 | for (n = 0; n < ARRAY_SIZE(methods); n++) |
| 206 | if (lws_hdr_total_length(wsi, methods[n])) |
| 207 | count++; |
| 208 | if (!count) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 209 | lwsl_warn("Missing URI in HTTP request\n"); |
| 210 | goto bail_nuke_ah; |
| 211 | } |
| 212 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 213 | if (count != 1) { |
| 214 | lwsl_warn("multiple methods?\n"); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 215 | goto bail_nuke_ah; |
| 216 | } |
| 217 | |
| 218 | if (libwebsocket_ensure_user_space(wsi)) |
| 219 | goto bail_nuke_ah; |
| 220 | |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 221 | for (n = 0; n < ARRAY_SIZE(methods); n++) |
| 222 | if (lws_hdr_total_length(wsi, methods[n])) { |
| 223 | uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]); |
| 224 | uri_len = lws_hdr_total_length(wsi, methods[n]); |
| 225 | lwsl_info("Method: %s request for '%s'\n", |
| 226 | method_names[n], uri_ptr); |
| 227 | break; |
| 228 | } |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 229 | |
| 230 | /* HTTP header had a content length? */ |
| 231 | |
| 232 | wsi->u.http.content_length = 0; |
Quinlan Pfiffer | 49f72aa | 2015-01-10 19:01:52 -0800 | [diff] [blame] | 233 | if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) || |
| 234 | lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) || |
| 235 | lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI)) |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 236 | wsi->u.http.content_length = 100 * 1024 * 1024; |
| 237 | |
| 238 | if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) { |
| 239 | lws_hdr_copy(wsi, content_length_str, |
| 240 | sizeof(content_length_str) - 1, |
| 241 | WSI_TOKEN_HTTP_CONTENT_LENGTH); |
| 242 | wsi->u.http.content_length = atoi(content_length_str); |
| 243 | } |
| 244 | |
| 245 | /* http_version? Default to 1.0, override with token: */ |
| 246 | request_version = HTTP_VERSION_1_0; |
| 247 | |
| 248 | /* Works for single digit HTTP versions. : */ |
| 249 | http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP); |
| 250 | if (http_version_len > 7) { |
| 251 | lws_hdr_copy(wsi, http_version_str, |
| 252 | sizeof(http_version_str) - 1, WSI_TOKEN_HTTP); |
| 253 | if (http_version_str[5] == '1' && http_version_str[7] == '1') |
| 254 | request_version = HTTP_VERSION_1_1; |
| 255 | } |
| 256 | wsi->u.http.request_version = request_version; |
| 257 | |
| 258 | /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */ |
| 259 | if (request_version == HTTP_VERSION_1_1) |
| 260 | connection_type = HTTP_CONNECTION_KEEP_ALIVE; |
| 261 | else |
| 262 | connection_type = HTTP_CONNECTION_CLOSE; |
| 263 | |
| 264 | /* Override default if http "Connection:" header: */ |
| 265 | if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) { |
| 266 | lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1, |
| 267 | WSI_TOKEN_CONNECTION); |
| 268 | http_conn_str[sizeof(http_conn_str) - 1] = '\0'; |
| 269 | if (!strcasecmp(http_conn_str, "keep-alive")) |
| 270 | connection_type = HTTP_CONNECTION_KEEP_ALIVE; |
| 271 | else |
| 272 | if (strcasecmp(http_conn_str, "close")) |
| 273 | connection_type = HTTP_CONNECTION_CLOSE; |
| 274 | } |
| 275 | wsi->u.http.connection_type = connection_type; |
| 276 | |
| 277 | n = 0; |
| 278 | if (wsi->protocol->callback) |
| 279 | n = wsi->protocol->callback(context, wsi, |
| 280 | LWS_CALLBACK_FILTER_HTTP_CONNECTION, |
| 281 | wsi->user_space, uri_ptr, uri_len); |
| 282 | |
| 283 | if (!n) { |
| 284 | /* |
| 285 | * if there is content supposed to be coming, |
| 286 | * put a timeout on it having arrived |
| 287 | */ |
| 288 | libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, |
| 289 | AWAITING_TIMEOUT); |
| 290 | |
| 291 | if (wsi->protocol->callback) |
| 292 | n = wsi->protocol->callback(context, wsi, |
| 293 | LWS_CALLBACK_HTTP, |
| 294 | wsi->user_space, uri_ptr, uri_len); |
| 295 | } |
| 296 | |
| 297 | /* now drop the header info we kept a pointer to */ |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 298 | lws_free2(wsi->u.http.ah); |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 299 | |
| 300 | if (n) { |
| 301 | lwsl_info("LWS_CALLBACK_HTTP closing\n"); |
| 302 | return 1; /* struct ah ptr already nuked */ } |
| 303 | |
| 304 | /* |
| 305 | * If we're not issuing a file, check for content_length or |
| 306 | * HTTP keep-alive. No keep-alive header allocation for |
| 307 | * ISSUING_FILE, as this uses HTTP/1.0. |
| 308 | * |
| 309 | * In any case, return 0 and let libwebsocket_read decide how to |
| 310 | * proceed based on state |
| 311 | */ |
| 312 | if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) |
| 313 | /* Prepare to read body if we have a content length: */ |
| 314 | if (wsi->u.http.content_length > 0) |
| 315 | wsi->state = WSI_STATE_HTTP_BODY; |
| 316 | |
| 317 | return 0; |
| 318 | |
| 319 | bail_nuke_ah: |
| 320 | /* drop the header info */ |
Alejandro Mery | ac3ec39 | 2014-12-05 00:09:20 +0100 | [diff] [blame] | 321 | lws_free2(wsi->u.hdr.ah); |
| 322 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 323 | return 1; |
| 324 | } |
| 325 | |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 326 | |
| 327 | int lws_handshake_server(struct libwebsocket_context *context, |
| 328 | struct libwebsocket *wsi, unsigned char **buf, size_t len) |
| 329 | { |
| 330 | struct allocated_headers *ah; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 331 | int protocol_len; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 332 | char protocol_list[128]; |
| 333 | char protocol_name[32]; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 334 | char *p; |
| 335 | int n, hit; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 336 | |
| 337 | /* LWS_CONNMODE_WS_SERVING */ |
| 338 | |
| 339 | while (len--) { |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 340 | if (libwebsocket_parse(context, wsi, *(*buf)++)) { |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 341 | lwsl_info("libwebsocket_parse failed\n"); |
| 342 | goto bail_nuke_ah; |
| 343 | } |
| 344 | |
| 345 | if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE) |
| 346 | continue; |
| 347 | |
| 348 | lwsl_parser("libwebsocket_parse sees parsing complete\n"); |
| 349 | |
| 350 | wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT; |
| 351 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 352 | |
| 353 | /* is this websocket protocol or normal http 1.0? */ |
| 354 | |
| 355 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) || |
| 356 | !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 357 | |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 358 | ah = wsi->u.hdr.ah; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 359 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 360 | lws_union_transition(wsi, LWS_CONNMODE_HTTP_SERVING_ACCEPTED); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 361 | wsi->state = WSI_STATE_HTTP; |
| 362 | wsi->u.http.fd = LWS_INVALID_FILE; |
| 363 | |
| 364 | /* expose it at the same offset as u.hdr */ |
| 365 | wsi->u.http.ah = ah; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 366 | |
| 367 | n = lws_http_action(context, wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 368 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 369 | return n; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 372 | if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE), |
| 373 | "websocket")) |
| 374 | goto upgrade_ws; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 375 | #ifdef LWS_USE_HTTP2 |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 376 | if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE), |
Andy Green | b08cb50 | 2014-09-30 16:33:56 +0800 | [diff] [blame] | 377 | "h2c-14")) |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 378 | goto upgrade_h2c; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 379 | #endif |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 380 | /* dunno what he wanted to upgrade to */ |
| 381 | goto bail_nuke_ah; |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 382 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 383 | #ifdef LWS_USE_HTTP2 |
| 384 | upgrade_h2c: |
| 385 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) { |
| 386 | lwsl_err("missing http2_settings\n"); |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 387 | goto bail_nuke_ah; |
| 388 | } |
| 389 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 390 | lwsl_err("h2c upgrade...\n"); |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 391 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 392 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS); |
| 393 | /* convert the peer's HTTP-Settings */ |
| 394 | n = lws_b64_decode_string(p, protocol_list, sizeof(protocol_list)); |
| 395 | if (n < 0) { |
| 396 | lwsl_parser("HTTP2_SETTINGS too long\n"); |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | /* adopt the header info */ |
| 401 | |
| 402 | ah = wsi->u.hdr.ah; |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 403 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 404 | lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING); |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 405 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 406 | /* http2 union member has http union struct at start */ |
| 407 | wsi->u.http.ah = ah; |
| 408 | |
| 409 | lws_http2_init(&wsi->u.http2.peer_settings); |
| 410 | lws_http2_init(&wsi->u.http2.my_settings); |
| 411 | |
| 412 | /* HTTP2 union */ |
| 413 | |
| 414 | lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings, (unsigned char *)protocol_list, n); |
| 415 | |
| 416 | strcpy(protocol_list, |
| 417 | "HTTP/1.1 101 Switching Protocols\x0d\x0a" |
| 418 | "Connection: Upgrade\x0d\x0a" |
| 419 | "Upgrade: h2c\x0d\x0a\x0d\x0a"); |
Andy Green | 97ee57f | 2014-10-29 09:39:08 +0800 | [diff] [blame] | 420 | n = lws_issue_raw(wsi, (unsigned char *)protocol_list, |
| 421 | strlen(protocol_list)); |
| 422 | if (n != strlen(protocol_list)) { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 423 | lwsl_debug("http2 switch: ERROR writing to socket\n"); |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | wsi->state = WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE; |
| 428 | |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 429 | return 0; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 430 | #endif |
Andy Green | a54f232 | 2014-09-30 09:43:14 +0800 | [diff] [blame] | 431 | |
Andy Green | 2af5856 | 2014-09-30 08:15:49 +0800 | [diff] [blame] | 432 | upgrade_ws: |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 433 | if (!wsi->protocol) |
| 434 | lwsl_err("NULL protocol at libwebsocket_read\n"); |
| 435 | |
| 436 | /* |
| 437 | * It's websocket |
| 438 | * |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 439 | * Select the first protocol we support from the list |
| 440 | * the client sent us. |
| 441 | * |
| 442 | * Copy it to remove header fragmentation |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 443 | */ |
| 444 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 445 | if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1, |
| 446 | WSI_TOKEN_PROTOCOL) < 0) { |
| 447 | lwsl_err("protocol list too long"); |
| 448 | goto bail_nuke_ah; |
| 449 | } |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 450 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 451 | protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL); |
| 452 | protocol_list[protocol_len] = '\0'; |
| 453 | p = protocol_list; |
| 454 | hit = 0; |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 455 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 456 | while (*p && !hit) { |
| 457 | n = 0; |
| 458 | while (n < sizeof(protocol_name) - 1 && *p && *p !=',') |
| 459 | protocol_name[n++] = *p++; |
| 460 | protocol_name[n] = '\0'; |
| 461 | if (*p) |
| 462 | p++; |
| 463 | |
| 464 | lwsl_info("checking %s\n", protocol_name); |
| 465 | |
| 466 | n = 0; |
Andy Green | f14ea7a | 2014-11-30 12:45:39 +0800 | [diff] [blame] | 467 | while (wsi->protocol && context->protocols[n].callback) { |
Andy Green | afa10d5 | 2014-08-11 09:11:57 +0800 | [diff] [blame] | 468 | if (!wsi->protocol->name) { |
| 469 | n++; |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 470 | continue; |
Andy Green | afa10d5 | 2014-08-11 09:11:57 +0800 | [diff] [blame] | 471 | } |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 472 | if (!strcmp(context->protocols[n].name, |
| 473 | protocol_name)) { |
| 474 | lwsl_info("prot match %d\n", n); |
| 475 | wsi->protocol = &context->protocols[n]; |
| 476 | hit = 1; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | n++; |
| 481 | } |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* we didn't find a protocol he wanted? */ |
| 485 | |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 486 | if (!hit) { |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 487 | if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) == |
| 488 | NULL) { |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 489 | /* |
| 490 | * some clients only have one protocol and |
| 491 | * do not sent the protocol list header... |
| 492 | * allow it and match to protocol 0 |
| 493 | */ |
| 494 | lwsl_info("defaulting to prot 0 handler\n"); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 495 | wsi->protocol = &context->protocols[0]; |
| 496 | } else { |
Andy Green | 7a8d86e | 2014-07-19 06:52:39 +0800 | [diff] [blame] | 497 | lwsl_err("No protocol from list \"%s\" supported\n", |
| 498 | protocol_list); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 499 | goto bail_nuke_ah; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* allocate wsi->user storage */ |
| 504 | if (libwebsocket_ensure_user_space(wsi)) |
| 505 | goto bail_nuke_ah; |
| 506 | |
| 507 | /* |
| 508 | * Give the user code a chance to study the request and |
| 509 | * have the opportunity to deny it |
| 510 | */ |
| 511 | |
| 512 | if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi, |
| 513 | LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, |
| 514 | wsi->user_space, |
| 515 | lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) { |
| 516 | lwsl_warn("User code denied connection\n"); |
| 517 | goto bail_nuke_ah; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | /* |
| 522 | * Perform the handshake according to the protocol version the |
| 523 | * client announced |
| 524 | */ |
| 525 | |
| 526 | switch (wsi->ietf_spec_revision) { |
| 527 | case 13: |
| 528 | lwsl_parser("lws_parse calling handshake_04\n"); |
| 529 | if (handshake_0405(context, wsi)) { |
| 530 | lwsl_info("hs0405 has failed the connection\n"); |
| 531 | goto bail_nuke_ah; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | default: |
| 536 | lwsl_warn("Unknown client spec version %d\n", |
| 537 | wsi->ietf_spec_revision); |
| 538 | goto bail_nuke_ah; |
| 539 | } |
| 540 | |
| 541 | /* drop the header info -- no bail_nuke_ah after this */ |
Andrew Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 542 | lws_free_header_table(wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 543 | |
Andy Green | 44c1161 | 2014-11-08 11:18:47 +0800 | [diff] [blame] | 544 | lws_union_transition(wsi, LWS_CONNMODE_WS_SERVING); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * create the frame buffer for this connection according to the |
| 548 | * size mentioned in the protocol definition. If 0 there, use |
| 549 | * a big default for compatibility |
| 550 | */ |
| 551 | |
| 552 | n = wsi->protocol->rx_buffer_size; |
| 553 | if (!n) |
| 554 | n = LWS_MAX_SOCKET_IO_BUF; |
| 555 | n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 556 | wsi->u.ws.rx_user_buffer = lws_malloc(n); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 557 | if (!wsi->u.ws.rx_user_buffer) { |
| 558 | lwsl_err("Out of Mem allocating rx buffer %d\n", n); |
| 559 | return 1; |
| 560 | } |
| 561 | lwsl_info("Allocating RX buffer %d\n", n); |
| 562 | |
| 563 | if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof n)) { |
| 564 | lwsl_warn("Failed to set SNDBUF to %d", n); |
| 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | lwsl_parser("accepted v%02d connection\n", |
| 569 | wsi->ietf_spec_revision); |
| 570 | } /* while all chars are handled */ |
| 571 | |
| 572 | return 0; |
| 573 | |
| 574 | bail_nuke_ah: |
| 575 | /* drop the header info */ |
Andrew Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 576 | lws_free_header_table(wsi); |
Andy Green | aad2eac | 2014-04-03 09:03:37 +0800 | [diff] [blame] | 577 | return 1; |
| 578 | } |
| 579 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 580 | struct libwebsocket * |
| 581 | libwebsocket_create_new_server_wsi(struct libwebsocket_context *context) |
| 582 | { |
| 583 | struct libwebsocket *new_wsi; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 584 | |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 585 | new_wsi = lws_zalloc(sizeof(struct libwebsocket)); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 586 | if (new_wsi == NULL) { |
| 587 | lwsl_err("Out of memory for new connection\n"); |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 591 | new_wsi->pending_timeout = NO_PENDING_TIMEOUT; |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 592 | new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 593 | |
| 594 | /* intialize the instance struct */ |
| 595 | |
| 596 | new_wsi->state = WSI_STATE_HTTP; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 597 | new_wsi->mode = LWS_CONNMODE_HTTP_SERVING; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 598 | new_wsi->hdr_parsing_completed = 0; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 599 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 600 | if (lws_allocate_header_table(new_wsi)) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 601 | lws_free(new_wsi); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 602 | return NULL; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * these can only be set once the protocol is known |
| 607 | * we set an unestablished connection's protocol pointer |
| 608 | * to the start of the supported list, so it can look |
| 609 | * for matching ones during the handshake |
| 610 | */ |
| 611 | new_wsi->protocol = context->protocols; |
| 612 | new_wsi->user_space = NULL; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 613 | new_wsi->ietf_spec_revision = 0; |
| 614 | |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 615 | /* |
| 616 | * outermost create notification for wsi |
| 617 | * no user_space because no protocol selection |
| 618 | */ |
| 619 | context->protocols[0].callback(context, new_wsi, |
| 620 | LWS_CALLBACK_WSI_CREATE, NULL, NULL, 0); |
| 621 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 622 | return new_wsi; |
| 623 | } |
| 624 | |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 625 | /** |
| 626 | * lws_http_transaction_completed() - wait for new http transaction or close |
| 627 | * @wsi: websocket connection |
| 628 | * |
| 629 | * Returns 1 if the HTTP connection must close now |
| 630 | * Returns 0 and resets connection to wait for new HTTP header / |
| 631 | * transaction if possible |
| 632 | */ |
| 633 | |
| 634 | LWS_VISIBLE |
| 635 | int lws_http_transaction_completed(struct libwebsocket *wsi) |
| 636 | { |
| 637 | /* if we can't go back to accept new headers, drop the connection */ |
| 638 | if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) { |
| 639 | lwsl_info("%s: close connection\n", __func__); |
| 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | /* otherwise set ourselves up ready to go again */ |
| 644 | wsi->state = WSI_STATE_HTTP; |
| 645 | |
| 646 | lwsl_info("%s: await new transaction\n", __func__); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 651 | int lws_server_socket_service(struct libwebsocket_context *context, |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 652 | struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 653 | { |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 654 | struct libwebsocket *new_wsi = NULL; |
| 655 | int accept_fd = 0; |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 656 | socklen_t clilen; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 657 | struct sockaddr_in cli_addr; |
| 658 | int n; |
Patrick Gansterer | ac49f1e | 2014-03-30 10:18:51 +0200 | [diff] [blame] | 659 | int len; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 660 | |
| 661 | switch (wsi->mode) { |
| 662 | |
| 663 | case LWS_CONNMODE_HTTP_SERVING: |
Andy Green | 7cf6cb0 | 2013-05-19 14:04:10 +0800 | [diff] [blame] | 664 | case LWS_CONNMODE_HTTP_SERVING_ACCEPTED: |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 665 | case LWS_CONNMODE_HTTP2_SERVING: |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 666 | |
| 667 | /* handle http headers coming in */ |
| 668 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 669 | /* pending truncated sends have uber priority */ |
| 670 | |
Andrew Canaday | af8db35 | 2014-08-23 21:45:12 -0400 | [diff] [blame] | 671 | if (wsi->truncated_send_len) { |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 672 | if (pollfd->revents & LWS_POLLOUT) |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 673 | if (lws_issue_raw(wsi, wsi->truncated_send_malloc + |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 674 | wsi->truncated_send_offset, |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 675 | wsi->truncated_send_len) < 0) { |
| 676 | lwsl_info("closing from socket service\n"); |
| 677 | return -1; |
| 678 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 679 | /* |
| 680 | * we can't afford to allow input processing send |
| 681 | * something new, so spin around he event loop until |
| 682 | * he doesn't have any partials |
| 683 | */ |
| 684 | break; |
| 685 | } |
| 686 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 687 | /* any incoming data ready? */ |
| 688 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 689 | if (pollfd->revents & LWS_POLLIN) { |
Andy Green | 1f5c9f0 | 2014-10-09 08:14:30 +0800 | [diff] [blame] | 690 | len = lws_ssl_capable_read(context, wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 691 | context->service_buffer, |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 692 | sizeof(context->service_buffer)); |
| 693 | switch (len) { |
| 694 | case 0: |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 695 | lwsl_info("lws_server_skt_srv: read 0 len\n"); |
| 696 | /* lwsl_info(" state=%d\n", wsi->state); */ |
| 697 | if (!wsi->hdr_parsing_completed) |
Andrew Canaday | a19d485 | 2014-11-07 11:21:09 +0800 | [diff] [blame] | 698 | lws_free_header_table(wsi); |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 699 | /* fallthru */ |
| 700 | case LWS_SSL_CAPABLE_ERROR: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 701 | libwebsocket_close_and_free_session( |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 702 | context, wsi, |
| 703 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 704 | return 0; |
Andy Green | 78f266a | 2014-04-05 16:48:48 +0100 | [diff] [blame] | 705 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
Christian Schüldt | fde9379 | 2014-10-09 08:37:12 +0800 | [diff] [blame] | 706 | goto try_pollout; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 707 | } |
| 708 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 709 | /* just ignore incoming if waiting for close */ |
| 710 | if (wsi->state != WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) { |
| 711 | |
| 712 | /* hm this may want to send (via HTTP callback for example) */ |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 713 | n = libwebsocket_read(context, wsi, |
| 714 | context->service_buffer, len); |
| 715 | if (n < 0) |
| 716 | /* we closed wsi */ |
| 717 | return 0; |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 718 | |
Andy Green | a1a24d2 | 2014-04-10 14:25:24 +0800 | [diff] [blame] | 719 | /* hum he may have used up the writability above */ |
| 720 | break; |
| 721 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 722 | } |
| 723 | |
Christian Schüldt | fde9379 | 2014-10-09 08:37:12 +0800 | [diff] [blame] | 724 | try_pollout: |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 725 | /* this handles POLLOUT for http serving fragments */ |
| 726 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 727 | if (!(pollfd->revents & LWS_POLLOUT)) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | /* one shot */ |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 731 | if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) |
| 732 | goto fail; |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 733 | |
| 734 | lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_WRITE); |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 735 | |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 736 | if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) { |
| 737 | n = user_callback_handle_rxflow( |
| 738 | wsi->protocol->callback, |
| 739 | wsi->protocol->owning_server, |
| 740 | wsi, LWS_CALLBACK_HTTP_WRITEABLE, |
| 741 | wsi->user_space, |
| 742 | NULL, |
| 743 | 0); |
| 744 | if (n < 0) |
Andy Green | 28e2ab6 | 2014-11-30 13:35:24 +0800 | [diff] [blame] | 745 | goto fail; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 746 | break; |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 747 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 748 | |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 749 | /* >0 == completion, <0 == error */ |
| 750 | n = libwebsockets_serve_http_file_fragment(context, wsi); |
| 751 | if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi))) |
Andy Green | 28e2ab6 | 2014-11-30 13:35:24 +0800 | [diff] [blame] | 752 | goto fail; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 753 | break; |
| 754 | |
| 755 | case LWS_CONNMODE_SERVER_LISTENER: |
| 756 | |
| 757 | /* pollin means a client has connected to us then */ |
| 758 | |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 759 | if (!(pollfd->revents & LWS_POLLIN)) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | /* listen socket got an unencrypted connection... */ |
| 763 | |
| 764 | clilen = sizeof(cli_addr); |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 765 | lws_latency_pre(context, wsi); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 766 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 767 | &clilen); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 768 | lws_latency(context, wsi, |
| 769 | "unencrypted accept LWS_CONNMODE_SERVER_LISTENER", |
| 770 | accept_fd, accept_fd >= 0); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 771 | if (accept_fd < 0) { |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 772 | if (LWS_ERRNO == LWS_EAGAIN || LWS_ERRNO == LWS_EWOULDBLOCK) { |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 773 | lwsl_debug("accept asks to try again\n"); |
| 774 | break; |
| 775 | } |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 776 | lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO)); |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 780 | lws_plat_set_socket_options(context, accept_fd); |
Andy Green | 6f047ee | 2013-01-28 11:23:52 +0800 | [diff] [blame] | 781 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 782 | /* |
| 783 | * look at who we connected to and give user code a chance |
| 784 | * to reject based on client IP. There's no protocol selected |
| 785 | * yet so we issue this to protocols[0] |
| 786 | */ |
| 787 | |
| 788 | if ((context->protocols[0].callback)(context, wsi, |
| 789 | LWS_CALLBACK_FILTER_NETWORK_CONNECTION, |
Edwin van den Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 790 | NULL, (void *)(long)accept_fd, 0)) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 791 | lwsl_debug("Callback denied network connection\n"); |
| 792 | compatible_close(accept_fd); |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | new_wsi = libwebsocket_create_new_server_wsi(context); |
| 797 | if (new_wsi == NULL) { |
| 798 | compatible_close(accept_fd); |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | new_wsi->sock = accept_fd; |
| 803 | |
Andy Green | 176de27 | 2014-02-15 14:36:02 +0800 | [diff] [blame] | 804 | /* the transport is accepted... give him time to negotiate */ |
| 805 | libwebsocket_set_timeout(new_wsi, |
| 806 | PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, |
| 807 | AWAITING_TIMEOUT); |
| 808 | |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 809 | /* |
| 810 | * A new connection was accepted. Give the user a chance to |
| 811 | * set properties of the newly created wsi. There's no protocol |
| 812 | * selected yet so we issue this to protocols[0] |
| 813 | */ |
| 814 | |
| 815 | (context->protocols[0].callback)(context, new_wsi, |
| 816 | LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED, NULL, NULL, 0); |
| 817 | |
Andy Green | a717df2 | 2014-04-11 13:14:37 +0800 | [diff] [blame] | 818 | lws_libev_accept(context, new_wsi, accept_fd); |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 819 | |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 820 | if (!LWS_SSL_ENABLED(context)) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 821 | lwsl_debug("accepted new conn port %u on fd=%d\n", |
| 822 | ntohs(cli_addr.sin_port), accept_fd); |
| 823 | |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 824 | insert_wsi_socket_into_fds(context, new_wsi); |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 825 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 826 | break; |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 827 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 828 | default: |
| 829 | break; |
| 830 | } |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 831 | |
Andy Green | 62824f9 | 2014-08-10 09:50:42 +0800 | [diff] [blame] | 832 | if (lws_server_socket_service_ssl(context, &wsi, new_wsi, |
| 833 | accept_fd, pollfd)) |
| 834 | goto fail; |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 835 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 836 | return 0; |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 837 | |
| 838 | fail: |
| 839 | libwebsocket_close_and_free_session(context, wsi, |
| 840 | LWS_CLOSE_STATUS_NOSTATUS); |
| 841 | return 1; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 842 | } |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 843 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 844 | #include "lextable-strings.h" |
| 845 | |
| 846 | const unsigned char *lws_token_to_string(enum lws_token_indexes token) |
| 847 | { |
| 848 | if ((unsigned int)token >= ARRAY_SIZE(set)) |
| 849 | return NULL; |
| 850 | |
| 851 | return (unsigned char *)set[token]; |
| 852 | } |
| 853 | |
| 854 | int lws_add_http_header_by_name(struct libwebsocket_context *context, |
| 855 | struct libwebsocket *wsi, |
| 856 | const unsigned char *name, |
| 857 | const unsigned char *value, |
| 858 | int length, |
| 859 | unsigned char **p, |
| 860 | unsigned char *end) |
| 861 | { |
| 862 | #ifdef LWS_USE_HTTP2 |
| 863 | if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) |
| 864 | return lws_add_http2_header_by_name(context, wsi, name, value, length, p, end); |
| 865 | #endif |
| 866 | if (name) { |
| 867 | while (*p < end && *name) |
| 868 | *((*p)++) = *name++; |
| 869 | |
| 870 | if (*p == end) |
| 871 | return 1; |
| 872 | |
| 873 | *((*p)++) = ' '; |
| 874 | } |
| 875 | if (*p + length + 3 >= end) |
| 876 | return 1; |
| 877 | |
| 878 | memcpy(*p, value, length); |
| 879 | *p += length; |
| 880 | |
| 881 | *((*p)++) = '\x0d'; |
| 882 | *((*p)++) = '\x0a'; |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | int lws_finalize_http_header(struct libwebsocket_context *context, |
| 888 | struct libwebsocket *wsi, |
| 889 | unsigned char **p, |
| 890 | unsigned char *end) |
| 891 | { |
| 892 | #ifdef LWS_USE_HTTP2 |
| 893 | if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) |
| 894 | return 0; |
| 895 | #endif |
| 896 | |
| 897 | if ((long)(end - *p) < 3) |
| 898 | return 1; |
| 899 | |
| 900 | *((*p)++) = '\x0d'; |
| 901 | *((*p)++) = '\x0a'; |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | int lws_add_http_header_by_token(struct libwebsocket_context *context, |
| 907 | struct libwebsocket *wsi, |
| 908 | enum lws_token_indexes token, |
| 909 | const unsigned char *value, |
| 910 | int length, |
| 911 | unsigned char **p, |
| 912 | unsigned char *end) |
| 913 | { |
| 914 | const unsigned char *name; |
| 915 | #ifdef LWS_USE_HTTP2 |
| 916 | if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) |
| 917 | return lws_add_http2_header_by_token(context, wsi, token, value, length, p, end); |
| 918 | #endif |
| 919 | name = lws_token_to_string(token); |
| 920 | if (!name) |
| 921 | return 1; |
| 922 | |
| 923 | return lws_add_http_header_by_name(context, wsi, name, value, length, p, end); |
| 924 | } |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 925 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 926 | int lws_add_http_header_content_length(struct libwebsocket_context *context, |
| 927 | struct libwebsocket *wsi, |
| 928 | unsigned long content_length, |
| 929 | unsigned char **p, |
| 930 | unsigned char *end) |
| 931 | { |
| 932 | char b[24]; |
| 933 | int n; |
| 934 | |
| 935 | n = sprintf(b, "%lu", content_length); |
| 936 | if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH, (unsigned char *)b, n, p, end)) |
| 937 | return 1; |
| 938 | wsi->u.http.content_length = content_length; |
| 939 | wsi->u.http.content_remain = content_length; |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 944 | static const char *err400[] = { |
| 945 | "Bad Request", |
| 946 | "Unauthorized", |
| 947 | "Payment Required", |
| 948 | "Forbidden", |
| 949 | "Not Found", |
| 950 | "Method Not Allowed", |
| 951 | "Not Acceptable", |
| 952 | "Proxy Auth Required", |
| 953 | "Request Timeout", |
| 954 | "Conflict", |
| 955 | "Gone", |
| 956 | "Length Required", |
| 957 | "Precondition Failed", |
| 958 | "Request Entity Too Large", |
| 959 | "Request URI too Long", |
| 960 | "Unsupported Media Type", |
| 961 | "Requested Range Not Satisfiable", |
| 962 | "Expectation Failed" |
| 963 | }; |
| 964 | |
| 965 | static const char *err500[] = { |
| 966 | "Internal Server Error", |
| 967 | "Not Implemented", |
| 968 | "Bad Gateway", |
| 969 | "Service Unavailable", |
| 970 | "Gateway Timeout", |
| 971 | "HTTP Version Not Supported" |
| 972 | }; |
| 973 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 974 | int lws_add_http_header_status(struct libwebsocket_context *context, |
| 975 | struct libwebsocket *wsi, |
| 976 | unsigned int code, |
| 977 | unsigned char **p, |
| 978 | unsigned char *end) |
| 979 | { |
| 980 | unsigned char code_and_desc[60]; |
| 981 | const char *description = ""; |
| 982 | int n; |
| 983 | |
| 984 | #ifdef LWS_USE_HTTP2 |
| 985 | if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) |
| 986 | return lws_add_http2_header_status(context, wsi, code, p, end); |
| 987 | #endif |
| 988 | if (code >= 400 && code < (400 + ARRAY_SIZE(err400))) |
| 989 | description = err400[code - 400]; |
| 990 | if (code >= 500 && code < (500 + ARRAY_SIZE(err500))) |
| 991 | description = err500[code - 500]; |
| 992 | |
| 993 | n = sprintf((char *)code_and_desc, "HTTP/1.0 %u %s", code, description); |
| 994 | |
| 995 | return lws_add_http_header_by_name(context, wsi, NULL, code_and_desc, n, p, end); |
| 996 | } |
| 997 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 998 | /** |
| 999 | * libwebsockets_return_http_status() - Return simple http status |
| 1000 | * @context: libwebsockets context |
| 1001 | * @wsi: Websocket instance (available from user callback) |
| 1002 | * @code: Status index, eg, 404 |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1003 | * @html_body: User-readable HTML description < 1KB, or NULL |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1004 | * |
| 1005 | * Helper to report HTTP errors back to the client cleanly and |
| 1006 | * consistently |
| 1007 | */ |
Andy Green | 83725d8 | 2014-02-27 07:19:21 +0800 | [diff] [blame] | 1008 | LWS_VISIBLE int libwebsockets_return_http_status( |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1009 | struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 1010 | unsigned int code, const char *html_body) |
| 1011 | { |
| 1012 | int n, m; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1013 | |
| 1014 | unsigned char *p = context->service_buffer + LWS_SEND_BUFFER_PRE_PADDING; |
| 1015 | unsigned char *start = p; |
| 1016 | unsigned char *end = p + sizeof(context->service_buffer) - |
| 1017 | LWS_SEND_BUFFER_PRE_PADDING; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1018 | |
| 1019 | if (!html_body) |
| 1020 | html_body = ""; |
| 1021 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1022 | if (lws_add_http_header_status(context, wsi, code, &p, end)) |
| 1023 | return 1; |
| 1024 | if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER, (unsigned char *)"libwebsockets", 13, &p, end)) |
| 1025 | return 1; |
| 1026 | if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (unsigned char *)"text/html", 9, &p, end)) |
| 1027 | return 1; |
| 1028 | if (lws_finalize_http_header(context, wsi, &p, end)) |
| 1029 | return 1; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1030 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1031 | m = libwebsocket_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS); |
Andy Green | 3adeca8 | 2014-11-14 07:21:02 +0800 | [diff] [blame] | 1032 | if (m != (int)(p - start)) |
| 1033 | return 1; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1034 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1035 | n = sprintf((char *)start, "<html><body><h1>%u</h1>%s</body></html>", code, html_body); |
| 1036 | m = libwebsocket_write(wsi, start, n, LWS_WRITE_HTTP); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1037 | |
Andy Green | 3adeca8 | 2014-11-14 07:21:02 +0800 | [diff] [blame] | 1038 | return m != n; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * libwebsockets_serve_http_file() - Send a file back to the client using http |
| 1043 | * @context: libwebsockets context |
| 1044 | * @wsi: Websocket instance (available from user callback) |
| 1045 | * @file: The file to issue over http |
| 1046 | * @content_type: The http content type, eg, text/html |
| 1047 | * @other_headers: NULL or pointer to \0-terminated other header string |
| 1048 | * |
| 1049 | * This function is intended to be called from the callback in response |
| 1050 | * to http requests from the client. It allows the callback to issue |
| 1051 | * local files down the http link in a single step. |
| 1052 | * |
| 1053 | * Returning <0 indicates error and the wsi should be closed. Returning |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 1054 | * >0 indicates the file was completely sent and |
| 1055 | * lws_http_transaction_completed() called on the wsi (and close if != 0) |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1056 | * ==0 indicates the file transfer is started and needs more service later, |
| 1057 | * the wsi should be left alone. |
| 1058 | */ |
| 1059 | |
| 1060 | LWS_VISIBLE int libwebsockets_serve_http_file( |
| 1061 | struct libwebsocket_context *context, |
| 1062 | struct libwebsocket *wsi, const char *file, |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1063 | const char *content_type, const char *other_headers, |
| 1064 | int other_headers_len) |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1065 | { |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 1066 | unsigned char *response = context->service_buffer + LWS_SEND_BUFFER_PRE_PADDING; |
| 1067 | unsigned char *p = response; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1068 | unsigned char *end = p + sizeof(context->service_buffer) - |
| 1069 | LWS_SEND_BUFFER_PRE_PADDING; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1070 | int ret = 0; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1071 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 1072 | 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] | 1073 | |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 1074 | if (wsi->u.http.fd == LWS_INVALID_FILE) { |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1075 | lwsl_err("Unable to open '%s'\n", file); |
| 1076 | libwebsockets_return_http_status(context, wsi, |
| 1077 | HTTP_STATUS_NOT_FOUND, NULL); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1078 | return -1; |
| 1079 | } |
| 1080 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1081 | if (lws_add_http_header_status(context, wsi, 200, &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 1082 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1083 | 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] | 1084 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1085 | 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] | 1086 | return -1; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 1087 | 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] | 1088 | return -1; |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1089 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1090 | if (other_headers) { |
| 1091 | if ((end - p) < other_headers_len) |
| 1092 | return -1; |
| 1093 | memcpy(p, other_headers, other_headers_len); |
| 1094 | p += other_headers_len; |
| 1095 | } |
| 1096 | |
| 1097 | if (lws_finalize_http_header(context, wsi, &p, end)) |
Andy Green | 91b0589 | 2014-10-17 08:38:44 +0800 | [diff] [blame] | 1098 | return -1; |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 1099 | |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 1100 | ret = libwebsocket_write(wsi, response, |
| 1101 | p - response, LWS_WRITE_HTTP_HEADERS); |
| 1102 | if (ret != (p - response)) { |
| 1103 | lwsl_err("_write returned %d from %d\n", ret, (p - response)); |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1104 | return -1; |
| 1105 | } |
| 1106 | |
| 1107 | wsi->u.http.filepos = 0; |
| 1108 | wsi->state = WSI_STATE_HTTP_ISSUING_FILE; |
| 1109 | |
| 1110 | return libwebsockets_serve_http_file_fragment(context, wsi); |
| 1111 | } |
| 1112 | |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 1113 | |
| 1114 | int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 1115 | unsigned char *buf, size_t len) |
| 1116 | { |
| 1117 | size_t n = 0; |
| 1118 | int m; |
| 1119 | |
| 1120 | #if 0 |
| 1121 | lwsl_parser("received %d byte packet\n", (int)len); |
| 1122 | lwsl_hexdump(buf, len); |
| 1123 | #endif |
| 1124 | |
| 1125 | /* let the rx protocol state machine have as much as it needs */ |
| 1126 | |
| 1127 | while (n < len) { |
| 1128 | /* |
| 1129 | * we were accepting input but now we stopped doing so |
| 1130 | */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 1131 | if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) { |
| 1132 | lws_rxflow_cache(wsi, buf, n, len); |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 1133 | |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
| 1137 | /* account for what we're using in rxflow buffer */ |
Andy Green | 024eb6c | 2014-10-08 12:00:53 +0800 | [diff] [blame] | 1138 | if (wsi->rxflow_buffer) |
| 1139 | wsi->rxflow_pos++; |
Andy Green | d7340c1 | 2014-04-10 14:08:10 +0800 | [diff] [blame] | 1140 | |
| 1141 | /* process the byte */ |
| 1142 | m = libwebsocket_rx_sm(wsi, buf[n++]); |
| 1143 | if (m < 0) |
| 1144 | return -1; |
| 1145 | } |
| 1146 | |
| 1147 | return 0; |
Andy Green | cdb9bf9 | 2014-04-12 10:07:02 +0800 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | LWS_VISIBLE void |
| 1151 | lws_server_get_canonical_hostname(struct libwebsocket_context *context, |
| 1152 | struct lws_context_creation_info *info) |
| 1153 | { |
| 1154 | if (info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME) |
| 1155 | return; |
| 1156 | |
| 1157 | /* find canonical hostname */ |
| 1158 | gethostname((char *)context->canonical_hostname, |
| 1159 | sizeof(context->canonical_hostname) - 1); |
| 1160 | |
| 1161 | lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname); |
vpeter4 | 4dd8ada | 2014-04-27 13:28:22 +0200 | [diff] [blame] | 1162 | } |