win32: enable 64-bit file lengths
[platform/upstream/libwebsockets.git] / lib / server.c
index 62832a4..54e974a 100644 (file)
@@ -87,10 +87,6 @@ lws_context_init_server(struct lws_context_creation_info *info,
 #if defined(LWS_WITH_ESP8266)
        sockfd = esp8266_create_tcp_listen_socket(vhost);
        if (!lws_sockfd_valid(sockfd)) {
-
-#else
-       sockfd = mbed3_create_tcp_stream_socket();
-       if (!lws_sockfd_valid(sockfd)) {
 #endif
 #endif
                lwsl_err("ERROR opening socket\n");
@@ -146,7 +142,7 @@ lws_context_init_server(struct lws_context_creation_info *info,
                goto bail;
        }
        wsi->context = vhost->context;
-       wsi->sock = sockfd;
+       wsi->desc.sockfd = sockfd;
        wsi->mode = LWSCM_SERVER_LISTENER;
        wsi->protocol = vhost->protocols;
        wsi->tsi = m;
@@ -165,7 +161,7 @@ lws_context_init_server(struct lws_context_creation_info *info,
        vhost->lserv_wsi = wsi;
 
 #if LWS_POSIX
-       n = listen(wsi->sock, LWS_SOMAXCONN);
+       n = listen(wsi->desc.sockfd, LWS_SOMAXCONN);
        if (n < 0) {
                lwsl_err("listen failed with error %d\n", LWS_ERRNO);
                vhost->lserv_wsi = NULL;
@@ -176,9 +172,7 @@ lws_context_init_server(struct lws_context_creation_info *info,
        } /* for each thread able to independently listen */
 #else
 #if defined(LWS_WITH_ESP8266)
-       esp8266_tcp_stream_bind(wsi->sock, info->port, wsi);
-#else
-       mbed3_tcp_stream_bind(wsi->sock, info->port, wsi);
+       esp8266_tcp_stream_bind(wsi->desc.sockfd, info->port, wsi);
 #endif
 #endif
        if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
@@ -216,7 +210,7 @@ lws_select_vhost(struct lws_context *context, int port, const char *servername)
        if (p)
                colon = p - servername;
 
-       /* first try exact matches */
+       /* Priotity 1: first try exact matches */
 
        while (vhost) {
                if (port == vhost->listen_port &&
@@ -228,7 +222,7 @@ lws_select_vhost(struct lws_context *context, int port, const char *servername)
        }
 
        /*
-        * if no exact matches, try matching *.vhost-name
+        * Priority 2: if no exact matches, try matching *.vhost-name
         * unintentional matches are possible but resolve to x.com for *.x.com
         * which is reasonable.  If exact match exists we already chose it and
         * never reach here.  SSL will still fail it if the cert doesn't allow
@@ -249,6 +243,20 @@ lws_select_vhost(struct lws_context *context, int port, const char *servername)
                vhost = vhost->vhost_next;
        }
 
+       /* Priority 3: match the first vhost on our port */
+
+       vhost = context->vhost_list;
+       while (vhost) {
+               if (port == vhost->listen_port) {
+                       lwsl_info("vhost match to %s based on port %d\n",
+                                       vhost->name, port);
+                       return vhost;
+               }
+               vhost = vhost->vhost_next;
+       }
+
+       /* no match */
+
        return NULL;
 }
 
@@ -300,6 +308,9 @@ lws_get_mimetype(const char *file, const struct lws_http_mount *m)
        if (!strcmp(&file[n - 4], ".ttf"))
                return "application/x-font-ttf";
 
+       if (!strcmp(&file[n - 4], ".otf"))
+               return "application/font-woff";
+
        if (!strcmp(&file[n - 5], ".woff"))
                return "application/font-woff";
 
@@ -318,6 +329,22 @@ lws_get_mimetype(const char *file, const struct lws_http_mount *m)
 
        return NULL;
 }
+static lws_fop_flags_t
+lws_vfs_prepare_flags(struct lws *wsi)
+{
+       lws_fop_flags_t f = 0;
+
+       if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
+               return f;
+
+       if (strstr(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING),
+                  "gzip")) {
+               lwsl_info("client indicates GZIP is acceptable\n");
+               f |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
+       }
+
+       return f;
+}
 
 static int
 lws_http_serve(struct lws *wsi, char *uri, const char *origin,
@@ -326,31 +353,76 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
        const struct lws_protocol_vhost_options *pvo = m->interpret;
        struct lws_process_html_args args;
        const char *mimetype;
-#if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32)
+#if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
+       const struct lws_plat_file_ops *fops;
+       const char *vpath;
+       lws_fop_flags_t fflags = LWS_O_RDONLY;
+#if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
+       struct _stat32i64 st;
+#else
        struct stat st;
+#endif
        int spin = 0;
 #endif
        char path[256], sym[512];
        unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
        unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
-#if !defined(WIN32) && LWS_POSIX
+#if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
        size_t len;
 #endif
        int n;
 
        lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
 
-#if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32)
+#if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
+
+       fflags |= lws_vfs_prepare_flags(wsi);
+
        do {
                spin++;
+               fops = lws_vfs_select_fops(wsi->context->fops, path, &vpath);
+
+               if (wsi->u.http.fop_fd)
+                       lws_vfs_file_close(&wsi->u.http.fop_fd);
 
+               wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
+                                                       path, vpath, &fflags);
+               if (!wsi->u.http.fop_fd) {
+                       lwsl_err("Unable to open '%s'\n", path);
+
+                       return -1;
+               }
+
+               /* if it can't be statted, don't try */
+               if (fflags & LWS_FOP_FLAG_VIRTUAL)
+                       break;
+#if defined(LWS_WITH_ESP32)
+               break;
+#endif
+#if !defined(WIN32)
+               if (fstat(wsi->u.http.fop_fd->fd, &st)) {
+                       lwsl_info("unable to stat %s\n", path);
+                       goto bail;
+               }
+#else
+#if defined(LWS_HAVE__STAT32I64)
+               if (_stat32i64(path, &st)) {
+                       lwsl_info("unable to stat %s\n", path);
+                       goto bail;
+               }
+#else
                if (stat(path, &st)) {
                        lwsl_info("unable to stat %s\n", path);
                        goto bail;
                }
+#endif
+#endif
+
+               wsi->u.http.fop_fd->mod_time = (uint32_t)st.st_mtime;
+               fflags |= LWS_FOP_FLAG_MOD_TIME_VALID;
 
                lwsl_debug(" %s mode %d\n", path, S_IFMT & st.st_mode);
-#if !defined(WIN32) && LWS_POSIX
+#if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
                if ((S_IFMT & st.st_mode) == S_IFLNK) {
                        len = readlink(path, sym, sizeof(sym) - 1);
                        if (len) {
@@ -373,8 +445,9 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
        if (spin == 5)
                lwsl_err("symlink loop %s \n", path);
 
-       n = sprintf(sym, "%08lX%08lX", (unsigned long)st.st_size,
-                                  (unsigned long)st.st_mtime);
+       n = sprintf(sym, "%08lX%08lX",
+                   (unsigned long)lws_vfs_get_length(wsi->u.http.fop_fd),
+                   (unsigned long)lws_vfs_get_mod_time(wsi->u.http.fop_fd));
 
        /* disable ranges if IF_RANGE token invalid */
 
@@ -388,13 +461,15 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
                 * he thinks he has some version of it already,
                 * check if the tag matches
                 */
-               if (!strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
+               if (!strcmp(sym, lws_hdr_simple_ptr(wsi,
+                                       WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
 
                        lwsl_debug("%s: ETAG match %s %s\n", __func__,
                                   uri, origin);
 
                        /* we don't need to send the payload */
-                       if (lws_add_http_header_status(wsi, 304, &p, end))
+                       if (lws_add_http_header_status(wsi,
+                                       HTTP_STATUS_NOT_MODIFIED, &p, end))
                                return -1;
 
                        if (lws_add_http_header_by_token(wsi,
@@ -413,6 +488,8 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
                                return -1;
                        }
 
+                       lws_vfs_file_close(&wsi->u.http.fop_fd);
+
                        return lws_http_transaction_completed(wsi);
                }
        }
@@ -676,7 +753,7 @@ lws_http_action(struct lws *wsi)
                lws_hdr_copy(wsi, content_length_str,
                             sizeof(content_length_str) - 1,
                             WSI_TOKEN_HTTP_CONTENT_LENGTH);
-               wsi->u.http.content_length = atoi(content_length_str);
+               wsi->u.http.content_length = atoll(content_length_str);
        }
 
        if (wsi->http2_substream) {
@@ -995,7 +1072,6 @@ lws_http_action(struct lws *wsi)
                        NULL, /* replace with cgi path */
                        NULL
                };
-               unsigned char *p, *end, buffer[1024];
 
                lwsl_debug("%s: cgi\n", __func__);
                cmd[0] = hit->origin;
@@ -1010,17 +1086,6 @@ lws_http_action(struct lws *wsi)
                        lwsl_err("%s: cgi failed\n", __func__);
                        return -1;
                }
-               p = buffer + LWS_PRE;
-               end = p + sizeof(buffer) - LWS_PRE;
-
-               if (lws_add_http_header_status(wsi, 200, &p, end))
-                       return 1;
-               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
-                               (unsigned char *)"close", 5, &p, end))
-                       return 1;
-               n = lws_write(wsi, buffer + LWS_PRE,
-                             p - (buffer + LWS_PRE),
-                             LWS_WRITE_HTTP_HEADERS);
 
                goto deal_body;
        }
@@ -1099,42 +1164,17 @@ transaction_result_n:
 }
 
 int
-lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p)
-{
-//     if (wsi->protocol == p)
-//             return 0;
-
-       if (wsi->protocol)
-               wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_DROP_PROTOCOL,
-                                       wsi->user_space, NULL, 0);
-       if (!wsi->user_space_externally_allocated)
-               lws_free_set_NULL(wsi->user_space);
-
-       wsi->protocol = p;
-       if (!p)
-               return 0;
-
-       if (lws_ensure_user_space(wsi))
-               return 1;
-
-       if (wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_BIND_PROTOCOL,
-                                   wsi->user_space, NULL, 0))
-               return 1;
-
-       return 0;
-}
-
-
-int
 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
 {
+       int protocol_len, n = 0, hit, non_space_char_found = 0, m;
        struct lws_context *context = lws_get_context(wsi);
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        struct _lws_header_related hdr;
        struct allocated_headers *ah;
-       int protocol_len, n = 0, hit, non_space_char_found = 0;
+       unsigned char *obuf = *buf;
        char protocol_list[128];
        char protocol_name[64];
+       size_t olen = len;
        char *p;
 
        if (len >= 10000000) {
@@ -1156,7 +1196,38 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
                        goto bail_nuke_ah;
                }
 
-               if (lws_parse(wsi, *(*buf)++)) {
+               m = lws_parse(wsi, *(*buf)++);
+               if (m) {
+                       if (m == 2) {
+                               /*
+                                * we are transitioning from http with
+                                * an AH, to raw.  Drop the ah and set
+                                * the mode.
+                                */
+raw_transition:
+                               lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+                               lws_bind_protocol(wsi, &wsi->vhost->protocols[
+                                                       wsi->vhost->
+                                                       raw_protocol_index]);
+                               lwsl_info("transition to raw vh %s prot %d\n",
+                                         wsi->vhost->name,
+                                         wsi->vhost->raw_protocol_index);
+                               if ((wsi->protocol->callback)(wsi,
+                                               LWS_CALLBACK_RAW_ADOPT,
+                                               wsi->user_space, NULL, 0))
+                                       goto bail_nuke_ah;
+
+                               wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
+                               lws_union_transition(wsi, LWSCM_RAW);
+                               lws_header_table_detach(wsi, 1);
+
+                               if (m == 2 && (wsi->protocol->callback)(wsi,
+                                               LWS_CALLBACK_RAW_RX,
+                                               wsi->user_space, obuf, olen))
+                                       return 1;
+
+                               return 0;
+                       }
                        lwsl_info("lws_parse failed\n");
                        goto bail_nuke_ah;
                }
@@ -1214,13 +1285,8 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
 
                if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
                        lwsl_info("Changing to RAW mode\n");
-                       lws_union_transition(wsi, LWSCM_RAW);
-                       if (!wsi->more_rx_waiting) {
-                               wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
-
-                               //lwsl_notice("%p: dropping ah EST\n", wsi);
-                               lws_header_table_detach(wsi, 1);
-                       }
+                       m = 0;
+                       goto raw_transition;
                }
 
                wsi->mode = LWSCM_PRE_WS_SERVING_ACCEPT;
@@ -1255,7 +1321,7 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
 
                lws_union_transition(wsi, LWSCM_HTTP_SERVING_ACCEPTED);
                wsi->state = LWSS_HTTP;
-               wsi->u.http.fd = LWS_INVALID_FILE;
+               wsi->u.http.fop_fd = NULL;
 
                /* expose it at the same offset as u.hdr */
                wsi->u.http.ah = ah;
@@ -1392,7 +1458,7 @@ upgrade_ws:
                         */
                        lwsl_info("defaulting to prot handler %d\n",
                                wsi->vhost->default_protocol_index);
-                       n = 0;
+                       n = wsi->vhost->default_protocol_index;
                        wsi->protocol = &wsi->vhost->protocols[
                                      (int)wsi->vhost->default_protocol_index];
                }
