transaction_completed: ignore if parsing not complete
[platform/upstream/libwebsockets.git] / lib / server.c
index c9cf9b2..9db2ec2 100644 (file)
 
 #include "private-libwebsockets.h"
 
+#if defined (LWS_WITH_ESP8266)
+#undef memcpy
+void *memcpy(void *dest, const void *src, size_t n)
+{
+       return ets_memcpy(dest, src, n);
+}
+#endif
+
 int
 lws_context_init_server(struct lws_context_creation_info *info,
                        struct lws_vhost *vhost)
 {
-#ifdef LWS_POSIX
+#if LWS_POSIX
        int n, opt = 1, limit = 1;
 #endif
        lws_sockfd_type sockfd;
@@ -34,9 +42,10 @@ lws_context_init_server(struct lws_context_creation_info *info,
        struct lws *wsi;
        int m = 0;
 
+       (void)opt;
        /* set up our external listening socket we serve on */
 
-       if (info->port == CONTEXT_PORT_NO_LISTEN)
+       if (info->port == CONTEXT_PORT_NO_LISTEN || info->port == CONTEXT_PORT_NO_LISTEN_SERVER)
                return 0;
 
        vh = vhost->context->vhost_list;
@@ -56,6 +65,7 @@ lws_context_init_server(struct lws_context_creation_info *info,
        }
 
 #if LWS_POSIX
+       (void)n;
 #if defined(__linux__)
        limit = vhost->context->count_threads;
 #endif
@@ -75,19 +85,39 @@ lws_context_init_server(struct lws_context_creation_info *info,
 
        if (sockfd == -1) {
 #else
-       sockfd = mbed3_create_tcp_stream_socket();
+#if defined(LWS_WITH_ESP8266)
+       sockfd = esp8266_create_tcp_listen_socket(vhost);
        if (!lws_sockfd_valid(sockfd)) {
 #endif
+#endif
                lwsl_err("ERROR opening socket\n");
                return 1;
        }
+#if LWS_POSIX && !defined(LWS_WITH_ESP32)
+
+#if (defined(WIN32) || defined(_WIN32)) && defined(SO_EXCLUSIVEADDRUSE)
+       /*
+        * only accept that we are the only listener on the port
+        * https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms740621(v=vs.85).aspx
+        *
+        * for lws, to match Linux, we default to exclusive listen
+        */
+       if (!lws_check_opt(vhost->options, LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE)) {
+               if (setsockopt(sockfd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
+                              (const void *)&opt, sizeof(opt)) < 0) {
+                       lwsl_err("reuseaddr failed\n");
+                       compatible_close(sockfd);
+                       return 1;
+               }
+       } else
+#endif
 
-#if LWS_POSIX
        /*
         * allow us to restart even if old sockets in TIME_WAIT
         */
        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
                       (const void *)&opt, sizeof(opt)) < 0) {
+               lwsl_err("reuseaddr failed\n");
                compatible_close(sockfd);
                return 1;
        }
@@ -96,7 +126,7 @@ lws_context_init_server(struct lws_context_creation_info *info,
        if (LWS_IPV6_ENABLED(vhost)) {
                if (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
                        int value = (vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
-                       if (setsockopt(sockfd, SOL_IPV6, IPV6_V6ONLY,
+                       if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
                                        (const void*)&value, sizeof(value)) < 0) {
                                compatible_close(sockfd);
                                return 1;
@@ -105,13 +135,19 @@ lws_context_init_server(struct lws_context_creation_info *info,
        }
 #endif
 
-#if defined(__linux__) && defined(SO_REUSEPORT) && LWS_MAX_SMP > 1
-       if (vhost->context->count_threads > 1)
-               if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
-                               (const void *)&opt, sizeof(opt)) < 0) {
-                       compatible_close(sockfd);
-                       return 1;
-               }
+#if defined(__linux__) && defined(SO_REUSEPORT)
+       n = lws_check_opt(vhost->options, LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE);
+#if LWS_MAX_SMP > 1
+       n = 1;
+#endif
+
+       if (n)
+               if (vhost->context->count_threads > 1)
+                       if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
+                                       (const void *)&opt, sizeof(opt)) < 0) {
+                               compatible_close(sockfd);
+                               return 1;
+                       }
 #endif
 #endif
        lws_plat_set_socket_options(vhost, sockfd);
@@ -131,14 +167,18 @@ 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;
        wsi->vhost = vhost;
        wsi->listener = 1;
 
-       vhost->context->pt[m].wsi_listening = wsi;
+#ifdef LWS_USE_LIBUV
+       if (LWS_LIBUV_ENABLED(vhost->context))
+               lws_uv_initvhost(vhost, wsi);
+#endif
+
        if (insert_wsi_socket_into_fds(vhost->context, wsi))
                goto bail;
 
@@ -146,10 +186,19 @@ lws_context_init_server(struct lws_context_creation_info *info,
        vhost->lserv_wsi = wsi;
 
 #if LWS_POSIX
-       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;
+               vhost->context->count_wsi_allocated--;
+               remove_wsi_socket_from_fds(wsi);
+               goto bail;
+       }
        } /* for each thread able to independently listen */
 #else
-       mbed3_tcp_stream_bind(wsi->sock, info->port, wsi);
+#if defined(LWS_WITH_ESP8266)
+       esp8266_tcp_stream_bind(wsi->desc.sockfd, info->port, wsi);
+#endif
 #endif
        if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
 #ifdef LWS_USE_UNIX_SOCK
@@ -168,58 +217,76 @@ bail:
        return 1;
 }
 
-int
-_lws_server_listen_accept_flow_control(struct lws *twsi, int on)
-{
-       struct lws_context_per_thread *pt = &twsi->context->pt[(int)twsi->tsi];
-       struct lws *wsi = pt->wsi_listening;
-       int n;
-
-       if (!wsi || twsi->context->being_destroyed)
-               return 0;
-
-       lwsl_debug("%s: Thr %d: LISTEN wsi %p: state %d\n",
-                  __func__, twsi->tsi, (void *)wsi, on);
-
-       if (on)
-               n = lws_change_pollfd(wsi, 0, LWS_POLLIN);
-       else
-               n = lws_change_pollfd(wsi, LWS_POLLIN, 0);
-
-       return n;
-}
+#if defined(LWS_WITH_ESP8266)
+#undef strchr
+#define strchr ets_strchr
+#endif
 
 struct lws_vhost *
 lws_select_vhost(struct lws_context *context, int port, const char *servername)
 {
        struct lws_vhost *vhost = context->vhost_list;
+       const char *p;
+       int n, m, colon;
+
+       n = strlen(servername);
+       colon = n;
+       p = strchr(servername, ':');
+       if (p)
+               colon = p - servername;
+
+       /* Priotity 1: first try exact matches */
 
        while (vhost) {
                if (port == vhost->listen_port &&
-                   !strcmp(vhost->name, servername)) {
+                   !strncmp(vhost->name, servername, colon)) {
                        lwsl_info("SNI: Found: %s\n", servername);
                        return vhost;
                }
                vhost = vhost->vhost_next;
        }
 
-       return NULL;
-}
+       /*
+        * 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
+        * *.x.com.
+        */
 
-LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
-lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name)
-{
-       int n;
+       vhost = context->vhost_list;
+       while (vhost) {
+               m = strlen(vhost->name);
+               if (port == vhost->listen_port &&
+                   m <= (colon - 2) &&
+                   servername[colon - m - 1] == '.' &&
+                   !strncmp(vhost->name, servername + colon - m, m)) {
+                       lwsl_info("SNI: Found %s on wildcard: %s\n",
+                                   servername, vhost->name);
+                       return vhost;
+               }
+               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;
+       }
 
-       for (n = 0; n < vh->count_protocols; n++)
-               if (!strcmp(name, vh->protocols[n].name))
-                       return &vh->protocols[n];
+       /* no match */
 
        return NULL;
 }
 
