fix onopen browser context patch
[profile/ivi/libwebsockets.git] / lib / libwebsockets.c
index 72a3fe5..b05f79e 100644 (file)
@@ -25,6 +25,7 @@
 
 #else
 #include <ifaddrs.h>
+#include <sys/un.h>
 #endif
 
 #ifdef LWS_OPENSSL_SUPPORT
@@ -152,6 +153,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
        int ret;
        int m;
        struct lws_tokens eff_buf;
+       struct libwebsocket_extension *ext;
 
        if (!wsi)
                return;
@@ -164,6 +166,35 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
        wsi->close_reason = reason;
 
        /*
+        * are his extensions okay with him closing?  Eg he might be a mux
+        * parent and just his ch1 aspect is closing?
+        */
+
+
+       for (n = 0; n < wsi->count_active_extensions; n++) {
+               if (!wsi->active_extensions[n]->callback)
+                       continue;
+
+               m = wsi->active_extensions[n]->callback(context,
+                       wsi->active_extensions[n], wsi,
+                       LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
+                                      wsi->active_extensions_user[n], NULL, 0);
+
+               /*
+                * if somebody vetoed actually closing him at this time....
+                * up to the extension to track the attempted close, let's
+                * just bail
+                */
+
+               if (m) {
+                       debug("extension vetoed close\n");
+                       return;
+               }
+       }
+
+
+
+       /*
         * flush any tx pending from extensions, since we may send close packet
         * if there are problems with send, just nuke the connection
         */
@@ -181,7 +212,8 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
 
                for (n = 0; n < wsi->count_active_extensions; n++) {
                        m = wsi->active_extensions[n]->callback(
-                               wsi->protocol->owning_server, wsi,
+                                       wsi->protocol->owning_server,
+                                       wsi->active_extensions[n], wsi,
                                        LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
                                   wsi->active_extensions_user[n], &eff_buf, 0);
                        if (m < 0) {
@@ -219,6 +251,9 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
 
        if (old_state == WSI_STATE_ESTABLISHED &&
                                          reason != LWS_CLOSE_STATUS_NOSTATUS) {
+
+               debug("sending close indication...\n");
+
                n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
                                                            0, LWS_WRITE_CLOSE);
                if (!n) {
@@ -234,7 +269,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
                        libwebsocket_set_timeout(wsi,
                                                  PENDING_TIMEOUT_CLOSE_ACK, 5);
 
-                       fprintf(stderr, "sent close indication, awaiting ack\n");
+                       debug("sent close indication, awaiting ack\n");
 
                        return;
                }
@@ -243,12 +278,16 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
        }
 
 just_kill_connection:
+
+       debug("libwebsocket_close_and_free_session: just_kill_connection\n");
+
        /*
         * we won't be servicing or receiving anything further from this guy
         * remove this fd from wsi mapping hashtable
         */
 
-       delete_from_fd(context, wsi->sock);
+       if (wsi->sock)
+               delete_from_fd(context, wsi->sock);
 
        /* delete it from the internal poll list if still present */
 
@@ -265,8 +304,8 @@ just_kill_connection:
        }
 
        /* remove also from external POLL support via protocol 0 */
