headers deleted after websocket established 18/3018/1
authorAndy Green <andy.green@linaro.org>
Wed, 6 Feb 2013 06:15:25 +0000 (15:15 +0900)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 7 Mar 2013 21:01:31 +0000 (13:01 -0800)
Signed-off-by: Andy Green <andy.green@linaro.org>
lib/client.c
lib/handshake.c
lib/libwebsockets.c
lib/libwebsockets.h
lib/parsers.c
lib/server-handshake.c
lib/server.c
libwebsockets-api-doc.html

index ef17ca8..9074ff8 100644 (file)
@@ -594,10 +594,25 @@ check_accept:
                                          !libwebsocket_ensure_user_space(wsi))
                goto bail2;
 
+       /*
+        * we seem to be good to go, give client last chance to check
+        * headers and OK it
+        */
+
+       wsi->protocol->callback(context, wsi,
+                               LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
+                                                    wsi->user_space, NULL, 0);
+
        /* clear his proxy connection timeout */
 
        libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
 
+       /* 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);
+
        /* mark him as being alive */
 
        wsi->state = WSI_STATE_ESTABLISHED;
@@ -645,6 +660,12 @@ bail2:
                         wsi->user_space,
                         NULL, 0);
        lwsl_info("closing connection due to bail2 connection error\n");
+       /* 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);
+
        libwebsocket_close_and_free_session(context, wsi,
                                                 LWS_CLOSE_STATUS_PROTOCOL_ERR);
 
index 763aa4d..5c397ca 100644 (file)
@@ -131,7 +131,6 @@ libwebsocket_read(struct libwebsocket_context *context,
                if (!wsi->protocol)
                        lwsl_err("NULL protocol at libwebsocket_read\n");
 
-
                /*
                 * It's websocket
                 *
@@ -168,15 +167,6 @@ libwebsocket_read(struct libwebsocket_context *context,
                }
 
                /*
-                * find out which spec version the client is using
-                * if this header is not given, we default to 00 (aka 76)
-                */
-
-               if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len)
-                       wsi->ietf_spec_revision =
-                                atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
-
-               /*
                 * Give the user code a chance to study the request and
                 * have the opportunity to deny it
                 */
@@ -209,6 +199,14 @@ libwebsocket_read(struct libwebsocket_context *context,
                        goto bail;
                }
 
+               /* drop the header info */
+
+               /* free up his parsing allocations... these are gone... */
+
+               for (n = 0; n < WSI_TOKEN_COUNT; n++)
+                       if (wsi->utf8_token[n].token)
+                               free(wsi->utf8_token[n].token);
+
                wsi->mode = LWS_CONNMODE_WS_SERVING;
 
                /* union transition */
@@ -253,6 +251,7 @@ libwebsocket_read(struct libwebsocket_context *context,
 
 bail:
        lwsl_info("closing connection at libwebsocket_read bail:\n");
+
        libwebsocket_close_and_free_session(context, wsi,
                                                     LWS_CLOSE_STATUS_NOSTATUS);
 
index d747166..5c30c92 100644 (file)
@@ -348,11 +348,6 @@ just_kill_connection:
        }
 #endif
 
-       /* 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);
 #ifndef LWS_NO_CLIENT
        if (wsi->c_address)
                free(wsi->c_address);
index f39f336..046ad0e 100644 (file)
@@ -124,6 +124,7 @@ enum libwebsocket_context_options {
 enum libwebsocket_callback_reasons {
        LWS_CALLBACK_ESTABLISHED,
        LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
+       LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
        LWS_CALLBACK_CLIENT_ESTABLISHED,
        LWS_CALLBACK_CLOSED,
        LWS_CALLBACK_RECEIVE,
@@ -377,12 +378,20 @@ struct libwebsocket_extension;
  *     You get an opportunity to initialize user data when called back with
  *     LWS_CALLBACK_ESTABLISHED reason.
  *
- *     LWS_CALLBACK_ESTABLISHED:  after the server completes a handshake with
+ *  LWS_CALLBACK_ESTABLISHED:  after the server completes a handshake with
  *                             an incoming client
  *
  *  LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
  *        been unable to complete a handshake with the remote server
  *
+ *  LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
+ *                             client user code to examine the http headers
+ *                             and decide to reject the connection.  If the
+ *                             content in the headers is interesting to the
+ *                             client (url, etc) it needs to copy it out at
+ *                             this point since it will be destroyed before
+ *                             the CLIENT_ESTABLISHED call
+ *
  *  LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
  *                             a handshake with the remote server
  *
index 5ee5f46..6afc510 100644 (file)
@@ -470,8 +470,14 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
                        }
                }
 
-               if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
+               if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) {
+                       if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token) {
+                               free(wsi->utf8_token[WSI_TOKEN_CHALLENGE].token);
+                               wsi->utf8_token[WSI_TOKEN_CHALLENGE].token = NULL;
+                       }
+                       wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len = 0;
                        goto set_parsing_complete;
+               }
 
                break;
 
@@ -504,14 +510,16 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
 
 set_parsing_complete:
 
-       if (!wsi->utf8_token[WSI_TOKEN_VERSION].token_len) {
-               lwsl_info("Missing Version Header\n");
-               return 1;
-       }
-       wsi->ietf_spec_revision =
-                atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
+       if (wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len) {
+               if (!wsi->utf8_token[WSI_TOKEN_VERSION].token_len) {
+//                     lwsl_info("Missing Version Header\n");
+//                     return 1;
+               } else
+                       wsi->ietf_spec_revision =
+                               atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
 
-       lwsl_parser("v%02d headers completed\n", wsi->ietf_spec_revision);
+               lwsl_parser("v%02d headers completed\n", wsi->ietf_spec_revision);
+       }
        wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
 
        return 0;
index 6fd9448..1afe22b 100644 (file)
@@ -270,6 +270,12 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
 
 
 bail:
+       /* 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);
+
        return -1;
 }
 
index e3914a0..5a58672 100644 (file)
@@ -118,13 +118,6 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context)
         */
        new_wsi->protocol = context->protocols;
        new_wsi->user_space = NULL;
-
-       /*
-        * 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;
 
        return new_wsi;
index 38f9e63..fb14ef0 100644 (file)
@@ -604,6 +604,16 @@ an incoming client
 the request client connection has
 been unable to complete a handshake with the remote server
 </blockquote>
+<h3>LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH</h3>
+<blockquote>
+this is the last chance for the
+client user code to examine the http headers
+and decide to reject the connection.  If the
+content in the headers is interesting to the
+client (url, etc) it needs to copy it out at
+this point since it will be destroyed before
+the CLIENT_ESTABLISHED call
+</blockquote>
 <h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
 <blockquote>
 after your client connection completed