-static const char *
-get_mimetype(const char *file, const struct lws_http_mount *m)
+LWS_VISIBLE LWS_EXTERN const char *
+lws_get_mimetype(const char *file, const struct lws_http_mount *m)
 {
        int n = strlen(file);
        const struct lws_protocol_vhost_options *pvo = NULL;
@@ -260,9 +327,15 @@ get_mimetype(const char *file, const struct lws_http_mount *m)
        if (!strcmp(&file[n - 4], ".txt"))
                return "text/plain";
 
+       if (!strcmp(&file[n - 4], ".svg"))
+               return "image/svg+xml";
+
        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";
 
@@ -270,6 +343,9 @@ get_mimetype(const char *file, const struct lws_http_mount *m)
                return "application/xml";
 
        while (pvo) {
+               if (pvo->name[0] == '*') /* ie, match anything */
+                       return pvo->value;
+
                if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
                        return pvo->value;
 
@@ -278,6 +354,22 @@ 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,
@@ -286,30 +378,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;
-#ifndef _WIN32_WCE
+#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)
+#if !defined(WIN32) && LWS_POSIX && !defined(LWS_WITH_ESP32)
        size_t len;
 #endif
-       int n, spin = 0;
+       int n;
+
+       lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
 
-       snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
+#if !defined(_WIN32_WCE) && !defined(LWS_WITH_ESP8266)
+
+       fflags |= lws_vfs_prepare_flags(wsi);
 
-#ifndef _WIN32_WCE
        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)
+#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) {
@@ -318,12 +456,12 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
                        }
                        sym[len] = '\0';
                        lwsl_debug("symlink %s -> %s\n", path, sym);
-                       snprintf(path, sizeof(path) - 1, "%s", sym);
+                       lws_snprintf(path, sizeof(path) - 1, "%s", sym);
                }
 #endif
                if ((S_IFMT & st.st_mode) == S_IFDIR) {
                        lwsl_debug("default filename append to dir\n");
-                       snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
+                       lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
                                 origin, uri);
                }
 
