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",
68 #if defined(__linux__)
69 limit = vhost->context->count_threads;
72 for (m = 0; m < limit; m++) {
73 #ifdef LWS_USE_UNIX_SOCK
74 if (LWS_UNIX_SOCK_ENABLED(vhost))
75 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
79 if (LWS_IPV6_ENABLED(vhost))
80 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
83 sockfd = socket(AF_INET, SOCK_STREAM, 0);
87 #if defined(LWS_WITH_ESP8266)
88 sockfd = esp8266_create_tcp_listen_socket(vhost);
89 if (!lws_sockfd_valid(sockfd)) {
92 lwsl_err("ERROR opening socket\n");
95 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
97 * allow us to restart even if old sockets in TIME_WAIT
99 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
100 (const void *)&opt, sizeof(opt)) < 0) {
101 lwsl_err("reuseaddr failed\n");
102 compatible_close(sockfd);
106 #if defined(LWS_USE_IPV6) && defined(IPV6_V6ONLY)
107 if (LWS_IPV6_ENABLED(vhost)) {
108 if (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
109 int value = (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
110 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
111 (const void*)&value, sizeof(value)) < 0) {
112 compatible_close(sockfd);
119 #if defined(__linux__) && defined(SO_REUSEPORT) && LWS_MAX_SMP > 1
120 if (vhost->context->count_threads > 1)
121 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
122 (const void *)&opt, sizeof(opt)) < 0) {
123 compatible_close(sockfd);
128 lws_plat_set_socket_options(vhost, sockfd);
131 n = lws_socket_bind(vhost, sockfd, info->port, info->iface);
136 vhost->listen_port = info->port;
137 vhost->iface = info->iface;
139 wsi = lws_zalloc(sizeof(struct lws));
141 lwsl_err("Out of mem\n");
144 wsi->context = vhost->context;
145 wsi->desc.sockfd = sockfd;
146 wsi->mode = LWSCM_SERVER_LISTENER;
147 wsi->protocol = vhost->protocols;
153 if (LWS_LIBUV_ENABLED(vhost->context))
154 lws_uv_initvhost(vhost, wsi);
157 if (insert_wsi_socket_into_fds(vhost->context, wsi))
160 vhost->context->count_wsi_allocated++;
161 vhost->lserv_wsi = wsi;
164 n = listen(wsi->desc.sockfd, LWS_SOMAXCONN);
166 lwsl_err("listen failed with error %d\n", LWS_ERRNO);
167 vhost->lserv_wsi = NULL;
168 vhost->context->count_wsi_allocated--;
169 remove_wsi_socket_from_fds(wsi);
172 } /* for each thread able to independently listen */
174 #if defined(LWS_WITH_ESP8266)
175 esp8266_tcp_stream_bind(wsi->desc.sockfd, info->port, wsi);
178 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
179 #ifdef LWS_USE_UNIX_SOCK
180 if (LWS_UNIX_SOCK_ENABLED(vhost))
181 lwsl_notice(" Listening on \"%s\"\n", info->iface);
184 lwsl_notice(" Listening on port %d\n", info->port);
190 compatible_close(sockfd);
195 #if defined(LWS_WITH_ESP8266)
197 #define strchr ets_strchr
201 lws_select_vhost(struct lws_context *context, int port, const char *servername)
203 struct lws_vhost *vhost = context->vhost_list;
207 n = strlen(servername);
209 p = strchr(servername, ':');
211 colon = p - servername;
213 /* Priotity 1: first try exact matches */
216 if (port == vhost->listen_port &&
217 !strncmp(vhost->name, servername, colon)) {
218 lwsl_info("SNI: Found: %s\n", servername);
221 vhost = vhost->vhost_next;
225 * Priority 2: if no exact matches, try matching *.vhost-name
226 * unintentional matches are possible but resolve to x.com for *.x.com
227 * which is reasonable. If exact match exists we already chose it and
228 * never reach here. SSL will still fail it if the cert doesn't allow
232 vhost = context->vhost_list;
234 m = strlen(vhost->name);
235 if (port == vhost->listen_port &&
237 servername[colon - m - 1] == '.' &&
238 !strncmp(vhost->name, servername + colon - m, m)) {
239 lwsl_info("SNI: Found %s on wildcard: %s\n",
240 servername, vhost->name);
243 vhost = vhost->vhost_next;
246 /* Priority 3: match the first vhost on our port */
248 vhost = context->vhost_list;
250 if (port == vhost->listen_port) {
251 lwsl_info("vhost match to %s based on port %d\n",
255 vhost = vhost->vhost_next;
263 LWS_VISIBLE LWS_EXTERN const char *
264 lws_get_mimetype(const char *file, const struct lws_http_mount *m)
266 int n = strlen(file);
267 const struct lws_protocol_vhost_options *pvo = NULL;
270 pvo = m->extra_mimetypes;
275 if (!strcmp(&file[n - 4], ".ico"))
276 return "image/x-icon";
278 if (!strcmp(&file[n - 4], ".gif"))
281 if (!strcmp(&file[n - 3], ".js"))
282 return "text/javascript";
284 if (!strcmp(&file[n - 4], ".png"))
287 if (!strcmp(&file[n - 4], ".jpg"))
290 if (!strcmp(&file[n - 3], ".gz"))
291 return "application/gzip";
293 if (!strcmp(&file[n - 4], ".JPG"))
296 if (!strcmp(&file[n - 5], ".html"))
299 if (!strcmp(&file[n - 4], ".css"))
302 if (!strcmp(&file[n - 4], ".txt"))
305 if (!strcmp(&file[n - 4], ".svg"))
306 return "image/svg+xml";
308 if (!strcmp(&file[n - 4], ".ttf"))
309 return "application/x-font-ttf";
311 if (!strcmp(&file[n - 4], ".otf"))
312 return "application/font-woff";
314 if (!strcmp(&file[n - 5], ".woff"))
315 return "application/font-woff";
317 if (!strcmp(&file[n - 4], ".xml"))
318 return "application/xml";
321 if (pvo->name[0] == '*') /* ie, match anything */
324 if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
332 static lws_fop_flags_t
333 lws_vfs_prepare_flags(struct lws *wsi)
335 lws_fop_flags_t f = 0;
337 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
340 if (strstr(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING),
342 lwsl_info("client indicates GZIP is acceptable\n");
343 f |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
350 lws_http_serve(struct lws *wsi, char *uri, const char *origin,
351 const struct lws_http_mount *m)
353 const struct lws_protocol_vhost_options *pvo = m->interpret;
354 struct lws_process_html_args args;
355 const char *mimetype;
356 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
357 const struct lws_plat_file_ops *fops;
359 lws_fop_flags_t fflags = LWS_O_RDONLY;
360 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
361 struct _stat32i64 st;
367 char path[256], sym[512];
368 unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
369 unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
370 #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
375 lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
377 #if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
379 fflags |= lws_vfs_prepare_flags(wsi);
383 fops = lws_vfs_select_fops(wsi->context->fops, path, &vpath);
385 if (wsi->u.http.fop_fd)
386 lws_vfs_file_close(&wsi->u.http.fop_fd);
388 wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
389 path, vpath, &fflags);
390 if (!wsi->u.http.fop_fd) {
391 lwsl_err("Unable to open '%s'\n", path);
396 /* if it can't be statted, don't try */
397 if (fflags & LWS_FOP_FLAG_VIRTUAL)
399 #if defined(LWS_WITH_ESP32)
403 if (fstat(wsi->u.http.fop_fd->fd, &st)) {
404 lwsl_info("unable to stat %s\n", path);
408 #if defined(LWS_HAVE__STAT32I64)
409 if (_stat32i64(path, &st)) {
410 lwsl_info("unable to stat %s\n", path);
414 if (stat(path, &st)) {
415 lwsl_info("unable to stat %s\n", path);
421 wsi->u.http.fop_fd->mod_time = (uint32_t)st.st_mtime;
422 fflags |= LWS_FOP_FLAG_MOD_TIME_VALID;
424 lwsl_debug(" %s mode %d\n", path, S_IFMT & st.st_mode);
425 #if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
426 if ((S_IFMT & st.st_mode) == S_IFLNK) {
427 len = readlink(path, sym, sizeof(sym) - 1);
429 lwsl_err("Failed to read link %s\n", path);
433 lwsl_debug("symlink %s -> %s\n", path, sym);
434 lws_snprintf(path, sizeof(path) - 1, "%s", sym);
437 if ((S_IFMT & st.st_mode) == S_IFDIR) {
438 lwsl_debug("default filename append to dir\n");
439 lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
443 } while ((S_IFMT & st.st_mode) != S_IFREG && spin < 5);
446 lwsl_err("symlink loop %s \n", path);
448 n = sprintf(sym, "%08lX%08lX",
449 (unsigned long)lws_vfs_get_length(wsi->u.http.fop_fd),
450 (unsigned long)lws_vfs_get_mod_time(wsi->u.http.fop_fd));
452 /* disable ranges if IF_RANGE token invalid */
454 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
455 if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
456 /* differs - defeat Range: */
457 wsi->u.http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
459 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
461 * he thinks he has some version of it already,
462 * check if the tag matches
464 if (!strcmp(sym, lws_hdr_simple_ptr(wsi,
465 WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
467 lwsl_debug("%s: ETAG match %s %s\n", __func__,
470 /* we don't need to send the payload */
471 if (lws_add_http_header_status(wsi,
472 HTTP_STATUS_NOT_MODIFIED, &p, end))
475 if (lws_add_http_header_by_token(wsi,
477 (unsigned char *)sym, n, &p, end))
480 if (lws_finalize_http_header(wsi, &p, end))
483 n = lws_write(wsi, start, p - start,
484 LWS_WRITE_HTTP_HEADERS);
485 if (n != (p - start)) {
486 lwsl_err("_write returned %d from %ld\n", n,
491 lws_vfs_file_close(&wsi->u.http.fop_fd);
493 return lws_http_transaction_completed(wsi);
497 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ETAG,
498 (unsigned char *)sym, n, &p, end))
502 mimetype = lws_get_mimetype(path, m);
504 lwsl_err("unknown mimetype for %s\n", path);
508 lwsl_debug("sending no mimetype for %s\n", path);
510 wsi->sending_chunked = 0;
513 * check if this is in the list of file suffixes to be interpreted by
518 if (n > (int)strlen(pvo->name) &&
519 !strcmp(&path[n - strlen(pvo->name)], pvo->name)) {
520 wsi->sending_chunked = 1;
521 wsi->protocol_interpret_idx = (char)(long)pvo->value;
522 lwsl_info("want %s interpreted by %s\n", path,
523 wsi->vhost->protocols[(int)(long)(pvo->value)].name);
524 wsi->protocol = &wsi->vhost->protocols[(int)(long)(pvo->value)];
525 if (lws_ensure_user_space(wsi))
533 const struct lws_protocols *pp = lws_vhost_name_to_protocol(
534 wsi->vhost, m->protocol);
536 if (lws_bind_protocol(wsi, pp))
539 args.max_len = end - p;
540 if (pp->callback(wsi, LWS_CALLBACK_ADD_HEADERS,
541 wsi->user_space, &args, 0))
543 p = (unsigned char *)args.p;
546 n = lws_serve_http_file(wsi, path, mimetype, (char *)start, p - start);
548 if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
549 return -1; /* error or can't reuse connection: close the socket */
557 const struct lws_http_mount *
558 lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
560 const struct lws_http_mount *hm, *hit = NULL;
563 hm = wsi->vhost->mount_list;
565 if (uri_len >= hm->mountpoint_len &&
566 !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
567 (uri_ptr[hm->mountpoint_len] == '\0' ||
568 uri_ptr[hm->mountpoint_len] == '/' ||
569 hm->mountpoint_len == 1)
571 if (hm->origin_protocol == LWSMPRO_CALLBACK ||
572 ((hm->origin_protocol == LWSMPRO_CGI ||
573 lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
575 hm->mountpoint_len > best)) {
576 best = hm->mountpoint_len;
589 lws_find_string_in_file(const char *filename, const char *string, int stringlen)
592 int fd, match = 0, pos = 0, n = 0, hit = 0;
594 fd = open(filename, O_RDONLY);
596 lwsl_err("can't open auth file: %s\n", filename);
602 n = read(fd, buf, sizeof(buf));
604 if (match == stringlen)
611 if (match == stringlen) {
612 if (buf[pos] == '\r' || buf[pos] == '\n') {
619 if (buf[pos] == string[match])
633 lws_unauthorised_basic_auth(struct lws *wsi)
635 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
636 unsigned char *start = pt->serv_buf + LWS_PRE,
637 *p = start, *end = p + 512;
641 /* no auth... tell him it is required */
643 if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
646 n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
647 if (lws_add_http_header_by_token(wsi,
648 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
649 (unsigned char *)buf, n, &p, end))
652 if (lws_finalize_http_header(wsi, &p, end))
655 n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
659 return lws_http_transaction_completed(wsi);
666 lws_http_action(struct lws *wsi)
668 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
669 enum http_connection_type connection_type;
670 enum http_version request_version;
671 char content_length_str[32];
672 struct lws_process_html_args args;
673 const struct lws_http_mount *hit = NULL;
674 unsigned int n, count = 0;
675 char http_version_str[10];
676 char http_conn_str[20];
677 int http_version_len;
678 char *uri_ptr = NULL, *s;
682 static const unsigned char methods[] = {
685 WSI_TOKEN_OPTIONS_URI,
688 WSI_TOKEN_DELETE_URI,
691 WSI_TOKEN_HTTP_COLON_PATH,
694 #if defined(_DEBUG) || defined(LWS_WITH_ACCESS_LOG)
695 static const char * const method_names[] = {
696 "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT",
702 static const char * const oprot[] = {
703 "http://", "https://"
706 /* it's not websocket.... shall we accept it as http? */
708 for (n = 0; n < ARRAY_SIZE(methods); n++)
709 if (lws_hdr_total_length(wsi, methods[n]))
712 lwsl_warn("Missing URI in HTTP request\n");
717 lwsl_warn("multiple methods?\n");
721 if (lws_ensure_user_space(wsi))
724 for (n = 0; n < ARRAY_SIZE(methods); n++)
725 if (lws_hdr_total_length(wsi, methods[n])) {
726 uri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
727 uri_len = lws_hdr_total_length(wsi, methods[n]);
728 lwsl_info("Method: %s request for '%s'\n",
729 method_names[n], uri_ptr);
736 /* we insist on absolute paths */
738 if (uri_ptr[0] != '/') {
739 lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
744 /* HTTP header had a content length? */
746 wsi->u.http.content_length = 0;
747 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
748 lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
749 lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
750 wsi->u.http.content_length = 100 * 1024 * 1024;
752 if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) {
753 lws_hdr_copy(wsi, content_length_str,
754 sizeof(content_length_str) - 1,
755 WSI_TOKEN_HTTP_CONTENT_LENGTH);
756 wsi->u.http.content_length = atoll(content_length_str);
759 if (wsi->http2_substream) {
760 wsi->u.http.request_version = HTTP_VERSION_2;
762 /* http_version? Default to 1.0, override with token: */
763 request_version = HTTP_VERSION_1_0;
765 /* Works for single digit HTTP versions. : */
766 http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
767 if (http_version_len > 7) {
768 lws_hdr_copy(wsi, http_version_str,
769 sizeof(http_version_str) - 1, WSI_TOKEN_HTTP);
770 if (http_version_str[5] == '1' && http_version_str[7] == '1')
771 request_version = HTTP_VERSION_1_1;
773 wsi->u.http.request_version = request_version;
775 /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
776 if (request_version == HTTP_VERSION_1_1)
777 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
779 connection_type = HTTP_CONNECTION_CLOSE;
781 /* Override default if http "Connection:" header: */
782 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
783 lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
784 WSI_TOKEN_CONNECTION);
785 http_conn_str[sizeof(http_conn_str) - 1] = '\0';
786 if (!strcasecmp(http_conn_str, "keep-alive"))
787 connection_type = HTTP_CONNECTION_KEEP_ALIVE;
789 if (!strcasecmp(http_conn_str, "close"))
790 connection_type = HTTP_CONNECTION_CLOSE;
792 wsi->u.http.connection_type = connection_type;
795 n = wsi->protocol->callback(wsi, LWS_CALLBACK_FILTER_HTTP_CONNECTION,
796 wsi->user_space, uri_ptr, uri_len);
798 lwsl_info("LWS_CALLBACK_HTTP closing\n");
803 * if there is content supposed to be coming,
804 * put a timeout on it having arrived
806 lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
807 wsi->context->timeout_secs);
808 #ifdef LWS_OPENSSL_SUPPORT
809 if (wsi->redirect_to_https) {
811 * we accepted http:// only so we could redirect to
812 * https://, so issue the redirect. Create the redirection
813 * URI from the host: header and ignore the path part
815 unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
818 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
821 n = sprintf((char *)end, "https://%s/",
822 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
824 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
829 return lws_http_transaction_completed(wsi);
833 #ifdef LWS_WITH_ACCESS_LOG
835 * Produce Apache-compatible log string for wsi, like this:
837 * 2.31.234.19 - - [27/Mar/2016:03:22:44 +0800]
838 * "GET /aep-screen.png HTTP/1.1"
839 * 200 152987 "https://libwebsockets.org/index.html"
840 * "Mozilla/5.0 (Macint... Chrome/49.0.2623.87 Safari/537.36"
844 static const char * const hver[] = {
845 "http/1.0", "http/1.1", "http/2"
848 char ads[INET6_ADDRSTRLEN];
850 char ads[INET_ADDRSTRLEN];
855 time_t t = time(NULL);
858 if (wsi->access_log_pending)
861 wsi->access_log.header_log = lws_malloc(l);
862 if (wsi->access_log.header_log) {
866 strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp);
868 strcpy(da, "01/Jan/1970:00:00:00 +0000");
870 pa = lws_get_peer_simple(wsi, ads, sizeof(ads));
875 me = method_names[meth];
879 lws_snprintf(wsi->access_log.header_log, l,
880 "%s - - [%s] \"%s %s %s\"",
882 hver[wsi->u.http.request_version]);
884 l = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_USER_AGENT);
886 wsi->access_log.user_agent = lws_malloc(l + 2);
887 if (wsi->access_log.user_agent)
888 lws_hdr_copy(wsi, wsi->access_log.user_agent,
889 l + 1, WSI_TOKEN_HTTP_USER_AGENT);
891 lwsl_err("OOM getting user agent\n");
893 wsi->access_log_pending = 1;
898 /* can we serve it from the mount list? */
900 hit = lws_find_mount(wsi, uri_ptr, uri_len);
902 /* deferred cleanup and reset to protocols[0] */
904 lwsl_info("no hit\n");
906 if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
909 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
910 wsi->user_space, uri_ptr, uri_len);
915 s = uri_ptr + hit->mountpoint_len;
918 * if we have a mountpoint like https://xxx.com/yyy
919 * there is an implied / at the end for our purposes since
920 * we can only mount on a "directory".
922 * But if we just go with that, the browser cannot understand
923 * that he is actually looking down one "directory level", so
924 * even though we give him /yyy/abc.html he acts like the
925 * current directory level is /. So relative urls like "x.png"
926 * wrongly look outside the mountpoint.
928 * Therefore if we didn't come in on a url with an explicit
929 * / at the end, we must redirect to add it so the browser
930 * understands he is one "directory level" down.
932 if ((hit->mountpoint_len > 1 ||
933 (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
934 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
936 (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
937 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
938 (hit->origin_protocol != LWSMPRO_CGI &&
939 hit->origin_protocol != LWSMPRO_CALLBACK //&&
940 //hit->protocol == NULL
942 unsigned char *start = pt->serv_buf + LWS_PRE,
943 *p = start, *end = p + 512;
945 lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
947 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
950 /* > at start indicates deal with by redirect */
951 if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
952 hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
953 n = lws_snprintf((char *)end, 256, "%s%s",
954 oprot[hit->origin_protocol & 1],
957 n = lws_snprintf((char *)end, 256,
958 "%s%s%s/", oprot[lws_is_ssl(wsi)],
959 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
962 n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
967 return lws_http_transaction_completed(wsi);
973 if (hit->basic_auth_login_file) {
974 char b64[160], plain[(sizeof(b64) * 3) / 4];
977 /* Did he send auth? */
978 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION))
979 return lws_unauthorised_basic_auth(wsi);
981 n = HTTP_STATUS_FORBIDDEN;
983 m = lws_hdr_copy(wsi, b64, sizeof(b64), WSI_TOKEN_HTTP_AUTHORIZATION);
985 lwsl_err("b64 auth too long\n");
986 goto transaction_result_n;
990 if (strcasecmp(b64, "Basic")) {
991 lwsl_err("auth missing basic: %s\n", b64);
992 goto transaction_result_n;
995 /* It'll be like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l */
997 m = lws_b64_decode_string(b64 + 6, plain, sizeof(plain));
999 lwsl_err("plain auth too long\n");
1000 goto transaction_result_n;
1003 // lwsl_notice(plain);
1005 if (!lws_find_string_in_file(hit->basic_auth_login_file, plain, m)) {
1006 lwsl_err("basic auth lookup failed\n");
1007 return lws_unauthorised_basic_auth(wsi);
1010 lwsl_notice("basic auth accepted\n");
1012 /* accept the auth */
1017 * A particular protocol callback is mounted here?
1019 * For the duration of this http transaction, bind us to the
1020 * associated protocol
1022 if (hit->origin_protocol == LWSMPRO_CALLBACK || hit->protocol) {
1023 const struct lws_protocols *pp;
1024 const char *name = hit->origin;
1026 name = hit->protocol;
1028 pp = lws_vhost_name_to_protocol(wsi->vhost, name);
1031 lwsl_err("Unable to find plugin '%s'\n",
1036 if (lws_bind_protocol(wsi, pp))
1041 args.max_len = hit->auth_mask;
1042 args.final = 0; /* used to signal callback dealt with it */
1044 n = wsi->protocol->callback(wsi, LWS_CALLBACK_CHECK_ACCESS_RIGHTS,
1045 wsi->user_space, &args, 0);
1047 lws_return_http_status(wsi, HTTP_STATUS_UNAUTHORIZED,
1051 if (args.final) /* callback completely handled it well */
1054 if (hit->cgienv && wsi->protocol->callback(wsi,
1055 LWS_CALLBACK_HTTP_PMO,
1056 wsi->user_space, (void *)hit->cgienv, 0))
1059 if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
1060 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
1062 uri_ptr + hit->mountpoint_len,
1063 uri_len - hit->mountpoint_len);
1069 /* did we hit something with a cgi:// origin? */
1070 if (hit->origin_protocol == LWSMPRO_CGI) {
1071 const char *cmd[] = {
1072 NULL, /* replace with cgi path */
1076 lwsl_debug("%s: cgi\n", __func__);
1077 cmd[0] = hit->origin;
1080 if (hit->cgi_timeout)
1081 n = hit->cgi_timeout;
1083 n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
1086 lwsl_err("%s: cgi failed\n", __func__);
1095 if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
1096 s = (char *)hit->def;
1100 wsi->cache_secs = hit->cache_max_age;
1101 wsi->cache_reuse = hit->cache_reusable;
1102 wsi->cache_revalidate = hit->cache_revalidate;
1103 wsi->cache_intermediaries = hit->cache_intermediaries;
1105 n = lws_http_serve(wsi, s, hit->origin, hit);
1108 * lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
1110 if (hit->protocol) {
1111 const struct lws_protocols *pp = lws_vhost_name_to_protocol(
1112 wsi->vhost, hit->protocol);
1114 if (lws_bind_protocol(wsi, pp))
1117 n = pp->callback(wsi, LWS_CALLBACK_HTTP,
1119 uri_ptr + hit->mountpoint_len,
1120 uri_len - hit->mountpoint_len);
1122 n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
1123 wsi->user_space, uri_ptr, uri_len);
1128 lwsl_info("LWS_CALLBACK_HTTP closing\n");
1137 * If we're not issuing a file, check for content_length or
1138 * HTTP keep-alive. No keep-alive header allocation for
1139 * ISSUING_FILE, as this uses HTTP/1.0.
1141 * In any case, return 0 and let lws_read decide how to
1142 * proceed based on state
1144 if (wsi->state != LWSS_HTTP_ISSUING_FILE)
1145 /* Prepare to read body if we have a content length: */
1146 if (wsi->u.http.content_length > 0)
1147 wsi->state = LWSS_HTTP_BODY;
1152 /* we're closing, losing some rx is OK */
1153 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1154 // lwsl_notice("%s: drop1\n", __func__);
1155 lws_header_table_detach(wsi, 1);
1159 transaction_result_n:
1160 lws_return_http_status(wsi, n, NULL);
1162 return lws_http_transaction_completed(wsi);
1167 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
1169 int protocol_len, n = 0, hit, non_space_char_found = 0, m;
1170 struct lws_context *context = lws_get_context(wsi);
1171 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
1172 struct _lws_header_related hdr;
1173 struct allocated_headers *ah;
1174 unsigned char *obuf = *buf;
1175 char protocol_list[128];
1176 char protocol_name[64];
1180 if (len >= 10000000) {
1181 lwsl_err("%s: assert: len %ld\n", __func__, (long)len);
1185 if (!wsi->u.hdr.ah) {
1186 lwsl_err("%s: assert: NULL ah\n", __func__);
1191 wsi->more_rx_waiting = !!len;
1193 if (wsi->mode != LWSCM_HTTP_SERVING &&
1194 wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED) {
1195 lwsl_err("%s: bad wsi mode %d\n", __func__, wsi->mode);
1199 m = lws_parse(wsi, *(*buf)++);
1203 * we are transitioning from http with
1204 * an AH, to raw. Drop the ah and set
1208 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1209 lws_bind_protocol(wsi, &wsi->vhost->protocols[
1211 raw_protocol_index]);
1212 lwsl_info("transition to raw vh %s prot %d\n",
1214 wsi->vhost->raw_protocol_index);
1215 if ((wsi->protocol->callback)(wsi,
1216 LWS_CALLBACK_RAW_ADOPT,
1217 wsi->user_space, NULL, 0))
1220 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1221 lws_union_transition(wsi, LWSCM_RAW);
1222 lws_header_table_detach(wsi, 1);
1224 if (m == 2 && (wsi->protocol->callback)(wsi,
1225 LWS_CALLBACK_RAW_RX,
1226 wsi->user_space, obuf, olen))
1231 lwsl_info("lws_parse failed\n");
1235 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
1238 lwsl_parser("%s: lws_parse sees parsing complete\n", __func__);
1239 lwsl_debug("%s: wsi->more_rx_waiting=%d\n", __func__,
1240 wsi->more_rx_waiting);
1242 /* check for unwelcome guests */
1244 if (wsi->context->reject_service_keywords) {
1245 const struct lws_protocol_vhost_options *rej =
1246 wsi->context->reject_service_keywords;
1247 char ua[384], *msg = NULL;
1249 if (lws_hdr_copy(wsi, ua, sizeof(ua) - 1,
1250 WSI_TOKEN_HTTP_USER_AGENT) > 0) {
1251 ua[sizeof(ua) - 1] = '\0';
1253 if (strstr(ua, rej->name)) {
1254 msg = strchr(rej->value, ' ');
1257 lws_return_http_status(wsi, atoi(rej->value), msg);
1259 wsi->vhost->conn_stats.rejected++;
1270 if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
1271 struct lws_vhost *vhost = lws_select_vhost(
1272 context, wsi->vhost->listen_port,
1273 lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
1278 lwsl_info("no host\n");
1280 wsi->vhost->conn_stats.trans++;
1281 if (!wsi->conn_stat_done) {
1282 wsi->vhost->conn_stats.conn++;
1283 wsi->conn_stat_done = 1;
1286 if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
1287 lwsl_info("Changing to RAW mode\n");
1289 goto raw_transition;
1292 wsi->mode = LWSCM_PRE_WS_SERVING_ACCEPT;
1293 lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
1295 /* is this websocket protocol or normal http 1.0? */
1297 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
1298 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1300 wsi->vhost->conn_stats.ws_upg++;
1301 lwsl_info("Upgrade to ws\n");
1304 #ifdef LWS_USE_HTTP2
1305 if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
1307 wsi->vhost->conn_stats.http2_upg++;
1308 lwsl_info("Upgrade to h2c\n");
1312 lwsl_info("Unknown upgrade\n");
1313 /* dunno what he wanted to upgrade to */
1317 /* no upgrade ack... he remained as HTTP */
1319 lwsl_info("No upgrade\n");
1322 lws_union_transition(wsi, LWSCM_HTTP_SERVING_ACCEPTED);
1323 wsi->state = LWSS_HTTP;
1324 wsi->u.http.fop_fd = NULL;
1326 /* expose it at the same offset as u.hdr */
1327 wsi->u.http.ah = ah;
1328 lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
1329 (void *)wsi->u.hdr.ah);
1331 n = lws_http_action(wsi);
1335 #ifdef LWS_USE_HTTP2
1337 if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
1338 lwsl_info("missing http2_settings\n");
1342 lwsl_info("h2c upgrade...\n");
1344 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
1345 /* convert the peer's HTTP-Settings */
1346 n = lws_b64_decode_string(p, protocol_list,
1347 sizeof(protocol_list));
1349 lwsl_parser("HTTP2_SETTINGS too long\n");
1353 /* adopt the header info */
1357 lws_union_transition(wsi, LWSCM_HTTP2_SERVING);
1359 /* http2 union member has http union struct at start */
1360 wsi->u.http.ah = ah;
1362 lws_http2_init(&wsi->u.http2.peer_settings);
1363 lws_http2_init(&wsi->u.http2.my_settings);
1367 lws_http2_interpret_settings_payload(&wsi->u.http2.peer_settings,
1368 (unsigned char *)protocol_list, n);
1370 strcpy(protocol_list,
1371 "HTTP/1.1 101 Switching Protocols\x0d\x0a"
1372 "Connection: Upgrade\x0d\x0a"
1373 "Upgrade: h2c\x0d\x0a\x0d\x0a");
1374 n = lws_issue_raw(wsi, (unsigned char *)protocol_list,
1375 strlen(protocol_list));
1376 if (n != strlen(protocol_list)) {
1377 lwsl_debug("http2 switch: ERROR writing to socket\n");
1381 wsi->state = LWSS_HTTP2_AWAIT_CLIENT_PREFACE;
1388 lwsl_err("NULL protocol at lws_read\n");
1393 * Select the first protocol we support from the list
1394 * the client sent us.
1396 * Copy it to remove header fragmentation
1399 if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
1400 WSI_TOKEN_PROTOCOL) < 0) {
1401 lwsl_err("protocol list too long");
1405 protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
1406 protocol_list[protocol_len] = '\0';
1410 while (*p && !hit) {
1412 non_space_char_found = 0;
1413 while (n < sizeof(protocol_name) - 1 && *p &&
1415 // ignore leading spaces
1416 if (!non_space_char_found && *p == ' ') {
1420 non_space_char_found = 1;
1421 protocol_name[n++] = *p++;
1423 protocol_name[n] = '\0';
1427 lwsl_info("checking %s\n", protocol_name);
1430 while (wsi->vhost->protocols[n].callback) {
1431 lwsl_info("try %s\n", wsi->vhost->protocols[n].name);
1433 if (wsi->vhost->protocols[n].name &&
1434 !strcmp(wsi->vhost->protocols[n].name,
1436 wsi->protocol = &wsi->vhost->protocols[n];
1445 /* we didn't find a protocol he wanted? */
1448 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)) {
1449 lwsl_info("No protocol from \"%s\" supported\n",
1454 * some clients only have one protocol and
1455 * do not send the protocol list header...
1456 * allow it and match to the vhost's default
1457 * protocol (which itself defaults to zero)
1459 lwsl_info("defaulting to prot handler %d\n",
1460 wsi->vhost->default_protocol_index);
1461 n = wsi->vhost->default_protocol_index;
1462 wsi->protocol = &wsi->vhost->protocols[
1463 (int)wsi->vhost->default_protocol_index];
1466 /* allocate wsi->user storage */
1467 if (lws_ensure_user_space(wsi))
1471 * Give the user code a chance to study the request and
1472 * have the opportunity to deny it
1474 if ((wsi->protocol->callback)(wsi,
1475 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
1477 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
1478 lwsl_warn("User code denied connection\n");
1483 * Perform the handshake according to the protocol version the
1487 switch (wsi->ietf_spec_revision) {
1489 lwsl_parser("lws_parse calling handshake_04\n");
1490 if (handshake_0405(context, wsi)) {
1491 lwsl_info("hs0405 has failed the connection\n");
1497 lwsl_info("Unknown client spec version %d\n",
1498 wsi->ietf_spec_revision);
1502 lws_same_vh_protocol_insert(wsi, n);
1504 /* we are upgrading to ws, so http/1.1 and keepalive +
1505 * pipelined header considerations about keeping the ah around
1506 * no longer apply. However it's common for the first ws
1507 * protocol data to have been coalesced with the browser
1508 * upgrade request and to already be in the ah rx buffer.
1511 lwsl_info("%s: %p: inheriting ah in ws mode (rxpos:%d, rxlen:%d)\n",
1512 __func__, wsi, wsi->u.hdr.ah->rxpos,
1513 wsi->u.hdr.ah->rxlen);
1517 lws_union_transition(wsi, LWSCM_WS_SERVING);
1519 * first service is WS mode will notice this, use the RX and
1520 * then detach the ah (caution: we are not in u.hdr union
1521 * mode any more then... ah_temp member is at start the same
1524 * Because rxpos/rxlen shows something in the ah, we will get
1525 * service guaranteed next time around the event loop
1527 * All union members begin with hdr, so we can use it even
1528 * though we transitioned to ws union mode (the ah detach
1529 * code uses it anyway).
1534 lws_restart_ws_ping_pong_timer(wsi);
1537 * create the frame buffer for this connection according to the
1538 * size mentioned in the protocol definition. If 0 there, use
1539 * a big default for compatibility
1542 n = wsi->protocol->rx_buffer_size;
1544 n = context->pt_serv_buf_size;
1546 wsi->u.ws.rx_ubuf = lws_malloc(n + 4 /* 0x0000ffff zlib */);
1547 if (!wsi->u.ws.rx_ubuf) {
1548 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
1551 wsi->u.ws.rx_ubuf_alloc = n;
1552 lwsl_debug("Allocating RX buffer %d\n", n);
1553 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
1554 if (setsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_SNDBUF,
1555 (const char *)&n, sizeof n)) {
1556 lwsl_warn("Failed to set SNDBUF to %d", n);
1561 lwsl_parser("accepted v%02d connection\n",
1562 wsi->ietf_spec_revision);
1564 /* notify user code that we're ready to roll */
1566 if (wsi->protocol->callback)
1567 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
1569 #ifdef LWS_OPENSSL_SUPPORT
1577 /* !!! drop ah unreservedly after ESTABLISHED */
1578 if (!wsi->more_rx_waiting) {
1579 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1581 //lwsl_notice("%p: dropping ah EST\n", wsi);
1582 lws_header_table_detach(wsi, 1);
1586 } /* while all chars are handled */
1591 /* drop the header info */
1592 /* we're closing, losing some rx is OK */
1593 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1594 //lwsl_notice("%s: drop2\n", __func__);
1595 lws_header_table_detach(wsi, 1);
1601 lws_get_idlest_tsi(struct lws_context *context)
1603 unsigned int lowest = ~0;
1604 int n = 0, hit = -1;
1606 for (; n < context->count_threads; n++) {
1607 if ((unsigned int)context->pt[n].fds_count !=
1608 context->fd_limit_per_thread - 1 &&
1609 (unsigned int)context->pt[n].fds_count < lowest) {
1610 lowest = context->pt[n].fds_count;
1619 lws_create_new_server_wsi(struct lws_vhost *vhost)
1621 struct lws *new_wsi;
1622 int n = lws_get_idlest_tsi(vhost->context);
1625 lwsl_err("no space for new conn\n");
1629 new_wsi = lws_zalloc(sizeof(struct lws));
1630 if (new_wsi == NULL) {
1631 lwsl_err("Out of memory for new connection\n");
1636 lwsl_debug("Accepted wsi %p to context %p, tsi %d\n", new_wsi,
1637 vhost->context, new_wsi->tsi);
1639 new_wsi->vhost = vhost;
1640 new_wsi->context = vhost->context;
1641 new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
1642 new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
1644 /* initialize the instance struct */
1646 new_wsi->state = LWSS_HTTP;
1647 new_wsi->mode = LWSCM_HTTP_SERVING;
1648 new_wsi->hdr_parsing_completed = 0;
1650 #ifdef LWS_OPENSSL_SUPPORT
1651 new_wsi->use_ssl = LWS_SSL_ENABLED(vhost);
1655 * these can only be set once the protocol is known
1656 * we set an unestablished connection's protocol pointer
1657 * to the start of the supported list, so it can look
1658 * for matching ones during the handshake
1660 new_wsi->protocol = vhost->protocols;
1661 new_wsi->user_space = NULL;
1662 new_wsi->ietf_spec_revision = 0;
1663 new_wsi->desc.sockfd = LWS_SOCK_INVALID;
1664 vhost->context->count_wsi_allocated++;
1667 * outermost create notification for wsi
1668 * no user_space because no protocol selection
1670 vhost->protocols[0].callback(new_wsi, LWS_CALLBACK_WSI_CREATE,
1676 LWS_VISIBLE int LWS_WARN_UNUSED_RESULT
1677 lws_http_transaction_completed(struct lws *wsi)
1679 int n = NO_PENDING_TIMEOUT;
1681 lws_access_log(wsi);
1683 lwsl_info("%s: wsi %p\n", __func__, wsi);
1684 /* if we can't go back to accept new headers, drop the connection */
1685 if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
1686 lwsl_info("%s: %p: close connection\n", __func__, wsi);
1690 if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
1693 /* otherwise set ourselves up ready to go again */
1694 wsi->state = LWSS_HTTP;
1695 wsi->mode = LWSCM_HTTP_SERVING;
1696 wsi->u.http.content_length = 0;
1697 wsi->u.http.content_remain = 0;
1698 wsi->hdr_parsing_completed = 0;
1699 #ifdef LWS_WITH_ACCESS_LOG
1700 wsi->access_log.sent = 0;
1703 if (wsi->vhost->keepalive_timeout)
1704 n = PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE;
1705 lws_set_timeout(wsi, n, wsi->vhost->keepalive_timeout);
1708 * We already know we are on http1.1 / keepalive and the next thing
1709 * coming will be another header set.
1711 * If there is no pending rx and we still have the ah, drop it and
1712 * reacquire a new ah when the new headers start to arrive. (Otherwise
1713 * we needlessly hog an ah indefinitely.)
1715 * However if there is pending rx and we know from the keepalive state
1716 * that is already at least the start of another header set, simply
1717 * reset the existing header table and keep it.
1719 if (wsi->u.hdr.ah) {
1720 lwsl_info("%s: wsi->more_rx_waiting=%d\n", __func__,
1721 wsi->more_rx_waiting);
1723 if (!wsi->more_rx_waiting) {
1724 wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
1725 lws_header_table_detach(wsi, 1);
1726 #ifdef LWS_OPENSSL_SUPPORT
1728 * additionally... if we are hogging an SSL instance
1729 * with no pending pipelined headers (or ah now), and
1730 * SSL is scarce, drop this connection without waiting
1733 if (wsi->vhost->use_ssl &&
1734 wsi->context->simultaneous_ssl_restriction &&
1735 wsi->context->simultaneous_ssl ==
1736 wsi->context->simultaneous_ssl_restriction) {
1737 lwsl_info("%s: simultaneous_ssl_restriction and nothing pipelined\n", __func__);
1742 lws_header_table_reset(wsi, 1);
1745 /* If we're (re)starting on headers, need other implied init */
1746 wsi->u.hdr.ues = URIES_IDLE;
1748 lwsl_info("%s: %p: keep-alive await new transaction\n", __func__, wsi);
1753 /* if not a socket, it's a raw, non-ssl file descriptor */
1755 LWS_VISIBLE struct lws *
1756 lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
1757 lws_sock_file_fd_type fd, const char *vh_prot_name,
1760 struct lws_context *context = vh->context;
1761 struct lws *new_wsi = lws_create_new_server_wsi(vh);
1762 struct lws_context_per_thread *pt;
1766 if (type & LWS_ADOPT_SOCKET)
1767 compatible_close(fd.sockfd);
1770 pt = &context->pt[(int)new_wsi->tsi];
1771 lws_stats_atomic_bump(context, pt, LWSSTATS_C_CONNECTIONS, 1);
1774 new_wsi->parent = parent;
1775 new_wsi->sibling_list = parent->child_list;
1776 parent->child_list = new_wsi;
1782 new_wsi->protocol = lws_vhost_name_to_protocol(new_wsi->vhost,
1784 if (!new_wsi->protocol) {
1785 lwsl_err("Protocol %s not enabled on vhost %s\n",
1786 vh_prot_name, new_wsi->vhost->name);
1789 if (lws_ensure_user_space(new_wsi)) {
1790 lwsl_notice("OOM trying to get user_space\n");
1794 if (type & LWS_ADOPT_HTTP) /* he will transition later */
1796 &vh->protocols[vh->default_protocol_index];
1797 else { /* this is the only time he will transition */
1798 lws_bind_protocol(new_wsi,
1799 &vh->protocols[vh->raw_protocol_index]);
1800 lws_union_transition(new_wsi, LWSCM_RAW);
1803 if (type & LWS_ADOPT_SOCKET) { /* socket desc */
1804 lwsl_debug("%s: new wsi %p, sockfd %d\n", __func__, new_wsi,
1805 (int)(size_t)fd.sockfd);
1807 if (type & LWS_ADOPT_HTTP)
1808 /* the transport is accepted...
1809 * give him time to negotiate */
1810 lws_set_timeout(new_wsi,
1811 PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
1812 context->timeout_secs);
1815 #if defined(LWS_WITH_ESP8266)
1816 esp8266_tcp_stream_accept(accept_fd, new_wsi);
1819 } else /* file desc */
1820 lwsl_debug("%s: new wsi %p, filefd %d\n", __func__, new_wsi,
1821 (int)(size_t)fd.filefd);
1824 * A new connection was accepted. Give the user a chance to
1825 * set properties of the newly created wsi. There's no protocol
1826 * selected yet so we issue this to the vhosts's default protocol,
1827 * itself by default protocols[0]
1829 n = LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED;
1830 if (!(type & LWS_ADOPT_HTTP)) {
1831 if (!(type & LWS_ADOPT_SOCKET))
1832 n = LWS_CALLBACK_RAW_ADOPT_FILE;
1834 n = LWS_CALLBACK_RAW_ADOPT;
1837 if (!LWS_SSL_ENABLED(new_wsi->vhost) || !(type & LWS_ADOPT_ALLOW_SSL) ||
1838 !(type & LWS_ADOPT_SOCKET)) {
1840 if (!(type & LWS_ADOPT_HTTP)) {
1841 if (!(type & LWS_ADOPT_SOCKET))
1842 new_wsi->mode = LWSCM_RAW_FILEDESC;
1844 new_wsi->mode = LWSCM_RAW;
1848 if (!(type & LWS_ADOPT_HTTP))
1849 new_wsi->mode = LWSCM_SSL_INIT_RAW;
1851 new_wsi->mode = LWSCM_SSL_INIT;
1856 lws_libev_accept(new_wsi, new_wsi->desc);
1857 lws_libuv_accept(new_wsi, new_wsi->desc);
1858 lws_libevent_accept(new_wsi, new_wsi->desc);
1861 if (insert_wsi_socket_into_fds(context, new_wsi)) {
1862 lwsl_err("%s: fail inserting socket\n", __func__);
1866 if (lws_server_socket_service_ssl(new_wsi, fd.sockfd)) {
1867 lwsl_err("%s: fail ssl negotiation\n", __func__);
1872 * by deferring callback to this point, after insertion to fds,
1873 * lws_callback_on_writable() can work from the callback
1875 if ((new_wsi->protocol->callback)(
1876 new_wsi, n, new_wsi->user_space, NULL, 0))
1879 if (type & LWS_ADOPT_HTTP) {
1880 if (!lws_header_table_attach(new_wsi, 0)) {
1881 lwsl_debug("Attached ah immediately\n");
1883 lwsl_notice("%s: waiting for ah\n", __func__);
1890 if (type & LWS_ADOPT_SOCKET)
1891 lws_close_free_wsi(new_wsi, LWS_CLOSE_STATUS_NOSTATUS);
1896 lwsl_notice("%s: exiting on bail\n", __func__);
1898 parent->child_list = new_wsi->sibling_list;
1899 if (new_wsi->user_space)
1900 lws_free(new_wsi->user_space);
1902 compatible_close(fd.sockfd);
1907 LWS_VISIBLE struct lws *
1908 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
1910 lws_sock_file_fd_type fd;
1912 fd.sockfd = accept_fd;
1913 return lws_adopt_descriptor_vhost(vh, LWS_ADOPT_SOCKET |
1914 LWS_ADOPT_HTTP | LWS_ADOPT_ALLOW_SSL, fd, NULL, NULL);
1917 LWS_VISIBLE struct lws *
1918 lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd)
1920 return lws_adopt_socket_vhost(context->vhost_list, accept_fd);
1923 /* Common read-buffer adoption for lws_adopt_*_readbuf */
1925 adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
1927 struct lws_context_per_thread *pt;
1928 struct allocated_headers *ah;
1929 struct lws_pollfd *pfd;
1934 if (!readbuf || len == 0)
1937 if (len > sizeof(ah->rx)) {
1938 lwsl_err("%s: rx in too big\n", __func__);
1943 * we can't process the initial read data until we can attach an ah.
1945 * if one is available, get it and place the data in his ah rxbuf...
1946 * wsi with ah that have pending rxbuf get auto-POLLIN service.
1948 * no autoservice because we didn't get a chance to attach the
1949 * readbuf data to wsi or ah yet, and we will do it next if we get
1952 if (wsi->u.hdr.ah || !lws_header_table_attach(wsi, 0)) {
1954 memcpy(ah->rx, readbuf, len);
1958 lwsl_notice("%s: calling service on readbuf ah\n", __func__);
1959 pt = &wsi->context->pt[(int)wsi->tsi];
1961 /* unlike a normal connect, we have the headers already
1962 * (or the first part of them anyway).
1963 * libuv won't come back and service us without a network
1964 * event, so we need to do the header service right here.
1966 pfd = &pt->fds[wsi->position_in_fds_table];
1967 pfd->revents |= LWS_POLLIN;
1968 lwsl_err("%s: calling service\n", __func__);
1969 if (lws_service_fd_tsi(wsi->context, pfd, wsi->tsi))
1970 /* service closed us */
1975 lwsl_err("%s: deferring handling ah\n", __func__);
1977 * hum if no ah came, we are on the wait list and must defer
1978 * dealing with this until the ah arrives.
1980 * later successful lws_header_table_attach() will apply the
1981 * below to the rx buffer (via lws_header_table_reset()).
1983 wsi->u.hdr.preamble_rx = lws_malloc(len);
1984 if (!wsi->u.hdr.preamble_rx) {
1988 memcpy(wsi->u.hdr.preamble_rx, readbuf, len);
1989 wsi->u.hdr.preamble_rx_len = len;
1994 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1999 LWS_VISIBLE struct lws *
2000 lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
2001 const char *readbuf, size_t len)
2003 return adopt_socket_readbuf(lws_adopt_socket(context, accept_fd), readbuf, len);
2006 LWS_VISIBLE struct lws *
2007 lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
2008 const char *readbuf, size_t len)
2010 return adopt_socket_readbuf(lws_adopt_socket_vhost(vhost, accept_fd), readbuf, len);
2014 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
2015 struct lws_pollfd *pollfd)
2017 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
2018 lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
2019 struct allocated_headers *ah;
2020 lws_sock_file_fd_type fd;
2021 int opts = LWS_ADOPT_SOCKET | LWS_ADOPT_ALLOW_SSL;
2023 struct sockaddr_storage cli_addr;
2028 // lwsl_notice("%s: mode %d\n", __func__, wsi->mode);
2030 switch (wsi->mode) {
2032 case LWSCM_HTTP_SERVING:
2033 case LWSCM_HTTP_SERVING_ACCEPTED:
2034 case LWSCM_HTTP2_SERVING:
2037 /* handle http headers coming in */
2039 /* pending truncated sends have uber priority */
2041 if (wsi->trunc_len) {
2042 if (!(pollfd->revents & LWS_POLLOUT))
2045 if (lws_issue_raw(wsi, wsi->trunc_alloc +
2047 wsi->trunc_len) < 0)
2050 * we can't afford to allow input processing to send
2051 * something new, so spin around he event loop until
2052 * he doesn't have any partials
2057 /* any incoming data ready? */
2059 if (!(pollfd->revents & pollfd->events & LWS_POLLIN))
2063 * If we previously just did POLLIN when IN and OUT were
2064 * signalled (because POLLIN processing may have used up
2065 * the POLLOUT), don't let that happen twice in a row...
2066 * next time we see the situation favour POLLOUT
2068 #if !defined(LWS_WITH_ESP8266)
2069 if (wsi->favoured_pollin &&
2070 (pollfd->revents & pollfd->events & LWS_POLLOUT)) {
2071 wsi->favoured_pollin = 0;
2076 /* these states imply we MUST have an ah attached */
2078 if (wsi->mode != LWSCM_RAW && (wsi->state == LWSS_HTTP ||
2079 wsi->state == LWSS_HTTP_ISSUING_FILE ||
2080 wsi->state == LWSS_HTTP_HEADERS)) {
2081 if (!wsi->u.hdr.ah) {
2083 //lwsl_err("wsi %p: missing ah\n", wsi);
2084 /* no autoservice beacuse we will do it next */
2085 if (lws_header_table_attach(wsi, 0)) {
2086 lwsl_info("wsi %p: failed to acquire ah\n", wsi);
2092 //lwsl_notice("%s: %p: rxpos:%d rxlen:%d\n", __func__, wsi,
2093 // ah->rxpos, ah->rxlen);
2095 /* if nothing in ah rx buffer, get some fresh rx */
2096 if (ah->rxpos == ah->rxlen) {
2097 ah->rxlen = lws_ssl_capable_read(wsi, ah->rx,
2100 //lwsl_notice("%s: wsi %p, ah->rxlen = %d\r\n",
2101 // __func__, wsi, ah->rxlen);
2102 switch (ah->rxlen) {
2104 lwsl_info("%s: read 0 len\n", __func__);
2105 /* lwsl_info(" state=%d\n", wsi->state); */
2106 // if (!wsi->hdr_parsing_completed)
2107 // lws_header_table_detach(wsi);
2109 case LWS_SSL_CAPABLE_ERROR:
2111 case LWS_SSL_CAPABLE_MORE_SERVICE:
2112 ah->rxlen = ah->rxpos = 0;
2117 if (!(ah->rxpos != ah->rxlen && ah->rxlen)) {
2118 lwsl_err("%s: assert: rxpos %d, rxlen %d\n",
2119 __func__, ah->rxpos, ah->rxlen);
2124 /* just ignore incoming if waiting for close */
2125 if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
2126 n = lws_read(wsi, ah->rx + ah->rxpos,
2127 ah->rxlen - ah->rxpos);
2128 if (n < 0) /* we closed wsi */
2130 if (wsi->u.hdr.ah) {
2131 if ( wsi->u.hdr.ah->rxlen)
2132 wsi->u.hdr.ah->rxpos += n;
2134 lwsl_debug("%s: wsi %p: ah read rxpos %d, rxlen %d\n", __func__, wsi, wsi->u.hdr.ah->rxpos, wsi->u.hdr.ah->rxlen);
2136 if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen &&
2137 (wsi->mode != LWSCM_HTTP_SERVING &&
2138 wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED &&
2139 wsi->mode != LWSCM_HTTP2_SERVING))
2140 lws_header_table_detach(wsi, 1);
2148 len = lws_ssl_capable_read(wsi, pt->serv_buf,
2149 context->pt_serv_buf_size);
2150 lwsl_debug("%s: wsi %p read %d\r\n", __func__, wsi, len);
2153 lwsl_info("%s: read 0 len\n", __func__);
2154 /* lwsl_info(" state=%d\n", wsi->state); */
2155 // if (!wsi->hdr_parsing_completed)
2156 // lws_header_table_detach(wsi);
2158 case LWS_SSL_CAPABLE_ERROR:
2160 case LWS_SSL_CAPABLE_MORE_SERVICE:
2164 if (wsi->mode == LWSCM_RAW) {
2165 n = user_callback_handle_rxflow(wsi->protocol->callback,
2166 wsi, LWS_CALLBACK_RAW_RX,
2167 wsi->user_space, pt->serv_buf, len);
2169 lwsl_info("LWS_CALLBACK_RAW_RX_fail\n");
2175 /* just ignore incoming if waiting for close */
2176 if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
2178 * this may want to send
2179 * (via HTTP callback for example)
2181 n = lws_read(wsi, pt->serv_buf, len);
2182 if (n < 0) /* we closed wsi */
2185 * he may have used up the
2186 * writability above, if we will defer POLLOUT
2187 * processing in favour of POLLIN, note it
2189 if (pollfd->revents & LWS_POLLOUT)
2190 wsi->favoured_pollin = 1;
2196 /* this handles POLLOUT for http serving fragments */
2198 if (!(pollfd->revents & LWS_POLLOUT))
2202 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
2203 lwsl_notice("%s a\n", __func__);
2207 if (wsi->mode == LWSCM_RAW) {
2208 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
2209 #if defined(LWS_WITH_STATS)
2211 uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
2213 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
2214 lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
2215 wsi->active_writable_req_us = 0;
2218 n = user_callback_handle_rxflow(wsi->protocol->callback,
2219 wsi, LWS_CALLBACK_RAW_WRITEABLE,
2220 wsi->user_space, NULL, 0);
2222 lwsl_info("writeable_fail\n");
2228 if (!wsi->hdr_parsing_completed)
2231 if (wsi->state != LWSS_HTTP_ISSUING_FILE) {
2233 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
2234 #if defined(LWS_WITH_STATS)
2236 uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
2238 lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
2239 lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
2240 wsi->active_writable_req_us = 0;
2244 n = user_callback_handle_rxflow(wsi->protocol->callback,
2245 wsi, LWS_CALLBACK_HTTP_WRITEABLE,
2246 wsi->user_space, NULL, 0);
2248 lwsl_info("writeable_fail\n");
2254 /* >0 == completion, <0 == error */
2255 n = lws_serve_http_file_fragment(wsi);
2256 if (n < 0 || (n > 0 && lws_http_transaction_completed(wsi))) {
2257 lwsl_info("completed\n");
2263 case LWSCM_SERVER_LISTENER:
2266 /* pollin means a client has connected to us then */
2269 if (!(pollfd->revents & LWS_POLLIN) || !(pollfd->events & LWS_POLLIN))
2272 #ifdef LWS_OPENSSL_SUPPORT
2274 * can we really accept it, with regards to SSL limit?
2275 * another vhost may also have had POLLIN on his listener this
2276 * round and used it up already
2279 if (wsi->vhost->use_ssl &&
2280 context->simultaneous_ssl_restriction &&
2281 context->simultaneous_ssl ==
2282 context->simultaneous_ssl_restriction)
2283 /* no... ignore it, he won't come again until we are
2284 * below the simultaneous_ssl_restriction limit and
2285 * POLLIN is enabled on him again
2289 /* listen socket got an unencrypted connection... */
2291 clilen = sizeof(cli_addr);
2292 lws_latency_pre(context, wsi);
2293 accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
2295 lws_latency(context, wsi, "listener accept", accept_fd,
2297 if (accept_fd < 0) {
2298 if (LWS_ERRNO == LWS_EAGAIN ||
2299 LWS_ERRNO == LWS_EWOULDBLOCK) {
2300 // lwsl_err("accept asks to try again\n");
2303 lwsl_err("ERROR on accept: %s\n", strerror(LWS_ERRNO));
2307 lws_plat_set_socket_options(wsi->vhost, accept_fd);
2309 #if defined(LWS_USE_IPV6)
2310 lwsl_debug("accepted new conn port %u on fd=%d\n",
2311 ((cli_addr.ss_family == AF_INET6) ?
2312 ntohs(((struct sockaddr_in6 *) &cli_addr)->sin6_port) :
2313 ntohs(((struct sockaddr_in *) &cli_addr)->sin_port)),
2316 lwsl_debug("accepted new conn port %u on fd=%d\n",
2317 ntohs(((struct sockaddr_in *) &cli_addr)->sin_port),
2322 /* not very beautiful... */
2323 accept_fd = (lws_sockfd_type)pollfd;
2326 * look at who we connected to and give user code a chance
2327 * to reject based on client IP. There's no protocol selected
2328 * yet so we issue this to protocols[0]
2330 if ((wsi->vhost->protocols[0].callback)(wsi,
2331 LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
2332 NULL, (void *)(long)accept_fd, 0)) {
2333 lwsl_debug("Callback denied network connection\n");
2334 compatible_close(accept_fd);
2338 if (!(wsi->vhost->options & LWS_SERVER_OPTION_ONLY_RAW))
2339 opts |= LWS_ADOPT_HTTP;
2341 fd.sockfd = accept_fd;
2342 if (!lws_adopt_descriptor_vhost(wsi->vhost, opts, fd,
2344 /* already closed cleanly as necessary */
2348 } while (pt->fds_count < context->fd_limit_per_thread - 1 &&
2349 lws_poll_listen_fd(&pt->fds[wsi->position_in_fds_table]) > 0);
2357 if (!lws_server_socket_service_ssl(wsi, accept_fd))
2361 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
2367 lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
2368 const char *other_headers, int other_headers_len)
2370 static const char * const intermediates[] = { "private", "public" };
2371 struct lws_context *context = lws_get_context(wsi);
2372 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
2373 #if defined(LWS_WITH_RANGES)
2374 struct lws_range_parsing *rp = &wsi->u.http.range;
2376 char cache_control[50], *cc = "no-store";
2377 unsigned char *response = pt->serv_buf + LWS_PRE;
2378 unsigned char *p = response;
2379 unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
2380 lws_filepos_t computed_total_content_length;
2381 int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
2382 lws_fop_flags_t fflags = LWS_O_RDONLY;
2383 #if defined(LWS_WITH_RANGES)
2386 const struct lws_plat_file_ops *fops;
2390 * We either call the platform fops .open with first arg platform fops,
2391 * or we call fops_zip .open with first arg platform fops, and fops_zip
2392 * open will decide whether to switch to fops_zip or stay with fops_def.
2394 * If wsi->u.http.fop_fd is already set, the caller already opened it
2396 if (!wsi->u.http.fop_fd) {
2397 fops = lws_vfs_select_fops(wsi->context->fops, file, &vpath);
2398 fflags |= lws_vfs_prepare_flags(wsi);
2399 wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
2400 file, vpath, &fflags);
2401 if (!wsi->u.http.fop_fd) {
2402 lwsl_err("Unable to open '%s'\n", file);
2407 wsi->u.http.filelen = lws_vfs_get_length(wsi->u.http.fop_fd);
2408 computed_total_content_length = wsi->u.http.filelen;
2410 #if defined(LWS_WITH_RANGES)
2411 ranges = lws_ranges_init(wsi, rp, wsi->u.http.filelen);
2413 lwsl_debug("Range count %d\n", ranges);
2416 * 1 range -> 206 + Content-Type: normal; Content-Range;
2417 * more -> 206 + Content-Type: multipart/byteranges
2418 * Repeat the true Content-Type in each multipart header
2419 * along with Content-Range
2422 /* it means he expressed a range in Range:, but it was illegal */
2423 lws_return_http_status(wsi, HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, NULL);
2424 if (lws_http_transaction_completed(wsi))
2425 return -1; /* <0 means just hang up */
2427 lws_vfs_file_close(&wsi->u.http.fop_fd);
2429 return 0; /* == 0 means we dealt with the transaction complete */
2432 n = HTTP_STATUS_PARTIAL_CONTENT;
2435 if (lws_add_http_header_status(wsi, n, &p, end))
2438 if ((wsi->u.http.fop_fd->flags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
2439 LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
2440 (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
2441 if (lws_add_http_header_by_token(wsi,
2442 WSI_TOKEN_HTTP_CONTENT_ENCODING,
2443 (unsigned char *)"gzip", 4, &p, end))
2445 lwsl_info("file is being provided in gzip\n");
2448 #if defined(LWS_WITH_RANGES)
2449 if (ranges < 2 && content_type && content_type[0])
2450 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2451 (unsigned char *)content_type,
2452 strlen(content_type), &p, end))
2455 if (ranges >= 2) { /* multipart byteranges */
2456 strncpy(wsi->u.http.multipart_content_type, content_type,
2457 sizeof(wsi->u.http.multipart_content_type) - 1);
2458 wsi->u.http.multipart_content_type[
2459 sizeof(wsi->u.http.multipart_content_type) - 1] = '\0';
2460 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
2461 (unsigned char *)"multipart/byteranges; boundary=_lws",
2466 * our overall content length has to include
2468 * - (n + 1) x "_lws\r\n"
2469 * - n x Content-Type: xxx/xxx\r\n
2470 * - n x Content-Range: bytes xxx-yyy/zzz\r\n
2472 * - the actual payloads (aggregated in rp->agg)
2474 * Precompute it for the main response header
2477 computed_total_content_length = (lws_filepos_t)rp->agg +
2478 6 /* final _lws\r\n */;
2480 lws_ranges_reset(rp);
2481 while (lws_ranges_next(rp)) {
2482 n = lws_snprintf(cache_control, sizeof(cache_control),
2483 "bytes %llu-%llu/%llu",
2484 rp->start, rp->end, rp->extent);
2486 computed_total_content_length +=
2487 6 /* header _lws\r\n */ +
2488 14 + strlen(content_type) + 2 + /* Content-Type: xxx/xxx\r\n */
2489 15 + n + 2 + /* Content-Range: xxxx\r\n */
2493 lws_ranges_reset(rp);
2494 lws_ranges_next(rp);
2498 computed_total_content_length = (lws_filepos_t)rp->agg;
2499 n = lws_snprintf(cache_control, sizeof(cache_control), "bytes %llu-%llu/%llu",
2500 rp->start, rp->end, rp->extent);
2502 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_RANGE,
2503 (unsigned char *)cache_control,
2508 wsi->u.http.range.inside = 0;
2510 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ACCEPT_RANGES,
2511 (unsigned char *)"bytes", 5, &p, end))
2515 if (!wsi->sending_chunked) {
2516 if (lws_add_http_header_content_length(wsi,
2517 computed_total_content_length,
2521 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING,
2522 (unsigned char *)"chunked",
2527 if (wsi->cache_secs && wsi->cache_reuse) {
2528 if (wsi->cache_revalidate) {
2530 cclen = sprintf(cache_control, "%s max-age: %u",
2531 intermediates[wsi->cache_intermediaries],
2539 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CACHE_CONTROL,
2540 (unsigned char *)cc, cclen, &p, end))
2543 if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE)
2544 if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
2545 (unsigned char *)"keep-alive", 10, &p, end))
2548 if (other_headers) {
2549 if ((end - p) < other_headers_len)
2551 memcpy(p, other_headers, other_headers_len);
2552 p += other_headers_len;
2555 if (lws_finalize_http_header(wsi, &p, end))
2558 ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
2559 if (ret != (p - response)) {
2560 lwsl_err("_write returned %d from %ld\n", ret, (long)(p - response));
2564 wsi->u.http.filepos = 0;
2565 wsi->state = LWSS_HTTP_ISSUING_FILE;
2567 return lws_serve_http_file_fragment(wsi);
2571 lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
2575 lwsl_parser("%s: received %d byte packet\n", __func__, (int)len);
2577 lwsl_hexdump(*buf, len);
2580 /* let the rx protocol state machine have as much as it needs */
2584 * we were accepting input but now we stopped doing so
2586 if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
2587 lws_rxflow_cache(wsi, *buf, 0, len);
2588 lwsl_parser("%s: cached %ld\n", __func__, (long)len);
2592 if (wsi->u.ws.rx_draining_ext) {
2593 // lwsl_notice("draining with 0\n");
2594 m = lws_rx_sm(wsi, 0);
2600 /* account for what we're using in rxflow buffer */
2601 if (wsi->rxflow_buffer)
2604 /* consume payload bytes efficiently */
2606 wsi->lws_rx_parse_state ==
2607 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
2608 m = lws_payload_until_length_exhausted(wsi, buf, &len);
2609 if (wsi->rxflow_buffer)
2610 wsi->rxflow_pos += m;
2613 /* process the byte */
2614 m = lws_rx_sm(wsi, *(*buf)++);
2620 lwsl_parser("%s: exit with %d unused\n", __func__, (int)len);
2626 lws_server_get_canonical_hostname(struct lws_context *context,
2627 struct lws_context_creation_info *info)
2629 if (lws_check_opt(info->options, LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME))
2631 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
2632 /* find canonical hostname */
2633 gethostname((char *)context->canonical_hostname,
2634 sizeof(context->canonical_hostname) - 1);
2636 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
2642 #define LWS_MAX_ELEM_NAME 32
2644 enum urldecode_stateful {
2658 static const char * const mp_hdr[] = {
2659 "content-disposition: ",
2664 typedef int (*lws_urldecode_stateful_cb)(void *data,
2665 const char *name, char **buf, int len, int final);
2667 struct lws_urldecode_stateful {
2670 char name[LWS_MAX_ELEM_NAME];
2671 char temp[LWS_MAX_ELEM_NAME];
2672 char content_type[32];
2673 char content_disp[32];
2674 char content_disp_filename[256];
2675 char mime_boundary[128];
2682 unsigned int multipart_form_data:1;
2683 unsigned int inside_quote:1;
2684 unsigned int subname:1;
2685 unsigned int boundary_real_crlf:1;
2687 enum urldecode_stateful state;
2689 lws_urldecode_stateful_cb output;
2692 static struct lws_urldecode_stateful *
2693 lws_urldecode_s_create(struct lws *wsi, char *out, int out_len, void *data,
2694 lws_urldecode_stateful_cb output)
2696 struct lws_urldecode_stateful *s = lws_zalloc(sizeof(*s));
2704 s->out_len = out_len;
2713 if (lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_CONTENT_TYPE) > 0) {
2714 /* multipart/form-data; boundary=----WebKitFormBoundarycc7YgAPEIHvgE9Bf */
2716 if (!strncmp(buf, "multipart/form-data", 19)) {
2717 s->multipart_form_data = 1;
2718 s->state = MT_LOOK_BOUND_IN;
2720 p = strstr(buf, "boundary=");
2723 s->mime_boundary[m++] = '\x0d';
2724 s->mime_boundary[m++] = '\x0a';
2725 s->mime_boundary[m++] = '-';
2726 s->mime_boundary[m++] = '-';
2727 while (m < sizeof(s->mime_boundary) - 1 &&
2729 s->mime_boundary[m++] = *p++;
2731 s->mime_boundary[m] = '\0';
2733 lwsl_notice("boundary '%s'\n", s->mime_boundary);
2742 lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int len)
2745 char c, was_end = 0;
2748 if (s->pos == s->out_len - s->mp - 1) {
2749 if (s->output(s->data, s->name, &s->out, s->pos, 0))
2757 /* states for url arg style */
2760 s->inside_quote = 0;
2762 s->name[s->pos] = '\0';
2769 s->name[s->pos] = '\0';
2770 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2777 if (s->pos >= sizeof(s->name) - 1) {
2778 lwsl_notice("Name too long\n");
2781 s->name[s->pos++] = *in++;
2790 s->out[s->pos] = '\0';
2791 if (s->output(s->data, s->name, &s->out, s->pos, 1))
2800 s->out[s->pos++] = ' ';
2803 s->out[s->pos++] = *in++;
2806 n = char_to_hex(*in);
2816 n = char_to_hex(*in);
2821 s->out[s->pos++] = s->sum | n;
2826 /* states for multipart / mime style */
2828 case MT_LOOK_BOUND_IN:
2830 if (*in == s->mime_boundary[s->mp] &&
2831 s->mime_boundary[s->mp]) {
2834 if (!s->mime_boundary[s->mp]) {
2836 s->state = MT_IGNORE1;
2838 if (s->pos || was_end)
2839 if (s->output(s->data, s->name,
2840 &s->out, s->pos, 1))
2845 s->content_disp[0] = '\0';
2847 s->content_disp_filename[0] = '\0';
2848 s->boundary_real_crlf = 1;
2854 if (!s->boundary_real_crlf)
2857 memcpy(s->out + s->pos, s->mime_boundary + n, s->mp - n);
2860 goto retry_as_first;
2863 s->out[s->pos++] = *in;
2871 if (c >= 'A' && c <= 'Z')
2873 for (n = 0; n < ARRAY_SIZE(mp_hdr); n++)
2874 if (c == mp_hdr[n][s->mp]) {
2888 if (mp_hdr[hit][s->mp])
2896 s->state = MT_LOOK_BOUND_IN;
2898 s->state += hit + 1;
2902 /* form-data; name="file"; filename="t.txt" */
2904 if (*in == '\x0d') {
2905 // lwsl_notice("disp: '%s', '%s', '%s'\n",
2906 // s->content_disp, s->name,
2907 // s->content_disp_filename);
2909 if (s->content_disp_filename[0])
2910 if (s->output(s->data, s->name,
2911 &s->out, s->pos, LWS_UFS_OPEN))
2913 s->state = MT_IGNORE2;
2924 s->inside_quote ^= 1;
2930 s->temp[s->mp] = '\0';
2935 if (s->mp < sizeof(s->temp) - 1 &&
2936 (*in != ' ' || s->inside_quote))
2937 s->temp[s->mp++] = *in;
2942 if (s->mp < sizeof(s->content_disp) - 1)
2943 s->content_disp[s->mp++] = *in;
2944 s->content_disp[s->mp] = '\0';
2948 if (!strcmp(s->temp, "name")) {
2949 if (s->mp < sizeof(s->name) - 1)
2950 s->name[s->mp++] = *in;
2951 s->name[s->mp] = '\0';
2955 if (!strcmp(s->temp, "filename")) {
2956 if (s->mp < sizeof(s->content_disp_filename) - 1)
2957 s->content_disp_filename[s->mp++] = *in;
2958 s->content_disp_filename[s->mp] = '\0';
2967 s->state = MT_IGNORE2;
2969 if (s->mp < sizeof(s->content_type) - 1)
2970 s->content_type[s->mp++] = *in;
2971 s->content_type[s->mp] = '\0';
2978 s->state = MT_IGNORE2;
2985 s->state = MT_HNAME;
2995 lws_urldecode_s_destroy(struct lws_urldecode_stateful *s)
2999 if (s->state != US_IDLE)
3003 if (s->output(s->data, s->name, &s->out, s->pos, 1))
3012 struct lws_urldecode_stateful *s;
3013 lws_spa_fileupload_cb opt_cb;
3014 const char * const *param_names;
3028 lws_urldecode_spa_lookup(struct lws_spa *spa,
3033 for (n = 0; n < spa->count_params; n++)
3034 if (!strcmp(spa->param_names[n], name))
3041 lws_urldecode_spa_cb(void *data, const char *name, char **buf, int len,
3044 struct lws_spa *spa =
3045 (struct lws_spa *)data;
3048 if (spa->s->content_disp_filename[0]) {
3050 n = spa->opt_cb(spa->opt_data, name,
3051 spa->s->content_disp_filename,
3059 n = lws_urldecode_spa_lookup(spa, name);
3061 if (n == -1 || !len) /* unrecognized */
3064 if (!spa->params[n])
3065 spa->params[n] = *buf;
3067 if ((*buf) + len >= spa->end) {
3068 lwsl_notice("%s: exceeded storage\n", __func__);
3072 spa->param_length[n] += len;
3074 /* move it on inside storage */
3078 spa->s->out_len -= len + 1;
3083 LWS_VISIBLE LWS_EXTERN struct lws_spa *
3084 lws_spa_create(struct lws *wsi, const char * const *param_names,
3085 int count_params, int max_storage,
3086 lws_spa_fileupload_cb opt_cb, void *opt_data)
3088 struct lws_spa *spa = lws_zalloc(sizeof(*spa));
3093 spa->param_names = param_names;
3094 spa->count_params = count_params;
3095 spa->max_storage = max_storage;
3096 spa->opt_cb = opt_cb;
3097 spa->opt_data = opt_data;
3099 spa->storage = lws_malloc(max_storage);
3102 spa->end = spa->storage + max_storage - 1;
3104 spa->params = lws_zalloc(sizeof(char *) * count_params);
3108 spa->s = lws_urldecode_s_create(wsi, spa->storage, max_storage, spa,
3109 lws_urldecode_spa_cb);
3113 spa->param_length = lws_zalloc(sizeof(int) * count_params);
3114 if (!spa->param_length)
3117 lwsl_info("%s: Created SPA %p\n", __func__, spa);
3122 lws_urldecode_s_destroy(spa->s);
3124 lws_free(spa->params);
3126 lws_free(spa->storage);
3133 LWS_VISIBLE LWS_EXTERN int
3134 lws_spa_process(struct lws_spa *ludspa, const char *in, int len)
3137 lwsl_err("%s: NULL spa\n", __func__);
3140 /* we reject any junk after the last part arrived and we finalized */
3141 if (ludspa->finalized)
3144 return lws_urldecode_s_process(ludspa->s, in, len);
3147 LWS_VISIBLE LWS_EXTERN int
3148 lws_spa_get_length(struct lws_spa *ludspa, int n)
3150 if (n >= ludspa->count_params)
3153 return ludspa->param_length[n];
3156 LWS_VISIBLE LWS_EXTERN const char *
3157 lws_spa_get_string(struct lws_spa *ludspa, int n)
3159 if (n >= ludspa->count_params)
3162 return ludspa->params[n];
3165 LWS_VISIBLE LWS_EXTERN int
3166 lws_spa_finalize(struct lws_spa *spa)
3169 lws_urldecode_s_destroy(spa->s);
3178 LWS_VISIBLE LWS_EXTERN int
3179 lws_spa_destroy(struct lws_spa *spa)
3183 lwsl_notice("%s: destroy spa %p\n", __func__, spa);
3186 lws_urldecode_s_destroy(spa->s);
3188 lwsl_debug("%s %p %p %p %p\n", __func__,
3195 lws_free(spa->param_length);
3196 lws_free(spa->params);
3197 lws_free(spa->storage);
3204 LWS_VISIBLE LWS_EXTERN int
3205 lws_spa_destroy(struct lws_spa *spa)
3209 lwsl_info("%s: destroy spa %p\n", __func__, spa);
3212 lws_urldecode_s_destroy(spa->s);
3214 lwsl_debug("%s\n", __func__);
3216 lws_free(spa->param_length);
3217 lws_free(spa->params);
3218 lws_free(spa->storage);
3224 LWS_VISIBLE LWS_EXTERN int
3225 lws_chunked_html_process(struct lws_process_html_args *args,
3226 struct lws_process_html_state *s)
3228 char *sp, buffer[32];
3232 /* do replacements */
3234 old_len = args->len;
3237 while (sp < args->p + old_len) {
3239 if (args->len + 7 >= args->max_len) {
3240 lwsl_err("Used up interpret padding\n");
3244 if ((!s->pos && *sp == '$') || s->pos) {
3245 int hits = 0, hit = 0;
3249 s->swallow[s->pos++] = *sp;
3250 if (s->pos == sizeof(s->swallow) - 1)
3252 for (n = 0; n < s->count_vars; n++)
3253 if (!strncmp(s->swallow, s->vars[n], s->pos)) {
3259 s->swallow[s->pos] = '\0';
3260 memcpy(s->start, s->swallow, s->pos);
3266 if (hits == 1 && s->pos == strlen(s->vars[hit])) {
3267 pc = s->replace(s->data, hit);
3271 s->swallow[s->pos] = '\0';
3273 memmove(s->start + n,
3275 old_len - (sp - args->p));
3276 old_len += (n - s->pos) + 1;
3278 memcpy(s->start, pc, n);
3292 /* no space left for final chunk trailer */
3293 if (args->final && args->len + 7 >= args->max_len)
3296 n = sprintf(buffer, "%X\x0d\x0a", args->len);
3299 memcpy(args->p, buffer, n);
3303 sp = args->p + args->len;
3313 sp = args->p + args->len;