diff --git a/lib/client-handshake.c b/lib/client-handshake.c index e21945087..df91923dd 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -498,7 +498,7 @@ check_accept: this->fds[this->fds_count++].events = POLLIN; /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_ADD_POLL_FD, (void *)(long)wsi->sock, NULL, POLLIN); @@ -510,7 +510,7 @@ check_accept: /* call him back to inform him he is up */ - wsi->protocol->callback(wsi, + wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLIENT_ESTABLISHED, wsi->user_space, NULL, 0); diff --git a/lib/handshake.c b/lib/handshake.c index 1ba4b9216..90219a2d5 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -208,7 +208,8 @@ handshake_00(struct libwebsocket *wsi) /* notify user code that we're ready to roll */ if (wsi->protocol->callback) - wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, LWS_CALLBACK_ESTABLISHED, wsi->user_space, NULL, 0); return 0; @@ -398,7 +399,8 @@ handshake_0405(struct libwebsocket *wsi) /* notify user code that we're ready to roll */ if (wsi->protocol->callback) - wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, LWS_CALLBACK_ESTABLISHED, wsi->user_space, NULL, 0); return 0; @@ -484,7 +486,7 @@ libwebsocket_read(struct libwebsocket_context *this, struct libwebsocket *wsi, if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) { if (wsi->protocol->callback) - (wsi->protocol->callback)(wsi, + (wsi->protocol->callback)(this, wsi, LWS_CALLBACK_HTTP, wsi->user_space, wsi->utf8_token[WSI_TOKEN_GET_URI].token, 0); wsi->state = WSI_STATE_HTTP; @@ -538,7 +540,7 @@ libwebsocket_read(struct libwebsocket_context *this, struct libwebsocket *wsi, * have the opportunity to deny it */ - if ((wsi->protocol->callback)(wsi, + if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi, LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, &wsi->utf8_token[0], NULL, 0)) { fprintf(stderr, "User code denied connection\n"); diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index d4db3f3eb..ec1e482f9 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -80,15 +80,16 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *this, struct libwebsocket *wsi) { int n; + int old_state; unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + LWS_SEND_BUFFER_POST_PADDING]; if (!wsi) return; - n = wsi->state; + old_state = wsi->state; - if (n == WSI_STATE_DEAD_SOCKET) + if (old_state == WSI_STATE_DEAD_SOCKET) return; /* remove this fd from wsi mapping hashtable */ @@ -111,7 +112,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *this, /* remove also from external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0); /* @@ -122,7 +123,7 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *this, * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this */ - if (n == WSI_STATE_ESTABLISHED) + if (old_state == WSI_STATE_ESTABLISHED) libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 0, LWS_WRITE_CLOSE); @@ -130,8 +131,8 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *this, /* tell the user it's all over for this guy */ - if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED) - wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED, + if (wsi->protocol->callback && old_state == WSI_STATE_ESTABLISHED) + wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLOSED, wsi->user_space, NULL, 0); /* free up his allocations */ @@ -317,7 +318,7 @@ libwebsocket_service_fd(struct libwebsocket_context *this, * yet so we issue this to protocols[0] */ - if ((this->protocols[0].callback)(wsi, + if ((this->protocols[0].callback)(this, wsi, LWS_CALLBACK_FILTER_NETWORK_CONNECTION, (void*)(long)accept_fd, NULL, 0)) { fprintf(stderr, "Callback denied network connection\n"); @@ -420,7 +421,7 @@ libwebsocket_service_fd(struct libwebsocket_context *this, this->fds[this->fds_count++].fd = accept_fd; /* external POLL support via protocol 0 */ - this->protocols[0].callback(new_wsi, + this->protocols[0].callback(this, new_wsi, LWS_CALLBACK_ADD_POLL_FD, (void *)(long)accept_fd, NULL, POLLIN); @@ -469,7 +470,7 @@ libwebsocket_service_fd(struct libwebsocket_context *this, this->fds[this->fds_count++].fd = accept_fd; /* external POLL support via protocol 0 */ - this->protocols[0].callback(new_wsi, + this->protocols[0].callback(this, new_wsi, LWS_CALLBACK_ADD_POLL_FD, (void *)(long)accept_fd, NULL, POLLIN); @@ -497,11 +498,11 @@ libwebsocket_service_fd(struct libwebsocket_context *this, pollfd->events &= ~POLLOUT; /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_CLEAR_MODE_POLL_FD, (void *)(long)wsi->sock, NULL, POLLOUT); - wsi->protocol->callback(wsi, + wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLIENT_WRITEABLE, wsi->user_space, NULL, 0); @@ -553,7 +554,7 @@ libwebsocket_service_fd(struct libwebsocket_context *this, /* broadcast it to this connection */ - new_wsi->protocol->callback(new_wsi, + new_wsi->protocol->callback(this, new_wsi, LWS_CALLBACK_BROADCAST, new_wsi->user_space, buf + LWS_SEND_BUFFER_PRE_PADDING, len); @@ -568,7 +569,7 @@ libwebsocket_service_fd(struct libwebsocket_context *this, if (pollfd->revents & (POLLERR | POLLHUP)) { - debug("Session Socket %p (fd=%d) dead\n", + fprintf(stderr, "Session Socket %p (fd=%d) dead\n", (void *)wsi, pollfd->fd); libwebsocket_close_and_free_session(this, wsi); @@ -582,11 +583,11 @@ libwebsocket_service_fd(struct libwebsocket_context *this, pollfd->events &= ~POLLOUT; /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_CLEAR_MODE_POLL_FD, (void *)(long)wsi->sock, NULL, POLLOUT); - wsi->protocol->callback(wsi, + wsi->protocol->callback(this, wsi, LWS_CALLBACK_CLIENT_WRITEABLE, wsi->user_space, NULL, 0); @@ -713,7 +714,7 @@ libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) if (n == 0) /* poll timeout */ return 0; - if (n < 0 || this->fds[0].revents & (POLLERR | POLLHUP)) { + if (n < 0) { /* fprintf(stderr, "Listen Socket dead\n"); */ @@ -738,9 +739,9 @@ libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) */ int -libwebsocket_callback_on_writable(struct libwebsocket *wsi) +libwebsocket_callback_on_writable(struct libwebsocket_context *this, + struct libwebsocket *wsi) { - struct libwebsocket_context *this = wsi->protocol->owning_server; int n; for (n = 0; n < this->fds_count; n++) @@ -750,7 +751,7 @@ libwebsocket_callback_on_writable(struct libwebsocket *wsi) } /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_SET_MODE_POLL_FD, (void *)(long)wsi->sock, NULL, POLLOUT); @@ -782,7 +783,7 @@ libwebsocket_callback_on_writable_all_protocol( wsi = this->fd_hashtable[n].wsi[m]; if (wsi->protocol == protocol) - libwebsocket_callback_on_writable(wsi); + libwebsocket_callback_on_writable(this, wsi); } } @@ -833,12 +834,12 @@ libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) if (enable) /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_SET_MODE_POLL_FD, (void *)(long)wsi->sock, NULL, POLLIN); else /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_CLEAR_MODE_POLL_FD, (void *)(long)wsi->sock, NULL, POLLIN); @@ -1154,7 +1155,7 @@ libwebsocket_create_context(int port, this->fds[this->fds_count++].events = POLLIN; /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_ADD_POLL_FD, (void *)(long)sockfd, NULL, POLLIN); @@ -1232,7 +1233,7 @@ libwebsocket_create_context(int port, this->fds_count++; /* external POLL support via protocol 0 */ - this->protocols[0].callback(wsi, + this->protocols[0].callback(this, wsi, LWS_CALLBACK_ADD_POLL_FD, (void *)(long)fd, NULL, POLLIN); } @@ -1395,7 +1396,7 @@ libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, if (wsi->protocol != protocol) continue; - wsi->protocol->callback(wsi, + wsi->protocol->callback(this, wsi, LWS_CALLBACK_BROADCAST, wsi->user_space, buf, len); diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 5a65c217a..7b73a6bf2 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -125,6 +125,7 @@ struct libwebsocket_context; /* document the generic callback (it's a fake prototype under this) */ /** * callback() - User server actions + * @this: Websockets context * @wsi: Opaque websocket instance pointer * @reason: The reason for the call * @user: Pointer to per-session user data allocated by library @@ -235,7 +236,8 @@ struct libwebsocket_context; * pollfd struct for this socket descriptor. If you are using the * internal polling loop, you can just ignore it. */ -extern int callback(struct libwebsocket *wsi, +extern int callback(struct libwebsocket_context * this, + struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len); @@ -269,7 +271,8 @@ extern int callback(struct libwebsocket *wsi, struct libwebsocket_protocols { const char *name; - int (*callback)(struct libwebsocket *wsi, + int (*callback)(struct libwebsocket_context * this, + struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len); size_t per_session_data_size; @@ -352,7 +355,8 @@ extern const struct libwebsocket_protocols * libwebsockets_get_protocol(struct libwebsocket *wsi); extern int -libwebsocket_callback_on_writable(struct libwebsocket *wsi); +libwebsocket_callback_on_writable(struct libwebsocket_context *this, + struct libwebsocket *wsi); extern int libwebsocket_callback_on_writable_all_protocol( diff --git a/lib/parsers.c b/lib/parsers.c index e095b060a..d6cae1c1e 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -128,10 +128,10 @@ int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c) /* no payload challenge in 01 + */ if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len && - atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) > 0) { - wsi->utf8_token[wsi->parser_state].token_len = 0; - free(wsi->utf8_token[wsi->parser_state].token); - wsi->utf8_token[wsi->parser_state].token = NULL; + atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) > 0) { + wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len = 0; + free(wsi->utf8_token[WSI_TOKEN_CHALLENGE].token); + wsi->utf8_token[WSI_TOKEN_CHALLENGE].token = NULL; } /* For any supported protocol we have enough payload */ @@ -555,7 +555,8 @@ post_mask: break; issue: if (wsi->protocol->callback) - wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, LWS_CALLBACK_RECEIVE, wsi->user_space, &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head); @@ -642,7 +643,8 @@ spill: wsi->rx_user_buffer_head] = '\0'; if (wsi->protocol->callback) - wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, LWS_CALLBACK_RECEIVE, wsi->user_space, &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head); @@ -880,7 +882,8 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) break; issue: if (wsi->protocol->callback) - wsi->protocol->callback(wsi, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, LWS_CALLBACK_CLIENT_RECEIVE, wsi->user_space, &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], @@ -958,7 +961,8 @@ spill: */ if (wsi->protocol->callback) - wsi->protocol->callback(wsi, callback_action, + wsi->protocol->callback(wsi->protocol->owning_server, + wsi, callback_action, wsi->user_space, &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head); diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index 1415ff5ef..3f080b207 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -123,7 +123,8 @@ nothing is pending, or as soon as it services whatever was pending.