@@ -1433,30 +1499,7 @@ upgrade_ws:
                        goto bail_nuke_ah;
                }
 
-               /*
-                * stitch protocol choice into the vh protocol linked list
-                * We always insert ourselves at the start of the list
-                *
-                * X <-> B
-                * X <-> pAn <-> pB
-                */
-               //lwsl_err("%s: pre insert vhost start wsi %p, that wsi prev == %p\n",
-               //              __func__,
-               //              wsi->vhost->same_vh_protocol_list[n],
-               //              wsi->same_vh_protocol_prev);
-               wsi->same_vh_protocol_prev = /* guy who points to us */
-                       &wsi->vhost->same_vh_protocol_list[n];
-               wsi->same_vh_protocol_next = /* old first guy is our next */
-                               wsi->vhost->same_vh_protocol_list[n];
-               /* we become the new first guy */
-               wsi->vhost->same_vh_protocol_list[n] = wsi;
-
-               if (wsi->same_vh_protocol_next)
-                       /* old first guy points back to us now */
-                       wsi->same_vh_protocol_next->same_vh_protocol_prev =
-                                       &wsi->same_vh_protocol_next;
-
-
+               lws_same_vh_protocol_insert(wsi, n);
 
                /* we are upgrading to ws, so http/1.1 and keepalive +
                 * pipelined header considerations about keeping the ah around
@@ -1508,7 +1551,7 @@ upgrade_ws:
                wsi->u.ws.rx_ubuf_alloc = n;
                lwsl_debug("Allocating RX buffer %d\n", n);
 #if LWS_POSIX && !defined(LWS_WITH_ESP32)
-               if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF,
+               if (setsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_SNDBUF,
                               (const char *)&n, sizeof n)) {
                        lwsl_warn("Failed to set SNDBUF to %d", n);
                        return 1;
@@ -1617,7 +1660,7 @@ lws_create_new_server_wsi(struct lws_vhost *vhost)
        new_wsi->protocol = vhost->protocols;
        new_wsi->user_space = NULL;
        new_wsi->ietf_spec_revision = 0;
-       new_wsi->sock = LWS_SOCK_INVALID;
+       new_wsi->desc.sockfd = LWS_SOCK_INVALID;
        vhost->context->count_wsi_allocated++;
 
        /*
@@ -1680,6 +1723,21 @@ lws_http_transaction_completed(struct lws *wsi)
                if (!wsi->more_rx_waiting) {
                        wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
                        lws_header_table_detach(wsi, 1);
+#ifdef LWS_OPENSSL_SUPPORT
+                       /*
+                        * additionally... if we are hogging an SSL instance
+                        * with no pending pipelined headers (or ah now), and
+                        * SSL is scarce, drop this connection without waiting
+                        */
+
+                       if (wsi->vhost->use_ssl &&
+                           wsi->context->simultaneous_ssl_restriction &&
+                           wsi->context->simultaneous_ssl ==
+                                  wsi->context->simultaneous_ssl_restriction) {
+                               lwsl_info("%s: simultaneous_ssl_restriction and nothing pipelined\n", __func__);
+                               return 1;
+                       }
+#endif
                } else
                        lws_header_table_reset(wsi, 1);
        }