-
-       context->protocols[0].callback(context, wsi,
+       if (wsi->sock)
+               context->protocols[0].callback(context, wsi,
                    LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
 
        wsi->state = WSI_STATE_DEAD_SOCKET;
@@ -274,9 +313,16 @@ just_kill_connection:
        /* tell the user it's all over for this guy */
 
        if (wsi->protocol && wsi->protocol->callback &&
-                                            old_state == WSI_STATE_ESTABLISHED)
+                               ((old_state == WSI_STATE_ESTABLISHED) ||
+                                (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
+                                (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
+               debug("calling back CLOSED\n");
                wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
                                                      wsi->user_space, NULL, 0);
+       } else
+               debug("not calling back closed due to old_state=%d\n",
+                                                                    old_state);
+
 
        /* deallocate any active extension contexts */
 
@@ -284,19 +330,36 @@ just_kill_connection:
                if (!wsi->active_extensions[n]->callback)
                        continue;
 
-               wsi->active_extensions[n]->callback(context, wsi,
-                       LWS_EXT_CALLBACK_DESTROY,
-                       wsi->active_extensions_user[n], NULL, 0);
+               wsi->active_extensions[n]->callback(context,
+                       wsi->active_extensions[n], wsi,
+                               LWS_EXT_CALLBACK_DESTROY,
+                                      wsi->active_extensions_user[n], NULL, 0);
 
                free(wsi->active_extensions_user[n]);
        }
 
+       /*
+        * inform all extensions in case they tracked this guy out of band
+        * even though not active on him specifically
+        */
+
+       ext = context->extensions;
+       while (ext && ext->callback) {
+               ext->callback(context, ext, wsi,
+                               LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
+                                      NULL, NULL, 0);
+               ext++;
+       }
+
        /* free up his parsing allocations */
 
        for (n = 0; n < WSI_TOKEN_COUNT; n++)
                if (wsi->utf8_token[n].token)
                        free(wsi->utf8_token[n].token);
 
+       if (wsi->c_address)
+               free(wsi->c_address);
+
 /*     fprintf(stderr, "closing fd=%d\n", wsi->sock); */
 
 #ifdef LWS_OPENSSL_SUPPORT
@@ -313,9 +376,11 @@ just_kill_connection:
 #endif
                shutdown(wsi->sock, SHUT_RDWR);
 #ifdef WIN32
-               closesocket(wsi->sock);
+               if (wsi->sock)
+                       closesocket(wsi->sock);
 #else
-               close(wsi->sock);
+               if (wsi->sock)
+                       close(wsi->sock);
 #endif
 #ifdef LWS_OPENSSL_SUPPORT
        }
@@ -369,8 +434,9 @@ libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
        struct hostent *host;
        struct hostent *host1;
        char ip[128];
-       char *p;
+       unsigned char *p;
        int n;
+       struct sockaddr_un *un;
 
        rip[0] = '\0';
        name[0] = '\0';
@@ -394,17 +460,28 @@ libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
        host1 = gethostbyname(host->h_name);
        if (host1 == NULL)
                return;
-       p = (char *)host1;
+       p = (unsigned char *)host1;
        n = 0;
        while (p != NULL) {
-               p = host1->h_addr_list[n++];
+               p = (unsigned char *)host1->h_addr_list[n++];
                if (p == NULL)
                        continue;
-               if (host1->h_addrtype != AF_INET)
+               if ((host1->h_addrtype != AF_INET)
+#ifdef AF_LOCAL
+                       && (host1->h_addrtype != AF_LOCAL)
+#endif
+                       )
                        continue;
 
-               sprintf(ip, "%d.%d.%d.%d",
-                               p[0], p[1], p[2], p[3]);
+               if (host1->h_addrtype == AF_INET)
+                       sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
+#ifdef AF_LOCAL
+               else {
+                       un = (struct sockaddr_un *)p;
+                       strncpy(ip, un->sun_path, sizeof(ip) -1);
+                       ip[sizeof(ip) - 1] = '\0';
+               }
+#endif
                p = NULL;
                strncpy(rip, ip, rip_len);
                rip[rip_len - 1] = '\0';
@@ -427,6 +504,12 @@ int libwebsockets_get_random(struct libwebsocket_context *context,
        return n;
 }
 
+unsigned char *
+libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md)
+{
+       return SHA1(d, n, md);
+}
+
 void libwebsockets_00_spaceout(char *key, int spaces, int seed)
 {
        char *p;
@@ -487,7 +570,7 @@ int lws_send_pipe_choked(struct libwebsocket *wsi)
        return 0;
 }
 
-static int
+int
 lws_handle_POLLOUT_event(struct libwebsocket_context *context,
                                struct libwebsocket *wsi, struct pollfd *pollfd)
 {
@@ -495,8 +578,24 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
        int n;
        int ret;
        int m;
+       int handled = 0;
+
+       for (n = 0; n < wsi->count_active_extensions; n++) {
+               if (!wsi->active_extensions[n]->callback)
+                       continue;
+
+               m = wsi->active_extensions[n]->callback(context,
+                       wsi->active_extensions[n], wsi,
+                       LWS_EXT_CALLBACK_IS_WRITEABLE,
+                                      wsi->active_extensions_user[n], NULL, 0);
+               if (m > handled)
+                       handled = m;
+       }
+
+       if (handled == 1)
+               goto notify_action;
 
-       if (!wsi->extension_data_pending)
+       if (!wsi->extension_data_pending || handled == 2)
                goto user_service;
 
        /*
@@ -520,7 +619,8 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
 
                for (n = 0; n < wsi->count_active_extensions; n++) {
                        m = wsi->active_extensions[n]->callback(
-                               wsi->protocol->owning_server, wsi,
+                               wsi->protocol->owning_server,
+                               wsi->active_extensions[n], wsi,
                                        LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
                                   wsi->active_extensions_user[n], &eff_buf, 0);
                        if (m < 0) {
@@ -558,7 +658,7 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
                        /* no we could add more */
                        continue;
 
-               fprintf(stderr, "choked in POLLOUT service\n");
+               debug("choked in POLLOUT service\n");
 
                /*
                 * Yes, he's choked.  Leave the POLLOUT masked on so we will
@@ -575,241 +675,925 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
 user_service:
        /* one shot */
 
-       pollfd->events &= ~POLLOUT;
+       if (pollfd) {
+               pollfd->events &= ~POLLOUT;
 
-       /* external POLL support via protocol 0 */
-       context->protocols[0].callback(context, wsi,
-               LWS_CALLBACK_CLEAR_MODE_POLL_FD,
-               (void *)(long)wsi->sock, NULL, POLLOUT);
+               /* external POLL support via protocol 0 */
+               context->protocols[0].callback(context, wsi,
+                       LWS_CALLBACK_CLEAR_MODE_POLL_FD,
+                       (void *)(long)wsi->sock, NULL, POLLOUT);
+       }
 
-       wsi->protocol->callback(context, wsi,
-               LWS_CALLBACK_CLIENT_WRITEABLE,
-               wsi->user_space,
-               NULL, 0);
+notify_action:
+
+       if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
+               n = LWS_CALLBACK_CLIENT_WRITEABLE;
+       else
+               n = LWS_CALLBACK_SERVER_WRITEABLE;
+
+       wsi->protocol->callback(context, wsi, n, wsi->user_space, NULL, 0);
 
        return 0;
 }
 
 
 
-/**
- * libwebsocket_service_fd() - Service polled socket with something waiting
- * @context:   Websocket context
- * @pollfd:    The pollfd entry describing the socket fd and which events
- *             happened.
- *
- *     This function closes any active connections and then frees the
- *     context.  After calling this, any further use of the context is
- *     undefined.
- */
-
-int
-libwebsocket_service_fd(struct libwebsocket_context *context,
-                                                         struct pollfd *pollfd)
+void
+libwebsocket_service_timeout_check(struct libwebsocket_context *context,
+                                    struct libwebsocket *wsi, unsigned int sec)
 {
-       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
-                                                 LWS_SEND_BUFFER_POST_PADDING];
-       struct libwebsocket *wsi;
-       struct libwebsocket *new_wsi;
        int n;
-       int m;
-       size_t len;
-       int accept_fd;
-       unsigned int clilen;
-       struct sockaddr_in cli_addr;
-       struct timeval tv;
-       static const char magic_websocket_guid[] =
-                                        "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
-       static const char magic_websocket_04_masking_guid[] =
-                                        "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
-       char hash[20];
-       char pkt[1024];
-       char *p = &pkt[0];
-       const char *pc;
-       const char *c;
-       int more = 1;
-       int okay = 0;
-       char ext_name[128];
-       struct lws_tokens eff_buf;
-       int ext_count = 0;
-       struct libwebsocket_extension *ext;
 
-#ifdef LWS_OPENSSL_SUPPORT
-       char ssl_err_buf[512];
-#endif
        /*
-        * you can call us with pollfd = NULL to just allow the once-per-second
-        * global timeout checks; if less than a second since the last check
-        * it returns immediately then.
+        * if extensions want in on it (eg, we are a mux parent)
+        * give them a chance to service child timeouts
         */
 
-       gettimeofday(&tv, NULL);
-
-       if (context->last_timeout_check_s != tv.tv_sec) {
-               context->last_timeout_check_s = tv.tv_sec;
+       for (n = 0; n < wsi->count_active_extensions; n++)
+               wsi->active_extensions[n]->callback(
+                                   context, wsi->active_extensions[n],
+                                   wsi, LWS_EXT_CALLBACK_1HZ,
+                                   wsi->active_extensions_user[n], NULL, sec);
 
-               /* global timeout check once per second */
+       if (!wsi->pending_timeout)
+               return;
+                         
+       /*
+        * if we went beyond the allowed time, kill the
+        * connection
+        */
 
-               for (n = 0; n < context->fds_count; n++) {
-                       wsi = wsi_from_fd(context, context->fds[n].fd);
-                       if (!wsi->pending_timeout)
-                               continue;
+       if (sec > wsi->pending_timeout_limit) {
+               debug("TIMEDOUT WAITING\n");
+               libwebsocket_close_and_free_session(context,
+                               wsi, LWS_CLOSE_STATUS_NOSTATUS);
+       }
+}
 
-                       /*
-                        * if we went beyond the allowed time, kill the
-                        * connection
-                        */
+struct libwebsocket *
+libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
+{
+       struct libwebsocket *new_wsi;
+       int n;
 
-                       if (tv.tv_sec > wsi->pending_timeout_limit) {
-                               fprintf(stderr, "TIMEDOUT WAITING\n");
-                               libwebsocket_close_and_free_session(context,
-                                               wsi, LWS_CLOSE_STATUS_NOSTATUS);
-                       }
-               }
+       new_wsi = malloc(sizeof(struct libwebsocket));
+       if (new_wsi == NULL) {
+               fprintf(stderr, "Out of memory for new connection\n");
+               return NULL;
        }
 
-       /* just here for timeout management? */
+       memset(new_wsi, 0, sizeof (struct libwebsocket));
+       new_wsi->count_active_extensions = 0;
+       new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
 
-       if (pollfd == NULL)
-               return 0;
+       /* intialize the instance struct */
 
-       /* no, here to service a socket descriptor */
+       new_wsi->state = WSI_STATE_HTTP;
+       new_wsi->name_buffer_pos = 0;
+       new_wsi->mode = LWS_CONNMODE_WS_SERVING;
 
-       wsi = wsi_from_fd(context, pollfd->fd);
+       for (n = 0; n < WSI_TOKEN_COUNT; n++) {
+               new_wsi->utf8_token[n].token = NULL;
+               new_wsi->utf8_token[n].token_len = 0;
+       }
 
-       if (wsi == NULL)
-               return 1;
+       /*
+        * these can only be set once the protocol is known
+        * we set an unestablished connection's protocol pointer
+        * to the start of the supported list, so it can look
+        * for matching ones during the handshake
+        */
+       new_wsi->protocol = context->protocols;
+       new_wsi->user_space = NULL;
 
-       switch (wsi->mode) {
-       case LWS_CONNMODE_SERVER_LISTENER:
+       /*
+        * Default protocol is 76 / 00
+        * After 76, there's a header specified to inform which
+        * draft the client wants, when that's seen we modify
+        * the individual connection's spec revision accordingly
+        */
+       new_wsi->ietf_spec_revision = 0;
 
-               /* pollin means a client has connected to us then */
+       return new_wsi;
+}
 
-               if (!pollfd->revents & POLLIN)
-                       break;
+char *
+libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
+               struct libwebsocket *wsi, char *pkt)
+{
+       char hash[20];
+       char *p = pkt;
+       int n;
+       struct libwebsocket_extension *ext;
+       struct libwebsocket_extension *ext1;
+       int ext_count = 0;
+       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
+                                                 LWS_SEND_BUFFER_POST_PADDING];
+       static const char magic_websocket_guid[] =
+                                        "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 
-               /* listen socket got an unencrypted connection... */
+       /*
+        * create the random key
+        */
 
-               clilen = sizeof(cli_addr);
-               accept_fd  = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
-                                                                      &clilen);
-               if (accept_fd < 0) {
-                       fprintf(stderr, "ERROR on accept");
-                       break;
-               }
+       n = libwebsockets_get_random(context, hash, 16);
+       if (n != 16) {
+               fprintf(stderr, "Unable to read from random dev %s\n",
+                                               SYSTEM_RANDOM_FILEPATH);
+               free(wsi->c_path);
+               free(wsi->c_host);
+               if (wsi->c_origin)
+                       free(wsi->c_origin);
+               if (wsi->c_protocol)
+                       free(wsi->c_protocol);
+               libwebsocket_close_and_free_session(context, wsi,
+                                            LWS_CLOSE_STATUS_NOSTATUS);
+               return NULL;
+       }
 
-               if (context->fds_count >= MAX_CLIENTS) {
-                       fprintf(stderr, "too busy to accept new client\n");
-#ifdef WIN32
-                       closesocket(accept_fd);
-#else
-                       close(accept_fd);
-#endif
-                       break;
-               }
+       lws_b64_encode_string(hash, 16, wsi->key_b64,
+                                                  sizeof wsi->key_b64);
 
-               /*
-                * look at who we connected to and give user code a chance
-                * to reject based on client IP.  There's no protocol selected
-                * yet so we issue this to protocols[0]
-                */
+       /*
+        * 00 example client handshake
+        *
+        * GET /socket.io/websocket HTTP/1.1
+        * Upgrade: WebSocket
+        * Connection: Upgrade
+        * Host: 127.0.0.1:9999
+        * Origin: http://127.0.0.1
+        * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7  92 ^
+        * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
+        * Cookie: socketio=websocket
+        *
+        * (Á®Ä0¶†≥
+        *
+        * 04 example client handshake
+        *
+        * GET /chat HTTP/1.1
+        * Host: server.example.com
+        * Upgrade: websocket
+        * Connection: Upgrade
+        * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
+        * Sec-WebSocket-Origin: http://example.com
+        * Sec-WebSocket-Protocol: chat, superchat
+        * Sec-WebSocket-Version: 4
+        */
 
