From 47943ae82d05fbcf28979c8d2ad91d7e71123d17 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Fri, 12 Nov 2010 11:15:49 +0000 Subject: [PATCH] fix-user-pointer-bug.patch Signed-off-by: Andy Green --- lib/handshake.c | 11 ++++++++--- lib/libwebsockets.c | 4 ++-- lib/parsers.c | 2 +- libwebsockets-api-doc.html | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/handshake.c b/lib/handshake.c index 7deb581..95297a0 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -96,7 +96,7 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) { if (wsi->protocol->callback) (wsi->protocol->callback)(wsi, LWS_CALLBACK_HTTP, - &wsi->user_space, + wsi->user_space, wsi->utf8_token[WSI_TOKEN_GET_URI].token, 0); wsi->state = WSI_STATE_HTTP; return 0; @@ -145,6 +145,9 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) wsi->protocol++; } + + /* we didn't find a protocol he wanted? */ + if (wsi->protocol->callback == NULL) { if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) fprintf(stderr, "[no protocol] " @@ -166,7 +169,9 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) "conn user space\n"); goto bail; } - } + fprintf(stderr, "user allocated %d at %p for wsi %p\n", wsi->protocol->per_session_data_size, wsi->user_space, wsi); + } else + wsi->user_space = NULL; /* create the response packet */ @@ -275,7 +280,7 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) if (wsi->protocol->callback) wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED, - &wsi->user_space, NULL, 0); + wsi->user_space, NULL, 0); break; case WSI_STATE_ESTABLISHED: diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 79d2e22..c680e52 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -82,7 +82,7 @@ libwebsocket_close_and_free_session(struct libwebsocket *wsi) wsi->state = WSI_STATE_DEAD_SOCKET; if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED) - wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, &wsi->user_space, + wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, wsi->user_space, NULL, 0); for (n = 0; n < WSI_TOKEN_COUNT; n++) @@ -444,7 +444,7 @@ poll_out: continue; wsi[client]->protocol->callback(wsi[client], LWS_CALLBACK_SEND, - &wsi[client]->user_space, NULL, 0); + wsi[client]->user_space, NULL, 0); } continue; diff --git a/lib/parsers.c b/lib/parsers.c index 52cd126..5a98f6f 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -212,7 +212,7 @@ static int libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) issue: if (wsi->protocol->callback) wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE, - &wsi->user_space, + wsi->user_space, &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head); wsi->rx_user_buffer_head = 0; diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index e7dcb82..b418db6 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -93,14 +93,23 @@ else ignored

Description

-This function forks to create the listening socket and takes care +This function creates the listening socket and takes care of all initialization in one step.

-The callback function is called for a handful of events including -http requests coming in, websocket connections becoming +It does not return since it sits in a service loop and operates via the +callbacks given in protocol. User code should fork before calling +libwebsocket_create_server if it wants to do other things in +parallel other than serve websockets. +

+The protocol callback functions are called for a handful of events +including http requests coming in, websocket connections becoming established, and data arriving; it's also called periodically to allow async transmission.

+HTTP requests are sent always to the FIRST protocol in protocol, since +at that time websocket protocol has not been negotiated. Other +protocols after the first one never see any HTTP callack activity. +

The server created is a simple http server by default; part of the websocket standard is upgrading this http connection to a websocket one.

-- 2.7.4