@@ -1692,35 +1750,76 @@ lws_http_transaction_completed(struct lws *wsi)
        return 0;
 }
 
+/* if not a socket, it's a raw, non-ssl file descriptor */
+
 LWS_VISIBLE struct lws *
-lws_adopt_socket_vhost2(struct lws_vhost *vh, lws_sockfd_type accept_fd,
-                       int allow_ssl, int raw)
+lws_adopt_descriptor_vhost(struct lws_vhost *vh, lws_adoption_type type,
+                          lws_sock_file_fd_type fd, const char *vh_prot_name,
+                          struct lws *parent)
 {
        struct lws_context *context = vh->context;
        struct lws *new_wsi = lws_create_new_server_wsi(vh);
-       int n;
+       struct lws_context_per_thread *pt;
+       int n, ssl = 0;
 
        if (!new_wsi) {
-               compatible_close(accept_fd);
+               if (type & LWS_ADOPT_SOCKET)
+                       compatible_close(fd.sockfd);
                return NULL;
        }
+       pt = &context->pt[(int)new_wsi->tsi];
+       lws_stats_atomic_bump(context, pt, LWSSTATS_C_CONNECTIONS, 1);
 
-       lwsl_debug("%s: new wsi %p, sockfd %d, cb %p\n", __func__, new_wsi,
-                  accept_fd, context->vhost_list->protocols[0].callback);
+       if (parent) {
+               new_wsi->parent = parent;
+               new_wsi->sibling_list = parent->child_list;
+               parent->child_list = new_wsi;
+       }
 
-       new_wsi->sock = accept_fd;
+       new_wsi->desc = fd;
 
-       /* the transport is accepted... give him time to negotiate */
-       lws_set_timeout(new_wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
-                       context->timeout_secs);
+       if (vh_prot_name) {
+               new_wsi->protocol = lws_vhost_name_to_protocol(new_wsi->vhost,
+                                                              vh_prot_name);
+               if (!new_wsi->protocol) {
+                       lwsl_err("Protocol %s not enabled on vhost %s\n",
+                                vh_prot_name, new_wsi->vhost->name);
+                       goto bail;
+               }
+               if (lws_ensure_user_space(new_wsi)) {
+                       lwsl_notice("OOM trying to get user_space\n");
+                       goto bail;
+               }
+       } else
+               if (type & LWS_ADOPT_HTTP) /* he will transition later */
+                       new_wsi->protocol =
+                               &vh->protocols[vh->default_protocol_index];
+               else { /* this is the only time he will transition */
+                       lws_bind_protocol(new_wsi,
+                               &vh->protocols[vh->raw_protocol_index]);
+                       lws_union_transition(new_wsi, LWSCM_RAW);
+               }
+
+       if (type & LWS_ADOPT_SOCKET) { /* socket desc */
+               lwsl_debug("%s: new wsi %p, sockfd %d\n", __func__, new_wsi,
+                          (int)(size_t)fd.sockfd);
+
+               if (type & LWS_ADOPT_HTTP)
+                       /* the transport is accepted...
+                        * give him time to negotiate */
+                       lws_set_timeout(new_wsi,
+                                       PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
+                                       context->timeout_secs);
 
 #if LWS_POSIX == 0
 #if defined(LWS_WITH_ESP8266)
-       esp8266_tcp_stream_accept(accept_fd, new_wsi);
-#else
-       mbed3_tcp_stream_accept(accept_fd, new_wsi);
+               esp8266_tcp_stream_accept(accept_fd, new_wsi);
 #endif
 #endif
+       } else /* file desc */
+               lwsl_debug("%s: new wsi %p, filefd %d\n", __func__, new_wsi,
+                          (int)(size_t)fd.filefd);
+
        /*
         * A new connection was accepted. Give the user a chance to
         * set properties of the newly created wsi. There's no protocol
@@ -1728,45 +1827,79 @@ lws_adopt_socket_vhost2(struct lws_vhost *vh, lws_sockfd_type accept_fd,
         * itself by default protocols[0]
         */
        n = LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED;
-       if (raw)
-               n = LWS_CALLBACK_RAW_ADOPT;
-       if ((context->vhost_list->protocols[vh->default_protocol_index].callback)(
-                       new_wsi, n, NULL, NULL, 0)) {
-               /* force us off the timeout list by hand */
-               lws_set_timeout(new_wsi, NO_PENDING_TIMEOUT, 0);
-               compatible_close(new_wsi->sock);
-               lws_free(new_wsi);
-               return NULL;
+       if (!(type & LWS_ADOPT_HTTP)) {
+               if (!(type & LWS_ADOPT_SOCKET))
+                       n = LWS_CALLBACK_RAW_ADOPT_FILE;
+               else
+                       n = LWS_CALLBACK_RAW_ADOPT;
        }
 
-       lws_libev_accept(new_wsi, new_wsi->sock);
-       lws_libuv_accept(new_wsi, new_wsi->sock);
-
-       if (!LWS_SSL_ENABLED(new_wsi->vhost) || allow_ssl == 0) {
-               if (raw)
-                       new_wsi->mode = LWSCM_RAW;
-               if (insert_wsi_socket_into_fds(context, new_wsi)) {
-                       lwsl_err("%s: fail inserting socket\n", __func__);
-                       goto fail;
+       if (!LWS_SSL_ENABLED(new_wsi->vhost) || !(type & LWS_ADOPT_ALLOW_SSL) ||
+           !(type & LWS_ADOPT_SOCKET)) {
+               /* non-SSL */
+               if (!(type & LWS_ADOPT_HTTP)) {
+                       if (!(type & LWS_ADOPT_SOCKET))
+                               new_wsi->mode = LWSCM_RAW_FILEDESC;
+                       else
+                               new_wsi->mode = LWSCM_RAW;
                }
        } else {
-               if (raw)
+               /* SSL */
+               if (!(type & LWS_ADOPT_HTTP))
                        new_wsi->mode = LWSCM_SSL_INIT_RAW;
                else
                        new_wsi->mode = LWSCM_SSL_INIT;
-               if (lws_server_socket_service_ssl(new_wsi, accept_fd)) {
+
+               ssl = 1;
+       }
+
+       lws_libev_accept(new_wsi, new_wsi->desc);
+       lws_libuv_accept(new_wsi, new_wsi->desc);
+       lws_libevent_accept(new_wsi, new_wsi->desc);
+
+       if (!ssl) {
+               if (insert_wsi_socket_into_fds(context, new_wsi)) {
+                       lwsl_err("%s: fail inserting socket\n", __func__);
+                       goto fail;
+               }
+       } else
+               if (lws_server_socket_service_ssl(new_wsi, fd.sockfd)) {
                        lwsl_err("%s: fail ssl negotiation\n", __func__);
                        goto fail;
                }
-       }
 
-       if (!lws_header_table_attach(new_wsi, 0))
-               lwsl_debug("Attached ah immediately\n");
+       /*
+        *  by deferring callback to this point, after insertion to fds,
+        * lws_callback_on_writable() can work from the callback
+        */
+       if ((new_wsi->protocol->callback)(
+                       new_wsi, n, new_wsi->user_space, NULL, 0))
+               goto fail;
+
+       if (type & LWS_ADOPT_HTTP) {
+               if (!lws_header_table_attach(new_wsi, 0)) {
+                       lwsl_debug("Attached ah immediately\n");
+               } else {
+                       lwsl_notice("%s: waiting for ah\n", __func__);
+               }
+       }
 
        return new_wsi;
 
 fail:
-       lws_close_free_wsi(new_wsi, LWS_CLOSE_STATUS_NOSTATUS);
+       if (type & LWS_ADOPT_SOCKET)
+               lws_close_free_wsi(new_wsi, LWS_CLOSE_STATUS_NOSTATUS);
+
+       return NULL;
+
+bail:
+       lwsl_notice("%s: exiting on bail\n", __func__);
+       if (parent)
+               parent->child_list = new_wsi->sibling_list;
+       if (new_wsi->user_space)
+               lws_free(new_wsi->user_space);
+       lws_free(new_wsi);
+       compatible_close(fd.sockfd);
 
        return NULL;
 }
@@ -1774,13 +1907,17 @@ fail:
 LWS_VISIBLE struct lws *
 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
 {
-       return lws_adopt_socket_vhost2(vh, accept_fd, 1, 0);
+       lws_sock_file_fd_type fd;
+
+       fd.sockfd = accept_fd;
+       return lws_adopt_descriptor_vhost(vh, LWS_ADOPT_SOCKET |
+                       LWS_ADOPT_HTTP | LWS_ADOPT_ALLOW_SSL, fd, NULL, NULL);
 }
 
 LWS_VISIBLE struct lws *
 lws_adopt_socket(struct lws_context *context, lws_sockfd_type accept_fd)
 {
-       return lws_adopt_socket_vhost2(context->vhost_list, accept_fd, 1, 0);
+       return lws_adopt_socket_vhost(context->vhost_list, accept_fd);
 }
 
 /* Common read-buffer adoption for lws_adopt_*_readbuf */
@@ -1880,8 +2017,10 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        lws_sockfd_type accept_fd = LWS_SOCK_INVALID;
        struct allocated_headers *ah;
+       lws_sock_file_fd_type fd;
+       int opts = LWS_ADOPT_SOCKET | LWS_ADOPT_ALLOW_SSL;
 #if LWS_POSIX
-       struct sockaddr_in cli_addr;
+       struct sockaddr_storage cli_addr;
        socklen_t clilen;
 #endif
        int n, len;
@@ -1893,6 +2032,7 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
        case LWSCM_HTTP_SERVING:
        case LWSCM_HTTP_SERVING_ACCEPTED:
        case LWSCM_HTTP2_SERVING:
+       case LWSCM_RAW:
 
                /* handle http headers coming in */
 
@@ -1935,15 +2075,15 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
 
                /* these states imply we MUST have an ah attached */
 
-               if (wsi->state == LWSS_HTTP ||
+               if (wsi->mode != LWSCM_RAW && (wsi->state == LWSS_HTTP ||
                    wsi->state == LWSS_HTTP_ISSUING_FILE ||
-                   wsi->state == LWSS_HTTP_HEADERS) {
+                   wsi->state == LWSS_HTTP_HEADERS)) {
                        if (!wsi->u.hdr.ah) {
                                
                                //lwsl_err("wsi %p: missing ah\n", wsi);
                                /* no autoservice beacuse we will do it next */
                                if (lws_header_table_attach(wsi, 0)) {
-                                       lwsl_err("wsi %p: failed to acquire ah\n", wsi);
+                                       lwsl_info("wsi %p: failed to acquire ah\n", wsi);
                                        goto try_pollout;
                                }
                        }
@@ -2007,7 +2147,7 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
 
                len = lws_ssl_capable_read(wsi, pt->serv_buf,
                                           context->pt_serv_buf_size);
-               lwsl_notice("%s: wsi %p read %d\r\n", __func__, wsi, len);
+               lwsl_debug("%s: wsi %p read %d\r\n", __func__, wsi, len);
                switch (len) {
                case 0:
                        lwsl_info("%s: read 0 len\n", __func__);
@@ -2026,10 +2166,10 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                                        wsi, LWS_CALLBACK_RAW_RX,
                                        wsi->user_space, pt->serv_buf, len);
                        if (n < 0) {
-                               lwsl_info("raw writeable_fail\n");
+                               lwsl_info("LWS_CALLBACK_RAW_RX_fail\n");
                                goto fail;
                        }
-                       break;
+                       goto try_pollout;
                }
 
                /* just ignore incoming if waiting for close */
@@ -2064,10 +2204,43 @@ try_pollout:
                        goto fail;
                }
 
+               if (wsi->mode == LWSCM_RAW) {
+                       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
+#if defined(LWS_WITH_STATS)
+                       {
+                               uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
+
+                               lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
+                               lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
+                               wsi->active_writable_req_us = 0;
+                       }
+#endif
+                       n = user_callback_handle_rxflow(wsi->protocol->callback,
+                                       wsi, LWS_CALLBACK_RAW_WRITEABLE,
+                                       wsi->user_space, NULL, 0);
+                       if (n < 0) {
+                               lwsl_info("writeable_fail\n");
+                               goto fail;
+                       }
+                       break;
+               }
+
                if (!wsi->hdr_parsing_completed)
                        break;
 
                if (wsi->state != LWSS_HTTP_ISSUING_FILE) {
+
+                       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB, 1);
+#if defined(LWS_WITH_STATS)
+                       {
+                               uint64_t ul = time_in_microseconds() - wsi->active_writable_req_us;
+
+                               lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_MS_WRITABLE_DELAY, ul);
+                               lws_stats_atomic_max(wsi->context, pt, LWSSTATS_MS_WORST_WRITABLE_DELAY, ul);
+                               wsi->active_writable_req_us = 0;
+                       }
+#endif
+
                        n = user_callback_handle_rxflow(wsi->protocol->callback,
                                        wsi, LWS_CALLBACK_HTTP_WRITEABLE,
                                        wsi->user_space, NULL, 0);
@@ -2096,6 +2269,23 @@ try_pollout:
                        if (!(pollfd->revents & LWS_POLLIN) || !(pollfd->events & LWS_POLLIN))
                                break;
 
+#ifdef LWS_OPENSSL_SUPPORT
+                       /*
+                        * can we really accept it, with regards to SSL limit?
+                        * another vhost may also have had POLLIN on his listener this
+                        * round and used it up already
+                        */
+
+                       if (wsi->vhost->use_ssl &&
+                           context->simultaneous_ssl_restriction &&
+                           context->simultaneous_ssl ==
+                                         context->simultaneous_ssl_restriction)
+                               /* no... ignore it, he won't come again until we are
+                                * below the simultaneous_ssl_restriction limit and
+                                * POLLIN is enabled on him again
+                                */
+                               break;
+#endif
                        /* listen socket got an unencrypted connection... */
 
                        clilen = sizeof(cli_addr);
@@ -2107,7 +2297,7 @@ try_pollout:
                        if (accept_fd < 0) {
                                if (LWS_ERRNO == LWS_EAGAIN ||
                                    LWS_ERRNO == LWS_EWOULDBLOCK) {
-                                       lwsl_err("accept asks to try again\n");
+//                                     lwsl_err("accept asks to try again\n");
                                        break;
                                }
                                lwsl_err("ERROR on accept: %s\n", strerror(LWS_ERRNO));
@@ -2116,8 +2306,17 @@ try_pollout:
 
                        lws_plat_set_socket_options(wsi->vhost, accept_fd);
 
-                       lwsl_debug("accepted new conn  port %u on fd=%d\n",
-                                         ntohs(cli_addr.sin_port), accept_fd);
+#if defined(LWS_USE_IPV6)
+                       lwsl_debug("accepted new conn port %u on fd=%d\n",
+                                         ((cli_addr.ss_family == AF_INET6) ?
+                                         ntohs(((struct sockaddr_in6 *) &cli_addr)->sin6_port) :
+                                         ntohs(((struct sockaddr_in *) &cli_addr)->sin_port)),
+                                         accept_fd);
+#else
+                       lwsl_debug("accepted new conn port %u on fd=%d\n",
+                                         ntohs(((struct sockaddr_in *) &cli_addr)->sin_port),
+                                         accept_fd);
+#endif
 
 #else
                        /* not very beautiful... */
@@ -2136,7 +2335,12 @@ try_pollout:
                                break;
                        }
 
-                       if (!lws_adopt_socket_vhost(wsi->vhost, accept_fd))
+                       if (!(wsi->vhost->options & LWS_SERVER_OPTION_ONLY_RAW))
+                               opts |= LWS_ADOPT_HTTP;
+
+                       fd.sockfd = accept_fd;
+                       if (!lws_adopt_descriptor_vhost(wsi->vhost, opts, fd,
+                                                       NULL, NULL))
                                /* already closed cleanly as necessary */
                                return 1;
 
@@ -2173,38 +2377,35 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
        unsigned char *response = pt->serv_buf + LWS_PRE;
        unsigned char *p = response;
        unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
-       unsigned long computed_total_content_length;
-       int ret = 0, cclen = 8, n = HTTP_STATUS_OK, fflags = O_RDONLY;
+       lws_filepos_t computed_total_content_length;
+       int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
+       lws_fop_flags_t fflags = LWS_O_RDONLY;
 #if defined(LWS_WITH_RANGES)
        int ranges;
 #endif
+       const struct lws_plat_file_ops *fops;
+       const char *vpath;
 
-       if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
-               if (strstr("gzip",  lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING)) &&
-                   strstr("deflate",  lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))) {
-                       lwsl_debug("client indicates GZIP is acceptable\n");
-                       fflags |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
-               }
-
-
-       wsi->u.http.fd = lws_plat_file_open(wsi, file, &wsi->u.http.filelen,
-                                           &fflags);
-       if (wsi->u.http.fd == LWS_INVALID_FILE) {
-               lwsl_err("Unable to open '%s'\n", file);
-
-               return -1;
-       }
-       computed_total_content_length = wsi->u.http.filelen;
+       /*
+        * We either call the platform fops .open with first arg platform fops,
+        * or we call fops_zip .open with first arg platform fops, and fops_zip
+        * open will decide whether to switch to fops_zip or stay with fops_def.
+        *
+        * If wsi->u.http.fop_fd is already set, the caller already opened it
+        */
+       if (!wsi->u.http.fop_fd) {
+               fops = lws_vfs_select_fops(wsi->context->fops, file, &vpath);
+               fflags |= lws_vfs_prepare_flags(wsi);
+               wsi->u.http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
+                                                       file, vpath, &fflags);
+               if (!wsi->u.http.fop_fd) {
+                       lwsl_err("Unable to open '%s'\n", file);
 
-       if ((fflags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
-                      LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
-           (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
-               if (lws_add_http_header_by_token(wsi,
-                       WSI_TOKEN_HTTP_CONTENT_ENCODING,
-                       (unsigned char *)"gzip, deflate", 13, &p, end))
                        return -1;
-               lwsl_debug("file is being provided in gzip\n");
+               }
        }
+       wsi->u.http.filelen = lws_vfs_get_length(wsi->u.http.fop_fd);
+       computed_total_content_length = wsi->u.http.filelen;
 
 #if defined(LWS_WITH_RANGES)
        ranges = lws_ranges_init(wsi, rp, wsi->u.http.filelen);
@@ -2223,6 +2424,8 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
                if (lws_http_transaction_completed(wsi))
                        return -1; /* <0 means just hang up */
 
+               lws_vfs_file_close(&wsi->u.http.fop_fd);
+
                return 0; /* == 0 means we dealt with the transaction complete */
        }
        if (ranges)
@@ -2232,6 +2435,16 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
        if (lws_add_http_header_status(wsi, n, &p, end))
                return -1;
 
+       if ((wsi->u.http.fop_fd->flags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
+                      LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
+           (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
+               if (lws_add_http_header_by_token(wsi,
+                       WSI_TOKEN_HTTP_CONTENT_ENCODING,
+                       (unsigned char *)"gzip", 4, &p, end))
+                       return -1;
+               lwsl_info("file is being provided in gzip\n");
+       }
+
 #if defined(LWS_WITH_RANGES)
        if (ranges < 2 && content_type && content_type[0])
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
@@ -2261,7 +2474,7 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
                 *  Precompute it for the main response header
                 */
 
-               computed_total_content_length = (unsigned long)rp->agg +
+               computed_total_content_length = (lws_filepos_t)rp->agg +
                                                6 /* final _lws\r\n */;
 
                lws_ranges_reset(rp);
@@ -2282,7 +2495,7 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
        }
 
        if (ranges == 1) {
-               computed_total_content_length = (unsigned long)rp->agg;
+               computed_total_content_length = (lws_filepos_t)rp->agg;
                n = lws_snprintf(cache_control, sizeof(cache_control), "bytes %llu-%llu/%llu",
                                rp->start, rp->end, rp->extent);
 
@@ -2377,6 +2590,7 @@ lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
                }
 
                if (wsi->u.ws.rx_draining_ext) {
+                       // lwsl_notice("draining with 0\n");
                        m = lws_rx_sm(wsi, 0);
                        if (m < 0)
                                return -1;
@@ -2388,7 +2602,8 @@ lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
                        wsi->rxflow_pos++;
 
                /* consume payload bytes efficiently */
-               if (wsi->lws_rx_parse_state ==
+               if (
+                   wsi->lws_rx_parse_state ==
                    LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
                        m = lws_payload_until_length_exhausted(wsi, buf, &len);
                        if (wsi->rxflow_buffer)
@@ -2413,7 +2628,7 @@ lws_server_get_canonical_hostname(struct lws_context *context,
 {
        if (lws_check_opt(info->options, LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME))
                return;
-#if LWS_POSIX
+#if LWS_POSIX && !defined(LWS_WITH_ESP32)
        /* find canonical hostname */
        gethostname((char *)context->canonical_hostname,
                    sizeof(context->canonical_hostname) - 1);
@@ -2805,6 +3020,8 @@ struct lws_spa {
        char *storage;
        char *end;
        int max_storage;
+
+       char finalized;
 };
 
 static int
@@ -2897,7 +3114,7 @@ lws_spa_create(struct lws *wsi, const char * const *param_names,
        if (!spa->param_length)
                goto bail5;
 
-       lwsl_notice("%s: Created SPA %p\n", __func__, spa);
+       lwsl_info("%s: Created SPA %p\n", __func__, spa);
 
        return spa;
 
@@ -2920,6 +3137,10 @@ lws_spa_process(struct lws_spa *ludspa, const char *in, int len)
                lwsl_err("%s: NULL spa\n", __func__);
                return -1;
        }
+       /* we reject any junk after the last part arrived and we finalized */
+       if (ludspa->finalized)
+               return 0;
+
        return lws_urldecode_s_process(ludspa->s, in, len);
 }
 
@@ -2949,6 +3170,8 @@ lws_spa_finalize(struct lws_spa *spa)
                spa->s = NULL;
        }
 
+       spa->finalized = 1;
+
        return 0;
 }
 
@@ -2962,7 +3185,12 @@ lws_spa_destroy(struct lws_spa *spa)
        if (spa->s)
                lws_urldecode_s_destroy(spa->s);
 
-       lwsl_debug("%s\n", __func__);
+       lwsl_debug("%s %p %p %p %p\n", __func__,
+                       spa->param_length,
+                       spa->params,
+                       spa->storage,
+                       spa
+                       );
 
        lws_free(spa->param_length);
        lws_free(spa->params);
@@ -2972,6 +3200,27 @@ lws_spa_destroy(struct lws_spa *spa)
        return n;
 }
 
+#if 0
+LWS_VISIBLE LWS_EXTERN int
+lws_spa_destroy(struct lws_spa *spa)
+{
+       int n = 0;
+
+       lwsl_info("%s: destroy spa %p\n", __func__, spa);
+
+       if (spa->s)
+               lws_urldecode_s_destroy(spa->s);
+
+       lwsl_debug("%s\n", __func__);
+
+       lws_free(spa->param_length);
+       lws_free(spa->params);
+       lws_free(spa->storage);
+       lws_free(spa);
+
+       return n;
+}
+#endif
 LWS_VISIBLE LWS_EXTERN int
 lws_chunked_html_process(struct lws_process_html_args *args,
                         struct lws_process_html_state *s)