@@ -332,36 +470,51 @@ 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 */
+
+       if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
+               if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
+                       /* differs - defeat Range: */
+                       wsi->u.http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
 
        if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
                /*
                 * 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,
                                        WSI_TOKEN_HTTP_ETAG,
                                        (unsigned char *)sym, n, &p, end))
                                return -1;
+
                        if (lws_finalize_http_header(wsi, &p, end))
                                return -1;
 
                        n = lws_write(wsi, start, p - start,
                                      LWS_WRITE_HTTP_HEADERS);
                        if (n != (p - start)) {
-                               lwsl_err("_write returned %d from %d\n", n, p - start);
+                               lwsl_err("_write returned %d from %ld\n", n,
+                                        (long)(p - start));
                                return -1;
                        }
 
+                       lws_vfs_file_close(&wsi->u.http.fop_fd);
+
                        return lws_http_transaction_completed(wsi);
                }
        }
@@ -371,11 +524,13 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
                return -1;
 #endif
 
-       mimetype = get_mimetype(path, m);
+       mimetype = lws_get_mimetype(path, m);
        if (!mimetype) {
-               lwsl_err("unknown mimetype for %s", path);
-               goto bail;
+               lwsl_err("unknown mimetype for %s\n", path);
+               goto bail;
        }
+       if (!mimetype[0])
+               lwsl_debug("sending no mimetype for %s\n", path);
 
        wsi->sending_chunked = 0;
 
@@ -424,6 +579,131 @@ bail:
        return -1;
 }
 
+const struct lws_http_mount *
+lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
+{
+       const struct lws_http_mount *hm, *hit = NULL;
+       int best = 0;
+
+       hm = wsi->vhost->mount_list;
+       while (hm) {
+               if (uri_len >= hm->mountpoint_len &&
+                   !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
+                   (uri_ptr[hm->mountpoint_len] == '\0' ||
+                    uri_ptr[hm->mountpoint_len] == '/' ||
+                    hm->mountpoint_len == 1)
+                   ) {
+                       if (hm->origin_protocol == LWSMPRO_CALLBACK ||
+                           ((hm->origin_protocol == LWSMPRO_CGI ||
+                            lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
+                            hm->protocol) &&
+                           hm->mountpoint_len > best)) {
+                               best = hm->mountpoint_len;
+                               hit = hm;
+                       }
+               }
+               hm = hm->mount_next;
+       }
+
+       return hit;
+}
+
+#if LWS_POSIX
+
+static int
+lws_find_string_in_file(const char *filename, const char *string, int stringlen)
+{
+       char buf[128];
+       int fd, match = 0, pos = 0, n = 0, hit = 0;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0) {
+               lwsl_err("can't open auth file: %s\n", filename);
+               return 1;
+       }
+
+       while (1) {
+               if (pos == n) {
+                       n = read(fd, buf, sizeof(buf));
+                       if (n <= 0) {
+                               if (match == stringlen)
+                                       hit = 1;
+                               break;
+                       }
+                       pos = 0;
+               }
+
+               if (match == stringlen) {
+                       if (buf[pos] == '\r' || buf[pos] == '\n') {
+                               hit = 1;
+                               break;
+                       }
+                       match = 0;
+               }
+
+               if (buf[pos] == string[match])
+                       match++;
+               else
+                       match = 0;
+
+               pos++;
+       }
+
+       close(fd);
+
+       return hit;
+}
+
+static int
+lws_unauthorised_basic_auth(struct lws *wsi)
+{
+       struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
+       unsigned char *start = pt->serv_buf + LWS_PRE,
+                     *p = start, *end = p + 512;
+       char buf[64];
+       int n;
+
+       /* no auth... tell him it is required */
+
+       if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
+               return -1;
+
+       n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
+       if (lws_add_http_header_by_token(wsi,
+                       WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
+                       (unsigned char *)buf, n, &p, end))
+               return -1;
+
+       if (lws_finalize_http_header(wsi, &p, end))
+               return -1;
+
+       n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
+       if (n < 0)
+               return -1;
+
+       return lws_http_transaction_completed(wsi);
+
+}
+
+#endif
+
+int lws_clean_url(char *p)
+{
+       while (*p) {
+               if (p[0] == '/' && p[1] == '/') {
+                       char *p1 = p;
+                       while (*p1) {
+                               *p1 = p1[1];
+                               p1++;
+                       }
+                       continue;
+               }
+               p++;
+       }
+
+       return 0;
+}
+
 int
 lws_http_action(struct lws *wsi)
 {
@@ -432,13 +712,13 @@ lws_http_action(struct lws *wsi)
        enum http_version request_version;
        char content_length_str[32];
        struct lws_process_html_args args;
-       const struct lws_http_mount *hm, *hit = NULL;
+       const struct lws_http_mount *hit = NULL;
        unsigned int n, count = 0;
        char http_version_str[10];
        char http_conn_str[20];
        int http_version_len;
-       char *uri_ptr = NULL;
-       int uri_len = 0, best = 0;
+       char *uri_ptr = NULL, *s;
+       int uri_len = 0;
        int meth = -1;
 
        static const unsigned char methods[] = {
@@ -448,13 +728,14 @@ lws_http_action(struct lws *wsi)
                WSI_TOKEN_PUT_URI,
                WSI_TOKEN_PATCH_URI,
                WSI_TOKEN_DELETE_URI,
+               WSI_TOKEN_CONNECT,
 #ifdef LWS_USE_HTTP2
                WSI_TOKEN_HTTP_COLON_PATH,
 #endif
        };
 #if defined(_DEBUG) || defined(LWS_WITH_ACCESS_LOG)
        static const char * const method_names[] = {
-               "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE",
+               "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT",
 #ifdef LWS_USE_HTTP2
                ":path",
 #endif
@@ -514,7 +795,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) {
@@ -637,7 +918,7 @@ lws_http_action(struct lws *wsi)
                        else
                                me = "unknown";
 
-                       snprintf(wsi->access_log.header_log, l,
+                       lws_snprintf(wsi->access_log.header_log, l,
                                 "%s - - [%s] \"%s %s %s\"",
                                 pa, da, me, uri_ptr,
                                 hver[wsi->u.http.request_version]);
@@ -658,44 +939,223 @@ lws_http_action(struct lws *wsi)
 
        /* can we serve it from the mount list? */
 
-       hm = wsi->vhost->mount_list;
-       while (hm) {
-               if (uri_len >= hm->mountpoint_len &&
-                   !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
-                   (uri_ptr[hm->mountpoint_len] == '\0' ||
-                    uri_ptr[hm->mountpoint_len] == '/' ||
-                    hm->mountpoint_len == 1)
-                   ) {
-                       if (hm->origin_protocol == LWSMPRO_CALLBACK ||
-                           ((hm->origin_protocol == LWSMPRO_CGI ||
-                            lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
-                            hm->protocol) &&
-                           hm->mountpoint_len > best)) {
-                               best = hm->mountpoint_len;
-                               hit = hm;
-                       }
+       hit = lws_find_mount(wsi, uri_ptr, uri_len);
+       if (!hit) {
+               /* deferred cleanup and reset to protocols[0] */
+
+               lwsl_info("no hit\n");
+
+               if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
+                       return 1;
+
+               n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
+                                   wsi->user_space, uri_ptr, uri_len);
+
+               goto after;
+       }
+
+       s = uri_ptr + hit->mountpoint_len;
+
+       /*
+        * if we have a mountpoint like https://xxx.com/yyy
+        * there is an implied / at the end for our purposes since
+        * we can only mount on a "directory".
+        *
+        * But if we just go with that, the browser cannot understand
+        * that he is actually looking down one "directory level", so
+        * even though we give him /yyy/abc.html he acts like the
+        * current directory level is /.  So relative urls like "x.png"
+        * wrongly look outside the mountpoint.
+        *
+        * Therefore if we didn't come in on a url with an explicit
+        * / at the end, we must redirect to add it so the browser
+        * understands he is one "directory level" down.
+        */
+       if ((hit->mountpoint_len > 1 ||
+            (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
+             hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
+           (*s != '/' ||
+            (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
+             hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
+           (hit->origin_protocol != LWSMPRO_CGI &&
+            hit->origin_protocol != LWSMPRO_CALLBACK //&&
+            //hit->protocol == NULL
+            )) {
+               unsigned char *start = pt->serv_buf + LWS_PRE,
+                             *p = start, *end = p + 512;
+
+               lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
+
+               if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
+                       goto bail_nuke_ah;
+
+               /* > at start indicates deal with by redirect */
+               if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
+                   hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
+                       n = lws_snprintf((char *)end, 256, "%s%s",
+                                   oprot[hit->origin_protocol & 1],
+                                   hit->origin);
+               else
+                       n = lws_snprintf((char *)end, 256,
+                           "%s%s%s/", oprot[lws_is_ssl(wsi)],
+                           lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
+                           uri_ptr);
+               lws_clean_url((char *)end);
+
+               n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
+                                     end, n, &p, end);
+               if ((int)n < 0)
+                       goto bail_nuke_ah;
+
+               return lws_http_transaction_completed(wsi);
+       }
+
+#if LWS_POSIX
+       /* basic auth? */
+
+       if (hit->basic_auth_login_file) {
+               char b64[160], plain[(sizeof(b64) * 3) / 4];
+               int m;
+
+               /* Did he send auth? */
+               if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION))
+                       return lws_unauthorised_basic_auth(wsi);
+
+               n = HTTP_STATUS_FORBIDDEN;
+
+               m = lws_hdr_copy(wsi, b64, sizeof(b64), WSI_TOKEN_HTTP_AUTHORIZATION);
+               if (m < 7) {
+                       lwsl_err("b64 auth too long\n");
+                       goto transaction_result_n;
                }
-               hm = hm->mount_next;
+
+               b64[5] = '\0';
+               if (strcasecmp(b64, "Basic")) {
+                       lwsl_err("auth missing basic: %s\n", b64);
+                       goto transaction_result_n;
+               }
+
+               /* It'll be like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l */
+
+               m = lws_b64_decode_string(b64 + 6, plain, sizeof(plain));
+               if (m < 0) {
+                       lwsl_err("plain auth too long\n");
+                       goto transaction_result_n;
+               }
+
+//             lwsl_notice(plain);
+
+               if (!lws_find_string_in_file(hit->basic_auth_login_file, plain, m)) {
+                       lwsl_err("basic auth lookup failed\n");
+                       return lws_unauthorised_basic_auth(wsi);
+               }
+
+               lwsl_notice("basic auth accepted\n");
+
+               /* accept the auth */
        }
-       if (hit) {
-               char *s = uri_ptr + hit->mountpoint_len;
+#endif
 
-               lwsl_debug("*** hit %d %d %s\n", hit->mountpoint_len,
-                          hit->origin_protocol , hit->origin);
+#if defined(LWS_WITH_HTTP_PROXY)
+       /*
+        * The mount is a reverse proxy?
+        */
 
-               if (hit->protocol) {
-                       const struct lws_protocols *pp = lws_vhost_name_to_protocol(
-                                       wsi->vhost, hit->protocol);
+       if (hit->origin_protocol == LWSMPRO_HTTPS ||
+           hit->origin_protocol == LWSMPRO_HTTP)  {
+               struct lws_client_connect_info i;
+               char ads[96], rpath[256], *pcolon, *pslash, *p;
+               int n, na;
 
-                       if (!pp) {
-                               lwsl_err("unknown protocol %s\n", hit->protocol);
-                               return 1;
+               memset(&i, 0, sizeof(i));
+               i.context = lws_get_context(wsi);
+
+               pcolon = strchr(hit->origin, ':');
+               pslash = strchr(hit->origin, '/');
+               if (!pslash) {
+                       lwsl_err("Proxy mount origin '%s' must have /\n", hit->origin);
+                       return -1;
+               }
+               if (pcolon > pslash)
+                       pcolon = NULL;
+               
+               if (pcolon)
+                       n = pcolon - hit->origin;
+               else
+                       n = pslash - hit->origin;
+
+               if (n >= sizeof(ads) - 2)
+                       n = sizeof(ads) - 2;
+
+               memcpy(ads, hit->origin, n);
+               ads[n] = '\0';
+
+               i.address = ads;
+               i.port = 80;
+               if (hit->origin_protocol == LWSMPRO_HTTPS) { 
+                       i.port = 443;
+                       i.ssl_connection = 1;
+               }
+               if (pcolon)
+                       i.port = atoi(pcolon + 1);
+               
+               lws_snprintf(rpath, sizeof(rpath) - 1, "/%s/%s", pslash + 1, uri_ptr + hit->mountpoint_len);
+               lws_clean_url(rpath);
+               na = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_URI_ARGS);
+               if (na) {
+                       p = rpath + strlen(rpath);
+                       *p++ = '?';
+                       lws_hdr_copy(wsi, p, &rpath[sizeof(rpath) - 1] - p, WSI_TOKEN_HTTP_URI_ARGS);
+                       while (--na) {
+                               if (*p == '\0')
+                                       *p = '&';
+                               p++;
                        }
+               }
+                               
+
+               i.path = rpath;
+               i.host = i.address;
+               i.origin = NULL;
+               i.method = "GET";
+               i.parent_wsi = wsi;
+               i.uri_replace_from = hit->origin;
+               i.uri_replace_to = hit->mountpoint;
+
+               lwsl_notice("proxying to %s port %d url %s, ssl %d, from %s, to %s\n",
+                               i.address, i.port, i.path, i.ssl_connection, i.uri_replace_from, i.uri_replace_to);
+       
+               if (!lws_client_connect_via_info(&i)) {
+                       lwsl_err("proxy connect fail\n");
+                       return 1;
+               }
 
-                       if (lws_bind_protocol(wsi, pp))
-                               return 1;
+               return 0;
+       }
+#endif
+
+       /*
+        * A particular protocol callback is mounted here?
+        *
+        * For the duration of this http transaction, bind us to the
+        * associated protocol
+        */
+       if (hit->origin_protocol == LWSMPRO_CALLBACK || hit->protocol) {
+               const struct lws_protocols *pp;
+               const char *name = hit->origin;
+               if (hit->protocol)
+                       name = hit->protocol;
+
+               pp = lws_vhost_name_to_protocol(wsi->vhost, name);
+               if (!pp) {
+                       n = -1;
+                       lwsl_err("Unable to find plugin '%s'\n",
+                                hit->origin);
+                       return 1;
                }
-               lwsl_info("wsi %s protocol '%s'\n", uri_ptr, wsi->protocol->name);
+
+               if (lws_bind_protocol(wsi, pp))
+                       return 1;
 
                args.p = uri_ptr;
                args.len = uri_len;
@@ -712,172 +1172,78 @@ lws_http_action(struct lws *wsi)
                if (args.final) /* callback completely handled it well */
                        return 0;
 
-               /*
-                * if we have a mountpoint like https://xxx.com/yyy
-                * there is an implied / at the end for our purposes since
-                * we can only mount on a "directory".
-                *
-                * But if we just go with that, the browser cannot understand
-                * that he is actually looking down one "directory level", so
-                * even though we give him /yyy/abc.html he acts like the
-                * current directory level is /.  So relative urls like "x.png"
-                * wrongly look outside the mountpoint.
-                *
-                * Therefore if we didn't come in on a url with an explicit
-                * / at the end, we must redirect to add it so the browser
-                * understands he is one "directory level" down.
-                */
-               if ((hit->mountpoint_len > 1 ||
-                    (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
-                     hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
-                   (*s != '/' ||
-                    (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
-                     hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
-                   (hit->origin_protocol != LWSMPRO_CGI &&
-                    hit->origin_protocol != LWSMPRO_CALLBACK //&&
-                    //hit->protocol == NULL
-                    )) {
-                       unsigned char *start = pt->serv_buf + LWS_PRE,
-                                             *p = start, *end = p + 512;
-
-                       lwsl_debug("Doing 301 '%s' org %s\n", s, hit->origin);
-
-                       if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
-                               goto bail_nuke_ah;
-
-                       /* > at start indicates deal with by redirect */
-                       if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
-                           hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
-                               n = snprintf((char *)end, 256, "%s%s",
-                                           oprot[hit->origin_protocol & 1],
-                                           hit->origin);
-                       else
-                               n = snprintf((char *)end, 256,
-                                   "%s%s%s/", oprot[lws_is_ssl(wsi)],
-                                   lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
-                                   uri_ptr);
-
-                       n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
-                                             end, n, &p, end);
-                       if ((int)n < 0)
-                               goto bail_nuke_ah;
-
-                       return lws_http_transaction_completed(wsi);
-               }
-
-               /*
-                * A particular protocol callback is mounted here?
-                *
-                * For the duration of this http transaction, bind us to the
-                * associated protocol
-                */
-
-               if (hit->origin_protocol == LWSMPRO_CALLBACK ||
-                   (hit->protocol && lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI))) {
-                       if (! hit->protocol) {
-                               for (n = 0; n < (unsigned int)wsi->vhost->count_protocols; n++)
-                                       if (!strcmp(wsi->vhost->protocols[n].name,
-                                                  hit->origin)) {
-                                               if (lws_bind_protocol(wsi, &wsi->vhost->protocols[n]))
-                                                       return 1;
-                                               break;
-                                       }
+               if (hit->cgienv && wsi->protocol->callback(wsi,
+                               LWS_CALLBACK_HTTP_PMO,
+                               wsi->user_space, (void *)hit->cgienv, 0))
+                       return 1;
 
-                               if (n == wsi->vhost->count_protocols) {
-                                       n = -1;
-                                       lwsl_err("Unable to find plugin '%s'\n",
-                                                hit->origin);
-                               }
-                       }
+               if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
                        n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
-                                                   wsi->user_space,
-                                                   uri_ptr + hit->mountpoint_len,
-                                                   uri_len - hit->mountpoint_len);
-
+                                           wsi->user_space,
+                                           uri_ptr + hit->mountpoint_len,
+                                           uri_len - hit->mountpoint_len);
                        goto after;
                }
+       }
 
 #ifdef LWS_WITH_CGI
-               /* did we hit something with a cgi:// origin? */
-               if (hit->origin_protocol == LWSMPRO_CGI) {
-                       const char *cmd[] = {
-                               NULL, /* replace with cgi path */
-                               NULL
-                       };
-                       unsigned char *p, *end, buffer[256];
-
-                       lwsl_debug("%s: cgi\n", __func__);
-                       cmd[0] = hit->origin;
-
-                       n = 5;
-                       if (hit->cgi_timeout)
-                               n = hit->cgi_timeout;
-
-                       n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
-                                   hit->cgienv);
-                       if (n) {
-                               lwsl_err("%s: cgi failed\n");
-                               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;
-               }
-#endif
-
-               n = strlen(s);
-               if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
-                       s = (char *)hit->def;
-               if (!s)
-                       s = "index.html";
+       /* did we hit something with a cgi:// origin? */
+       if (hit->origin_protocol == LWSMPRO_CGI) {
+               const char *cmd[] = {
+                       NULL, /* replace with cgi path */
+                       NULL
+               };
 
-               wsi->cache_secs = hit->cache_max_age;
-               wsi->cache_reuse = hit->cache_reusable;
-               wsi->cache_revalidate = hit->cache_revalidate;
-               wsi->cache_intermediaries = hit->cache_intermediaries;
+               lwsl_debug("%s: cgi\n", __func__);
+               cmd[0] = hit->origin;
 
+               n = 5;
+               if (hit->cgi_timeout)
+                       n = hit->cgi_timeout;
 
-               n = lws_http_serve(wsi, s, hit->origin, hit);
+               n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
+                           hit->cgienv);
                if (n) {
-                       /*
-                        *      lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
-                        */
-                       if (hit->protocol) {
-                               const struct lws_protocols *pp = lws_vhost_name_to_protocol(
-                                               wsi->vhost, hit->protocol);
+                       lwsl_err("%s: cgi failed\n", __func__);
+                       return -1;
+               }
+
+               goto deal_body;
+       }
+#endif
 
-                               if (lws_bind_protocol(wsi, pp))
-                                       return 1;
+       n = strlen(s);
+       if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
+               s = (char *)hit->def;
+       if (!s)
+               s = "index.html";
 
-                               n = pp->callback(wsi, LWS_CALLBACK_HTTP,
-                                                wsi->user_space,
-                                                uri_ptr + hit->mountpoint_len,
-                                                uri_len - hit->mountpoint_len);
-                       } else
-                               n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
-                                           wsi->user_space, uri_ptr, uri_len);
-               }
-       } else {
-               /* deferred cleanup and reset to protocols[0] */
+       wsi->cache_secs = hit->cache_max_age;
+       wsi->cache_reuse = hit->cache_reusable;
+       wsi->cache_revalidate = hit->cache_revalidate;
+       wsi->cache_intermediaries = hit->cache_intermediaries;
 
-               lwsl_notice("no hit\n");
+       n = lws_http_serve(wsi, s, hit->origin, hit);
+       if (n) {
+               /*
+                *      lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
+                */
+               if (hit->protocol) {
+                       const struct lws_protocols *pp = lws_vhost_name_to_protocol(
+                                       wsi->vhost, hit->protocol);
 
-               if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0]))
-                       return 1;
+                       if (lws_bind_protocol(wsi, pp))
+                               return 1;
 
-               n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
+                       n = pp->callback(wsi, LWS_CALLBACK_HTTP,
+                                        wsi->user_space,
+                                        uri_ptr + hit->mountpoint_len,
+                                        uri_len - hit->mountpoint_len);
+               } else
+                       n = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
                                    wsi->user_space, uri_ptr, uri_len);
        }