-               if ((context->protocols[0].callback)(context, wsi,
-                               LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
-                                            (void*)(long)accept_fd, NULL, 0)) {
-                       fprintf(stderr, "Callback denied network connection\n");
-#ifdef WIN32
-                       closesocket(accept_fd);
-#else
-                       close(accept_fd);
-#endif
-                       break;
-               }
+       p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
 
-               /* accepting connection to main listener */
+       if (wsi->ietf_spec_revision == 0) {
+               unsigned char spaces_1, spaces_2;
+               unsigned int max_1, max_2;
+               unsigned int num_1, num_2;
+               unsigned long product_1, product_2;
+               char key_1[40];
+               char key_2[40];
+               unsigned int seed;
+               unsigned int count;
+               char challenge[16];
 
-               new_wsi = malloc(sizeof(struct libwebsocket));
-               if (new_wsi == NULL) {
-                       fprintf(stderr, "Out of memory for new connection\n");
-                       break;
-               }
+               libwebsockets_get_random(context, &spaces_1,
+                                                         sizeof(char));
+               libwebsockets_get_random(context, &spaces_2,
+                                                         sizeof(char));
 
-               memset(new_wsi, 0, sizeof (struct libwebsocket));
-               new_wsi->sock = accept_fd;
-               new_wsi->count_active_extensions = 0;
-               new_wsi->pending_timeout = NO_PENDING_TIMEOUT;
+               spaces_1 = (spaces_1 % 12) + 1;
+               spaces_2 = (spaces_2 % 12) + 1;
 
-#ifdef LWS_OPENSSL_SUPPORT
-               new_wsi->ssl = NULL;
+               max_1 = 4294967295 / spaces_1;
+               max_2 = 4294967295 / spaces_2;
 
-               if (context->use_ssl) {
+               libwebsockets_get_random(context, &num_1, sizeof(int));
+               libwebsockets_get_random(context, &num_2, sizeof(int));
 
-                       new_wsi->ssl = SSL_new(context->ssl_ctx);
-                       if (new_wsi->ssl == NULL) {
-                               fprintf(stderr, "SSL_new failed: %s\n",
-                                   ERR_error_string(SSL_get_error(
-                                   new_wsi->ssl, 0), NULL));
-                                   libwebsockets_decode_ssl_error();
-                               free(new_wsi);
-                               break;
-                       }
+               num_1 = (num_1 % max_1);
+               num_2 = (num_2 % max_2);
 
-                       SSL_set_fd(new_wsi->ssl, accept_fd);
+               challenge[0] = num_1 >> 24;
+               challenge[1] = num_1 >> 16;
+               challenge[2] = num_1 >> 8;
+               challenge[3] = num_1;
+               challenge[4] = num_2 >> 24;
+               challenge[5] = num_2 >> 16;
+               challenge[6] = num_2 >> 8;
+               challenge[7] = num_2;
 
-                       n = SSL_accept(new_wsi->ssl);
-                       if (n != 1) {
-                               /*
-                                * browsers seem to probe with various
-                                * ssl params which fail then retry
-                                * and succeed
-                                */
-                               debug("SSL_accept failed skt %u: %s\n",
-                                     pollfd->fd,
-                                     ERR_error_string(SSL_get_error(
-                                     new_wsi->ssl, n), NULL));
-                               SSL_free(
-                                      new_wsi->ssl);
-                               free(new_wsi);
-                               break;
-                       }
-                       
-                       debug("accepted new SSL conn  "
-                             "port %u on fd=%d SSL ver %s\n",
-                               ntohs(cli_addr.sin_port), accept_fd,
-                                 SSL_get_version(new_wsi->ssl));
+               product_1 = num_1 * spaces_1;
+               product_2 = num_2 * spaces_2;
 
-               } else
-#endif
-                       debug("accepted new conn  port %u on fd=%d\n",
-                                         ntohs(cli_addr.sin_port), accept_fd);
+               sprintf(key_1, "%lu", product_1);
+               sprintf(key_2, "%lu", product_2);
 
-               /* intialize the instance struct */
+               libwebsockets_get_random(context, &seed, sizeof(int));
+               libwebsockets_get_random(context, &count, sizeof(int));
 
-               new_wsi->state = WSI_STATE_HTTP;
-               new_wsi->name_buffer_pos = 0;
-               new_wsi->mode = LWS_CONNMODE_WS_SERVING;
+               libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
 
-               for (n = 0; n < WSI_TOKEN_COUNT; n++) {
-                       new_wsi->utf8_token[n].token = NULL;
-                       new_wsi->utf8_token[n].token_len = 0;
-               }
+               libwebsockets_get_random(context, &seed, sizeof(int));
+               libwebsockets_get_random(context, &count, sizeof(int));
 
-               /*
-                * these can only be set once the protocol is known
-                * we set an unestablished connection's protocol pointer
-                * to the start of the supported list, so it can look
-                * for matching ones during the handshake
-                */
-               new_wsi->protocol = context->protocols;
-               new_wsi->user_space = NULL;
+               libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
 
-               /*
-                * Default protocol is 76 / 00
-                * After 76, there's a header specified to inform which
-                * draft the client wants, when that's seen we modify
-                * the individual connection's spec revision accordingly
-                */
-               new_wsi->ietf_spec_revision = 0;
+               libwebsockets_get_random(context, &seed, sizeof(int));
+
+               libwebsockets_00_spaceout(key_1, spaces_1, seed);
+               libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
+
+               p += sprintf(p, "Upgrade: WebSocket\x0d\x0a"
+                       "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
+                       wsi->c_host);
+               if (wsi->c_origin)
+                       p += sprintf(p, "Origin: %s\x0d\x0a",
+                       wsi->c_origin);
+
+               if (wsi->c_protocol)
+                       p += sprintf(p, "Sec-WebSocket-Protocol: %s"
+                                        "\x0d\x0a", wsi->c_protocol);
+
+               p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a",
+                       key_1);
+               p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a",
+                       key_2);
+
+               /* give userland a chance to append, eg, cookies */
+
+               context->protocols[0].callback(context, wsi,
+                       LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
+                               NULL, &p, (pkt + sizeof(pkt)) - p - 12);
+
+               p += sprintf(p, "\x0d\x0a");
+
+               if (libwebsockets_get_random(context, p, 8) != 8)
+                       return NULL;
+               memcpy(&challenge[8], p, 8);
+               p += 8;
+
+               /* precompute what we want to see from the server */
+
+               MD5((unsigned char *)challenge, 16,
+                  (unsigned char *)wsi->initial_handshake_hash_base64);
+
+               goto issue_hdr;
+       }
+
+       p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
+       p += sprintf(p, "Upgrade: websocket\x0d\x0a");
+       p += sprintf(p, "Connection: Upgrade\x0d\x0a"
+                               "Sec-WebSocket-Key: ");
+       strcpy(p, wsi->key_b64);
+       p += strlen(wsi->key_b64);
+       p += sprintf(p, "\x0d\x0a");
+       if (wsi->c_origin)
+               p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
+                                                        wsi->c_origin);
+       if (wsi->c_protocol)
+               p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
+                                                      wsi->c_protocol);
+
+       /* tell the server what extensions we could support */
+
+       p += sprintf(p, "Sec-WebSocket-Extensions: ");
+
+       ext =context->extensions;
+       while (ext && ext->callback) {
+
+               n = 0;
+               ext1 = context->extensions;
+               while (ext1 && ext1->callback) {
+
+                       n |= ext1->callback(context, ext1, wsi,
+                               LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
+                                       NULL, (char *)ext->name, 0);
+
+                       ext1++;
+               }
+
+               if (n) {
+
+                       /* an extension vetos us */
+                       debug("ext %s vetoed\n", (char *)ext->name);
+                       ext++;
+                       continue;
+               }
+
+               n = context->protocols[0].callback(context, wsi,
+                       LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
+                               wsi->user_space, (char *)ext->name, 0);
+
+               /*
+                * zero return from callback means
+                * go ahead and allow the extension,
+                * it's what we get if the callback is
+                * unhandled
+                */
+
+               if (n) {
+                       ext++;
+                       continue;
+               }
+
+               /* apply it */
+
+               if (ext_count)
+                       *p++ = ',';
+               p += sprintf(p, "%s", ext->name);
+               ext_count++;
+
+               ext++;
+       }
+
+       p += sprintf(p, "\x0d\x0a");
+
+       if (wsi->ietf_spec_revision)
+               p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
+                                              wsi->ietf_spec_revision);
+
+       /* give userland a chance to append, eg, cookies */
+
+       context->protocols[0].callback(context, wsi,
+               LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
+               NULL, &p, (pkt + sizeof(pkt)) - p - 12);
+
+       p += sprintf(p, "\x0d\x0a");
+
+       /* prepare the expected server accept response */
+
+       strcpy((char *)buf, wsi->key_b64);
+       strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
+
+       SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
+
+       lws_b64_encode_string(hash, 20,
+                       wsi->initial_handshake_hash_base64,
+                            sizeof wsi->initial_handshake_hash_base64);
+
+issue_hdr:
+
+//     puts(pkt);
+
+       /* done with these now */
+
+       free(wsi->c_path);
+       free(wsi->c_host);
+       if (wsi->c_origin)
+               free(wsi->c_origin);
+
+       return p;
+}
+
+int
+lws_client_interpret_server_handshake(struct libwebsocket_context *context,
+               struct libwebsocket *wsi)
+{
+       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
+                                                 LWS_SEND_BUFFER_POST_PADDING];
+       char pkt[1024];
+       char *p = &pkt[0];
+       const char *pc;
+       const char *c;
+       int more = 1;
+       int okay = 0;
+       char ext_name[128];
+       struct libwebsocket_extension *ext;
+       void *v;
+       int len = 0;
+       int n;
+       static const char magic_websocket_04_masking_guid[] =
+                                        "61AC5F19-FBBA-4540-B96F-6561F1AB40A8";
+
+       /*
+        * 00 / 76 -->
+        *
+        * HTTP/1.1 101 WebSocket Protocol Handshake
+        * Upgrade: WebSocket
+        * Connection: Upgrade
+        * Sec-WebSocket-Origin: http://127.0.0.1
+        * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
+        *
+        * xxxxxxxxxxxxxxxx
+        */
+
+       if (wsi->ietf_spec_revision == 0) {
+               if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
+                       !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
+                       !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
+                       !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
+                       (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
+                       wsi->c_protocol != NULL)) {
+                       debug("libwebsocket_client_handshake "
+                                       "missing required header(s)\n");
+                       pkt[len] = '\0';
+                       debug("%s", pkt);
+                       goto bail3;
+               }
+
+               strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
+               if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
+                                                           "101", 3)) {
+                       fprintf(stderr, "libwebsocket_client_handshake "
+                               "server sent bad HTTP response '%s'\n",
+                               wsi->utf8_token[WSI_TOKEN_HTTP].token);
+                       goto bail3;
+               }
+
+               if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len <
+                                                                  16) {
+                       fprintf(stderr, "libwebsocket_client_handshake "
+                               "challenge reply too short %d\n",
+                               wsi->utf8_token[
+                                       WSI_TOKEN_CHALLENGE].token_len);
+                       pkt[len] = '\0';
+                       debug("%s", pkt);
+                       goto bail3;
+
+               }
+
+               goto select_protocol;
+       }
+
+       /*
+        * well, what the server sent looked reasonable for syntax.
+        * Now let's confirm it sent all the necessary headers
+        */
+#if 0
+       fprintf(stderr, "WSI_TOKEN_HTTP: %d\n", wsi->utf8_token[WSI_TOKEN_HTTP].token_len);
+       fprintf(stderr, "WSI_TOKEN_UPGRADE: %d\n", wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len);
+       fprintf(stderr, "WSI_TOKEN_CONNECTION: %d\n", wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len);
+       fprintf(stderr, "WSI_TOKEN_ACCEPT: %d\n", wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len);
+       fprintf(stderr, "WSI_TOKEN_NONCE: %d\n", wsi->utf8_token[WSI_TOKEN_NONCE].token_len);
+       fprintf(stderr, "WSI_TOKEN_PROTOCOL: %d\n", wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len);
+#endif
+        if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
+               !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
+               !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
+               !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
+               (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
+                                  wsi->ietf_spec_revision == 4) ||
+               (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
+                                                wsi->c_protocol != NULL)) {
+                debug("libwebsocket_client_handshake "
+                                       "missing required header(s)\n");
+               pkt[len] = '\0';
+               debug("%s", pkt);
+               goto bail3;
+       }
+
+       /*
+        * Everything seems to be there, now take a closer look at what
+        * is in each header
+        */
+
+       strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
+       if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) {
+               fprintf(stderr, "libwebsocket_client_handshake "
+                               "server sent bad HTTP response '%s'\n",
+                                wsi->utf8_token[WSI_TOKEN_HTTP].token);
+               goto bail3;
+       }
+
+       strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
+       if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
+                                                        "websocket")) {
+               fprintf(stderr, "libwebsocket_client_handshake server "
+                               "sent bad Upgrade header '%s'\n",
+                                 wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
+               goto bail3;
+       }
+
+       strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
+       if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
+                                                          "upgrade")) {
+               fprintf(stderr, "libwebsocket_client_handshake server "
+                               "sent bad Connection hdr '%s'\n",
+                          wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
+               goto bail3;
+       }
+
+select_protocol:
+       pc = wsi->c_protocol;
+       if (pc == NULL)
+               fprintf(stderr, "lws_client_interpret_server_handshake: NULL c_protocol\n");
+       else
+               debug("lws_client_interpret_server_handshake: cPprotocol='%s'\n", pc);
+
+       /*
+        * confirm the protocol the server wants to talk was in the list
+        * of protocols we offered
+        */
+
+       if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
+
+               fprintf(stderr, "lws_client_interpret_server_handshake WSI_TOKEN_PROTOCOL is null\n");
+               /*
+                * no protocol name to work from,
+                * default to first protocol
+                */
+               wsi->protocol = &context->protocols[0];
+
+               free(wsi->c_protocol);
+
+               goto check_accept;
+       }
+
+       while (*pc && !okay) {
+               if ((!strncmp(pc,
+                       wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
+                  wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
+        (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
+          pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
+                       okay = 1;
+                       continue;
+               }
+               while (*pc && *pc != ',')
+                       pc++;
+               while (*pc && *pc != ' ')
+                       pc++;
+       }
+
+       /* done with him now */
+
+       if (wsi->c_protocol)
+               free(wsi->c_protocol);
+
+
+       if (!okay) {
+               fprintf(stderr, "libwebsocket_client_handshake server "
+                                       "sent bad protocol '%s'\n",
+                                wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
+               goto bail2;
+       }
+
+       /*
+        * identify the selected protocol struct and set it
+        */
+       n = 0;
+       wsi->protocol = NULL;
+       while (context->protocols[n].callback) {
+               if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
+                                          context->protocols[n].name) == 0)
+                       wsi->protocol = &context->protocols[n];
+               n++;
+       }
+
+       if (wsi->protocol == NULL) {
+               fprintf(stderr, "libwebsocket_client_handshake server "
+                               "requested protocol '%s', which we "
+                               "said we supported but we don't!\n",
+                                wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
+               goto bail2;
+       }
+
+
+       /* instantiate the accepted extensions */
+
+       if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) {
+               debug("no client extenstions allowed by server \n");
+               goto check_accept;
+       }
+
+       /*
+        * break down the list of server accepted extensions
+        * and go through matching them or identifying bogons
+        */
+
+       c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
+       n = 0;
+       while (more) {
+
+               if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
+                       ext_name[n] = *c++;
+                       if (n < sizeof(ext_name) - 1)
+                               n++;
+                       continue;
+               }
+               ext_name[n] = '\0';
+               if (!*c)
+                       more = 0;
+               else {
+                       c++;
+                       if (!n)
+                               continue;
+               }
+
+               /* check we actually support it */
+
+               debug("checking client ext %s\n", ext_name);
+
+               n = 0;
+               ext = wsi->protocol->owning_server->extensions;
+               while (ext && ext->callback) {
+
+                       if (strcmp(ext_name, ext->name)) {
+                               ext++;
+                               continue;
+                       }
+
+                       n = 1;
+
+                       debug("instantiating client ext %s\n", ext_name);
+
+                       /* instantiate the extension on this conn */
+
+                       wsi->active_extensions_user[
+                               wsi->count_active_extensions] =
+                                        malloc(ext->per_session_data_size);
+                       memset(wsi->active_extensions_user[
+                               wsi->count_active_extensions], 0,
+                                                   ext->per_session_data_size);
+                       wsi->active_extensions[
+                                 wsi->count_active_extensions] = ext;
+
+                       /* allow him to construct his context */
+
+                       ext->callback(wsi->protocol->owning_server,
+                               ext, wsi,
+                                  LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
+                                       wsi->active_extensions_user[
+                                        wsi->count_active_extensions],
+                                                                  NULL, 0);
+
+                       wsi->count_active_extensions++;
+
+                       ext++;
+               }
+
+               if (n == 0) {
+                       fprintf(stderr, "Server said we should use"
+                                 "an unknown extension '%s'!\n", ext_name);
+                       goto bail2;
+               }
+
+               n = 0;
+       }
+
+
+check_accept:
+
+       if (wsi->ietf_spec_revision == 0) {
+
+               if (memcmp(wsi->initial_handshake_hash_base64,
+                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
+                       fprintf(stderr, "libwebsocket_client_handshake "
+                                          "failed 00 challenge compare\n");
+                               pkt[len] = '\0';
+                               fprintf(stderr, "%s", pkt);
+                               goto bail2;
+               }
+
+               goto accept_ok;
+       }
+
+       /*
+        * Confirm his accept token is the one we precomputed
+        */
+
+       if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
+                                 wsi->initial_handshake_hash_base64)) {
+               fprintf(stderr, "libwebsocket_client_handshake server "
+                       "sent bad ACCEPT '%s' vs computed '%s'\n",
+                       wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
+                                       wsi->initial_handshake_hash_base64);
+               goto bail2;
+       }
+
+       if (wsi->ietf_spec_revision == 4) {
+               /*
+                * Calculate the 04 masking key to use when
+                * sending data to server
+                */
+
+               strcpy((char *)buf, wsi->key_b64);
+               p = (char *)buf + strlen(wsi->key_b64);
+               strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
+               p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
+               strcpy(p, magic_websocket_04_masking_guid);
+               SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
+       }
+       accept_ok:
+
+       /* allocate the per-connection user memory (if any) */
+       if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
+         goto bail2;
+
+       /* clear his proxy connection timeout */
+
+       libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
+
+       /* mark him as being alive */
+
+       wsi->state = WSI_STATE_ESTABLISHED;
+       wsi->mode = LWS_CONNMODE_WS_CLIENT;
+
+       fprintf(stderr, "handshake OK for protocol %s\n",
+                                                  wsi->protocol->name);
+
+       /* call him back to inform him he is up */
+
+       wsi->protocol->callback(context, wsi,
+                        LWS_CALLBACK_CLIENT_ESTABLISHED,
+                        wsi->user_space,
+                        NULL, 0);
+
+       /*
+        * inform all extensions, not just active ones since they
+        * already know
+        */
+
+       ext = context->extensions;
+
+       while (ext && ext->callback) {
+               v = NULL;
+               for (n = 0; n < wsi->count_active_extensions; n++)
+                       if (wsi->active_extensions[n] == ext)
+                               v = wsi->active_extensions_user[n];
+
+               ext->callback(context, ext, wsi,
+                         LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
+               ext++;
+       }
+
+       return 0;
+
+bail3:
+       if (wsi->c_protocol)
+               free(wsi->c_protocol);
+
+bail2:
+       libwebsocket_close_and_free_session(context, wsi,
+                                                LWS_CLOSE_STATUS_NOSTATUS);
+       return 1;
+}
+
+
+
+/**
+ * libwebsocket_service_fd() - Service polled socket with something waiting
+ * @context:   Websocket context
+ * @pollfd:    The pollfd entry describing the socket fd and which events
+ *             happened.
+ *
+ *     This function closes any active connections and then frees the
+ *     context.  After calling this, any further use of the context is
+ *     undefined.
+ */
+
+int
+libwebsocket_service_fd(struct libwebsocket_context *context,
+                                                         struct pollfd *pollfd)
+{
+       unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + MAX_BROADCAST_PAYLOAD +
+                                                 LWS_SEND_BUFFER_POST_PADDING];
+       struct libwebsocket *wsi;
+       struct libwebsocket *new_wsi;
+       int n;
+       int m;
+       ssize_t len;
+       int accept_fd;
+       unsigned int clilen;
+       struct sockaddr_in cli_addr;
+       struct timeval tv;
+       char pkt[1024];
+       char *p = &pkt[0];
+       int more = 1;
+       struct lws_tokens eff_buf;
+       int opt = 1;
+       char c;
+
+#ifdef LWS_OPENSSL_SUPPORT
+       char ssl_err_buf[512];
+#endif
+       /*
+        * you can call us with pollfd = NULL to just allow the once-per-second
+        * global timeout checks; if less than a second since the last check
+        * it returns immediately then.
+        */
+
+       gettimeofday(&tv, NULL);
+
+       if (context->last_timeout_check_s != tv.tv_sec) {
+               context->last_timeout_check_s = tv.tv_sec;
+
+               /* global timeout check once per second */
+
+               for (n = 0; n < context->fds_count; n++) {
+                       wsi = wsi_from_fd(context, context->fds[n].fd);
+
+                       libwebsocket_service_timeout_check(context, wsi,
+                                                                    tv.tv_sec);
+               }
+       }
+
+       /* just here for timeout management? */
+
+       if (pollfd == NULL)
+               return 0;
+
+       /* no, here to service a socket descriptor */
+
+       wsi = wsi_from_fd(context, pollfd->fd);
+
+       if (wsi == NULL)
+               return 1;
+
+       switch (wsi->mode) {
+       case LWS_CONNMODE_SERVER_LISTENER:
+
+               /* pollin means a client has connected to us then */
+
+               if (!pollfd->revents & POLLIN)
+                       break;
+
+               if (context->fds_count >= MAX_CLIENTS) {
+                       fprintf(stderr, "too busy to accept new client\n");
+                       break;
+               }
+
+               /* listen socket got an unencrypted connection... */
+
+               clilen = sizeof(cli_addr);
+               accept_fd  = accept(pollfd->fd, (struct sockaddr *)&cli_addr,
+                                                                      &clilen);
+               if (accept_fd < 0) {
+                       fprintf(stderr, "ERROR on accept");
+                       break;
+               }
+
+               /* Disable Nagle */
+               opt = 1;
+        setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt,
+                               sizeof(opt));
+
+               /*
+                * look at who we connected to and give user code a chance
+                * to reject based on client IP.  There's no protocol selected
+                * yet so we issue this to protocols[0]
+                */
+
+               if ((context->protocols[0].callback)(context, wsi,
+                               LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
+                                            (void*)(long)accept_fd, NULL, 0)) {
+                       debug("Callback denied network connection\n");
+#ifdef WIN32
+                       closesocket(accept_fd);
+#else
+                       close(accept_fd);
+#endif
+                       break;
+               }
+
+               /* accepting connection to main listener */
+
+               new_wsi = libwebsocket_create_new_server_wsi(context);
+               if (new_wsi == NULL)
+                       break;
+
+               new_wsi->sock = accept_fd;
+
+
+#ifdef LWS_OPENSSL_SUPPORT
+               new_wsi->ssl = NULL;
+
+               if (context->use_ssl) {
+
+                       new_wsi->ssl = SSL_new(context->ssl_ctx);
+                       if (new_wsi->ssl == NULL) {
+                               fprintf(stderr, "SSL_new failed: %s\n",
+                                   ERR_error_string(SSL_get_error(
+                                   new_wsi->ssl, 0), NULL));
+                                   libwebsockets_decode_ssl_error();
+                               free(new_wsi);
+                               break;
+                       }
+
+                       SSL_set_fd(new_wsi->ssl, accept_fd);
+
+                       n = SSL_accept(new_wsi->ssl);
+                       if (n != 1) {
+                               /*
+                                * browsers seem to probe with various
+                                * ssl params which fail then retry
+                                * and succeed
+                                */
+                               debug("SSL_accept failed skt %u: %s\n",
+                                     pollfd->fd,
+                                     ERR_error_string(SSL_get_error(
+                                     new_wsi->ssl, n), NULL));
+                               SSL_free(
+                                      new_wsi->ssl);
+                               free(new_wsi);
+                               break;
+                       }
+                       
+                       debug("accepted new SSL conn  "
+                             "port %u on fd=%d SSL ver %s\n",
+                               ntohs(cli_addr.sin_port), accept_fd,
+                                 SSL_get_version(new_wsi->ssl));
+
+               } else
+#endif
+                       debug("accepted new conn  port %u on fd=%d\n",
+                                         ntohs(cli_addr.sin_port), accept_fd);
 
                insert_wsi(context, new_wsi);
 
