2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
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.
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.
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,
23 #include "private-libwebsockets.h"
25 int lws_context_init_server(struct lws_context_creation_info *info,
26 struct lws_context *context)
29 struct sockaddr_in6 serv_addr6;
32 struct sockaddr_in serv_addr4;
33 socklen_t len = sizeof(struct sockaddr);
34 struct sockaddr_in sin;
38 lws_sockfd_type sockfd;
41 /* set up our external listening socket we serve on */
43 if (info->port == CONTEXT_PORT_NO_LISTEN)
48 if (LWS_IPV6_ENABLED(context))
49 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
52 sockfd = socket(AF_INET, SOCK_STREAM, 0);
56 sockfd = mbed3_create_tcp_stream_socket();
57 if (!lws_sockfd_valid(sockfd)) {
59 lwsl_err("ERROR opening socket\n");
65 * allow us to restart even if old sockets in TIME_WAIT
67 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
68 (const void *)&opt, sizeof(opt)) < 0) {
69 compatible_close(sockfd);
73 lws_plat_set_socket_options(context, sockfd);
77 if (LWS_IPV6_ENABLED(context)) {
78 v = (struct sockaddr *)&serv_addr6;
79 n = sizeof(struct sockaddr_in6);
80 bzero((char *) &serv_addr6, sizeof(serv_addr6));
81 serv_addr6.sin6_addr = in6addr_any;
82 serv_addr6.sin6_family = AF_INET6;
83 serv_addr6.sin6_port = htons(info->port);
87 v = (struct sockaddr *)&serv_addr4;
88 n = sizeof(serv_addr4);
89 bzero((char *) &serv_addr4, sizeof(serv_addr4));
90 serv_addr4.sin_addr.s_addr = INADDR_ANY;
91 serv_addr4.sin_family = AF_INET;
94 if (interface_to_sa(context, info->iface,
95 (struct sockaddr_in *)v, n) < 0) {
96 lwsl_err("Unable to find interface %s\n",
98 compatible_close(sockfd);
103 serv_addr4.sin_port = htons(info->port);
106 n = bind(sockfd, v, n);
108 lwsl_err("ERROR on binding to port %d (%d %d)\n",
109 info->port, n, LWS_ERRNO);
110 compatible_close(sockfd);
114 if (getsockname(sockfd, (struct sockaddr *)&sin, &len) == -1)
115 lwsl_warn("getsockname: %s\n", strerror(LWS_ERRNO));
117 info->port = ntohs(sin.sin_port);
120 context->listen_port = info->port;
122 wsi = lws_zalloc(sizeof(struct lws));
124 lwsl_err("Out of mem\n");
125 compatible_close(sockfd);
128 wsi->context = context;
130 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
131 wsi->protocol = context->protocols;
133 if (insert_wsi_socket_into_fds(context, wsi)) {
134 compatible_close(sockfd);
138 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
139 context->listen_service_count = 0;
140 context->listen_service_fd = sockfd;
143 listen(sockfd, LWS_SOMAXCONN);
145 mbed3_tcp_stream_bind(sockfd, info->port, wsi);
147 lwsl_notice(" Listening on port %d\n", info->port);
153 _lws_rx_flow_control(struct lws *wsi)
155 /* there is no pending change */
156 if (!(wsi->rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE))
159 /* stuff is still buffered, not ready to really accept new input */
160 if (wsi->rxflow_buffer) {
161 /* get ourselves called back to deal with stashed buffer */
162 lws_callback_on_writable(wsi);
166 /* pending is cleared, we can change rxflow state */
168 wsi->rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE;
170 lwsl_info("rxflow: wsi %p change_to %d\n", wsi,
171 wsi->rxflow_change_to & LWS_RXFLOW_ALLOW);
173 /* adjust the pollfd for this wsi */
175 if (wsi->rxflow_change_to & LWS_RXFLOW_ALLOW) {
176 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
177 lwsl_info("%s: fail\n", __func__);
181 if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
187 int lws_http_action(struct lws *wsi)
189 struct lws_context *context = wsi->context;
190 enum http_connection_type connection_type;
191 enum http_version request_version;
192 char content_length_str[32];
193 unsigned int n, count = 0;
194 char http_version_str[10];
195 char http_conn_str[20];
196 int http_version_len;
197 char *uri_ptr = NULL;
200 static const unsigned char methods[] = {
203 WSI_TOKEN_OPTIONS_URI,
206 WSI_TOKEN_DELETE_URI,
208 WSI_TOKEN_HTTP_COLON_PATH,
212 static const char * const method_names[] = {
213 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
220 /* it's not websocket.... shall we accept it as http? */
222 for (n = 0; n < ARRAY_SIZE(methods); n++)
223 if (lws_hdr_total_length(wsi, methods[n]))
226 lwsl_warn("Missing URI in HTTP request\n");
231 lwsl_warn("multiple methods?\n");
235 if (lws_ensure_user_space(wsi))
238 for (n = 0; n < ARRAY_SIZE(methods); n++)
239 if (lws_hdr_total_length(wsi, methods[n])) {
240 uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
241 uri_len = lws_hdr_total_length(wsi, methods[n]);
242 lwsl_info("Method: %s request for '%s'\n",
243 method_names[n], uri_ptr);
247 /* HTTP header had a content length? */
249 wsi->u.http.content_length = 0;
250 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
251 lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
252 lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
253 wsi->u.http.content_length = 100 * 1024 * 1024;
255 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
256 lws_hdr_copy(wsi, content_length_str,
257 sizeof(content_length_str) - 1,
258 WSI_TOKEN_HTTP_CONTENT_LENGTH);
259 wsi->u.http.content_length = atoi(content_length_str);
262 /* http_version? Default to 1.0, override with token: */
263 request_version = HTTP_VERSION_1_0;
265 /* Works for single digit HTTP versions. : */
266 http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
267 if (http_version_len > 7) {
268 lws_hdr_copy(wsi, http_version_str,
269 sizeof(http_version_str) - 1, WSI_TOKEN_HTTP);
270 if (http_version_str[5] == '1' && http_version_str[7] == '1')
271 request_version = HTTP_VERSION_1_1;
273 wsi->u.http.request_version = request_version;
275 /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
276 if (request_version == HTTP_VERSION_1_1)
277 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
279 connection_type = HTTP_CONNECTION_CLOSE;
281 /* Override default if http "Connection:" header: */
282 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
283 lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
284 WSI_TOKEN_CONNECTION);
285 http_conn_str[sizeof(http_conn_str) - 1] = '\0';
286 if (!strcasecmp(http_conn_str, "keep-alive"))
287 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
289 if (!strcasecmp(http_conn_str, "close"))
290 connection_type = HTTP_CONNECTION_CLOSE;
292 wsi->u.http.connection_type = connection_type;
294 n = wsi->protocol->callback(context, wsi,
295 LWS_CALLBACK_FILTER_HTTP_CONNECTION,
296 wsi->user_space, uri_ptr, uri_len);
300 * if there is content supposed to be coming,
301 * put a timeout on it having arrived
303 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
306 n = wsi->protocol->callback(context, wsi, LWS_CALLBACK_HTTP,
307 wsi->user_space, uri_ptr, uri_len);
310 /* now drop the header info we kept a pointer to */
311 lws_free2(wsi->u.http.ah);
314 lwsl_info("LWS_CALLBACK_HTTP closing\n");
315 return 1; /* struct ah ptr already nuked */ }
318 * If we're not issuing a file, check for content_length or
319 * HTTP keep-alive. No keep-alive header allocation for
320 * ISSUING_FILE, as this uses HTTP/1.0.
322 * In any case, return 0 and let lws_read decide how to
323 * proceed based on state
325 if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE)
326 /* Prepare to read body if we have a content length: */
327 if (wsi->u.http.content_length > 0)
328 wsi->state = WSI_STATE_HTTP_BODY;
333 /* drop the header info */
334 lws_free2(wsi->u.hdr.ah);
340 int lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
342 struct lws_context *context = lws_get_ctx(wsi);
343 struct allocated_headers *ah;
344 int protocol_len, n, hit;
345 char protocol_list[128];
346 char protocol_name[32];
349 /* LWS_CONNMODE_WS_SERVING */
352 if (lws_parse(wsi, *(*buf)++)) {
353 lwsl_info("lws_parse failed\n");
357 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
360 lwsl_parser("lws_parse sees parsing complete\n");
362 wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT;
363 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
365 /* is this websocket protocol or normal http 1.0? */
367 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
368 !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
372 lws_union_transition(wsi, LWS_CONNMODE_HTTP_SERVING_ACCEPTED);
373 wsi->state = WSI_STATE_HTTP;
374 wsi->u.http.fd = LWS_INVALID_FILE;
376 /* expose it at the same offset as u.hdr */
379 n = lws_http_action(wsi);
384 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
388 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
392 /* dunno what he wanted to upgrade to */
397 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
398 lwsl_err("missing http2_settings\n");
402 lwsl_err("h2c upgrade...\n");
404 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
405 /* convert the peer's HTTP-Settings */
406 n = lws_b64_decode_string(p, protocol_list,
407 sizeof(protocol_list));
409 lwsl_parser("HTTP2_SETTINGS too long\n");
413 /* adopt the header info */
417 lws_union_transition(wsi, LWS_CONNMODE_HTTP2_SERVING);
419 /* http2 union member has http union struct at start */
422 lws_http2_init(&wsi->u.http2.peer_settings);
423 lws_http2_init(&wsi->u.http2.my_settings);
427 lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings,
428 (unsigned char *)protocol_list, n);
430 strcpy(protocol_list,
431 "HTTP/1.1 101 Switching Protocols\x0d\x0a"
432 "Connection: Upgrade\x0d\x0a"
433 "Upgrade: h2c\x0d\x0a\x0d\x0a");
434 n = lws_issue_raw(wsi, (unsigned char *)protocol_list,
435 strlen(protocol_list));
436 if (n != strlen(protocol_list)) {
437 lwsl_debug("http2 switch: ERROR writing to socket\n");
441 wsi->state = WSI_STATE_HTTP2_AWAIT_CLIENT_PREFACE;
448 lwsl_err("NULL protocol at lws_read\n");
453 * Select the first protocol we support from the list
454 * the client sent us.
456 * Copy it to remove header fragmentation
459 if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
460 WSI_TOKEN_PROTOCOL) < 0) {
461 lwsl_err("protocol list too long");
465 protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
466 protocol_list[protocol_len] = '\0';
472 while (n < sizeof(protocol_name) - 1 && *p && *p !=',')
473 protocol_name[n++] = *p++;
474 protocol_name[n] = '\0';
478 lwsl_info("checking %s\n", protocol_name);
481 while (wsi->protocol && context->protocols[n].callback) {
482 if (!wsi->protocol->name) {
486 if (!strcmp(context->protocols[n].name,
488 lwsl_info("prot match %d\n", n);
489 wsi->protocol = &context->protocols[n];
498 /* we didn't find a protocol he wanted? */
501 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
504 * some clients only have one protocol and
505 * do not sent the protocol list header...
506 * allow it and match to protocol 0
508 lwsl_info("defaulting to prot 0 handler\n");
509 wsi->protocol = &context->protocols[0];
511 lwsl_err("No protocol from list \"%s\" supported\n",
517 /* allocate wsi->user storage */
518 if (lws_ensure_user_space(wsi))
522 * Give the user code a chance to study the request and
523 * have the opportunity to deny it
526 if ((wsi->protocol->callback)(lws_get_ctx(wsi), wsi,
527 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
529 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
530 lwsl_warn("User code denied connection\n");
536 * Perform the handshake according to the protocol version the
540 switch (wsi->ietf_spec_revision) {
542 lwsl_parser("lws_parse calling handshake_04\n");
543 if (handshake_0405(context, wsi)) {
544 lwsl_info("hs0405 has failed the connection\n");
550 lwsl_warn("Unknown client spec version %d\n",
551 wsi->ietf_spec_revision);
555 /* drop the header info -- no bail_nuke_ah after this */
556 lws_free_header_table(wsi);
558 lws_union_transition(wsi, LWS_CONNMODE_WS_SERVING);
561 * create the frame buffer for this connection according to the
562 * size mentioned in the protocol definition. If 0 there, use
563 * a big default for compatibility
566 n = wsi->protocol->rx_buffer_size;
568 n = LWS_MAX_SOCKET_IO_BUF;
569 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
570 wsi->u.ws.rx_user_buffer = lws_malloc(n);
571 if (!wsi->u.ws.rx_user_buffer) {
572 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
575 lwsl_info("Allocating RX buffer %d\n", n);
577 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF,
578 (const char *)&n, sizeof n)) {
579 lwsl_warn("Failed to set SNDBUF to %d", n);
583 lwsl_parser("accepted v%02d connection\n",
584 wsi->ietf_spec_revision);
585 } /* while all chars are handled */
590 /* drop the header info */
591 lws_free_header_table(wsi);
596 lws_create_new_server_wsi(struct lws_context *context)
600 new_wsi = lws_zalloc(sizeof(struct lws));
601 if (new_wsi == NULL) {
602 lwsl_err("Out of memory for new connection\n");
606 new_wsi->context = context;
607 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
608 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
610 /* intialize the instance struct */
612 new_wsi->state = WSI_STATE_HTTP;
613 new_wsi->mode = LWS_CONNMODE_HTTP_SERVING;
614 new_wsi->hdr_parsing_completed = 0;
616 #ifdef LWS_OPENSSL_SUPPORT
617 new_wsi->use_ssl = LWS_SSL_ENABLED(context);
620 if (lws_allocate_header_table(new_wsi)) {
626 * these can only be set once the protocol is known
627 * we set an unestablished connection's protocol pointer
628 * to the start of the supported list, so it can look
629 * for matching ones during the handshake
631 new_wsi->protocol = context->protocols;
632 new_wsi->user_space = NULL;
633 new_wsi->ietf_spec_revision = 0;
634 new_wsi->sock = LWS_SOCK_INVALID;
637 * outermost create notification for wsi
638 * no user_space because no protocol selection
640 context->protocols[0].callback(context, new_wsi,
641 LWS_CALLBACK_WSI_CREATE, NULL, NULL, 0);
647 * lws_http_transaction_completed() - wait for new http transaction or close
648 * @wsi: websocket connection
650 * Returns 1 if the HTTP connection must close now
651 * Returns 0 and resets connection to wait for new HTTP header /
652 * transaction if possible
656 int lws_http_transaction_completed(struct lws *wsi)
658 /* if we can't go back to accept new headers, drop the connection */
659 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
660 lwsl_info("%s: close connection\n", __func__);
664 /* otherwise set ourselves up ready to go again */
665 wsi->state = WSI_STATE_HTTP;
666 wsi->mode = LWS_CONNMODE_HTTP_SERVING;
667 wsi->u.http.content_length = 0;
669 /* He asked for it to stay alive indefinitely */
670 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
672 if (lws_allocate_header_table(wsi))
675 /* If we're (re)starting on headers, need other implied init */
676 wsi->u.hdr.ues = URIES_IDLE;
678 lwsl_info("%s: keep-alive await new transaction\n", __func__);
684 int lws_server_socket_service(struct lws_context *context,
685 struct lws *wsi, struct lws_pollfd *pollfd)
687 lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
689 struct sockaddr_in cli_addr;
692 struct lws *new_wsi = NULL;
697 case LWS_CONNMODE_HTTP_SERVING:
698 case LWS_CONNMODE_HTTP_SERVING_ACCEPTED:
699 case LWS_CONNMODE_HTTP2_SERVING:
701 /* handle http headers coming in */
703 /* pending truncated sends have uber priority */
705 if (wsi->truncated_send_len) {
706 if (pollfd->revents & LWS_POLLOUT)
707 if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
708 wsi->truncated_send_offset,
709 wsi->truncated_send_len) < 0) {
713 * we can't afford to allow input processing send
714 * something new, so spin around he event loop until
715 * he doesn't have any partials
720 /* any incoming data ready? */
722 if (pollfd->revents & LWS_POLLIN) {
723 len = lws_ssl_capable_read(wsi, context->service_buffer,
724 sizeof(context->service_buffer));
725 lwsl_debug("%s: read %d\r\n", __func__, len);
728 lwsl_info("lws_server_skt_srv: read 0 len\n");
729 /* lwsl_info(" state=%d\n", wsi->state); */
730 if (!wsi->hdr_parsing_completed)
731 lws_free_header_table(wsi);
733 case LWS_SSL_CAPABLE_ERROR:
735 case LWS_SSL_CAPABLE_MORE_SERVICE:
739 /* just ignore incoming if waiting for close */
740 if (wsi->state != WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
743 * hm this may want to send
744 * (via HTTP callback for example)
746 n = lws_read(wsi, context->service_buffer, len);
747 if (n < 0) /* we closed wsi */
750 /* hum he may have used up the
751 * writability above */
757 /* this handles POLLOUT for http serving fragments */
759 if (!(pollfd->revents & LWS_POLLOUT))
763 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
766 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
768 if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) {
769 n = user_callback_handle_rxflow(
770 wsi->protocol->callback,
772 wsi, LWS_CALLBACK_HTTP_WRITEABLE,
781 /* >0 == completion, <0 == error */
782 n = lws_serve_http_file_fragment(wsi);
783 if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi)))
787 case LWS_CONNMODE_SERVER_LISTENER:
790 /* pollin means a client has connected to us then */
792 if (!(pollfd->revents & LWS_POLLIN))
795 /* listen socket got an unencrypted connection... */
797 clilen = sizeof(cli_addr);
798 lws_latency_pre(context, wsi);
799 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
801 lws_latency(context, wsi,
802 "unencrypted accept LWS_CONNMODE_SERVER_LISTENER",
803 accept_fd, accept_fd >= 0);
805 if (LWS_ERRNO == LWS_EAGAIN ||
806 LWS_ERRNO == LWS_EWOULDBLOCK) {
807 lwsl_debug("accept asks to try again\n");
810 lwsl_warn("ERROR on accept: %s\n", strerror(LWS_ERRNO));
814 lws_plat_set_socket_options(context, accept_fd);
816 /* not very beautiful... */
817 accept_fd = (lws_sockfd_type)pollfd;
820 * look at who we connected to and give user code a chance
821 * to reject based on client IP. There's no protocol selected
822 * yet so we issue this to protocols[0]
825 if ((context->protocols[0].callback)(context, wsi,
826 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
827 NULL, (void *)(long)accept_fd, 0)) {
828 lwsl_debug("Callback denied network connection\n");
829 compatible_close(accept_fd);
833 new_wsi = lws_create_new_server_wsi(context);
834 if (new_wsi == NULL) {
835 compatible_close(accept_fd);
839 new_wsi->sock = accept_fd;
841 /* the transport is accepted... give him time to negotiate */
842 lws_set_timeout(new_wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
846 mbed3_tcp_stream_accept(accept_fd, new_wsi);
850 * A new connection was accepted. Give the user a chance to
851 * set properties of the newly created wsi. There's no protocol
852 * selected yet so we issue this to protocols[0]
854 (context->protocols[0].callback)(context, new_wsi,
855 LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
858 lws_libev_accept(new_wsi, accept_fd);
860 if (!LWS_SSL_ENABLED(context)) {
862 lwsl_debug("accepted new conn port %u on fd=%d\n",
863 ntohs(cli_addr.sin_port), accept_fd);
865 if (insert_wsi_socket_into_fds(context, new_wsi))
874 if (!lws_server_socket_service_ssl(&wsi, new_wsi, accept_fd, pollfd))
878 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
884 * lws_serve_http_file() - Send a file back to the client using http
885 * @context: libwebsockets context
886 * @wsi: Websocket instance (available from user callback)
887 * @file: The file to issue over http
888 * @content_type: The http content type, eg, text/html
889 * @other_headers: NULL or pointer to \0-terminated other header string
891 * This function is intended to be called from the callback in response
892 * to http requests from the client. It allows the callback to issue
893 * local files down the http link in a single step.
895 * Returning <0 indicates error and the wsi should be closed. Returning
896 * >0 indicates the file was completely sent and
897 * lws_http_transaction_completed() called on the wsi (and close if != 0)
898 * ==0 indicates the file transfer is started and needs more service later,
899 * the wsi should be left alone.
902 LWS_VISIBLE int lws_serve_http_file(struct lws *wsi, const char *file,
903 const char *content_type,
904 const char *other_headers,
905 int other_headers_len)
907 struct lws_context *context = lws_get_ctx(wsi);
908 unsigned char *response = context->service_buffer +
909 LWS_SEND_BUFFER_PRE_PADDING;
910 unsigned char *p = response;
911 unsigned char *end = p + sizeof(context->service_buffer) -
912 LWS_SEND_BUFFER_PRE_PADDING;
915 wsi->u.http.fd = lws_plat_file_open(wsi, file, &wsi->u.http.filelen, O_RDONLY);
917 if (wsi->u.http.fd == LWS_INVALID_FILE) {
918 lwsl_err("Unable to open '%s'\n", file);
919 lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
924 if (lws_add_http_header_status(wsi, 200, &p, end))
926 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
927 (unsigned char *)"libwebsockets", 13,
930 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
931 (unsigned char *)content_type,
932 strlen(content_type), &p, end))
934 if (lws_add_http_header_content_length(wsi, wsi->u.http.filelen, &p, end))
938 if ((end - p) < other_headers_len)
940 memcpy(p, other_headers, other_headers_len);
941 p += other_headers_len;
944 if (lws_finalize_http_header(wsi, &p, end))
947 ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
948 if (ret != (p - response)) {
949 lwsl_err("_write returned %d from %d\n", ret, (p - response));
953 wsi->u.http.filepos = 0;
954 wsi->state = WSI_STATE_HTTP_ISSUING_FILE;
956 return lws_serve_http_file_fragment(wsi);
959 int lws_interpret_incoming_packet(struct lws *wsi, unsigned char *buf,
966 lwsl_parser("received %d byte packet\n", (int)len);
967 lwsl_hexdump(buf, len);
970 /* let the rx protocol state machine have as much as it needs */
974 * we were accepting input but now we stopped doing so
976 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
977 lws_rxflow_cache(wsi, buf, n, len);
982 /* account for what we're using in rxflow buffer */
983 if (wsi->rxflow_buffer)
986 /* process the byte */
987 m = lws_rx_sm(wsi, buf[n++]);
996 lws_server_get_canonical_hostname(struct lws_context *context,
997 struct lws_context_creation_info *info)
999 if (info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)
1002 /* find canonical hostname */
1003 gethostname((char *)context->canonical_hostname,
1004 sizeof(context->canonical_hostname) - 1);
1006 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);