+
 after:
        if (n) {
                lwsl_info("LWS_CALLBACK_HTTP closing\n");
@@ -906,48 +1272,30 @@ deal_body:
 bail_nuke_ah:
        /* we're closing, losing some rx is OK */
        wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
+       // lwsl_notice("%s: drop1\n", __func__);
        lws_header_table_detach(wsi, 1);
 
        return 1;
-}
-
-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;
+#if LWS_POSIX
+transaction_result_n:
+       lws_return_http_status(wsi, n, NULL);
 
-       return 0;
+       return lws_http_transaction_completed(wsi);
+#endif
 }
 
-
 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, hit;
+       unsigned char *obuf = *buf;
        char protocol_list[128];
-       char protocol_name[32];
+       char protocol_name[64];
+       size_t olen = len;
        char *p;
 
        if (len >= 10000000) {
@@ -969,7 +1317,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;
                }
@@ -981,6 +1360,32 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
                lwsl_debug("%s: wsi->more_rx_waiting=%d\n", __func__,
                                wsi->more_rx_waiting);
 
+               /* check for unwelcome guests */
+
+               if (wsi->context->reject_service_keywords) {
+                       const struct lws_protocol_vhost_options *rej =
+                                       wsi->context->reject_service_keywords;
+                       char ua[384], *msg = NULL;
+
+                       if (lws_hdr_copy(wsi, ua, sizeof(ua) - 1,
+                                         WSI_TOKEN_HTTP_USER_AGENT) > 0) {
+                               ua[sizeof(ua) - 1] = '\0';
+                               while (rej) {
+                                       if (strstr(ua, rej->name)) {
+                                               msg = strchr(rej->value, ' ');
+                                               if (msg)
+                                                       msg++;
+                                               lws_return_http_status(wsi, atoi(rej->value), msg);
+
+                                               wsi->vhost->conn_stats.rejected++;
+
+                                               goto bail_nuke_ah;
+                                       }
+                                       rej = rej->next;
+                               }
+                       }
+               }
+
                /* select vhost */
 
                if (lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
@@ -990,14 +1395,21 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
 
                        if (vhost)
                                wsi->vhost = vhost;
-               }
+               } else
+                       lwsl_info("no host\n");
 