@@ -1015,259 +1799,37 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
                                                                       context);
 
                        if (SSL_connect(wsi->ssl) <= 0) {
-                               fprintf(stderr, "SSL connect error %s\n",
-                                       ERR_error_string(ERR_get_error(),
-                                                                 ssl_err_buf));
-                               libwebsocket_close_and_free_session(context, wsi,
-                                                    LWS_CLOSE_STATUS_NOSTATUS);
-                               return 1;
-                       }
-
-                       n = SSL_get_verify_result(wsi->ssl);
-                       if ((n != X509_V_OK) && (
-                               n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
-                                                          wsi->use_ssl != 2)) {
-
-                               fprintf(stderr, "server's cert didn't "
-                                                          "look good %d\n", n);
-                               libwebsocket_close_and_free_session(context,
-                                               wsi, LWS_CLOSE_STATUS_NOSTATUS);
-                               return 1;
-                       }
-               } else {
-                       wsi->ssl = NULL;
-       #endif
-
-
-       #ifdef LWS_OPENSSL_SUPPORT
-               }
-       #endif
-
-               /*
-                * create the random key
-                */
-
-               n = libwebsockets_get_random(context, hash, 16);
-               if (n != 16) {
-                       fprintf(stderr, "Unable to read from random dev %s\n",
-                                                       SYSTEM_RANDOM_FILEPATH);
-                       free(wsi->c_path);
-                       free(wsi->c_host);
-                       if (wsi->c_origin)
-                               free(wsi->c_origin);
-                       if (wsi->c_protocol)
-                               free(wsi->c_protocol);
-                       libwebsocket_close_and_free_session(context, wsi,
-                                                    LWS_CLOSE_STATUS_NOSTATUS);
-                       return 1;
-               }
-
-               lws_b64_encode_string(hash, 16, wsi->key_b64,
-                                                          sizeof wsi->key_b64);
-
-               /*
-                * 00 example client handshake
-                *
-                * GET /socket.io/websocket HTTP/1.1
-                * Upgrade: WebSocket
-                * Connection: Upgrade
-                * Host: 127.0.0.1:9999
-                * Origin: http://127.0.0.1
-                * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7  92 ^
-                * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
-                * Cookie: socketio=websocket
-                * 
-                * (Á®Ä0¶†≥
-                * 
-                * 04 example client handshake
-                *
-                * GET /chat HTTP/1.1
-                * Host: server.example.com
-                * Upgrade: websocket
-                * Connection: Upgrade
-                * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
-                * Sec-WebSocket-Origin: http://example.com
-                * Sec-WebSocket-Protocol: chat, superchat
-                * Sec-WebSocket-Version: 4
-                */
-
-               p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path);
-
-               if (wsi->ietf_spec_revision == 0) {
-                       unsigned char spaces_1, spaces_2;
-                       unsigned int max_1, max_2;
-                       unsigned int num_1, num_2;
-                       unsigned long product_1, product_2;
-                       char key_1[40];
-                       char key_2[40];
-                       unsigned int seed;
-                       unsigned int count;
-                       char challenge[16];
-
-                       libwebsockets_get_random(context, &spaces_1,
-                                                                 sizeof(char));
-                       libwebsockets_get_random(context, &spaces_2,
-                                                                 sizeof(char));
-                       
-                       spaces_1 = (spaces_1 % 12) + 1;
-                       spaces_2 = (spaces_2 % 12) + 1;
-                       
-                       max_1 = 4294967295 / spaces_1;
-                       max_2 = 4294967295 / spaces_2;
-
-                       libwebsockets_get_random(context, &num_1, sizeof(int));
-                       libwebsockets_get_random(context, &num_2, sizeof(int));
-                       
-                       num_1 = (num_1 % max_1);
-                       num_2 = (num_2 % max_2);
-                       
-                       challenge[0] = num_1 >> 24;
-                       challenge[1] = num_1 >> 16;
-                       challenge[2] = num_1 >> 8;
-                       challenge[3] = num_1;
-                       challenge[4] = num_2 >> 24;
-                       challenge[5] = num_2 >> 16;
-                       challenge[6] = num_2 >> 8;
-                       challenge[7] = num_2;
-                       
-                       product_1 = num_1 * spaces_1;
-                       product_2 = num_2 * spaces_2;
-                       
-                       sprintf(key_1, "%lu", product_1);
-                       sprintf(key_2, "%lu", product_2);
-
-                       libwebsockets_get_random(context, &seed, sizeof(int));
-                       libwebsockets_get_random(context, &count, sizeof(int));
-                       
-                       libwebsockets_00_spam(key_1, (count % 12) + 1, seed);
-                       
-                       libwebsockets_get_random(context, &seed, sizeof(int));
-                       libwebsockets_get_random(context, &count, sizeof(int));
-                       
-                       libwebsockets_00_spam(key_2, (count % 12) + 1, seed);
-                       
-                       libwebsockets_get_random(context, &seed, sizeof(int));
-                       
-                       libwebsockets_00_spaceout(key_1, spaces_1, seed);
-                       libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16);
-                       
-                       p += sprintf(p, "Upgrade: websocket\x0d\x0a"
-                               "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a",
-                               wsi->c_host);
-                       if (wsi->c_origin)
-                               p += sprintf(p, "Origin: %s\x0d\x0a",
-                               wsi->c_origin);
-                       
-                       if (wsi->c_protocol)
-                               p += sprintf(p, "Sec-WebSocket-Protocol: %s"
-                                                "\x0d\x0a", wsi->c_protocol);
-                       
-                       p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a",
-                               key_1);
-                       p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a",
-                               key_2);
-
-                       /* give userland a chance to append, eg, cookies */
-                       
-                       context->protocols[0].callback(context, wsi,
-                               LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
-                                       NULL, &p, (pkt + sizeof(pkt)) - p - 12);
-
-                       p += sprintf(p, "\x0d\x0a");
-                       
-                       read(context->fd_random, p, 8);
-                       memcpy(&challenge[8], p, 8);
-                       p += 8;
-                       
-                       /* precompute what we want to see from the server */
-                       
-                       MD5((unsigned char *)challenge, 16,
-                          (unsigned char *)wsi->initial_handshake_hash_base64);
-                       
-                       goto issue_hdr;
-               }
-
-               p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host);
-               p += sprintf(p, "Upgrade: websocket\x0d\x0a");
-               p += sprintf(p, "Connection: Upgrade\x0d\x0a"
-                                       "Sec-WebSocket-Key: ");
-               strcpy(p, wsi->key_b64);
-               p += strlen(wsi->key_b64);
-               p += sprintf(p, "\x0d\x0a");
-               if (wsi->c_origin)
-                       p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a",
-                                                                wsi->c_origin);
-               if (wsi->c_protocol)
-                       p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
-                                                              wsi->c_protocol);
-
-               /* tell the server what extensions we could support */
-
-               p += sprintf(p, "Sec-WebSocket-Extensions: ");
-
-               ext =context->extensions;
-               while (ext && ext->callback) {
-
-                       n = 0;
-                       n = context->protocols[0].callback(context, wsi,
-                               LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
-                                       wsi->user_space, (char *)ext->name, 0);
-
-                       /*
-                        * zero return from callback means
-                        * go ahead and allow the extension,
-                        * it's what we get if the callback is
-                        * unhandled
-                        */
-
-                       if (n) {
-                               ext++;
-                               continue;
-                       }
-
-                       /* apply it */
-                       
-                       if (ext_count)
-                               *p++ = ',';
-                       p += sprintf(p, ext->name);
-                       ext_count++;
-
-                       ext++;
-               }
-
-               p += sprintf(p, "\x0d\x0a");
-
-               if (wsi->ietf_spec_revision)
-                       p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
-                                                      wsi->ietf_spec_revision); 
-
-               /* give userland a chance to append, eg, cookies */
-               
-               context->protocols[0].callback(context, wsi,
-                       LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
-                       NULL, &p, (pkt + sizeof(pkt)) - p - 12);
-               
-               p += sprintf(p, "\x0d\x0a");
+                               fprintf(stderr, "SSL connect error %s\n",
+                                       ERR_error_string(ERR_get_error(),
+                                                                 ssl_err_buf));
+                               libwebsocket_close_and_free_session(context, wsi,
+                                                    LWS_CLOSE_STATUS_NOSTATUS);
+                               return 1;
+                       }
 
