2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2016 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 #if defined (LWS_WITH_ESP8266)
27 void *memcpy(void *dest, const void *src, size_t n)
29 return ets_memcpy(dest, src, n);
34 lws_context_init_server(struct lws_context_creation_info *info,
35 struct lws_vhost *vhost)
38 int n, opt = 1, limit = 1;
40 lws_sockfd_type sockfd;
46 /* set up our external listening socket we serve on */
48 if (info->port == CONTEXT_PORT_NO_LISTEN || info->port == CONTEXT_PORT_NO_LISTEN_SERVER)
51 vh = vhost->context->vhost_list;
53 if (vh->listen_port == info->port) {
54 if ((!info->iface && !vh->iface) ||
55 (info->iface && vh->iface &&
56 !strcmp(info->iface, vh->iface))) {
57 vhost->listen_port = info->port;
58 vhost->iface = info->iface;
59 lwsl_notice(" using listen skt from vhost %s\n",
69 #if defined(__linux__)
70 limit = vhost->context->count_threads;
73 for (m = 0; m < limit; m++) {
74 #ifdef LWS_USE_UNIX_SOCK
75 if (LWS_UNIX_SOCK_ENABLED(vhost))
76 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
80 if (LWS_IPV6_ENABLED(vhost))
81 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
84 sockfd = socket(AF_INET, SOCK_STREAM, 0);
88 #if defined(LWS_WITH_ESP8266)
89 sockfd = esp8266_create_tcp_listen_socket(vhost);
90 if (!lws_sockfd_valid(sockfd)) {
93 lwsl_err("ERROR opening socket\n");
96 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
98 #if (defined(WIN32) || defined(_WIN32)) && defined(SO_EXCLUSIVEADDRUSE)
100 * only accept that we are the only listener on the port
101 * https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms740621(v=vs.85).aspx
103 * for lws, to match Linux, we default to exclusive listen
105 if (!lws_check_opt(vhost->options, LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE)) {
106 if (setsockopt(sockfd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
107 (const void *)&opt, sizeof(opt)) < 0) {
108 lwsl_err("reuseaddr failed\n");
109 compatible_close(sockfd);
116 * allow us to restart even if old sockets in TIME_WAIT
118 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
119 (const void *)&opt, sizeof(opt)) < 0) {
120 lwsl_err("reuseaddr failed\n");
121 compatible_close(sockfd);
125 #if defined(LWS_USE_IPV6) && defined(IPV6_V6ONLY)
126 if (LWS_IPV6_ENABLED(vhost)) {
127 if (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
128 int value = (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
129 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
130 (const void*)&value, sizeof(value)) < 0) {
131 compatible_close(sockfd);
138 #if defined(__linux__) && defined(SO_REUSEPORT)
139 n = lws_check_opt(vhost->options, LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE);
145 if (vhost->context->count_threads > 1)
146 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
147 (const void *)&opt, sizeof(opt)) < 0) {
148 compatible_close(sockfd);
153 lws_plat_set_socket_options(vhost, sockfd);
156 n = lws_socket_bind(vhost, sockfd, info->port, info->iface);
161 vhost->listen_port = info->port;
162 vhost->iface = info->iface;
164 wsi = lws_zalloc(sizeof(struct lws));
166 lwsl_err("Out of mem\n");
169 wsi->context = vhost->context;
170 wsi->desc.sockfd = sockfd;
171 wsi->mode = LWSCM_SERVER_LISTENER;
172 wsi->protocol = vhost->protocols;
178 if (LWS_LIBUV_ENABLED(vhost->context))
179 lws_uv_initvhost(vhost, wsi);
182 if (insert_wsi_socket_into_fds(vhost->context, wsi))
185 vhost->context->count_wsi_allocated++;
186 vhost->lserv_wsi = wsi;
189 n = listen(wsi->desc.sockfd, LWS_SOMAXCONN);
191 lwsl_err("listen failed with error %d\n", LWS_ERRNO);
192 vhost->lserv_wsi = NULL;
193 vhost->context->count_wsi_allocated--;
194 remove_wsi_socket_from_fds(wsi);
197 } /* for each thread able to independently listen */
199 #if defined(LWS_WITH_ESP8266)
200 esp8266_tcp_stream_bind(wsi->desc.sockfd, info->port, wsi);
203 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
204 #ifdef LWS_USE_UNIX_SOCK
205 if (LWS_UNIX_SOCK_ENABLED(vhost))
206 lwsl_notice(" Listening on \"%s\"\n", info->iface);
209 lwsl_notice(" Listening on port %d\n", info->port);
215 compatible_close(sockfd);
220 #if defined(LWS_WITH_ESP8266)
222 #define strchr ets_strchr
226 lws_select_vhost(struct lws_context *context, int port, const char *servername)
228 struct lws_vhost *vhost = context->vhost_list;
232 n = strlen(servername);
234 p = strchr(servername, ':');
236 colon = p - servername;
238 /* Priotity 1: first try exact matches */
241 if (port == vhost->listen_port &&
242 !strncmp(vhost->name, servername, colon)) {
243 lwsl_info("SNI: Found: %s\n", servername);
246 vhost = vhost->vhost_next;
250 * Priority 2: if no exact matches, try matching *.vhost-name
251 * unintentional matches are possible but resolve to x.com for *.x.com
252 * which is reasonable. If exact match exists we already chose it and
253 * never reach here. SSL will still fail it if the cert doesn't allow
257 vhost = context->vhost_list;
259 m = strlen(vhost->name);
260 if (port == vhost->listen_port &&
262 servername[colon - m - 1] == '.' &&
263 !strncmp(vhost->name, servername + colon - m, m)) {
264 lwsl_info("SNI: Found %s on wildcard: %s\n",
265 servername, vhost->name);
268 vhost = vhost->vhost_next;
271 /* Priority 3: match the first vhost on our port */
273 vhost = context->vhost_list;
275 if (port == vhost->listen_port) {
276 lwsl_info("vhost match to %s based on port %d\n",
280 vhost = vhost->vhost_next;
288 LWS_VISIBLE LWS_EXTERN const char *
289 lws_get_mimetype(const char *file, const struct lws_http_mount *m)
291 int n = strlen(file);
292 const struct lws_protocol_vhost_options *pvo = NULL;
295 pvo = m->extra_mimetypes;
300 if (!strcmp(&file[n - 4], ".ico"))
301 return "image/x-icon";
303 if (!strcmp(&file[n - 4], ".gif"))
306 if (!strcmp(&file[n - 3], ".js"))
307 return "text/javascript";
309 if (!strcmp(&file[n - 4], ".png"))
312 if (!strcmp(&file[n - 4], ".jpg"))
315 if (!strcmp(&file[n - 3], ".gz"))
316 return "application/gzip";
318 if (!strcmp(&file[n - 4], ".JPG"))
321 if (!strcmp(&file[n - 5], ".html"))
324 if (!strcmp(&file[n - 4], ".css"))
327 if (!strcmp(&file[n - 4], ".txt"))
330 if (!strcmp(&file[n - 4], ".svg"))
331 return "image/svg+xml";
333 if (!strcmp(&file[n - 4], ".ttf"))
334 return "application/x-font-ttf";
336 if (!strcmp(&file[n - 4], ".otf"))
337 return "application/font-woff";
339 if (!strcmp(&file[n - 5], ".woff"))
340 return "application/font-woff";
342 if (!strcmp(&file[n - 4], ".xml"))
343 return "application/xml";
346 if (pvo->name[0] == '*') /* ie, match anything */
349 if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
357 static lws_fop_flags_t
358 lws_vfs_prepare_flags(struct lws *wsi)
360 lws_fop_flags_t f = 0;
362 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
365 if (strstr(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING),
367 lwsl_info("client indicates GZIP is acceptable\n");
368 f |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
375 lws_http_serve(struct lws *wsi, char *uri, const char *origin,
376 const struct lws_http_mount *m)
378 const struct lws_protocol_vhost_options *pvo = m->interpret;
379 struct lws_process_html_args args;
380 const char *mimetype;
381 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
382 const struct lws_plat_file_ops *fops;
384 lws_fop_flags_t fflags = LWS_O_RDONLY;
385 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
386 struct _stat32i64 st;
392 char path[256], sym[512];
393 unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
394 unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
395 #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
400 lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
402 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
404 fflags |= lws_vfs_prepare_flags(wsi);
408 fops = lws_vfs_select_fops(wsi->context->fops, path, &vpath);
410 if (wsi->u.http.fop_fd)
411 lws_vfs_file_close(&wsi->u.http.fop_fd);
413 wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
414 path, vpath, &fflags);
415 if (!wsi->u.http.fop_fd) {
416 lwsl_err("Unable to open '%s'\n", path);
421 /* if it can't be statted, don't try */
422 if (fflags & LWS_FOP_FLAG_VIRTUAL)
424 #if defined(LWS_WITH_ESP32)
428 if (fstat(wsi->u.http.fop_fd->fd, &st)) {
429 lwsl_info("unable to stat %s\n", path);
433 #if defined(LWS_HAVE__STAT32I64)
434 if (_stat32i64(path, &st)) {
435 lwsl_info("unable to stat %s\n", path);
439 if (stat(path, &st)) {
440 lwsl_info("unable to stat %s\n", path);
446 wsi->u.http.fop_fd->mod_time = (uint32_t)st.st_mtime;
447 fflags |= LWS_FOP_FLAG_MOD_TIME_VALID;
449 lwsl_debug(" %s mode %d\n", path, S_IFMT & st.st_mode);
450 #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
451 if ((S_IFMT & st.st_mode) == S_IFLNK) {
452 len = readlink(path, sym, sizeof(sym) - 1);
454 lwsl_err("Failed to read link %s\n", path);
458 lwsl_debug("symlink %s -> %s\n", path, sym);
459 lws_snprintf(path, sizeof(path) - 1, "%s", sym);
462 if ((S_IFMT & st.st_mode) == S_IFDIR) {
463 lwsl_debug("default filename append to dir\n");
464 lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
468 } while ((S_IFMT & st.st_mode) != S_IFREG && spin < 5);
471 lwsl_err("symlink loop %s \n", path);
473 n = sprintf(sym, "%08lX%08lX",
474 (unsigned long)lws_vfs_get_length(wsi->u.http.fop_fd),
475 (unsigned long)lws_vfs_get_mod_time(wsi->u.http.fop_fd));
477 /* disable ranges if IF_RANGE token invalid */
479 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
480 if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
481 /* differs - defeat Range: */
482 wsi->u.http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
484 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
486 * he thinks he has some version of it already,
487 * check if the tag matches
489 if (!strcmp(sym, lws_hdr_simple_ptr(wsi,
490 WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
492 lwsl_debug("%s: ETAG match %s %s\n", __func__,
495 /* we don't need to send the payload */
496 if (lws_add_http_header_status(wsi,
497 HTTP_STATUS_NOT_MODIFIED, &p, end))
500 if (lws_add_http_header_by_token(wsi,
502 (unsigned char *)sym, n, &p, end))
505 if (lws_finalize_http_header(wsi, &p, end))
508 n = lws_write(wsi, start, p - start,
509 LWS_WRITE_HTTP_HEADERS);
510 if (n != (p - start)) {
511 lwsl_err("_write returned %d from %ld\n", n,
516 lws_vfs_file_close(&wsi->u.http.fop_fd);
518 return lws_http_transaction_completed(wsi);
522 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ETAG,
523 (unsigned char *)sym, n, &p, end))
527 mimetype = lws_get_mimetype(path, m);
529 lwsl_err("unknown mimetype for %s\n", path);
533 lwsl_debug("sending no mimetype for %s\n", path);
535 wsi->sending_chunked = 0;
538 * check if this is in the list of file suffixes to be interpreted by
543 if (n > (int)strlen(pvo->name) &&
544 !strcmp(&path[n - strlen(pvo->name)], pvo->name)) {
545 wsi->sending_chunked = 1;
546 wsi->protocol_interpret_idx = (char)(long)pvo->value;
547 lwsl_info("want %s interpreted by %s\n", path,
548 wsi->vhost->protocols[(int)(long)(pvo->value)].name);
549 wsi->protocol = &wsi->vhost->protocols[(int)(long)(pvo->value)];
550 if (lws_ensure_user_space(wsi))
558 const struct lws_protocols *pp = lws_vhost_name_to_protocol(
559 wsi->vhost, m->protocol);
561 if (lws_bind_protocol(wsi, pp))
564 args.max_len = end - p;
565 if (pp->callback(wsi, LWS_CALLBACK_ADD_HEADERS,
566 wsi->user_space, &args, 0))
568 p = (unsigned char *)args.p;
571 n = lws_serve_http_file(wsi, path, mimetype, (char *)start, p - start);
573 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
574 return -1; /* error or can't reuse connection: close the socket */
582 const struct lws_http_mount *
583 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
585 const struct lws_http_mount *hm, *hit = NULL;
588 hm = wsi->vhost->mount_list;
590 if (uri_len >= hm->mountpoint_len &&
591 !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
592 (uri_ptr[hm->mountpoint_len] == '\0' ||
593 uri_ptr[hm->mountpoint_len] == '/' ||
594 hm->mountpoint_len == 1)
596 if (hm->origin_protocol == LWSMPRO_CALLBACK ||
597 ((hm->origin_protocol == LWSMPRO_CGI ||
598 lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
600 hm->mountpoint_len > best)) {
601 best = hm->mountpoint_len;
614 lws_find_string_in_file(const char *filename, const char *string, int stringlen)
617 int fd, match = 0, pos = 0, n = 0, hit = 0;
619 fd = open(filename, O_RDONLY);
621 lwsl_err("can't open auth file: %s\n", filename);
627 n = read(fd, buf, sizeof(buf));
629 if (match == stringlen)
636 if (match == stringlen) {
637 if (buf[pos] == '\r' || buf[pos] == '\n') {
644 if (buf[pos] == string[match])
658 lws_unauthorised_basic_auth(struct lws *wsi)
660 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
661 unsigned char *start = pt->serv_buf + LWS_PRE,
662 *p = start, *end = p + 512;
666 /* no auth... tell him it is required */
668 if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
671 n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
672 if (lws_add_http_header_by_token(wsi,
673 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
674 (unsigned char *)buf, n, &p, end))
677 if (lws_finalize_http_header(wsi, &p, end))
680 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
684 return lws_http_transaction_completed(wsi);
690 int lws_clean_url(char *p)
693 if (p[0] == '/' && p[1] == '/') {
708 lws_http_action(struct lws *wsi)
710 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
711 enum http_connection_type connection_type;
712 enum http_version request_version;
713 char content_length_str[32];
714 struct lws_process_html_args args;
715 const struct lws_http_mount *hit = NULL;
716 unsigned int n, count = 0;
717 char http_version_str[10];
718 char http_conn_str[20];
719 int http_version_len;
720 char *uri_ptr = NULL, *s;
724 static const unsigned char methods[] = {
727 WSI_TOKEN_OPTIONS_URI,
730 WSI_TOKEN_DELETE_URI,
733 WSI_TOKEN_HTTP_COLON_PATH,
736 #if defined(_DEBUG) || defined(LWS_WITH_ACCESS_LOG)
737 static const char * const method_names[] = {
738 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT",
744 static const char * const oprot[] = {
745 "http://", "https://"
748 /* it's not websocket.... shall we accept it as http? */
750 for (n = 0; n < ARRAY_SIZE(methods); n++)
751 if (lws_hdr_total_length(wsi, methods[n]))
754 lwsl_warn("Missing URI in HTTP request\n");
759 lwsl_warn("multiple methods?\n");
763 if (lws_ensure_user_space(wsi))
766 for (n = 0; n < ARRAY_SIZE(methods); n++)
767 if (lws_hdr_total_length(wsi, methods[n])) {
768 uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
769 uri_len = lws_hdr_total_length(wsi, methods[n]);
770 lwsl_info("Method: %s request for '%s'\n",
771 method_names[n], uri_ptr);
778 /* we insist on absolute paths */
780 if (uri_ptr[0] != '/') {
781 lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
786 /* HTTP header had a content length? */
788 wsi->u.http.content_length = 0;
789 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
790 lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
791 lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
792 wsi->u.http.content_length = 100 * 1024 * 1024;
794 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
795 lws_hdr_copy(wsi, content_length_str,
796 sizeof(content_length_str) - 1,
797 WSI_TOKEN_HTTP_CONTENT_LENGTH);
798 wsi->u.http.content_length = atoll(content_length_str);
801 if (wsi->http2_substream) {
802 wsi->u.http.request_version = HTTP_VERSION_2;
804 /* http_version? Default to 1.0, override with token: */
805 request_version = HTTP_VERSION_1_0;
807 /* Works for single digit HTTP versions. : */
808 http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
809 if (http_version_len > 7) {
810 lws_hdr_copy(wsi, http_version_str,
811 sizeof(http_version_str) - 1, WSI_TOKEN_HTTP);
812 if (http_version_str[5] == '1' && http_version_str[7] == '1')
813 request_version = HTTP_VERSION_1_1;
815 wsi->u.http.request_version = request_version;
817 /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
818 if (request_version == HTTP_VERSION_1_1)
819 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
821 connection_type = HTTP_CONNECTION_CLOSE;
823 /* Override default if http "Connection:" header: */
824 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
825 lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
826 WSI_TOKEN_CONNECTION);
827 http_conn_str[sizeof(http_conn_str) - 1] = '\0';
828 if (!strcasecmp(http_conn_str, "keep-alive"))
829 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
831 if (!strcasecmp(http_conn_str, "close"))
832 connection_type = HTTP_CONNECTION_CLOSE;
834 wsi->u.http.connection_type = connection_type;
837 n = wsi->protocol->callback(wsi, LWS_CALLBACK_FILTER_HTTP_CONNECTION,
838 wsi->user_space, uri_ptr, uri_len);
840 lwsl_info("LWS_CALLBACK_HTTP closing\n");
845 * if there is content supposed to be coming,
846 * put a timeout on it having arrived
848 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
849 wsi->context->timeout_secs);
850 #ifdef LWS_OPENSSL_SUPPORT
851 if (wsi->redirect_to_https) {
853 * we accepted http:// only so we could redirect to
854 * https://, so issue the redirect. Create the redirection
855 * URI from the host: header and ignore the path part
857 unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
860 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
863 n = sprintf((char *)end, "https://%s/",
864 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
866 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
871 return lws_http_transaction_completed(wsi);
875 #ifdef LWS_WITH_ACCESS_LOG
877 * Produce Apache-compatible log string for wsi, like this:
879 * 2.31.234.19 - - [27/Mar/2016:03:22:44 +0800]
880 * "GET /aep-screen.png HTTP/1.1"
881 * 200 152987 "https://libwebsockets.org/index.html"
882 * "Mozilla/5.0 (Macint... Chrome/49.0.2623.87 Safari/537.36"
886 static const char * const hver[] = {
887 "http/1.0", "http/1.1", "http/2"
890 char ads[INET6_ADDRSTRLEN];
892 char ads[INET_ADDRSTRLEN];
897 time_t t = time(NULL);
900 if (wsi->access_log_pending)
903 wsi->access_log.header_log = lws_malloc(l);
904 if (wsi->access_log.header_log) {
908 strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
910 strcpy(da, "01/Jan/1970:00:00:00 +0000");
912 pa = lws_get_peer_simple(wsi, ads, sizeof(ads));
917 me = method_names[meth];
921 lws_snprintf(wsi->access_log.header_log, l,
922 "%s - - [%s] \"%s %s %s\"",
924 hver[wsi->u.http.request_version]);
926 l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
928 wsi->access_log.user_agent = lws_malloc(l + 2);
929 if (wsi->access_log.user_agent)
930 lws_hdr_copy(wsi, wsi->access_log.user_agent,
931 l + 1, WSI_TOKEN_HTTP_USER_AGENT);
933 lwsl_err("OOM getting user agent\n");
935 wsi->access_log_pending = 1;
940 /* can we serve it from the mount list? */
942 hit = lws_find_mount(wsi, uri_ptr, uri_len);
944 /* deferred cleanup and reset to protocols[0] */
946 lwsl_info("no hit\n");
948 if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
951 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
952 wsi->user_space, uri_ptr, uri_len);
957 s = uri_ptr + hit->mountpoint_len;
960 * if we have a mountpoint like https://xxx.com/yyy
961 * there is an implied / at the end for our purposes since
962 * we can only mount on a "directory".
964 * But if we just go with that, the browser cannot understand
965 * that he is actually looking down one "directory level", so
966 * even though we give him /yyy/abc.html he acts like the
967 * current directory level is /. So relative urls like "x.png"
968 * wrongly look outside the mountpoint.
970 * Therefore if we didn't come in on a url with an explicit
971 * / at the end, we must redirect to add it so the browser
972 * understands he is one "directory level" down.
974 if ((hit->mountpoint_len > 1 ||
975 (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
976 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
978 (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
979 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
980 (hit->origin_protocol != LWSMPRO_CGI &&
981 hit->origin_protocol != LWSMPRO_CALLBACK //&&
982 //hit->protocol == NULL
984 unsigned char *start = pt->serv_buf + LWS_PRE,
985 *p = start, *end = p + 512;
987 lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
989 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
992 /* > at start indicates deal with by redirect */
993 if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
994 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
995 n = lws_snprintf((char *)end, 256, "%s%s",
996 oprot[hit->origin_protocol & 1],
999 n = lws_snprintf((char *)end, 256,
1000 "%s%s%s/", oprot[lws_is_ssl(wsi)],
1001 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
1003 lws_clean_url((char *)end);
1005 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
1010 return lws_http_transaction_completed(wsi);
1016 if (hit->basic_auth_login_file) {
1017 char b64[160], plain[(sizeof(b64) * 3) / 4];
1020 /* Did he send auth? */
1021 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION))
1022 return lws_unauthorised_basic_auth(wsi);
1024 n = HTTP_STATUS_FORBIDDEN;
1026 m = lws_hdr_copy(wsi, b64, sizeof(b64), WSI_TOKEN_HTTP_AUTHORIZATION);
1028 lwsl_err("b64 auth too long\n");
1029 goto transaction_result_n;
1033 if (strcasecmp(b64, "Basic")) {
1034 lwsl_err("auth missing basic: %s\n", b64);
1035 goto transaction_result_n;
1038 /* It'll be like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l */
1040 m = lws_b64_decode_string(b64 + 6, plain, sizeof(plain));
1042 lwsl_err("plain auth too long\n");
1043 goto transaction_result_n;
1046 // lwsl_notice(plain);
1048 if (!lws_find_string_in_file(hit->basic_auth_login_file, plain, m)) {
1049 lwsl_err("basic auth lookup failed\n");
1050 return lws_unauthorised_basic_auth(wsi);
1053 lwsl_notice("basic auth accepted\n");
1055 /* accept the auth */
1059 #if defined(LWS_WITH_HTTP_PROXY)
1061 * The mount is a reverse proxy?
1064 if (hit->origin_protocol == LWSMPRO_HTTPS ||
1065 hit->origin_protocol == LWSMPRO_HTTP) {
1066 struct lws_client_connect_info i;
1067 char ads[96], rpath[256], *pcolon, *pslash, *p;
1070 memset(&i, 0, sizeof(i));
1071 i.context = lws_get_context(wsi);
1073 pcolon = strchr(hit->origin, ':');
1074 pslash = strchr(hit->origin, '/');
1076 lwsl_err("Proxy mount origin '%s' must have /\n", hit->origin);
1079 if (pcolon > pslash)
1083 n = pcolon - hit->origin;
1085 n = pslash - hit->origin;
1087 if (n >= sizeof(ads) - 2)
1088 n = sizeof(ads) - 2;
1090 memcpy(ads, hit->origin, n);
1095 if (hit->origin_protocol == LWSMPRO_HTTPS) {
1097 i.ssl_connection = 1;
1100 i.port = atoi(pcolon + 1);
1102 lws_snprintf(rpath, sizeof(rpath) - 1, "/%s/%s", pslash + 1, uri_ptr + hit->mountpoint_len);
1103 lws_clean_url(rpath);
1104 na = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_URI_ARGS);
1106 p = rpath + strlen(rpath);
1108 lws_hdr_copy(wsi, p, &rpath[sizeof(rpath) - 1] - p, WSI_TOKEN_HTTP_URI_ARGS);
1122 i.uri_replace_from = hit->origin;
1123 i.uri_replace_to = hit->mountpoint;
1125 lwsl_notice("proxying to %s port %d url %s, ssl %d, from %s, to %s\n",
1126 i.address, i.port, i.path, i.ssl_connection, i.uri_replace_from, i.uri_replace_to);
1128 if (!lws_client_connect_via_info(&i)) {
1129 lwsl_err("proxy connect fail\n");
1138 * A particular protocol callback is mounted here?
1140 * For the duration of this http transaction, bind us to the
1141 * associated protocol
1143 if (hit->origin_protocol == LWSMPRO_CALLBACK || hit->protocol) {
1144 const struct lws_protocols *pp;
1145 const char *name = hit->origin;
1147 name = hit->protocol;
1149 pp = lws_vhost_name_to_protocol(wsi->vhost, name);
1152 lwsl_err("Unable to find plugin '%s'\n",
1157 if (lws_bind_protocol(wsi, pp))
1162 args.max_len = hit->auth_mask;
1163 args.final = 0; /* used to signal callback dealt with it */
1165 n = wsi->protocol->callback(wsi, LWS_CALLBACK_CHECK_ACCESS_RIGHTS,
1166 wsi->user_space, &args, 0);
1168 lws_return_http_status(wsi, HTTP_STATUS_UNAUTHORIZED,
1172 if (args.final) /* callback completely handled it well */
1175 if (hit->cgienv && wsi->protocol->callback(wsi,
1176 LWS_CALLBACK_HTTP_PMO,
1177 wsi->user_space, (void *)hit->cgienv, 0))
1180 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
1181 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
1183 uri_ptr + hit->mountpoint_len,
1184 uri_len - hit->mountpoint_len);
1190 /* did we hit something with a cgi:// origin? */
1191 if (hit->origin_protocol == LWSMPRO_CGI) {
1192 const char *cmd[] = {
1193 NULL, /* replace with cgi path */
1197 lwsl_debug("%s: cgi\n", __func__);
1198 cmd[0] = hit->origin;
1201 if (hit->cgi_timeout)
1202 n = hit->cgi_timeout;
1204 n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
1207 lwsl_err("%s: cgi failed\n", __func__);
1216 if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
1217 s = (char *)hit->def;
1221 wsi->cache_secs = hit->cache_max_age;
1222 wsi->cache_reuse = hit->cache_reusable;
1223 wsi->cache_revalidate = hit->cache_revalidate;
1224 wsi->cache_intermediaries = hit->cache_intermediaries;
1226 n = lws_http_serve(wsi, s, hit->origin, hit);
1229 * lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
1231 if (hit->protocol) {
1232 const struct lws_protocols *pp = lws_vhost_name_to_protocol(
1233 wsi->vhost, hit->protocol);
1235 if (lws_bind_protocol(wsi, pp))
1238 n = pp->callback(wsi, LWS_CALLBACK_HTTP,
1240 uri_ptr + hit->mountpoint_len,
1241 uri_len - hit->mountpoint_len);
1243 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
1244 wsi->user_space, uri_ptr, uri_len);
1249 lwsl_info("LWS_CALLBACK_HTTP closing\n");
1258 * If we're not issuing a file, check for content_length or
1259 * HTTP keep-alive. No keep-alive header allocation for
1260 * ISSUING_FILE, as this uses HTTP/1.0.
1262 * In any case, return 0 and let lws_read decide how to
1263 * proceed based on state
1265 if (wsi->state != LWSS_HTTP_ISSUING_FILE)
1266 /* Prepare to read body if we have a content length: */
1267 if (wsi->u.http.content_length > 0)
1268 wsi->state = LWSS_HTTP_BODY;
1273 /* we're closing, losing some rx is OK */
1274 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1275 // lwsl_notice("%s: drop1\n", __func__);
1276 lws_header_table_detach(wsi, 1);
1280 transaction_result_n:
1281 lws_return_http_status(wsi, n, NULL);
1283 return lws_http_transaction_completed(wsi);
1288 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
1290 int protocol_len, n = 0, hit, non_space_char_found = 0, m;
1291 struct lws_context *context = lws_get_context(wsi);
1292 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
1293 struct _lws_header_related hdr;
1294 struct allocated_headers *ah;
1295 unsigned char *obuf = *buf;
1296 char protocol_list[128];
1297 char protocol_name[64];
1301 if (len >= 10000000) {
1302 lwsl_err("%s: assert: len %ld\n", __func__, (long)len);
1306 if (!wsi->u.hdr.ah) {
1307 lwsl_err("%s: assert: NULL ah\n", __func__);
1312 wsi->more_rx_waiting = !!len;
1314 if (wsi->mode != LWSCM_HTTP_SERVING &&
1315 wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED) {
1316 lwsl_err("%s: bad wsi mode %d\n", __func__, wsi->mode);
1320 m = lws_parse(wsi, *(*buf)++);
1324 * we are transitioning from http with
1325 * an AH, to raw. Drop the ah and set
1329 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1330 lws_bind_protocol(wsi, &wsi->vhost->protocols[
1332 raw_protocol_index]);
1333 lwsl_info("transition to raw vh %s prot %d\n",
1335 wsi->vhost->raw_protocol_index);
1336 if ((wsi->protocol->callback)(wsi,
1337 LWS_CALLBACK_RAW_ADOPT,
1338 wsi->user_space, NULL, 0))
1341 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1342 lws_union_transition(wsi, LWSCM_RAW);
1343 lws_header_table_detach(wsi, 1);
1345 if (m == 2 && (wsi->protocol->callback)(wsi,
1346 LWS_CALLBACK_RAW_RX,
1347 wsi->user_space, obuf, olen))
1352 lwsl_info("lws_parse failed\n");
1356 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
1359 lwsl_parser("%s: lws_parse sees parsing complete\n", __func__);
1360 lwsl_debug("%s: wsi->more_rx_waiting=%d\n", __func__,
1361 wsi->more_rx_waiting);
1363 /* check for unwelcome guests */
1365 if (wsi->context->reject_service_keywords) {
1366 const struct lws_protocol_vhost_options *rej =
1367 wsi->context->reject_service_keywords;
1368 char ua[384], *msg = NULL;
1370 if (lws_hdr_copy(wsi, ua, sizeof(ua) - 1,
1371 WSI_TOKEN_HTTP_USER_AGENT) > 0) {
1372 ua[sizeof(ua) - 1] = '\0';
1374 if (strstr(ua, rej->name)) {
1375 msg = strchr(rej->value, ' ');
1378 lws_return_http_status(wsi, atoi(rej->value), msg);
1380 wsi->vhost->conn_stats.rejected++;
1391 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
1392 struct lws_vhost *vhost = lws_select_vhost(
1393 context, wsi->vhost->listen_port,
1394 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
1399 lwsl_info("no host\n");
1401 wsi->vhost->conn_stats.trans++;
1402 if (!wsi->conn_stat_done) {
1403 wsi->vhost->conn_stats.conn++;
1404 wsi->conn_stat_done = 1;
1407 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
1408 lwsl_info("Changing to RAW mode\n");
1410 goto raw_transition;
1413 wsi->mode = LWSCM_PRE_WS_SERVING_ACCEPT;
1414 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1416 /* is this websocket protocol or normal http 1.0? */
1418 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
1419 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1421 wsi->vhost->conn_stats.ws_upg++;
1422 lwsl_info("Upgrade to ws\n");
1425 #ifdef LWS_USE_HTTP2
1426 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1428 wsi->vhost->conn_stats.http2_upg++;
1429 lwsl_info("Upgrade to h2c\n");
1433 lwsl_info("Unknown upgrade\n");
1434 /* dunno what he wanted to upgrade to */
1438 /* no upgrade ack... he remained as HTTP */
1440 lwsl_info("No upgrade\n");
1443 lws_union_transition(wsi, LWSCM_HTTP_SERVING_ACCEPTED);
1444 wsi->state = LWSS_HTTP;
1445 wsi->u.http.fop_fd = NULL;
1447 /* expose it at the same offset as u.hdr */
1448 wsi->u.http.ah = ah;
1449 lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
1450 (void *)wsi->u.hdr.ah);
1452 n = lws_http_action(wsi);
1456 #ifdef LWS_USE_HTTP2
1458 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
1459 lwsl_info("missing http2_settings\n");
1463 lwsl_info("h2c upgrade...\n");
1465 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
1466 /* convert the peer's HTTP-Settings */
1467 n = lws_b64_decode_string(p, protocol_list,
1468 sizeof(protocol_list));
1470 lwsl_parser("HTTP2_SETTINGS too long\n");
1474 /* adopt the header info */
1478 lws_union_transition(wsi, LWSCM_HTTP2_SERVING);
1480 /* http2 union member has http union struct at start */
1481 wsi->u.http.ah = ah;
1483 lws_http2_init(&wsi->u.http2.peer_settings);
1484 lws_http2_init(&wsi->u.http2.my_settings);
1488 lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings,
1489 (unsigned char *)protocol_list, n);
1491 strcpy(protocol_list,
1492 "HTTP/1.1 101 Switching Protocols\x0d\x0a"
1493 "Connection: Upgrade\x0d\x0a"
1494 "Upgrade: h2c\x0d\x0a\x0d\x0a");
1495 n = lws_issue_raw(wsi, (unsigned char *)protocol_list,
1496 strlen(protocol_list));
1497 if (n != strlen(protocol_list)) {
1498 lwsl_debug("http2 switch: ERROR writing to socket\n");
1502 wsi->state = LWSS_HTTP2_AWAIT_CLIENT_PREFACE;
1509 lwsl_err("NULL protocol at lws_read\n");
1514 * Select the first protocol we support from the list
1515 * the client sent us.
1517 * Copy it to remove header fragmentation
1520 if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
1521 WSI_TOKEN_PROTOCOL) < 0) {
1522 lwsl_err("protocol list too long");
1526 protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
1527 protocol_list[protocol_len] = '\0';
1531 while (*p && !hit) {
1533 non_space_char_found = 0;
1534 while (n < sizeof(protocol_name) - 1 && *p &&
1536 // ignore leading spaces
1537 if (!non_space_char_found && *p == ' ') {
1541 non_space_char_found = 1;
1542 protocol_name[n++] = *p++;
1544 protocol_name[n] = '\0';
1548 lwsl_info("checking %s\n", protocol_name);
1551 while (wsi->vhost->protocols[n].callback) {
1552 lwsl_info("try %s\n", wsi->vhost->protocols[n].name);
1554 if (wsi->vhost->protocols[n].name &&
1555 !strcmp(wsi->vhost->protocols[n].name,
1557 wsi->protocol = &wsi->vhost->protocols[n];
1566 /* we didn't find a protocol he wanted? */
1569 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)) {
1570 lwsl_info("No protocol from \"%s\" supported\n",
1575 * some clients only have one protocol and
1576 * do not send the protocol list header...
1577 * allow it and match to the vhost's default
1578 * protocol (which itself defaults to zero)
1580 lwsl_info("defaulting to prot handler %d\n",
1581 wsi->vhost->default_protocol_index);
1582 n = wsi->vhost->default_protocol_index;
1583 wsi->protocol = &wsi->vhost->protocols[
1584 (int)wsi->vhost->default_protocol_index];
1587 /* allocate wsi->user storage */
1588 if (lws_ensure_user_space(wsi))
1592 * Give the user code a chance to study the request and
1593 * have the opportunity to deny it
1595 if ((wsi->protocol->callback)(wsi,
1596 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
1598 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
1599 lwsl_warn("User code denied connection\n");
1604 * Perform the handshake according to the protocol version the
1608 switch (wsi->ietf_spec_revision) {
1610 lwsl_parser("lws_parse calling handshake_04\n");
1611 if (handshake_0405(context, wsi)) {
1612 lwsl_info("hs0405 has failed the connection\n");
1618 lwsl_info("Unknown client spec version %d\n",
1619 wsi->ietf_spec_revision);
1623 lws_same_vh_protocol_insert(wsi, n);
1625 /* we are upgrading to ws, so http/1.1 and keepalive +
1626 * pipelined header considerations about keeping the ah around
1627 * no longer apply. However it's common for the first ws
1628 * protocol data to have been coalesced with the browser
1629 * upgrade request and to already be in the ah rx buffer.
1632 lwsl_info("%s: %p: inheriting ah in ws mode (rxpos:%d, rxlen:%d)\n",
1633 __func__, wsi, wsi->u.hdr.ah->rxpos,
1634 wsi->u.hdr.ah->rxlen);
1638 lws_union_transition(wsi, LWSCM_WS_SERVING);
1640 * first service is WS mode will notice this, use the RX and
1641 * then detach the ah (caution: we are not in u.hdr union
1642 * mode any more then... ah_temp member is at start the same
1645 * Because rxpos/rxlen shows something in the ah, we will get
1646 * service guaranteed next time around the event loop
1648 * All union members begin with hdr, so we can use it even
1649 * though we transitioned to ws union mode (the ah detach
1650 * code uses it anyway).
1655 lws_restart_ws_ping_pong_timer(wsi);
1658 * create the frame buffer for this connection according to the
1659 * size mentioned in the protocol definition. If 0 there, use
1660 * a big default for compatibility
1663 n = wsi->protocol->rx_buffer_size;
1665 n = context->pt_serv_buf_size;
1667 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
1668 if (!wsi->u.ws.rx_ubuf) {
1669 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
1672 wsi->u.ws.rx_ubuf_alloc = n;
1673 lwsl_debug("Allocating RX buffer %d\n", n);
1674 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
1675 if (setsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_SNDBUF,
1676 (const char *)&n, sizeof n)) {
1677 lwsl_warn("Failed to set SNDBUF to %d", n);
1682 lwsl_parser("accepted v%02d connection\n",
1683 wsi->ietf_spec_revision);
1685 /* notify user code that we're ready to roll */
1687 if (wsi->protocol->callback)
1688 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
1690 #ifdef LWS_OPENSSL_SUPPORT
1698 /* !!! drop ah unreservedly after ESTABLISHED */
1699 if (!wsi->more_rx_waiting) {
1700 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1702 //lwsl_notice("%p: dropping ah EST\n", wsi);
1703 lws_header_table_detach(wsi, 1);
1707 } /* while all chars are handled */
1712 /* drop the header info */
1713 /* we're closing, losing some rx is OK */
1714 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1715 //lwsl_notice("%s: drop2\n", __func__);
1716 lws_header_table_detach(wsi, 1);
1722 lws_get_idlest_tsi(struct lws_context *context)
1724 unsigned int lowest = ~0;
1725 int n = 0, hit = -1;
1727 for (; n < context->count_threads; n++) {
1728 if ((unsigned int)context->pt[n].fds_count !=
1729 context->fd_limit_per_thread - 1 &&
1730 (unsigned int)context->pt[n].fds_count < lowest) {
1731 lowest = context->pt[n].fds_count;
1740 lws_create_new_server_wsi(struct lws_vhost *vhost)
1742 struct lws *new_wsi;
1743 int n = lws_get_idlest_tsi(vhost->context);
1746 lwsl_err("no space for new conn\n");
1750 new_wsi = lws_zalloc(sizeof(struct lws));
1751 if (new_wsi == NULL) {
1752 lwsl_err("Out of memory for new connection\n");
1757 lwsl_debug("Accepted wsi %p to context %p, tsi %d\n", new_wsi,
1758 vhost->context, new_wsi->tsi);
1760 new_wsi->vhost = vhost;
1761 new_wsi->context = vhost->context;
1762 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
1763 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
1765 /* initialize the instance struct */
1767 new_wsi->state = LWSS_HTTP;
1768 new_wsi->mode = LWSCM_HTTP_SERVING;
1769 new_wsi->hdr_parsing_completed = 0;
1771 #ifdef LWS_OPENSSL_SUPPORT
1772 new_wsi->use_ssl = LWS_SSL_ENABLED(vhost);
1776 * these can only be set once the protocol is known
1777 * we set an unestablished connection's protocol pointer
1778 * to the start of the supported list, so it can look
1779 * for matching ones during the handshake
1781 new_wsi->protocol = vhost->protocols;
1782 new_wsi->user_space = NULL;
1783 new_wsi->ietf_spec_revision = 0;
1784 new_wsi->desc.sockfd = LWS_SOCK_INVALID;
1785 vhost->context->count_wsi_allocated++;
1788 * outermost create notification for wsi
1789 * no user_space because no protocol selection
1791 vhost->protocols[0].callback(new_wsi, LWS_CALLBACK_WSI_CREATE,
1797 LWS_VISIBLE int LWS_WARN_UNUSED_RESULT
1798 lws_http_transaction_completed(struct lws *wsi)
1800 int n = NO_PENDING_TIMEOUT;
1802 lws_access_log(wsi);
1804 if (!wsi->hdr_parsing_completed) {
1805 lwsl_notice("%s: ignoring, ah parsing incomplete\n", __func__);
1809 lwsl_notice("%s: wsi %p\n", __func__, wsi);
1810 /* if we can't go back to accept new headers, drop the connection */
1811 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
1812 lwsl_info("%s: %p: close connection\n", __func__, wsi);
1816 if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
1819 /* otherwise set ourselves up ready to go again */
1820 wsi->state = LWSS_HTTP;
1821 wsi->mode = LWSCM_HTTP_SERVING;
1822 wsi->u.http.content_length = 0;
1823 wsi->u.http.content_remain = 0;
1824 wsi->hdr_parsing_completed = 0;
1825 #ifdef LWS_WITH_ACCESS_LOG
1826 wsi->access_log.sent = 0;
1829 if (wsi->vhost->keepalive_timeout)
1830 n = PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE;
1831 lws_set_timeout(wsi, n, wsi->vhost->keepalive_timeout);
1834 * We already know we are on http1.1 / keepalive and the next thing
1835 * coming will be another header set.
1837 * If there is no pending rx and we still have the ah, drop it and
1838 * reacquire a new ah when the new headers start to arrive. (Otherwise
1839 * we needlessly hog an ah indefinitely.)
1841 * However if there is pending rx and we know from the keepalive state
1842 * that is already at least the start of another header set, simply
1843 * reset the existing header table and keep it.
1845 if (wsi->u.hdr.ah) {
1846 lwsl_info("%s: wsi->more_rx_waiting=%d\n", __func__,
1847 wsi->more_rx_waiting);
1849 if (!wsi->more_rx_waiting) {
1850 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1851 lws_header_table_detach(wsi, 1);
1852 #ifdef LWS_OPENSSL_SUPPORT
1854 * additionally... if we are hogging an SSL instance
1855 * with no pending pipelined headers (or ah now), and
1856 * SSL is scarce, drop this connection without waiting
1859 if (wsi->vhost->use_ssl &&
1860 wsi->context->simultaneous_ssl_restriction &&
1861 wsi->context->simultaneous_ssl ==
1862 wsi->context->simultaneous_ssl_restriction) {
1863 lwsl_info("%s: simultaneous_ssl_restriction and nothing pipelined\n", __func__);
1868 lws_header_table_reset(wsi, 1);
1871 /* If we're (re)starting on headers, need other implied init */
1872 wsi->u.hdr.ues = URIES_IDLE;
1874 lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
1879 /* if not a socket, it's a raw, non-ssl file descriptor */
1881 LWS_VISIBLE struct lws *
1882 lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
1883 lws_sock_file_fd_type fd, const char *vh_prot_name,
1886 struct lws_context *context = vh->context;
1887 struct lws *new_wsi = lws_create_new_server_wsi(vh);
1888 struct lws_context_per_thread *pt;
1892 if (type & LWS_ADOPT_SOCKET)
1893 compatible_close(fd.sockfd);
1896 pt = &context->pt[(int)new_wsi->tsi];
1897 lws_stats_atomic_bump(context, pt, LWSSTATS_C_CONNECTIONS, 1);
1900 new_wsi->parent = parent;
1901 new_wsi->sibling_list = parent->child_list;
1902 parent->child_list = new_wsi;
1908 new_wsi->protocol = lws_vhost_name_to_protocol(new_wsi->vhost,
1910 if (!new_wsi->protocol) {
1911 lwsl_err("Protocol %s not enabled on vhost %s\n",
1912 vh_prot_name, new_wsi->vhost->name);
1915 if (lws_ensure_user_space(new_wsi)) {
1916 lwsl_notice("OOM trying to get user_space\n");
1920 if (type & LWS_ADOPT_HTTP) /* he will transition later */
1922 &vh->protocols[vh->default_protocol_index];
1923 else { /* this is the only time he will transition */
1924 lws_bind_protocol(new_wsi,
1925 &vh->protocols[vh->raw_protocol_index]);
1926 lws_union_transition(new_wsi, LWSCM_RAW);
1929 if (type & LWS_ADOPT_SOCKET) { /* socket desc */
1930 lwsl_debug("%s: new wsi %p, sockfd %d\n", __func__, new_wsi,
1931 (int)(size_t)fd.sockfd);
1933 if (type & LWS_ADOPT_HTTP)
1934 /* the transport is accepted...
1935 * give him time to negotiate */
1936 lws_set_timeout(new_wsi,
1937 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1938 context->timeout_secs);
1941 #if defined(LWS_WITH_ESP8266)
1942 esp8266_tcp_stream_accept(accept_fd, new_wsi);
1945 } else /* file desc */
1946 lwsl_debug("%s: new wsi %p, filefd %d\n", __func__, new_wsi,
1947 (int)(size_t)fd.filefd);
1950 * A new connection was accepted. Give the user a chance to
1951 * set properties of the newly created wsi. There's no protocol
1952 * selected yet so we issue this to the vhosts's default protocol,
1953 * itself by default protocols[0]
1955 n = LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED;
1956 if (!(type & LWS_ADOPT_HTTP)) {
1957 if (!(type & LWS_ADOPT_SOCKET))
1958 n = LWS_CALLBACK_RAW_ADOPT_FILE;
1960 n = LWS_CALLBACK_RAW_ADOPT;
1963 if (!LWS_SSL_ENABLED(new_wsi->vhost) || !(type & LWS_ADOPT_ALLOW_SSL) ||
1964 !(type & LWS_ADOPT_SOCKET)) {
1966 if (!(type & LWS_ADOPT_HTTP)) {
1967 if (!(type & LWS_ADOPT_SOCKET))
1968 new_wsi->mode = LWSCM_RAW_FILEDESC;
1970 new_wsi->mode = LWSCM_RAW;
1974 if (!(type & LWS_ADOPT_HTTP))
1975 new_wsi->mode = LWSCM_SSL_INIT_RAW;
1977 new_wsi->mode = LWSCM_SSL_INIT;
1982 lws_libev_accept(new_wsi, new_wsi->desc);
1983 lws_libuv_accept(new_wsi, new_wsi->desc);
1984 lws_libevent_accept(new_wsi, new_wsi->desc);
1987 if (insert_wsi_socket_into_fds(context, new_wsi)) {
1988 lwsl_err("%s: fail inserting socket\n", __func__);
1992 if (lws_server_socket_service_ssl(new_wsi, fd.sockfd)) {
1993 lwsl_err("%s: fail ssl negotiation\n", __func__);
1998 * by deferring callback to this point, after insertion to fds,
1999 * lws_callback_on_writable() can work from the callback
2001 if ((new_wsi->protocol->callback)(
2002 new_wsi, n, new_wsi->user_space, NULL, 0))
2005 if (type & LWS_ADOPT_HTTP) {
2006 if (!lws_header_table_attach(new_wsi, 0)) {
2007 lwsl_debug("Attached ah immediately\n");
2009 lwsl_notice("%s: waiting for ah\n", __func__);
2016 if (type & LWS_ADOPT_SOCKET)
2017 lws_close_free_wsi(new_wsi, LWS_CLOSE_STATUS_NOSTATUS);
2022 lwsl_notice("%s: exiting on bail\n", __func__);
2024 parent->child_list = new_wsi->sibling_list;
2025 if (new_wsi->user_space)
2026 lws_free(new_wsi->user_space);
2028 compatible_close(fd.sockfd);
2033 LWS_VISIBLE struct lws *
2034 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
2036 lws_sock_file_fd_type fd;
2038 fd.sockfd = accept_fd;
2039 return lws_adopt_descriptor_vhost(vh, LWS_ADOPT_SOCKET |
2040 LWS_ADOPT_HTTP | LWS_ADOPT_ALLOW_SSL, fd, NULL, NULL);
2043 LWS_VISIBLE struct lws *
2044 lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd)
2046 return lws_adopt_socket_vhost(context->vhost_list, accept_fd);
2049 /* Common read-buffer adoption for lws_adopt_*_readbuf */
2051 adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
2053 struct lws_context_per_thread *pt;
2054 struct allocated_headers *ah;
2055 struct lws_pollfd *pfd;
2060 if (!readbuf || len == 0)
2063 if (len > sizeof(ah->rx)) {
2064 lwsl_err("%s: rx in too big\n", __func__);
2069 * we can't process the initial read data until we can attach an ah.
2071 * if one is available, get it and place the data in his ah rxbuf...
2072 * wsi with ah that have pending rxbuf get auto-POLLIN service.
2074 * no autoservice because we didn't get a chance to attach the
2075 * readbuf data to wsi or ah yet, and we will do it next if we get
2078 if (wsi->u.hdr.ah || !lws_header_table_attach(wsi, 0)) {
2080 memcpy(ah->rx, readbuf, len);
2084 lwsl_notice("%s: calling service on readbuf ah\n", __func__);
2085 pt = &wsi->context->pt[(int)wsi->tsi];
2087 /* unlike a normal connect, we have the headers already
2088 * (or the first part of them anyway).
2089 * libuv won't come back and service us without a network
2090 * event, so we need to do the header service right here.
2092 pfd = &pt->fds[wsi->position_in_fds_table];
2093 pfd->revents |= LWS_POLLIN;
2094 lwsl_err("%s: calling service\n", __func__);
2095 if (lws_service_fd_tsi(wsi->context, pfd, wsi->tsi))
2096 /* service closed us */
2101 lwsl_err("%s: deferring handling ah\n", __func__);
2103 * hum if no ah came, we are on the wait list and must defer
2104 * dealing with this until the ah arrives.
2106 * later successful lws_header_table_attach() will apply the
2107 * below to the rx buffer (via lws_header_table_reset()).
2109 wsi->u.hdr.preamble_rx = lws_malloc(len);
2110 if (!wsi->u.hdr.preamble_rx) {
2114 memcpy(wsi->u.hdr.preamble_rx, readbuf, len);
2115 wsi->u.hdr.preamble_rx_len = len;
2120 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
2125 LWS_VISIBLE struct lws *
2126 lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
2127 const char *readbuf, size_t len)
2129 return adopt_socket_readbuf(lws_adopt_socket(context, accept_fd), readbuf, len);
2132 LWS_VISIBLE struct lws *
2133 lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
2134 const char *readbuf, size_t len)
2136 return adopt_socket_readbuf(lws_adopt_socket_vhost(vhost, accept_fd), readbuf, len);
2140 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
2141 struct lws_pollfd *pollfd)
2143 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
2144 lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
2145 struct allocated_headers *ah;
2146 lws_sock_file_fd_type fd;
2147 int opts = LWS_ADOPT_SOCKET | LWS_ADOPT_ALLOW_SSL;
2149 struct sockaddr_storage cli_addr;
2154 // lwsl_notice("%s: mode %d\n", __func__, wsi->mode);
2156 switch (wsi->mode) {
2158 case LWSCM_HTTP_SERVING:
2159 case LWSCM_HTTP_SERVING_ACCEPTED:
2160 case LWSCM_HTTP2_SERVING:
2163 /* handle http headers coming in */
2165 /* pending truncated sends have uber priority */
2167 if (wsi->trunc_len) {
2168 if (!(pollfd->revents & LWS_POLLOUT))
2171 if (lws_issue_raw(wsi, wsi->trunc_alloc +
2173 wsi->trunc_len) < 0)
2176 * we can't afford to allow input processing to send
2177 * something new, so spin around he event loop until
2178 * he doesn't have any partials
2183 /* any incoming data ready? */
2185 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
2189 * If we previously just did POLLIN when IN and OUT were
2190 * signalled (because POLLIN processing may have used up
2191 * the POLLOUT), don't let that happen twice in a row...
2192 * next time we see the situation favour POLLOUT
2194 #if !defined(LWS_WITH_ESP8266)
2195 if (wsi->favoured_pollin &&
2196 (pollfd->revents & pollfd->events & LWS_POLLOUT)) {
2197 wsi->favoured_pollin = 0;
2202 /* these states imply we MUST have an ah attached */
2204 if (wsi->mode != LWSCM_RAW && (wsi->state == LWSS_HTTP ||
2205 wsi->state == LWSS_HTTP_ISSUING_FILE ||
2206 wsi->state == LWSS_HTTP_HEADERS)) {
2207 if (!wsi->u.hdr.ah) {
2209 //lwsl_err("wsi %p: missing ah\n", wsi);
2210 /* no autoservice beacuse we will do it next */
2211 if (lws_header_table_attach(wsi, 0)) {
2212 lwsl_info("wsi %p: failed to acquire ah\n", wsi);
2218 //lwsl_notice("%s: %p: rxpos:%d rxlen:%d\n", __func__, wsi,
2219 // ah->rxpos, ah->rxlen);
2221 /* if nothing in ah rx buffer, get some fresh rx */
2222 if (ah->rxpos == ah->rxlen) {
2223 ah->rxlen = lws_ssl_capable_read(wsi, ah->rx,
2226 //lwsl_notice("%s: wsi %p, ah->rxlen = %d\r\n",
2227 // __func__, wsi, ah->rxlen);
2228 switch (ah->rxlen) {
2230 lwsl_info("%s: read 0 len\n", __func__);
2231 /* lwsl_info(" state=%d\n", wsi->state); */
2232 // if (!wsi->hdr_parsing_completed)
2233 // lws_header_table_detach(wsi);
2235 case LWS_SSL_CAPABLE_ERROR:
2237 case LWS_SSL_CAPABLE_MORE_SERVICE:
2238 ah->rxlen = ah->rxpos = 0;
2243 if (!(ah->rxpos != ah->rxlen && ah->rxlen)) {
2244 lwsl_err("%s: assert: rxpos %d, rxlen %d\n",
2245 __func__, ah->rxpos, ah->rxlen);
2250 /* just ignore incoming if waiting for close */
2251 if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
2252 wsi->state != LWSS_HTTP_ISSUING_FILE) {
2253 n = lws_read(wsi, ah->rx + ah->rxpos,
2254 ah->rxlen - ah->rxpos);
2255 if (n < 0) /* we closed wsi */
2257 if (wsi->u.hdr.ah) {
2258 if ( wsi->u.hdr.ah->rxlen)
2259 wsi->u.hdr.ah->rxpos += n;
2261 lwsl_debug("%s: wsi %p: ah read rxpos %d, rxlen %d\n", __func__, wsi, wsi->u.hdr.ah->rxpos, wsi->u.hdr.ah->rxlen);
2263 if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen &&
2264 (wsi->mode != LWSCM_HTTP_SERVING &&
2265 wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED &&
2266 wsi->mode != LWSCM_HTTP2_SERVING))
2267 lws_header_table_detach(wsi, 1);
2275 len = lws_ssl_capable_read(wsi, pt->serv_buf,
2276 context->pt_serv_buf_size);
2277 lwsl_debug("%s: wsi %p read %d\r\n", __func__, wsi, len);
2280 lwsl_info("%s: read 0 len\n", __func__);
2281 /* lwsl_info(" state=%d\n", wsi->state); */
2282 // if (!wsi->hdr_parsing_completed)
2283 // lws_header_table_detach(wsi);
2285 case LWS_SSL_CAPABLE_ERROR:
2287 case LWS_SSL_CAPABLE_MORE_SERVICE:
2291 if (wsi->mode == LWSCM_RAW) {
2292 n = user_callback_handle_rxflow(wsi->protocol->callback,
2293 wsi, LWS_CALLBACK_RAW_RX,
2294 wsi->user_space, pt->serv_buf, len);
2296 lwsl_info("LWS_CALLBACK_RAW_RX_fail\n");
2302 /* just ignore incoming if waiting for close */
2303 if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
2304 wsi->state != LWSS_HTTP_ISSUING_FILE) {
2306 * this may want to send
2307 * (via HTTP callback for example)
2309 n = lws_read(wsi, pt->serv_buf, len);
2310 if (n < 0) /* we closed wsi */
2313 * he may have used up the
2314 * writability above, if we will defer POLLOUT
2315 * processing in favour of POLLIN, note it
2317 if (pollfd->revents & LWS_POLLOUT)
2318 wsi->favoured_pollin = 1;
2324 /* this handles POLLOUT for http serving fragments */
2326 if (!(pollfd->revents & LWS_POLLOUT))
2330 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
2331 lwsl_notice("%s a\n", __func__);
2335 if (wsi->mode == LWSCM_RAW) {
2336 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
2337 #if defined(LWS_WITH_STATS)
2339 uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
2341 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
2342 lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
2343 wsi->active_writable_req_us = 0;
2346 n = user_callback_handle_rxflow(wsi->protocol->callback,
2347 wsi, LWS_CALLBACK_RAW_WRITEABLE,
2348 wsi->user_space, NULL, 0);
2350 lwsl_info("writeable_fail\n");
2356 if (!wsi->hdr_parsing_completed)
2359 if (wsi->state != LWSS_HTTP_ISSUING_FILE) {
2361 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
2362 #if defined(LWS_WITH_STATS)
2364 uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
2366 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
2367 lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
2368 wsi->active_writable_req_us = 0;
2372 n = user_callback_handle_rxflow(wsi->protocol->callback,
2373 wsi, LWS_CALLBACK_HTTP_WRITEABLE,
2374 wsi->user_space, NULL, 0);
2376 lwsl_info("writeable_fail\n");
2382 /* >0 == completion, <0 == error */
2383 n = lws_serve_http_file_fragment(wsi);
2384 if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi))) {
2385 lwsl_info("completed\n");
2391 case LWSCM_SERVER_LISTENER:
2394 /* pollin means a client has connected to us then */
2397 if (!(pollfd->revents & LWS_POLLIN) || !(pollfd->events & LWS_POLLIN))
2400 #ifdef LWS_OPENSSL_SUPPORT
2402 * can we really accept it, with regards to SSL limit?
2403 * another vhost may also have had POLLIN on his listener this
2404 * round and used it up already
2407 if (wsi->vhost->use_ssl &&
2408 context->simultaneous_ssl_restriction &&
2409 context->simultaneous_ssl ==
2410 context->simultaneous_ssl_restriction)
2411 /* no... ignore it, he won't come again until we are
2412 * below the simultaneous_ssl_restriction limit and
2413 * POLLIN is enabled on him again
2417 /* listen socket got an unencrypted connection... */
2419 clilen = sizeof(cli_addr);
2420 lws_latency_pre(context, wsi);
2421 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
2423 lws_latency(context, wsi, "listener accept", accept_fd,
2425 if (accept_fd < 0) {
2426 if (LWS_ERRNO == LWS_EAGAIN ||
2427 LWS_ERRNO == LWS_EWOULDBLOCK) {
2428 // lwsl_err("accept asks to try again\n");
2431 lwsl_err("ERROR on accept: %s\n", strerror(LWS_ERRNO));
2435 lws_plat_set_socket_options(wsi->vhost, accept_fd);
2437 #if defined(LWS_USE_IPV6)
2438 lwsl_debug("accepted new conn port %u on fd=%d\n",
2439 ((cli_addr.ss_family == AF_INET6) ?
2440 ntohs(((struct sockaddr_in6 *) &cli_addr)->sin6_port) :
2441 ntohs(((struct sockaddr_in *) &cli_addr)->sin_port)),
2444 lwsl_debug("accepted new conn port %u on fd=%d\n",
2445 ntohs(((struct sockaddr_in *) &cli_addr)->sin_port),
2450 /* not very beautiful... */
2451 accept_fd = (lws_sockfd_type)pollfd;
2454 * look at who we connected to and give user code a chance
2455 * to reject based on client IP. There's no protocol selected
2456 * yet so we issue this to protocols[0]
2458 if ((wsi->vhost->protocols[0].callback)(wsi,
2459 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
2460 NULL, (void *)(long)accept_fd, 0)) {
2461 lwsl_debug("Callback denied network connection\n");
2462 compatible_close(accept_fd);
2466 if (!(wsi->vhost->options & LWS_SERVER_OPTION_ONLY_RAW))
2467 opts |= LWS_ADOPT_HTTP;
2469 fd.sockfd = accept_fd;
2470 if (!lws_adopt_descriptor_vhost(wsi->vhost, opts, fd,
2472 /* already closed cleanly as necessary */
2476 } while (pt->fds_count < context->fd_limit_per_thread - 1 &&
2477 lws_poll_listen_fd(&pt->fds[wsi->position_in_fds_table]) > 0);
2485 if (!lws_server_socket_service_ssl(wsi, accept_fd))
2489 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
2495 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
2496 const char *other_headers, int other_headers_len)
2498 static const char * const intermediates[] = { "private", "public" };
2499 struct lws_context *context = lws_get_context(wsi);
2500 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
2501 #if defined(LWS_WITH_RANGES)
2502 struct lws_range_parsing *rp = &wsi->u.http.range;
2504 char cache_control[50], *cc = "no-store";
2505 unsigned char *response = pt->serv_buf + LWS_PRE;
2506 unsigned char *p = response;
2507 unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
2508 lws_filepos_t computed_total_content_length;
2509 int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
2510 lws_fop_flags_t fflags = LWS_O_RDONLY;
2511 #if defined(LWS_WITH_RANGES)
2514 const struct lws_plat_file_ops *fops;
2518 * We either call the platform fops .open with first arg platform fops,
2519 * or we call fops_zip .open with first arg platform fops, and fops_zip
2520 * open will decide whether to switch to fops_zip or stay with fops_def.
2522 * If wsi->u.http.fop_fd is already set, the caller already opened it
2524 if (!wsi->u.http.fop_fd) {
2525 fops = lws_vfs_select_fops(wsi->context->fops, file, &vpath);
2526 fflags |= lws_vfs_prepare_flags(wsi);
2527 wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
2528 file, vpath, &fflags);
2529 if (!wsi->u.http.fop_fd) {
2530 lwsl_err("Unable to open '%s'\n", file);
2535 wsi->u.http.filelen = lws_vfs_get_length(wsi->u.http.fop_fd);
2536 computed_total_content_length = wsi->u.http.filelen;
2538 #if defined(LWS_WITH_RANGES)
2539 ranges = lws_ranges_init(wsi, rp, wsi->u.http.filelen);
2541 lwsl_debug("Range count %d\n", ranges);
2544 * 1 range -> 206 + Content-Type: normal; Content-Range;
2545 * more -> 206 + Content-Type: multipart/byteranges
2546 * Repeat the true Content-Type in each multipart header
2547 * along with Content-Range
2550 /* it means he expressed a range in Range:, but it was illegal */
2551 lws_return_http_status(wsi, HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, NULL);
2552 if (lws_http_transaction_completed(wsi))
2553 return -1; /* <0 means just hang up */
2555 lws_vfs_file_close(&wsi->u.http.fop_fd);
2557 return 0; /* == 0 means we dealt with the transaction complete */
2560 n = HTTP_STATUS_PARTIAL_CONTENT;
2563 if (lws_add_http_header_status(wsi, n, &p, end))
2566 if ((wsi->u.http.fop_fd->flags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
2567 LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
2568 (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
2569 if (lws_add_http_header_by_token(wsi,
2570 WSI_TOKEN_HTTP_CONTENT_ENCODING,
2571 (unsigned char *)"gzip", 4, &p, end))
2573 lwsl_info("file is being provided in gzip\n");
2576 #if defined(LWS_WITH_RANGES)
2577 if (ranges < 2 && content_type && content_type[0])
2578 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2579 (unsigned char *)content_type,
2580 strlen(content_type), &p, end))
2583 if (ranges >= 2) { /* multipart byteranges */
2584 strncpy(wsi->u.http.multipart_content_type, content_type,
2585 sizeof(wsi->u.http.multipart_content_type) - 1);
2586 wsi->u.http.multipart_content_type[
2587 sizeof(wsi->u.http.multipart_content_type) - 1] = '\0';
2588 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2589 (unsigned char *)"multipart/byteranges; boundary=_lws",
2594 * our overall content length has to include
2596 * - (n + 1) x "_lws\r\n"
2597 * - n x Content-Type: xxx/xxx\r\n
2598 * - n x Content-Range: bytes xxx-yyy/zzz\r\n
2600 * - the actual payloads (aggregated in rp->agg)
2602 * Precompute it for the main response header
2605 computed_total_content_length = (lws_filepos_t)rp->agg +
2606 6 /* final _lws\r\n */;
2608 lws_ranges_reset(rp);
2609 while (lws_ranges_next(rp)) {
2610 n = lws_snprintf(cache_control, sizeof(cache_control),
2611 "bytes %llu-%llu/%llu",
2612 rp->start, rp->end, rp->extent);
2614 computed_total_content_length +=
2615 6 /* header _lws\r\n */ +
2616 14 + strlen(content_type) + 2 + /* Content-Type: xxx/xxx\r\n */
2617 15 + n + 2 + /* Content-Range: xxxx\r\n */
2621 lws_ranges_reset(rp);
2622 lws_ranges_next(rp);
2626 computed_total_content_length = (lws_filepos_t)rp->agg;
2627 n = lws_snprintf(cache_control, sizeof(cache_control), "bytes %llu-%llu/%llu",
2628 rp->start, rp->end, rp->extent);
2630 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_RANGE,
2631 (unsigned char *)cache_control,
2636 wsi->u.http.range.inside = 0;
2638 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ACCEPT_RANGES,
2639 (unsigned char *)"bytes", 5, &p, end))
2643 if (!wsi->sending_chunked) {
2644 if (lws_add_http_header_content_length(wsi,
2645 computed_total_content_length,
2649 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING,
2650 (unsigned char *)"chunked",
2655 if (wsi->cache_secs && wsi->cache_reuse) {
2656 if (wsi->cache_revalidate) {
2658 cclen = sprintf(cache_control, "%s max-age: %u",
2659 intermediates[wsi->cache_intermediaries],
2667 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CACHE_CONTROL,
2668 (unsigned char *)cc, cclen, &p, end))
2671 if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE)
2672 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
2673 (unsigned char *)"keep-alive", 10, &p, end))
2676 if (other_headers) {
2677 if ((end - p) < other_headers_len)
2679 memcpy(p, other_headers, other_headers_len);
2680 p += other_headers_len;
2683 if (lws_finalize_http_header(wsi, &p, end))
2686 ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
2687 if (ret != (p - response)) {
2688 lwsl_err("_write returned %d from %ld\n", ret, (long)(p - response));
2692 wsi->u.http.filepos = 0;
2693 wsi->state = LWSS_HTTP_ISSUING_FILE;
2695 return lws_serve_http_file_fragment(wsi);
2699 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
2703 lwsl_parser("%s: received %d byte packet\n", __func__, (int)len);
2705 lwsl_hexdump(*buf, len);
2708 /* let the rx protocol state machine have as much as it needs */
2712 * we were accepting input but now we stopped doing so
2714 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
2715 lws_rxflow_cache(wsi, *buf, 0, len);
2716 lwsl_parser("%s: cached %ld\n", __func__, (long)len);
2720 if (wsi->u.ws.rx_draining_ext) {
2721 // lwsl_notice("draining with 0\n");
2722 m = lws_rx_sm(wsi, 0);
2728 /* account for what we're using in rxflow buffer */
2729 if (wsi->rxflow_buffer)
2732 /* consume payload bytes efficiently */
2734 wsi->lws_rx_parse_state ==
2735 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
2736 m = lws_payload_until_length_exhausted(wsi, buf, &len);
2737 if (wsi->rxflow_buffer)
2738 wsi->rxflow_pos += m;
2741 /* process the byte */
2742 m = lws_rx_sm(wsi, *(*buf)++);
2748 lwsl_parser("%s: exit with %d unused\n", __func__, (int)len);
2754 lws_server_get_canonical_hostname(struct lws_context *context,
2755 struct lws_context_creation_info *info)
2757 if (lws_check_opt(info->options, LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME))
2759 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
2760 /* find canonical hostname */
2761 gethostname((char *)context->canonical_hostname,
2762 sizeof(context->canonical_hostname) - 1);
2764 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
2770 #define LWS_MAX_ELEM_NAME 32
2772 enum urldecode_stateful {
2786 static const char * const mp_hdr[] = {
2787 "content-disposition: ",
2792 typedef int (*lws_urldecode_stateful_cb)(void *data,
2793 const char *name, char **buf, int len, int final);
2795 struct lws_urldecode_stateful {
2798 char name[LWS_MAX_ELEM_NAME];
2799 char temp[LWS_MAX_ELEM_NAME];
2800 char content_type[32];
2801 char content_disp[32];
2802 char content_disp_filename[256];
2803 char mime_boundary[128];
2810 unsigned int multipart_form_data:1;
2811 unsigned int inside_quote:1;
2812 unsigned int subname:1;
2813 unsigned int boundary_real_crlf:1;
2815 enum urldecode_stateful state;
2817 lws_urldecode_stateful_cb output;
2820 static struct lws_urldecode_stateful *
2821 lws_urldecode_s_create(struct lws *wsi, char *out, int out_len, void *data,
2822 lws_urldecode_stateful_cb output)
2824 struct lws_urldecode_stateful *s = lws_zalloc(sizeof(*s));
2832 s->out_len = out_len;
2841 if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_CONTENT_TYPE) > 0) {
2842 /* multipart/form-data; boundary=----WebKitFormBoundarycc7YgAPEIHvgE9Bf */
2844 if (!strncmp(buf, "multipart/form-data", 19)) {
2845 s->multipart_form_data = 1;
2846 s->state = MT_LOOK_BOUND_IN;
2848 p = strstr(buf, "boundary=");
2851 s->mime_boundary[m++] = '\x0d';
2852 s->mime_boundary[m++] = '\x0a';
2853 s->mime_boundary[m++] = '-';
2854 s->mime_boundary[m++] = '-';
2855 while (m < sizeof(s->mime_boundary) - 1 &&
2857 s->mime_boundary[m++] = *p++;
2859 s->mime_boundary[m] = '\0';
2861 lwsl_notice("boundary '%s'\n", s->mime_boundary);
2870 lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int len)
2873 char c, was_end = 0;
2876 if (s->pos == s->out_len - s->mp - 1) {
2877 if (s->output(s->data, s->name, &s->out, s->pos, 0))
2885 /* states for url arg style */
2888 s->inside_quote = 0;
2890 s->name[s->pos] = '\0';
2897 s->name[s->pos] = '\0';
2898 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2905 if (s->pos >= sizeof(s->name) - 1) {
2906 lwsl_notice("Name too long\n");
2909 s->name[s->pos++] = *in++;
2918 s->out[s->pos] = '\0';
2919 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2928 s->out[s->pos++] = ' ';
2931 s->out[s->pos++] = *in++;
2934 n = char_to_hex(*in);
2944 n = char_to_hex(*in);
2949 s->out[s->pos++] = s->sum | n;
2954 /* states for multipart / mime style */
2956 case MT_LOOK_BOUND_IN:
2958 if (*in == s->mime_boundary[s->mp] &&
2959 s->mime_boundary[s->mp]) {
2962 if (!s->mime_boundary[s->mp]) {
2964 s->state = MT_IGNORE1;
2966 if (s->pos || was_end)
2967 if (s->output(s->data, s->name,
2968 &s->out, s->pos, 1))
2973 s->content_disp[0] = '\0';
2975 s->content_disp_filename[0] = '\0';
2976 s->boundary_real_crlf = 1;
2982 if (!s->boundary_real_crlf)
2985 memcpy(s->out + s->pos, s->mime_boundary + n, s->mp - n);
2988 goto retry_as_first;
2991 s->out[s->pos++] = *in;
2999 if (c >= 'A' && c <= 'Z')
3001 for (n = 0; n < ARRAY_SIZE(mp_hdr); n++)
3002 if (c == mp_hdr[n][s->mp]) {
3016 if (mp_hdr[hit][s->mp])
3024 s->state = MT_LOOK_BOUND_IN;
3026 s->state += hit + 1;
3030 /* form-data; name="file"; filename="t.txt" */
3032 if (*in == '\x0d') {
3033 // lwsl_notice("disp: '%s', '%s', '%s'\n",
3034 // s->content_disp, s->name,
3035 // s->content_disp_filename);
3037 if (s->content_disp_filename[0])
3038 if (s->output(s->data, s->name,
3039 &s->out, s->pos, LWS_UFS_OPEN))
3041 s->state = MT_IGNORE2;
3052 s->inside_quote ^= 1;
3058 s->temp[s->mp] = '\0';
3063 if (s->mp < sizeof(s->temp) - 1 &&
3064 (*in != ' ' || s->inside_quote))
3065 s->temp[s->mp++] = *in;
3070 if (s->mp < sizeof(s->content_disp) - 1)
3071 s->content_disp[s->mp++] = *in;
3072 s->content_disp[s->mp] = '\0';
3076 if (!strcmp(s->temp, "name")) {
3077 if (s->mp < sizeof(s->name) - 1)
3078 s->name[s->mp++] = *in;
3079 s->name[s->mp] = '\0';
3083 if (!strcmp(s->temp, "filename")) {
3084 if (s->mp < sizeof(s->content_disp_filename) - 1)
3085 s->content_disp_filename[s->mp++] = *in;
3086 s->content_disp_filename[s->mp] = '\0';
3095 s->state = MT_IGNORE2;
3097 if (s->mp < sizeof(s->content_type) - 1)
3098 s->content_type[s->mp++] = *in;
3099 s->content_type[s->mp] = '\0';
3106 s->state = MT_IGNORE2;
3113 s->state = MT_HNAME;
3123 lws_urldecode_s_destroy(struct lws_urldecode_stateful *s)
3127 if (s->state != US_IDLE)
3131 if (s->output(s->data, s->name, &s->out, s->pos, 1))
3140 struct lws_urldecode_stateful *s;
3141 lws_spa_fileupload_cb opt_cb;
3142 const char * const *param_names;
3156 lws_urldecode_spa_lookup(struct lws_spa *spa,
3161 for (n = 0; n < spa->count_params; n++)
3162 if (!strcmp(spa->param_names[n], name))
3169 lws_urldecode_spa_cb(void *data, const char *name, char **buf, int len,
3172 struct lws_spa *spa =
3173 (struct lws_spa *)data;
3176 if (spa->s->content_disp_filename[0]) {
3178 n = spa->opt_cb(spa->opt_data, name,
3179 spa->s->content_disp_filename,
3187 n = lws_urldecode_spa_lookup(spa, name);
3189 if (n == -1 || !len) /* unrecognized */
3192 if (!spa->params[n])
3193 spa->params[n] = *buf;
3195 if ((*buf) + len >= spa->end) {
3196 lwsl_notice("%s: exceeded storage\n", __func__);
3200 spa->param_length[n] += len;
3202 /* move it on inside storage */
3206 spa->s->out_len -= len + 1;
3211 LWS_VISIBLE LWS_EXTERN struct lws_spa *
3212 lws_spa_create(struct lws *wsi, const char * const *param_names,
3213 int count_params, int max_storage,
3214 lws_spa_fileupload_cb opt_cb, void *opt_data)
3216 struct lws_spa *spa = lws_zalloc(sizeof(*spa));
3221 spa->param_names = param_names;
3222 spa->count_params = count_params;
3223 spa->max_storage = max_storage;
3224 spa->opt_cb = opt_cb;
3225 spa->opt_data = opt_data;
3227 spa->storage = lws_malloc(max_storage);
3230 spa->end = spa->storage + max_storage - 1;
3232 spa->params = lws_zalloc(sizeof(char *) * count_params);
3236 spa->s = lws_urldecode_s_create(wsi, spa->storage, max_storage, spa,
3237 lws_urldecode_spa_cb);
3241 spa->param_length = lws_zalloc(sizeof(int) * count_params);
3242 if (!spa->param_length)
3245 lwsl_info("%s: Created SPA %p\n", __func__, spa);
3250 lws_urldecode_s_destroy(spa->s);
3252 lws_free(spa->params);
3254 lws_free(spa->storage);
3261 LWS_VISIBLE LWS_EXTERN int
3262 lws_spa_process(struct lws_spa *ludspa, const char *in, int len)
3265 lwsl_err("%s: NULL spa\n", __func__);
3268 /* we reject any junk after the last part arrived and we finalized */
3269 if (ludspa->finalized)
3272 return lws_urldecode_s_process(ludspa->s, in, len);
3275 LWS_VISIBLE LWS_EXTERN int
3276 lws_spa_get_length(struct lws_spa *ludspa, int n)
3278 if (n >= ludspa->count_params)
3281 return ludspa->param_length[n];
3284 LWS_VISIBLE LWS_EXTERN const char *
3285 lws_spa_get_string(struct lws_spa *ludspa, int n)
3287 if (n >= ludspa->count_params)
3290 return ludspa->params[n];
3293 LWS_VISIBLE LWS_EXTERN int
3294 lws_spa_finalize(struct lws_spa *spa)
3297 lws_urldecode_s_destroy(spa->s);
3306 LWS_VISIBLE LWS_EXTERN int
3307 lws_spa_destroy(struct lws_spa *spa)
3311 lwsl_notice("%s: destroy spa %p\n", __func__, spa);
3314 lws_urldecode_s_destroy(spa->s);
3316 lwsl_debug("%s %p %p %p %p\n", __func__,
3323 lws_free(spa->param_length);
3324 lws_free(spa->params);
3325 lws_free(spa->storage);
3332 LWS_VISIBLE LWS_EXTERN int
3333 lws_spa_destroy(struct lws_spa *spa)
3337 lwsl_info("%s: destroy spa %p\n", __func__, spa);
3340 lws_urldecode_s_destroy(spa->s);
3342 lwsl_debug("%s\n", __func__);
3344 lws_free(spa->param_length);
3345 lws_free(spa->params);
3346 lws_free(spa->storage);
3352 LWS_VISIBLE LWS_EXTERN int
3353 lws_chunked_html_process(struct lws_process_html_args *args,
3354 struct lws_process_html_state *s)
3356 char *sp, buffer[32];
3360 /* do replacements */
3362 old_len = args->len;
3365 while (sp < args->p + old_len) {
3367 if (args->len + 7 >= args->max_len) {
3368 lwsl_err("Used up interpret padding\n");
3372 if ((!s->pos && *sp == '$') || s->pos) {
3373 int hits = 0, hit = 0;
3377 s->swallow[s->pos++] = *sp;
3378 if (s->pos == sizeof(s->swallow) - 1)
3380 for (n = 0; n < s->count_vars; n++)
3381 if (!strncmp(s->swallow, s->vars[n], s->pos)) {
3387 s->swallow[s->pos] = '\0';
3388 memcpy(s->start, s->swallow, s->pos);
3394 if (hits == 1 && s->pos == strlen(s->vars[hit])) {
3395 pc = s->replace(s->data, hit);
3399 s->swallow[s->pos] = '\0';
3401 memmove(s->start + n,
3403 old_len - (sp - args->p));
3404 old_len += (n - s->pos) + 1;
3406 memcpy(s->start, pc, n);
3420 /* no space left for final chunk trailer */
3421 if (args->final && args->len + 7 >= args->max_len)
3424 n = sprintf(buffer, "%X\x0d\x0a", args->len);
3427 memcpy(args->p, buffer, n);
3431 sp = args->p + args->len;
3441 sp = args->p + args->len;