-               wsi->vhost->trans++;
+               wsi->vhost->conn_stats.trans++;
                if (!wsi->conn_stat_done) {
-                       wsi->vhost->conn++;
+                       wsi->vhost->conn_stats.conn++;
                        wsi->conn_stat_done = 1;
                }
 
+               if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
+                       lwsl_info("Changing to RAW mode\n");
+                       m = 0;
+                       goto raw_transition;
+               }
+
                wsi->mode = LWSCM_PRE_WS_SERVING_ACCEPT;
                lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
 
@@ -1006,14 +1418,14 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
                if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
                        if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
                                        "websocket")) {
-                               wsi->vhost->ws_upgrades++;
+                               wsi->vhost->conn_stats.ws_upg++;
                                lwsl_info("Upgrade to ws\n");
                                goto upgrade_ws;
                        }
 #ifdef LWS_USE_HTTP2
                        if (!strcasecmp(lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE),
                                        "h2c")) {
-                               wsi->vhost->http2_upgrades++;
+                               wsi->vhost->conn_stats.http2_upg++;
                                lwsl_info("Upgrade to h2c\n");
                                goto upgrade_h2c;
                        }
@@ -1030,7 +1442,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;
@@ -1118,8 +1530,17 @@ upgrade_ws:
 
                while (*p && !hit) {
                        n = 0;
-                       while (n < sizeof(protocol_name) - 1 && *p && *p !=',')
+                       non_space_char_found = 0;
+                       while (n < sizeof(protocol_name) - 1 && *p &&
+                              *p != ',') {
+                               // ignore leading spaces
+                               if (!non_space_char_found && *p == ' ') {
+                                       n++;
+                                       continue;
+                               }
+                               non_space_char_found = 1;
                                protocol_name[n++] = *p++;
+                       }
                        protocol_name[n] = '\0';
                        if (*p)
                                p++;
@@ -1128,6 +1549,8 @@ upgrade_ws:
 
                        n = 0;
                        while (wsi->vhost->protocols[n].callback) {
+                               lwsl_info("try %s\n", wsi->vhost->protocols[n].name);
+
                                if (wsi->vhost->protocols[n].name &&
                                    !strcmp(wsi->vhost->protocols[n].name,
                                            protocol_name)) {
@@ -1156,7 +1579,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];
                }
@@ -1197,30 +1620,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
@@ -1252,6 +1652,8 @@ upgrade_ws:
                wsi->u.hdr = hdr;
                lws_pt_unlock(pt);
 
+               lws_restart_ws_ping_pong_timer(wsi);
+
                /*
                 * create the frame buffer for this connection according to the
                 * size mentioned in the protocol definition.  If 0 there, use
@@ -1268,17 +1670,39 @@ upgrade_ws:
                        return 1;
                }
                wsi->u.ws.rx_ubuf_alloc = n;
-               lwsl_info("Allocating RX buffer %d\n", n);
-#if LWS_POSIX
-               if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF,
+               lwsl_debug("Allocating RX buffer %d\n", n);
+#if LWS_POSIX && !defined(LWS_WITH_ESP32)
+               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;
                }
 #endif
+
                lwsl_parser("accepted v%02d connection\n",
                            wsi->ietf_spec_revision);
 
+               /* notify user code that we're ready to roll */
+
+               if (wsi->protocol->callback)
+                       if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
+                                                   wsi->user_space,
+#ifdef LWS_OPENSSL_SUPPORT
+                                                   wsi->ssl,
+#else
+                                                   NULL,
+#endif
+                                                   0))
+                               return 1;
+
+               /* !!! drop ah unreservedly after ESTABLISHED */
+               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);
+               }
+
                return 0;
        } /* while all chars are handled */
 