-               /* prepare the expected server accept response */
+                       n = SSL_get_verify_result(wsi->ssl);
+                       if ((n != X509_V_OK) && (
+                               n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
+                                                          wsi->use_ssl != 2)) {
 
-               strcpy((char *)buf, wsi->key_b64);
-               strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid);
+                               fprintf(stderr, "server's cert didn't "
+                                                          "look good %d\n", n);
+                               libwebsocket_close_and_free_session(context,
+                                               wsi, LWS_CLOSE_STATUS_NOSTATUS);
+                               return 1;
+                       }
+               } else {
+                       wsi->ssl = NULL;
+       #endif
 
-               SHA1(buf, strlen((char *)buf), (unsigned char *)hash);
 
-               lws_b64_encode_string(hash, 20,
-                               wsi->initial_handshake_hash_base64,
-                                    sizeof wsi->initial_handshake_hash_base64);
+       #ifdef LWS_OPENSSL_SUPPORT
+               }
+       #endif
 
-issue_hdr:
-               
-               /* done with these now */
-               
-               free(wsi->c_path);
-               free(wsi->c_host);
-               if (wsi->c_origin)
-                       free(wsi->c_origin);
+               p = libwebsockets_generate_client_handshake(context, wsi, p);
+               if (p ==NULL)
+                       return 1;
 
                /* send our request to the server */
 