@@ -1288,6 +1712,7 @@ bail_nuke_ah:
        /* drop the header info */
        /* we're closing, losing some rx is OK */
        wsi->u.hdr.ah->rxpos = wsi->u.hdr.ah->rxlen;
+       //lwsl_notice("%s: drop2\n", __func__);
        lws_header_table_detach(wsi, 1);
 
        return 1;
@@ -1329,14 +1754,15 @@ lws_create_new_server_wsi(struct lws_vhost *vhost)
        }
 
        new_wsi->tsi = n;
-       lwsl_info("Accepted %p to tsi %d\n", new_wsi, new_wsi->tsi);
+       lwsl_debug("Accepted wsi %p to context %p, tsi %d\n", new_wsi,
+                   vhost->context, new_wsi->tsi);
 
        new_wsi->vhost = vhost;
        new_wsi->context = vhost->context;
        new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
        new_wsi->rxflow_change_to = LWS_RXFLOW_ALLOW;
 
-       /* intialize the instance struct */
+       /* initialize the instance struct */
 
        new_wsi->state = LWSS_HTTP;
        new_wsi->mode = LWSCM_HTTP_SERVING;
@@ -1355,7 +1781,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++;
 
        /*
@@ -1375,7 +1801,12 @@ lws_http_transaction_completed(struct lws *wsi)
 
        lws_access_log(wsi);
 
-       lwsl_debug("%s: wsi %p\n", __func__, wsi);
+       if (!wsi->hdr_parsing_completed) {
+               lwsl_notice("%s: ignoring, ah parsing incomplete\n", __func__);
+               return 0;
+       }
+
+       lwsl_notice("%s: wsi %p\n", __func__, wsi);
        /* if we can't go back to accept new headers, drop the connection */
        if (wsi->u.http.connection_type != HTTP_CONNECTION_KEEP_ALIVE) {
                lwsl_info("%s: %p: close connection\n", __func__, wsi);
@@ -1418,6 +1849,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);
        }
@@ -1430,79 +1876,180 @@ lws_http_transaction_completed(struct lws *wsi)
        return 0;
 }
 
-static struct lws *
-lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
+/* if not a socket, it's a raw, non-ssl file descriptor */
+
+LWS_VISIBLE struct lws *
+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);
+       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_info("%s: new wsi %p, sockfd %d\n", __func__, new_wsi, accept_fd);
+       if (parent) {
+               new_wsi->parent = parent;
+               new_wsi->sibling_list = parent->child_list;
+               parent->child_list = new_wsi;
+       }
+
+       new_wsi->desc = fd;
+
+       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);
+               }
 
-       new_wsi->sock = accept_fd;
+       if (type & LWS_ADOPT_SOCKET) { /* socket desc */
+               lwsl_debug("%s: new wsi %p, sockfd %d\n", __func__, new_wsi,
+                          (int)(size_t)fd.sockfd);
 
-       /* the transport is accepted... give him time to negotiate */
-       lws_set_timeout(new_wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
-                       context->timeout_secs);
+               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
-       mbed3_tcp_stream_accept(accept_fd, new_wsi);
+#if defined(LWS_WITH_ESP8266)
+               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
-        * selected yet so we issue this to protocols[0]
+        * selected yet so we issue this to the vhosts's default protocol,
+        * itself by default protocols[0]
         */
-       if ((context->vhost_list->protocols[0].callback)(new_wsi,
-            LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED, NULL, NULL, 0)) {
-               compatible_close(new_wsi->sock);
-               lws_free(new_wsi);
-               return NULL;
+       n = LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED;
+       if (!(type & LWS_ADOPT_HTTP)) {
+               if (!(type & LWS_ADOPT_SOCKET))
+                       n = LWS_CALLBACK_RAW_ADOPT_FILE;
+               else
+                       n = LWS_CALLBACK_RAW_ADOPT;
+       }
+
+       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 {
+               /* SSL */
+               if (!(type & LWS_ADOPT_HTTP))
+                       new_wsi->mode = LWSCM_SSL_INIT_RAW;
+               else
+                       new_wsi->mode = LWSCM_SSL_INIT;
+
+               ssl = 1;
        }
 
-       lws_libev_accept(new_wsi, new_wsi->sock);
-       lws_libuv_accept(new_wsi, new_wsi->sock);
+       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 (!LWS_SSL_ENABLED(new_wsi->vhost)) {
+       if (!ssl) {
                if (insert_wsi_socket_into_fds(context, new_wsi)) {
                        lwsl_err("%s: fail inserting socket\n", __func__);
                        goto fail;
                }
-       } else {
-               new_wsi->mode = LWSCM_SSL_INIT;
-               if (lws_server_socket_service_ssl(new_wsi, accept_fd)) {
+       } 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;
 }
 
 LWS_VISIBLE struct lws *
+lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd)
+{
+       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_vhost(context->vhost_list, accept_fd);
 }
 
-LWS_VISIBLE LWS_EXTERN struct lws *
-lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
-                        const char *readbuf, size_t len)
+/* Common read-buffer adoption for lws_adopt_*_readbuf */
+static struct lws*
+adopt_socket_readbuf(struct lws *wsi, const char *readbuf, size_t len)
 {
-       struct lws *wsi = lws_adopt_socket(context, accept_fd);
        struct lws_context_per_thread *pt;
        struct allocated_headers *ah;
        struct lws_pollfd *pfd;
@@ -1510,13 +2057,14 @@ lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
        if (!wsi)
                return NULL;
 
-       if (!readbuf)
+       if (!readbuf || len == 0)
                return wsi;
 
        if (len > sizeof(ah->rx)) {
                lwsl_err("%s: rx in too big\n", __func__);
                goto bail;
        }
+
        /*
         * we can't process the initial read data until we can attach an ah.
         *
@@ -1534,7 +2082,7 @@ lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
                ah->rxlen = len;
 
                lwsl_notice("%s: calling service on readbuf ah\n", __func__);
-               pt = &context->pt[(int)wsi->tsi];
+               pt = &wsi->context->pt[(int)wsi->tsi];
 
                /* unlike a normal connect, we have the headers already
                 * (or the first part of them anyway).
@@ -1544,7 +2092,7 @@ lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
                pfd = &pt->fds[wsi->position_in_fds_table];
                pfd->revents |= LWS_POLLIN;
                lwsl_err("%s: calling service\n", __func__);
-               if (lws_service_fd_tsi(context, pfd, wsi->tsi))
+               if (lws_service_fd_tsi(wsi->context, pfd, wsi->tsi))
                        /* service closed us */
                        return NULL;
 
@@ -1574,6 +2122,20 @@ bail:
        return NULL;
 }
 
+LWS_VISIBLE struct lws *
+lws_adopt_socket_readbuf(struct lws_context *context, lws_sockfd_type accept_fd,
+                        const char *readbuf, size_t len)
+{
+        return adopt_socket_readbuf(lws_adopt_socket(context, accept_fd), readbuf, len);
+}
+
+LWS_VISIBLE struct lws *
+lws_adopt_socket_vhost_readbuf(struct lws_vhost *vhost, lws_sockfd_type accept_fd,
+                        const char *readbuf, size_t len)
+{
+        return adopt_socket_readbuf(lws_adopt_socket_vhost(vhost, accept_fd), readbuf, len);
+}
+
 LWS_VISIBLE int
 lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                          struct lws_pollfd *pollfd)