@@ -1316,355 +1878,59 @@ issue_hdr:
                 *  Sec-WebSocket-Protocol: chat
                 */
 
-       #ifdef LWS_OPENSSL_SUPPORT
-               if (wsi->use_ssl)
-                       len = SSL_read(wsi->ssl, pkt, sizeof pkt);
-               else
-       #endif
-                       len = recv(wsi->sock, pkt, sizeof pkt, 0);
-
-               if (len < 0) {
-                       fprintf(stderr,
-                                 "libwebsocket_client_handshake read error\n");
-                       goto bail3;
-               }
-
-               p = pkt;
-               for (n = 0; n < len; n++)
-                       libwebsocket_parse(wsi, *p++);
-
-               if (wsi->parser_state != WSI_PARSING_COMPLETE) {
-                       fprintf(stderr, "libwebsocket_client_handshake "
-                                       "server response failed parsing\n");
-                       goto bail3;
-               }
-
-               /*
-                * 00 / 76 -->
-                *
-                * HTTP/1.1 101 WebSocket Protocol Handshake
-                * Upgrade: WebSocket
-                * Connection: Upgrade
-                * Sec-WebSocket-Origin: http://127.0.0.1
-                * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket
-                *
-                * xxxxxxxxxxxxxxxx
-                */
-               
-               if (wsi->ietf_spec_revision == 0) {
-                       if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
-                           !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
-                           !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
-                           !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
-                           (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
-                           wsi->c_protocol != NULL)) {
-                               fprintf(stderr, "libwebsocket_client_handshake "
-                                               "missing required header(s)\n");
-                               pkt[len] = '\0';
-                               fprintf(stderr, "%s", pkt);
-                               goto bail3;
-                       }
-                       
-                       strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
-                       if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
-                               "101 websocket protocol handshake")) {
-                               fprintf(stderr, "libwebsocket_client_handshake "
-                                       "server sent bad HTTP response '%s'\n",
-                                       wsi->utf8_token[WSI_TOKEN_HTTP].token);
-                               goto bail3;
-                       }
-                       
-                       if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len <
-                                                                          16) {
-                               fprintf(stderr, "libwebsocket_client_handshake "
-                                       "challenge reply too short %d\n",
-                                       wsi->utf8_token[
-                                               WSI_TOKEN_CHALLENGE].token_len);
-                               pkt[len] = '\0';
-                               fprintf(stderr, "%s", pkt);
-                               goto bail3;
-                               
-                       }
-                       
-                       goto select_protocol;
-               }
-               
-               /*
-                * well, what the server sent looked reasonable for syntax.
-                * Now let's confirm it sent all the necessary headers
-                */
-
-                if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
-                       !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
-                       (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
-                                          wsi->ietf_spec_revision == 4) ||
-                       (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
-                                                    wsi->c_protocol != NULL)) {
-                       fprintf(stderr, "libwebsocket_client_handshake "
-                                               "missing required header(s)\n");
-                       pkt[len] = '\0';
-                       fprintf(stderr, "%s", pkt);
-                       goto bail3;
-               }
-
-               /*
-                * Everything seems to be there, now take a closer look at what
-                * is in each header
-                */
-
-               strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token);
-               if (strcmp(wsi->utf8_token[WSI_TOKEN_HTTP].token,
-                                                  "101 switching protocols")) {
-                       fprintf(stderr, "libwebsocket_client_handshake "
-                                       "server sent bad HTTP response '%s'\n",
-                                        wsi->utf8_token[WSI_TOKEN_HTTP].token);
-                       goto bail3;
-               }
-
-               strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
-               if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token,
-                                                                "websocket")) {
-                       fprintf(stderr, "libwebsocket_client_handshake server "
-                                       "sent bad Upgrade header '%s'\n",
-                                     wsi->utf8_token[WSI_TOKEN_UPGRADE].token);
-                       goto bail3;
-               }
-
-               strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
-               if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token,
-                                                                  "upgrade")) {
-                       fprintf(stderr, "libwebsocket_client_handshake server "
-                                       "sent bad Connection hdr '%s'\n",
-                                  wsi->utf8_token[WSI_TOKEN_CONNECTION].token);
-                       goto bail3;
-               }
-
-select_protocol:
-               pc = wsi->c_protocol;
-
                /*
-                * confirm the protocol the server wants to talk was in the list
-                * of protocols we offered
+                * we have to take some care here to only take from the
+                * socket bytewise.  The browser may (and has been seen to
+                * in the case that onopen() performs websocket traffic)
+                * coalesce both handshake response and websocket traffic
+                * in one packet, since at that point the connection is
+                * definitively ready from browser pov.
                 */
 
-               if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) {
-
-                       /*
-                        * no protocol name to work from,
-                        * default to first protocol
-                        */
-                       wsi->protocol = &context->protocols[0];
-
-                       free(wsi->c_protocol);
-
-                       goto check_accept;
-               }
-
-               while (*pc && !okay) {
-                       if ((!strncmp(pc,
-                               wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-                          wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) &&
-                (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' ||
-                  pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) {
-                               okay = 1;
-                               continue;
-                       }
-                       while (*pc && *pc != ',')
-                               pc++;
-                       while (*pc && *pc != ' ')
-                               pc++;
-               }
-
-               /* done with him now */
-
-               if (wsi->c_protocol)
-                       free(wsi->c_protocol);
-
-
-               if (!okay) {
-                       fprintf(stderr, "libwebsocket_client_handshake server "
-                                               "sent bad protocol '%s'\n",
-                                    wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
-                       goto bail2;
-               }
-
-               /*
-                * identify the selected protocol struct and set it
-                */
-               n = 0;
-               wsi->protocol = NULL;
-               while (context->protocols[n].callback) {
-                       if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
-                                              context->protocols[n].name) == 0)
-                               wsi->protocol = &context->protocols[n];
-                       n++;
-               }
+               len = 1;
+               while (wsi->parser_state != WSI_PARSING_COMPLETE && len > 0) {
+#ifdef LWS_OPENSSL_SUPPORT
+                       if (wsi->use_ssl)
+                               len = SSL_read(wsi->ssl, &c, 1);
+                        else
+#endif
+                               len = recv(wsi->sock, &c, 1, 0);
 
-               if (wsi->protocol == NULL) {
-                       fprintf(stderr, "libwebsocket_client_handshake server "
-                                       "requested protocol '%s', which we "
-                                       "said we supported but we don't!\n",
-                                    wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
-                       goto bail2;
+                       libwebsocket_parse(wsi, c);
                }
 
-
-               /* instantiate the accepted extensions */
-
-               if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len)
-                       goto check_accept;
-
                /*
-                * break down the list of server accepted extensions
-                * and go through matching them or identifying bogons
+                * hs may also be coming in multiple packets, there is a 5-second
+                * libwebsocket timeout still active here too, so if parsing did
+                * not complete just wait for next packet coming in this state
                 */
 
-               c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token;
-               n = 0;
-               while (more) {
-                       
-                       if (*c && *c != ',') {
-                               ext_name[n] = *c++;
-                               if (n < sizeof(ext_name) - 1)
-                                       n++;
-                               continue;
-                       }
-                       ext_name[n] = '\0';
-                       if (!*c)
-                               more = 0;
-
-                       /* check we actually support it */
-
-                       n = 0;
-                       ext = wsi->protocol->owning_server->extensions;
-                       while (ext && ext->callback) {
-
-                               if (strcmp(ext_name, ext->name)) {
-                                       ext++;
-                                       continue;
-                               }
-
-                               n = 1;
-
-                               /* instantiate the extension on this conn */
-
-                               wsi->active_extensions_user[
-                                       wsi->count_active_extensions] =
-                                            malloc(ext->per_session_data_size);
-                               wsi->active_extensions[
-                                         wsi->count_active_extensions] = ext;
-
-                               /* allow him to construct his context */
-
-                               ext->callback(wsi->protocol->owning_server,
-                                       wsi, LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
-                                               wsi->active_extensions_user[
-                                       wsi->count_active_extensions], NULL, 0);
-
-                               wsi->count_active_extensions++;
-
-                               ext++;
-                       }
-
-                       if (n == 0) {
-                               fprintf(stderr, "Server said we should use"
-                                     "an unknown extension '%s'!\n", ext_name);
-                               goto bail2;
-                       }
-
-                       n = 0;
-               }
-
-
-       check_accept:
-
-               if (wsi->ietf_spec_revision == 0) {
-                       
-                       if (memcmp(wsi->initial_handshake_hash_base64,
-                             wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) {
-                               fprintf(stderr, "libwebsocket_client_handshake "
-                                              "failed 00 challenge compare\n");        
-                                       pkt[len] = '\0';
-                                       fprintf(stderr, "%s", pkt);
-                                       goto bail2;
-                       }
-                       
-                       goto accept_ok;
-               }
+               if (wsi->parser_state != WSI_PARSING_COMPLETE)
+                       break;
 
                /*
-                * Confirm his accept token is the one we precomputed
+                * otherwise deal with the handshake.  If there's any
+                * packet traffic already arrived we'll trigger poll() again
+                * right away and deal with it that way
                 */
 
-               if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
-                                         wsi->initial_handshake_hash_base64)) {
-                       fprintf(stderr, "libwebsocket_client_handshake server "
-                               "sent bad ACCEPT '%s' vs computed '%s'\n",
-                               wsi->utf8_token[WSI_TOKEN_ACCEPT].token,
-                                           wsi->initial_handshake_hash_base64);
-                       goto bail2;
-               }
-
-               if (wsi->ietf_spec_revision == 4) {
-                       /*
-                        * Calculate the 04 masking key to use when
-                        * sending data to server
-                        */
-
-                       strcpy((char *)buf, wsi->key_b64);
-                       p = (char *)buf + strlen(wsi->key_b64);
-                       strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token);
-                       p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len;
-                       strcpy(p, magic_websocket_04_masking_guid);
-                       SHA1(buf, strlen((char *)buf), wsi->masking_key_04);
-               }
-accept_ok:
-
-               /* allocate the per-connection user memory (if any) */
-
-               if (wsi->protocol->per_session_data_size) {
-                       wsi->user_space = malloc(
-                                         wsi->protocol->per_session_data_size);
-                       if (wsi->user_space  == NULL) {
-                               fprintf(stderr, "Out of memory for "
-                                                          "conn user space\n");
-                               goto bail2;
-                       }
-               } else
-                       wsi->user_space = NULL;
-
-               /* clear his proxy connection timeout */
-
-               libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
-
-               /* mark him as being alive */
-
-               wsi->state = WSI_STATE_ESTABLISHED;
-               wsi->mode = LWS_CONNMODE_WS_CLIENT;
-
-               fprintf(stderr, "handshake OK for protocol %s\n",
-                                                          wsi->protocol->name);
-
-               /* call him back to inform him he is up */
-
-               wsi->protocol->callback(context, wsi,
-                                LWS_CALLBACK_CLIENT_ESTABLISHED,
-                                wsi->user_space,
-                                NULL, 0);
-
-               break;
+               return lws_client_interpret_server_handshake(context, wsi);
 
 bail3:
                if (wsi->c_protocol)
                        free(wsi->c_protocol);