@@ -1581,17 +2143,22 @@ 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;
+       
+       // lwsl_notice("%s: mode %d\n", __func__, wsi->mode);
 
        switch (wsi->mode) {
 
        case LWSCM_HTTP_SERVING:
        case LWSCM_HTTP_SERVING_ACCEPTED:
        case LWSCM_HTTP2_SERVING:
+       case LWSCM_RAW:
 
                /* handle http headers coming in */
 
@@ -1624,35 +2191,40 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                 * the POLLOUT), don't let that happen twice in a row...
                 * next time we see the situation favour POLLOUT
                 */
-
+#if !defined(LWS_WITH_ESP8266)
                if (wsi->favoured_pollin &&
                    (pollfd->revents & pollfd->events & LWS_POLLOUT)) {
                        wsi->favoured_pollin = 0;
                        goto try_pollout;
                }
+#endif
 
                /* 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) {
-                       if (!wsi->u.hdr.ah)
+                   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))
+                               if (lws_header_table_attach(wsi, 0)) {
+                                       lwsl_info("wsi %p: failed to acquire ah\n", wsi);
                                        goto try_pollout;
-
+                               }
+                       }
                        ah = wsi->u.hdr.ah;
 
-                       lwsl_debug("%s: %p: rxpos:%d rxlen:%d\n", __func__, wsi,
-                                  ah->rxpos, ah->rxlen);
+                       //lwsl_notice("%s: %p: rxpos:%d rxlen:%d\n", __func__, wsi,
+                       //         ah->rxpos, ah->rxlen);
 
                        /* if nothing in ah rx buffer, get some fresh rx */
                        if (ah->rxpos == ah->rxlen) {
                                ah->rxlen = lws_ssl_capable_read(wsi, ah->rx,
                                                   sizeof(ah->rx));
                                ah->rxpos = 0;
-                               lwsl_debug("%s: wsi %p, ah->rxlen = %d\r\n",
-                                          __func__, wsi, ah->rxlen);
+                               //lwsl_notice("%s: wsi %p, ah->rxlen = %d\r\n",
+                               //         __func__, wsi, ah->rxlen);
                                switch (ah->rxlen) {
                                case 0:
                                        lwsl_info("%s: read 0 len\n", __func__);
@@ -1667,14 +2239,17 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                                        goto try_pollout;
                                }
                        }
+
                        if (!(ah->rxpos != ah->rxlen && ah->rxlen)) {
                                lwsl_err("%s: assert: rxpos %d, rxlen %d\n",
                                         __func__, ah->rxpos, ah->rxlen);
 
                                assert(0);
                        }
+                       
                        /* just ignore incoming if waiting for close */
-                       if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
+                       if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
+                           wsi->state != LWSS_HTTP_ISSUING_FILE) {
                                n = lws_read(wsi, ah->rx + ah->rxpos,
                                             ah->rxlen - ah->rxpos);
                                if (n < 0) /* we closed wsi */
@@ -1683,6 +2258,8 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                                        if ( wsi->u.hdr.ah->rxlen)
                                                 wsi->u.hdr.ah->rxpos += n;
 
+                                       lwsl_debug("%s: wsi %p: ah read rxpos %d, rxlen %d\n", __func__, wsi, wsi->u.hdr.ah->rxpos, wsi->u.hdr.ah->rxlen);
+
                                        if (wsi->u.hdr.ah->rxpos == wsi->u.hdr.ah->rxlen &&
                                            (wsi->mode != LWSCM_HTTP_SERVING &&
                                             wsi->mode != LWSCM_HTTP_SERVING_ACCEPTED &&
@@ -1710,9 +2287,21 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                case LWS_SSL_CAPABLE_MORE_SERVICE:
                        goto try_pollout;
                }
+               
+               if (wsi->mode == LWSCM_RAW) {
+                       n = user_callback_handle_rxflow(wsi->protocol->callback,
+                                       wsi, LWS_CALLBACK_RAW_RX,
+                                       wsi->user_space, pt->serv_buf, len);
+                       if (n < 0) {
+                               lwsl_info("LWS_CALLBACK_RAW_RX_fail\n");
+                               goto fail;
+                       }
+                       goto try_pollout;
+               }
 
                /* just ignore incoming if waiting for close */
-               if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
+               if (wsi->state != LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
+                   wsi->state != LWSS_HTTP_ISSUING_FILE) {
                        /*
                         * this may want to send
                         * (via HTTP callback for example)
@@ -1731,6 +2320,7 @@ lws_server_socket_service(struct lws_context *context, struct lws *wsi,
                }
 
 try_pollout:
+               
                /* this handles POLLOUT for http serving fragments */
 
                if (!(pollfd->revents & LWS_POLLOUT))
@@ -1742,10 +2332,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);
@@ -1762,6 +2385,7 @@ try_pollout:
                        lwsl_info("completed\n");
                        goto fail;
                }
+
                break;
 
        case LWSCM_SERVER_LISTENER:
@@ -1773,6 +2397,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);
@@ -1784,7 +2425,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));
@@ -1793,8 +2434,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... */
@@ -1813,7 +2463,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;
 
@@ -1843,31 +2498,152 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
        static const char * const intermediates[] = { "private", "public" };
        struct lws_context *context = lws_get_context(wsi);
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
+#if defined(LWS_WITH_RANGES)
+       struct lws_range_parsing *rp = &wsi->u.http.range;
+#endif
        char cache_control[50], *cc = "no-store";
        unsigned char *response = pt->serv_buf + LWS_PRE;
        unsigned char *p = response;
        unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
-       int ret = 0, cclen = 8;
+       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;
+
+       /*
+        * 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);
+
+                       return -1;
+               }
+       }
+       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);
 
-       wsi->u.http.fd = lws_plat_file_open(wsi, file, &wsi->u.http.filelen,
-                                           O_RDONLY);
+       lwsl_debug("Range count %d\n", ranges);
+       /*
+        * no ranges -> 200;
+        *  1 range  -> 206 + Content-Type: normal; Content-Range;
+        *  more     -> 206 + Content-Type: multipart/byteranges
+        *              Repeat the true Content-Type in each multipart header
+        *              along with Content-Range
+        */
+       if (ranges < 0) {
+               /* it means he expressed a range in Range:, but it was illegal */
+               lws_return_http_status(wsi, HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, NULL);
+               if (lws_http_transaction_completed(wsi))
+                       return -1; /* <0 means just hang up */
 
-       if (wsi->u.http.fd == LWS_INVALID_FILE) {
-               lwsl_err("Unable to open '%s'\n", file);
-               lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
+               lws_vfs_file_close(&wsi->u.http.fop_fd);
 
-               return -1;
+               return 0; /* == 0 means we dealt with the transaction complete */
        }
+       if (ranges)
+               n = HTTP_STATUS_PARTIAL_CONTENT;
+#endif
 
-       if (lws_add_http_header_status(wsi, 200, &p, end))
+       if (lws_add_http_header_status(wsi, n, &p, end))
                return -1;
-       if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
-                                        (unsigned char *)content_type,
-                                        strlen(content_type), &p, end))
+
+       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,
+                                                (unsigned char *)content_type,
+                                                strlen(content_type), &p, end))
+                       return -1;
+
+       if (ranges >= 2) { /* multipart byteranges */
+               strncpy(wsi->u.http.multipart_content_type, content_type,
+                       sizeof(wsi->u.http.multipart_content_type) - 1);
+               wsi->u.http.multipart_content_type[
+                        sizeof(wsi->u.http.multipart_content_type) - 1] = '\0';
+               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
+                                                (unsigned char *)"multipart/byteranges; boundary=_lws",
+                                                20, &p, end))
+                       return -1;
+
+               /*
+                *  our overall content length has to include
+                *
+                *  - (n + 1) x "_lws\r\n"
+                *  - n x Content-Type: xxx/xxx\r\n
+                *  - n x Content-Range: bytes xxx-yyy/zzz\r\n
+                *  - n x /r/n
+                *  - the actual payloads (aggregated in rp->agg)
+                *
+                *  Precompute it for the main response header
+                */
+
+               computed_total_content_length = (lws_filepos_t)rp->agg +
+                                               6 /* final _lws\r\n */;
+
+               lws_ranges_reset(rp);
+               while (lws_ranges_next(rp)) {
+                       n = lws_snprintf(cache_control, sizeof(cache_control),
+                                       "bytes %llu-%llu/%llu",
+                                       rp->start, rp->end, rp->extent);
+
+                       computed_total_content_length +=
+                                       6 /* header _lws\r\n */ +
+                                       14 + strlen(content_type) + 2 + /* Content-Type: xxx/xxx\r\n */
+                                       15 + n + 2 + /* Content-Range: xxxx\r\n */
+                                       2; /* /r/n */
+               }
+
+               lws_ranges_reset(rp);
+               lws_ranges_next(rp);
+       }
+
+       if (ranges == 1) {
+               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);
+
+               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_RANGE,
+                                                (unsigned char *)cache_control,
+                                                n, &p, end))
+                       return -1;
+       }
+
+       wsi->u.http.range.inside = 0;
+
+       if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ACCEPT_RANGES,
+                                        (unsigned char *)"bytes", 5, &p, end))
                return -1;