-
-bail2:
                libwebsocket_close_and_free_session(context, wsi,
-                                                    LWS_CLOSE_STATUS_NOSTATUS);
+                                                                LWS_CLOSE_STATUS_NOSTATUS);
                return 1;
-               
+
+       case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
+               fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
+               break;
+
+       case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
+               fprintf(stderr, "LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
+               break;
+
 
        case LWS_CONNMODE_WS_SERVING:
        case LWS_CONNMODE_WS_CLIENT:
@@ -1709,7 +1975,10 @@ bail2:
                if (eff_buf.token_len < 0) {
                        fprintf(stderr, "Socket read returned %d\n",
                                                            eff_buf.token_len);
-                       break;
+                       if (errno != EINTR)
+                               libwebsocket_close_and_free_session(context, wsi,
+                                                                LWS_CLOSE_STATUS_NOSTATUS);
+                       return 1;
                }
                if (!eff_buf.token_len) {
                        libwebsocket_close_and_free_session(context, wsi,
@@ -1737,9 +2006,11 @@ bail2:
                        more = 0;
 
                        for (n = 0; n < wsi->count_active_extensions; n++) {
-                               m = wsi->active_extensions[n]->callback(context, wsi,
+                               m = wsi->active_extensions[n]->callback(context,
+                                       wsi->active_extensions[n], wsi,
                                        LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
-                                    wsi->active_extensions_user[n], &eff_buf, 0);
+                                       wsi->active_extensions_user[n],
+                                                                  &eff_buf, 0);
                                if (m < 0) {
                                        fprintf(stderr, "Extension reports fatal error\n");
                                        libwebsocket_close_and_free_session(context, wsi,
@@ -1784,6 +2055,7 @@ libwebsocket_context_destroy(struct libwebsocket_context *context)
        int n;
        int m;
        struct libwebsocket *wsi;
+       struct libwebsocket_extension *ext;
 
        for (n = 0; n < FD_HASHTABLE_MODULUS; n++)
                for (m = 0; m < context->fd_hashtable[n].length; m++) {
@@ -1792,6 +2064,20 @@ libwebsocket_context_destroy(struct libwebsocket_context *context)
                                                    LWS_CLOSE_STATUS_GOINGAWAY);
                }
 
+       /*
+        * give all extensions a chance to clean up any per-context
+        * allocations they might have made
+        */
+
+       ext = context->extensions;
+       m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
+       if (context->listen_port)
+               m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
+       while (ext->callback) {
+               ext->callback(context, ext, NULL, m, NULL, NULL, 0);
+               ext++;
+       }
+
 #ifdef WIN32
 #else
        close(context->fd_random);
@@ -1879,6 +2165,50 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
        return 0;
 }
 
+int
+lws_any_extension_handled(struct libwebsocket_context *context,
+                                                      struct libwebsocket *wsi,
+                                                      enum libwebsocket_extension_callback_reasons r,
+                                                      void *v, size_t len)
+{
+       int n;
+       int handled = 0;
+
+       /* maybe an extension will take care of it for us */
+
+       for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
+               if (!wsi->active_extensions[n]->callback)
+                       continue;
+
+               handled |= wsi->active_extensions[n]->callback(context,
+                       wsi->active_extensions[n], wsi,
+                       r, wsi->active_extensions_user[n], v, len);
+       }
+
+       return handled;
+}
+
+
+void *
+lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
+                                                       struct libwebsocket_extension * ext)
+{
+       int n = 0;
+
+       if (wsi == NULL)
+               return NULL;
+
+       while (n < wsi->count_active_extensions) {
+               if (wsi->active_extensions[n] != ext) {
+                       n++;
+                       continue;
+               }
+               return wsi->active_extensions_user[n];
+       }
+
+       return NULL;
+}
+
 /**
  * libwebsocket_callback_on_writable() - Request a callback when this socket
  *                                      becomes able to be written to without
@@ -1893,13 +2223,32 @@ libwebsocket_callback_on_writable(struct libwebsocket_context *context,
                                                       struct libwebsocket *wsi)
 {
        int n;
+       int handled = 0;
+
+       /* maybe an extension will take care of it for us */
+
+       for (n = 0; n < wsi->count_active_extensions; n++) {
+               if (!wsi->active_extensions[n]->callback)
+                       continue;
+
+               handled |= wsi->active_extensions[n]->callback(context,
+                       wsi->active_extensions[n], wsi,
+                       LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
+                                      wsi->active_extensions_user[n], NULL, 0);
+       }
+
+       if (handled)
+               return 1;
 
        for (n = 0; n < context->fds_count; n++)
                if (context->fds[n].fd == wsi->sock) {
                        context->fds[n].events |= POLLOUT;
-                       n = context->fds_count;
+                       n = context->fds_count + 1;
                }
 
+       if (n == context->fds_count)
+               fprintf(stderr, "libwebsocket_callback_on_writable: failed to find socket %d\n", wsi->sock);
+
        /* external POLL support via protocol 0 */
        context->protocols[0].callback(context, wsi,
                LWS_CALLBACK_SET_MODE_POLL_FD,
@@ -2015,9 +2364,10 @@ libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
                        LWS_CALLBACK_CLEAR_MODE_POLL_FD,
                        (void *)(long)wsi->sock, NULL, POLLIN);
 
-
-       fprintf(stderr, "libwebsocket_callback_on_writable "
+#if 0
+       fprintf(stderr, "libwebsocket_rx_flow_control "
                                                     "unable to find socket\n");
+#endif
        return 1;
 }
 
@@ -2134,6 +2484,7 @@ libwebsocket_create_context(int port, const char *interf,
                               int gid, int uid, unsigned int options)
 {
        int n;
+       int m;
        int sockfd = 0;
        int fd;
        struct sockaddr_in serv_addr, cli_addr;
@@ -2155,6 +2506,7 @@ libwebsocket_create_context(int port, const char *interf,
                WORD wVersionRequested;
                WSADATA wsaData;
                int err;
+        HMODULE wsdll;
 
                /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
                wVersionRequested = MAKEWORD(2, 2);
@@ -2167,6 +2519,17 @@ libwebsocket_create_context(int port, const char *interf,
                                                                           err);
                        return NULL;
                }
+
+        wsdll = GetModuleHandle("Ws2_32.dll");
+        if (wsdll)
+        {
+            poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
+        }
+
+        if (!poll)
+        {
+            poll = emulated_poll;
+        }
        }
 #endif
 
@@ -2406,7 +2769,12 @@ libwebsocket_create_context(int port, const char *interf,
                }
 
                /* allow us to restart even if old sockets in TIME_WAIT */
-               setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+               setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));
+
+
+               /* Disable Nagle */
+               opt = 1;
+               setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt));
 
                bzero((char *) &serv_addr, sizeof(serv_addr));
                serv_addr.sin_family = AF_INET;
@@ -2463,6 +2831,9 @@ libwebsocket_create_context(int port, const char *interf,
        for (context->count_protocols = 0;
                        protocols[context->count_protocols].callback;
                                                   context->count_protocols++) {
+
+               fprintf(stderr, "  Protocol: %s\n", protocols[context->count_protocols].name);
+
                protocols[context->count_protocols].owning_server = context;
                protocols[context->count_protocols].protocol_index =
                                                       context->count_protocols;
@@ -2474,7 +2845,7 @@ libwebsocket_create_context(int port, const char *interf,
                }
 
                /* allow us to restart even if old sockets in TIME_WAIT */
-               setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+               setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));
 
                bzero((char *) &serv_addr, sizeof(serv_addr));
                serv_addr.sin_family = AF_INET;
@@ -2527,6 +2898,21 @@ libwebsocket_create_context(int port, const char *interf,
                        (void *)(long)fd, NULL, POLLIN);
        }
 
+       /*
+        * give all extensions a chance to create any per-context
+        * allocations they need
+        */
+
+       m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
+       if (port)
+               m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
+       while (extensions->callback) {
+               fprintf(stderr, "  Extension: %s\n", extensions->name);
+               extensions->callback(context, extensions,
+                                                       NULL, m, NULL, NULL, 0);
+               extensions++;
+       }
+
        return context;
 }
 
@@ -2709,3 +3095,27 @@ libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
 
        return n;
 }
+
+int
+libwebsocket_is_final_fragment(struct libwebsocket *wsi)
+{
+       return wsi->final;
+}
+
+void *
+libwebsocket_ensure_user_space(struct libwebsocket *wsi)
+{
+       /* allocate the per-connection user memory (if any) */
+
+       if (wsi->protocol->per_session_data_size && !wsi->user_space) {
+               wsi->user_space = malloc(
+                                 wsi->protocol->per_session_data_size);
+               if (wsi->user_space  == NULL) {
+                       fprintf(stderr, "Out of memory for "
+                                                  "conn user space\n");
+                       return NULL;
+               }
+               memset(wsi->user_space, 0, wsi->protocol->per_session_data_size);
+       }
+       return wsi->user_space;
+}