+#endif
 
        if (!wsi->sending_chunked) {
-               if (lws_add_http_header_content_length(wsi, wsi->u.http.filelen, &p, end))
+               if (lws_add_http_header_content_length(wsi,
+                                                      computed_total_content_length,
+                                                      &p, end))
                        return -1;
        } else {
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_TRANSFER_ENCODING,
@@ -1892,6 +2668,11 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
                        (unsigned char *)cc, cclen, &p, end))
                return -1;
 
+       if (wsi->u.http.connection_type == HTTP_CONNECTION_KEEP_ALIVE)
+               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
+                               (unsigned char *)"keep-alive", 10, &p, end))
+                       return -1;
+
        if (other_headers) {
                if ((end - p) < other_headers_len)
                        return -1;
@@ -1904,7 +2685,7 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
 
        ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
        if (ret != (p - response)) {
-               lwsl_err("_write returned %d from %d\n", ret, (p - response));
+               lwsl_err("_write returned %d from %ld\n", ret, (long)(p - response));
                return -1;
        }
 
@@ -1932,11 +2713,12 @@ lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
                 */
                if (!(wsi->rxflow_change_to & LWS_RXFLOW_ALLOW)) {
                        lws_rxflow_cache(wsi, *buf, 0, len);
-                       lwsl_parser("%s: cached %d\n", __func__, len);
+                       lwsl_parser("%s: cached %ld\n", __func__, (long)len);
                        return 1;
                }
 
                if (wsi->u.ws.rx_draining_ext) {
+                       // lwsl_notice("draining with 0\n");
                        m = lws_rx_sm(wsi, 0);
                        if (m < 0)
                                return -1;
@@ -1948,9 +2730,13 @@ 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 ==
-                   LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED)
-                       lws_payload_until_length_exhausted(wsi, buf, &len);
+               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)
+                               wsi->rxflow_pos += m;
+               }
 
                /* process the byte */
                m = lws_rx_sm(wsi, *(*buf)++);
@@ -1970,7 +2756,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);
@@ -2019,6 +2805,7 @@ struct lws_urldecode_stateful {
        int pos;
        int hdr_idx;
        int mp;
+       int sum;
 
        unsigned int multipart_form_data:1;
        unsigned int inside_quote:1;
@@ -2045,6 +2832,7 @@ lws_urldecode_s_create(struct lws *wsi, char *out, int out_len, void *data,
        s->out_len  = out_len;
        s->output = output;
        s->pos = 0;
+       s->sum = 0;
        s->mp = 0;
        s->state = US_NAME;
        s->name[0] = '\0';
@@ -2082,13 +2870,14 @@ static int
 lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int len)
 {
        int n, m, hit = 0;
-       char sum = 0, c;
+       char c, was_end = 0;
 
        while (len--) {
                if (s->pos == s->out_len - s->mp - 1) {
                        if (s->output(s->data, s->name, &s->out, s->pos, 0))
                                return -1;
 
+                       was_end = s->pos;
                        s->pos = 0;
                }
                switch (s->state) {
@@ -2147,7 +2936,7 @@ lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int le
                                return -1;
 
                        in++;
-                       sum = n << 4;
+                       s->sum = n << 4;
                        s->state++;
                        break;
 
@@ -2157,7 +2946,7 @@ lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int le
                                return -1;
 
                        in++;
-                       s->out[s->pos++] = sum | n;
+                       s->out[s->pos++] = s->sum | n;
                        s->state = US_IDLE;
                        break;
 
@@ -2165,6 +2954,7 @@ lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int le
                /* states for multipart / mime style */
 
                case MT_LOOK_BOUND_IN:
+retry_as_first:
                        if (*in == s->mime_boundary[s->mp] &&
                            s->mime_boundary[s->mp]) {
                                in++;
@@ -2173,7 +2963,7 @@ lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int le
                                        s->mp = 0;
                                        s->state = MT_IGNORE1;
 
-                                       if (s->pos)
+                                       if (s->pos || was_end)
                                                if (s->output(s->data, s->name,
                                                      &s->out, s->pos, 1))
                                                        return -1;
@@ -2191,8 +2981,11 @@ lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in, int le
                                n = 0;
                                if (!s->boundary_real_crlf)
                                        n = 2;
+
                                memcpy(s->out + s->pos, s->mime_boundary + n, s->mp - n);
                                s->pos += s->mp;
+                               s->mp = 0;
+                               goto retry_as_first;
                        }
 
                        s->out[s->pos++] = *in;
@@ -2355,6 +3148,8 @@ struct lws_spa {
        char *storage;
        char *end;
        int max_storage;
+
+       char finalized;
 };
 
 static int
@@ -2447,7 +3242,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;
 
@@ -2467,9 +3262,13 @@ LWS_VISIBLE LWS_EXTERN int
 lws_spa_process(struct lws_spa *ludspa, const char *in, int len)
 {
        if (!ludspa) {
-               lwsl_err("%s: NULL spa\n");
+               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);
 }
 
@@ -2499,6 +3298,8 @@ lws_spa_finalize(struct lws_spa *spa)
                spa->s = NULL;
        }
 
+       spa->finalized = 1;
+
        return 0;
 }
 
@@ -2512,7 +3313,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);
@@ -2522,6 +3328,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)
@@ -2543,12 +3370,12 @@ lws_chunked_html_process(struct lws_process_html_args *args,
                }
 
                if ((!s->pos && *sp == '$') || s->pos) {
-                       int hits = 0, hit;
+                       int hits = 0, hit = 0;
 
                        if (!s->pos)
                                s->start = sp;
                        s->swallow[s->pos++] = *sp;
-                       if (s->pos == sizeof(s->swallow))
+                       if (s->pos == sizeof(s->swallow) - 1)
                                goto skip;
                        for (n = 0; n < s->count_vars; n++)
                                if (!strncmp(s->swallow, s->vars[n], s->pos)) {