diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 329492b0..cf2161a5 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -299,12 +299,6 @@ libwebsocket_client_connect(struct libwebsocket_context *context, int ietf_version_or_minus_one) { struct libwebsocket *wsi; -#ifndef LWS_NO_EXTENSIONS - int n; - int m; - struct libwebsocket_extension *ext; - int handled; -#endif #ifndef LWS_OPENSSL_SUPPORT if (ssl_connection) { @@ -330,9 +324,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context, wsi->state = WSI_STATE_CLIENT_UNCONNECTED; wsi->protocol = NULL; wsi->pending_timeout = NO_PENDING_TIMEOUT; -#ifndef LWS_NO_EXTENSIONS - wsi->count_active_extensions = 0; -#endif + #ifdef LWS_OPENSSL_SUPPORT wsi->use_ssl = ssl_connection; #endif @@ -371,30 +363,16 @@ libwebsocket_client_connect(struct libwebsocket_context *context, wsi->protocol = &context->protocols[0]; -#ifndef LWS_NO_EXTENSIONS /* * Check with each extension if it is able to route and proxy this * connection for us. For example, an extension like x-google-mux * can handle this and then we don't need an actual socket for this * connection. */ - - handled = 0; - ext = context->extensions; - n = 0; - - while (ext && ext->callback && !handled) { - m = ext->callback(context, ext, wsi, + + if (lws_ext_callback_for_each_extension_type(context, wsi, LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION, - (void *)(long)n, (void *)address, port); - if (m) - handled = 1; - - ext++; - n++; - } - - if (handled) { + (void *)address, port) > 0) { lwsl_client("libwebsocket_client_connect: ext handling conn\n"); libwebsocket_set_timeout(wsi, @@ -404,7 +382,6 @@ libwebsocket_client_connect(struct libwebsocket_context *context, wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT; return wsi; } -#endif lwsl_client("libwebsocket_client_connect: direct conn\n"); return libwebsocket_client_connect_2(context, wsi); diff --git a/lib/client-parser.c b/lib/client-parser.c index 677b3dff..d7b97cc3 100644 --- a/lib/client-parser.c +++ b/lib/client-parser.c @@ -27,9 +27,6 @@ int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c) int handled; struct lws_tokens eff_buf; int m; -#ifndef LWS_NO_EXTENSIONS - int n; -#endif switch (wsi->lws_rx_parse_state) { case LWS_RXPS_NEW: @@ -319,7 +316,7 @@ spill: default: lwsl_parser("Reserved opc 0x%2X\n", wsi->u.ws.opcode); -#ifndef LWS_NO_EXTENSIONS + /* * It's something special we can't understand here. * Pass the payload up to the extension's parsing @@ -329,29 +326,18 @@ spill: eff_buf.token = &wsi->u.ws.rx_user_buffer[ LWS_SEND_BUFFER_PRE_PADDING]; eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; + + if (lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, + &eff_buf, 0) <= 0) { /* not handle or fail */ - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, - LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, - wsi->active_extensions_user[n], - &eff_buf, 0); - if (m) - handled = 1; - } - - if (!handled) { -#else - { -#endif lwsl_ext("Unhandled ext opc 0x%x\n", wsi->u.ws.opcode); wsi->u.ws.rx_user_buffer_head = 0; return 0; } - + handled = 1; break; } @@ -366,22 +352,12 @@ spill: eff_buf.token = &wsi->u.ws.rx_user_buffer[ LWS_SEND_BUFFER_PRE_PADDING]; eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; -#ifndef LWS_NO_EXTENSIONS - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, + + if (lws_ext_callback_for_each_active(wsi, LWS_EXT_CALLBACK_PAYLOAD_RX, - wsi->active_extensions_user[n], - &eff_buf, 0); - if (m < 0) { - lwsl_ext( - "Ext '%s' failed to handle payload!\n", - wsi->active_extensions[n]->name); - return -1; - } - } -#endif + &eff_buf, 0) < 0) /* fail */ + return -1; + if (eff_buf.token_len <= 0 && callback_action != LWS_CALLBACK_CLIENT_RECEIVE_PONG) goto already_done; @@ -421,7 +397,6 @@ illegal_ctl_length: lwsl_warn("Control frame asking for extended length is illegal\n"); /* kill the connection */ return -1; - } diff --git a/lib/client.c b/lib/client.c index 0705949f..be90022e 100644 --- a/lib/client.c +++ b/lib/client.c @@ -1,7 +1,7 @@ /* * libwebsockets - small server side websockets and web server implementation * - * Copyright (C) 2010-2013 Andy Green + * Copyright (C) 2010-2014 Andy Green * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,21 +21,8 @@ #include "private-libwebsockets.h" -#ifdef WIN32 -#include -#else -#ifdef LWS_BUILTIN_GETIFADDRS -#include -#else -#include -#endif -#include -#include -#include -#endif - int lws_client_socket_service(struct libwebsocket_context *context, - struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd) + struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd) { int n; char *p = (char *)&context->service_buffer[0]; @@ -111,13 +98,12 @@ int lws_client_socket_service(struct libwebsocket_context *context, * timeout protection set in client-handshake.c */ - #ifdef LWS_OPENSSL_SUPPORT +#ifdef LWS_OPENSSL_SUPPORT /* * take care of our libwebsocket_callback_on_writable * happening at a time when there's no real connection yet */ - if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) return -1; @@ -130,20 +116,21 @@ int lws_client_socket_service(struct libwebsocket_context *context, SSL_set_mode(wsi->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); #endif - - /* use server name indication (SNI), if supported, - * when establishing connection */ + /* + * use server name indication (SNI), if supported, + * when establishing connection + */ #ifdef USE_CYASSL #ifdef CYASSL_SNI_HOST_NAME - const char *hostname = lws_hdr_simple_ptr(wsi, - _WSI_TOKEN_CLIENT_PEER_ADDRESS); - CyaSSL_UseSNI(wsi->ssl, CYASSL_SNI_HOST_NAME, - hostname, strlen(hostname)); + const char *hostname = lws_hdr_simple_ptr(wsi, + _WSI_TOKEN_CLIENT_PEER_ADDRESS); + CyaSSL_UseSNI(wsi->ssl, CYASSL_SNI_HOST_NAME, + hostname, strlen(hostname)); #endif #else - const char *hostname = lws_hdr_simple_ptr(wsi, - _WSI_TOKEN_CLIENT_PEER_ADDRESS); - SSL_set_tlsext_host_name(wsi->ssl, hostname); + const char *hostname = lws_hdr_simple_ptr(wsi, + _WSI_TOKEN_CLIENT_PEER_ADDRESS); + SSL_set_tlsext_host_name(wsi->ssl, hostname); #endif @@ -387,7 +374,6 @@ int lws_client_socket_service(struct libwebsocket_context *context, * in one packet, since at that point the connection is * definitively ready from browser pov. */ - len = 1; while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE && len > 0) { @@ -819,7 +805,6 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, int n; #ifndef LWS_NO_EXTENSIONS struct libwebsocket_extension *ext; - struct libwebsocket_extension *ext1; int ext_count = 0; #endif @@ -892,17 +877,9 @@ libwebsockets_generate_client_handshake(struct libwebsocket_context *context, 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++; - } - + n = lws_ext_callback_for_each_extension_type(context, wsi, + LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, + (char *)ext->name, 0); if (n) { /* an extension vetos us */ lwsl_ext("ext %s vetoed\n", (char *)ext->name); ext++; diff --git a/lib/extension.c b/lib/extension.c index d4660c05..0003fe9a 100644 --- a/lib/extension.c +++ b/lib/extension.c @@ -31,3 +31,187 @@ LWS_VISIBLE struct libwebsocket_extension *libwebsocket_get_internal_extensions( { return libwebsocket_internal_extensions; } + + +/* 0 = nobody had nonzero return, 1 = somebody had positive return, -1 = fail */ + +int lws_ext_callback_for_each_active(struct libwebsocket *wsi, int reason, + void *arg, int len) +{ + int n, m, handled = 0; + + for (n = 0; n < wsi->count_active_extensions; n++) { + m = wsi->active_extensions[n]->callback( + wsi->protocol->owning_server, + wsi->active_extensions[n], wsi, + reason, + wsi->active_extensions_user[n], + arg, len); + if (m < 0) { + lwsl_ext( + "Extension '%s' failed to handle callback %d!\n", + wsi->active_extensions[n]->name, reason); + return -1; + } + if (m) + handled = 1; + } + + return handled; +} + +int lws_ext_callback_for_each_extension_type( + struct libwebsocket_context *context, struct libwebsocket *wsi, + int reason, void *arg, int len) +{ + int n = 0, m, handled = 0; + struct libwebsocket_extension *ext = context->extensions; + + while (ext && ext->callback && !handled) { + m = ext->callback(context, ext, wsi, reason, + (void *)(long)n, arg, len); + if (m < 0) { + lwsl_ext( + "Extension '%s' failed to handle callback %d!\n", + wsi->active_extensions[n]->name, reason); + return -1; + } + if (m) + handled = 1; + + ext++; + n++; + } + + return 0; +} + +int +lws_issue_raw_ext_access(struct libwebsocket *wsi, + unsigned char *buf, size_t len) +{ + int ret; + struct lws_tokens eff_buf; + int m; + int n; + + eff_buf.token = (char *)buf; + eff_buf.token_len = len; + + /* + * while we have original buf to spill ourselves, or extensions report + * more in their pipeline + */ + + ret = 1; + while (ret == 1) { + + /* default to nobody has more to spill */ + + ret = 0; + + /* show every extension the new incoming data */ + m = lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_PACKET_TX_PRESEND, &eff_buf, 0); + if (m < 0) + return -1; + if (m) /* handled */ + ret = 1; + + if ((char *)buf != eff_buf.token) + /* + * extension recreated it: + * need to buffer this if not all sent + */ + wsi->u.ws.clean_buffer = 0; + + /* assuming they left us something to send, send it */ + + if (eff_buf.token_len) { + n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token, + eff_buf.token_len); + if (n < 0) + return -1; + + /* always either sent it all or privately buffered */ + } + + lwsl_parser("written %d bytes to client\n", eff_buf.token_len); + + /* no extension has more to spill? Then we can go */ + + if (!ret) + break; + + /* we used up what we had */ + + eff_buf.token = NULL; + eff_buf.token_len = 0; + + /* + * Did that leave the pipe choked? + * Or we had to hold on to some of it? + */ + + if (!lws_send_pipe_choked(wsi) && !wsi->truncated_send_len) + /* no we could add more, lets's do that */ + continue; + + lwsl_debug("choked\n"); + + /* + * Yes, he's choked. Don't spill the rest now get a callback + * when he is ready to send and take care of it there + */ + libwebsocket_callback_on_writable( + wsi->protocol->owning_server, wsi); + wsi->extension_data_pending = 1; + ret = 0; + } + + return len; +} + +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; +} diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index ba85dd2c..c28c8a7f 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -1279,53 +1279,6 @@ libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) return lws_plat_service(context, timeout_ms); } - -#ifndef LWS_NO_EXTENSIONS -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; -} -#endif - int lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or) { diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 51f12a6f..8d564584 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -210,7 +210,6 @@ struct libwebsocket_pollfd { #define libwebsocket_pollfd pollfd #endif -#ifndef LWS_NO_EXTENSIONS enum libwebsocket_extension_callback_reasons { LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT, LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, @@ -236,7 +235,6 @@ enum libwebsocket_extension_callback_reasons { LWS_EXT_CALLBACK_PAYLOAD_TX, LWS_EXT_CALLBACK_PAYLOAD_RX, }; -#endif enum libwebsocket_write_protocol { LWS_WRITE_TEXT, diff --git a/lib/output.c b/lib/output.c index c73e917d..da1d15e4 100644 --- a/lib/output.c +++ b/lib/output.c @@ -95,9 +95,7 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) struct libwebsocket_context *context = wsi->protocol->owning_server; int n; size_t real_len = len; -#ifndef LWS_NO_EXTENSIONS int m; -#endif if (!len) return 0; @@ -110,32 +108,14 @@ int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len) assert(0); } -#ifndef LWS_NO_EXTENSIONS - /* - * one of the extensions is carrying our data itself? Like mux? - */ - - for (n = 0; n < wsi->count_active_extensions; n++) { - /* - * there can only be active extensions after handshake completed - * so we can rely on protocol being set already in here - */ - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, - LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, - wsi->active_extensions_user[n], &buf, len); - if (m < 0) { - lwsl_ext("Extension reports fatal error\n"); - return -1; - } - if (m) /* handled */ { -/* lwsl_ext("ext sent it\n"); */ - n = m; - goto handle_truncated_send; - } + m = lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len); + if (m < 0) + return -1; + if (m) /* handled */ { + n = m; + goto handle_truncated_send; } -#endif if (wsi->sock < 0) lwsl_warn("** error invalid sock but expected to send\n"); @@ -258,105 +238,6 @@ lws_issue_raw_ext_access(struct libwebsocket *wsi, { return lws_issue_raw(wsi, buf, len); } -#else -int -lws_issue_raw_ext_access(struct libwebsocket *wsi, - unsigned char *buf, size_t len) -{ - int ret; - struct lws_tokens eff_buf; - int m; - int n; - - eff_buf.token = (char *)buf; - eff_buf.token_len = len; - - /* - * while we have original buf to spill ourselves, or extensions report - * more in their pipeline - */ - - ret = 1; - while (ret == 1) { - - /* default to nobody has more to spill */ - - ret = 0; - - /* show every extension the new incoming data */ - - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - 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) { - lwsl_ext("Extension: fatal error\n"); - return -1; - } - if (m) - /* - * at least one extension told us he has more - * to spill, so we will go around again after - */ - ret = 1; - } - - if ((char *)buf != eff_buf.token) - /* - * extension recreated it: - * need to buffer this if not all sent - */ - wsi->u.ws.clean_buffer = 0; - - /* assuming they left us something to send, send it */ - - if (eff_buf.token_len) { - n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token, - eff_buf.token_len); - if (n < 0) - return -1; - - /* always either sent it all or privately buffered */ - } - - lwsl_parser("written %d bytes to client\n", eff_buf.token_len); - - /* no extension has more to spill? Then we can go */ - - if (!ret) - break; - - /* we used up what we had */ - - eff_buf.token = NULL; - eff_buf.token_len = 0; - - /* - * Did that leave the pipe choked? - * Or we had to hold on to some of it? - */ - - if (!lws_send_pipe_choked(wsi) && - !wsi->truncated_send_len) - /* no we could add more, lets's do that */ - continue; - - lwsl_debug("choked\n"); - - /* - * Yes, he's choked. Don't spill the rest now get a callback - * when he is ready to send and take care of it there - */ - libwebsocket_callback_on_writable( - wsi->protocol->owning_server, wsi); - wsi->extension_data_pending = 1; - ret = 0; - } - - return len; -} #endif /** @@ -399,10 +280,7 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, unsigned char *dropmask = NULL; unsigned char is_masked_bit = 0; size_t orig_len = len; -#ifndef LWS_NO_EXTENSIONS struct lws_tokens eff_buf; - int m; -#endif if (len == 0 && protocol != LWS_WRITE_CLOSE && protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) { @@ -426,7 +304,6 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, /* if he wants all partials buffered, never have a clean_buffer */ wsi->u.ws.clean_buffer = !wsi->protocol->no_buffer_all_partial_tx; -#ifndef LWS_NO_EXTENSIONS /* * give a chance to the extensions to modify payload * pre-TX mangling is not allowed to truncate @@ -440,16 +317,9 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, case LWS_WRITE_CLOSE: break; default: - - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, - LWS_EXT_CALLBACK_PAYLOAD_TX, - wsi->active_extensions_user[n], &eff_buf, 0); - if (m < 0) - return -1; - } + if (lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_PAYLOAD_TX, &eff_buf, 0) < 0) + return -1; } /* @@ -466,7 +336,6 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, buf = (unsigned char *)eff_buf.token; len = eff_buf.token_len; -#endif switch (wsi->ietf_spec_revision) { case 13: diff --git a/lib/parsers.c b/lib/parsers.c index 423bd21b..aeefcd89 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -41,17 +41,17 @@ int lextable_decode(int pos, char c) if (lextable[pos] == FAIL_CHAR) return -1; return pos; - } else { /* b7 = 0, end or 3-byte */ - if (lextable[pos] < FAIL_CHAR) /* terminal marker */ - return pos; - - if (lextable[pos] == c) /* goto */ - return pos + (lextable[pos + 1]) + - (lextable[pos + 2] << 8); - /* fall thru goto */ - pos += 3; - /* continue */ } + /* b7 = 0, end or 3-byte */ + if (lextable[pos] < FAIL_CHAR) /* terminal marker */ + return pos; + + if (lextable[pos] == c) /* goto */ + return pos + (lextable[pos + 1]) + + (lextable[pos + 2] << 8); + /* fall thru goto */ + pos += 3; + /* continue */ } } @@ -75,9 +75,8 @@ LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_in int len = 0; n = wsi->u.hdr.ah->frag_index[h]; - if (n == 0) + if (!n) return 0; - do { len += wsi->u.hdr.ah->frags[n].len; n = wsi->u.hdr.ah->frags[n].next_frag_index; @@ -96,7 +95,7 @@ LWS_VISIBLE int lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, return -1; n = wsi->u.hdr.ah->frag_index[h]; - if (n == 0) + if (!n) return 0; do { @@ -552,14 +551,6 @@ libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) int n; struct lws_tokens eff_buf; int ret = 0; -#ifndef LWS_NO_EXTENSIONS - int handled; - int m; -#endif - -#if 0 - lwsl_debug("RX: %02X ", c); -#endif switch (wsi->lws_rx_parse_state) { case LWS_RXPS_NEW: @@ -893,10 +884,8 @@ spill: break; default: -#ifndef LWS_NO_EXTENSIONS lwsl_parser("passing opc %x up to exts\n", wsi->u.ws.opcode); - /* * It's something special we can't understand here. * Pass the payload up to the extension's parsing @@ -907,20 +896,9 @@ spill: LWS_SEND_BUFFER_PRE_PADDING]; eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; - handled = 0; - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, - LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, - wsi->active_extensions_user[n], - &eff_buf, 0); - if (m) - handled = 1; - } - - if (!handled) -#endif + if (lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, + &eff_buf, 0) <= 0) /* not handle or fail */ lwsl_ext("ext opc opcode 0x%x unknown\n", wsi->u.ws.opcode); @@ -937,22 +915,11 @@ spill: eff_buf.token = &wsi->u.ws.rx_user_buffer[ LWS_SEND_BUFFER_PRE_PADDING]; eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; -#ifndef LWS_NO_EXTENSIONS - for (n = 0; n < wsi->count_active_extensions; n++) { - m = wsi->active_extensions[n]->callback( - wsi->protocol->owning_server, - wsi->active_extensions[n], wsi, - LWS_EXT_CALLBACK_PAYLOAD_RX, - wsi->active_extensions_user[n], - &eff_buf, 0); - if (m < 0) { - lwsl_ext( - "Extension '%s' failed to handle payload!\n", - wsi->active_extensions[n]->name); - return -1; - } - } -#endif + + if (lws_ext_callback_for_each_active(wsi, + LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0) + return -1; + if (eff_buf.token_len > 0) { eff_buf.token[eff_buf.token_len] = '\0'; diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 1d46ac51..9458d6d6 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -639,9 +639,17 @@ lws_any_extension_handled(struct libwebsocket_context *context, enum libwebsocket_extension_callback_reasons r, void *v, size_t len); -LWS_EXTERN void * -lws_get_extension_user_matching_ext(struct libwebsocket *wsi, - struct libwebsocket_extension *ext); +LWS_EXTERN int +lws_ext_callback_for_each_active(struct libwebsocket *wsi, int reason, + void *buf, int len); +LWS_EXTERN int +lws_ext_callback_for_each_extension_type( + struct libwebsocket_context *context, struct libwebsocket *wsi, + int reason, void *arg, int len); +#else +#define lws_any_extension_handled(_a, _b, _c, _d, _e) (0) +#define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0) +#define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0) #endif LWS_EXTERN int diff --git a/lib/server-handshake.c b/lib/server-handshake.c index 53da4e7a..b4cdd660 100644 --- a/lib/server-handshake.c +++ b/lib/server-handshake.c @@ -225,14 +225,11 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi) /* end of response packet */ LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a"); - -#ifndef LWS_NO_EXTENSIONS + if (!lws_any_extension_handled(context, wsi, LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX, response, p - response)) { -#else - { -#endif + /* okay send the handshake response accepting the connection */ lwsl_parser("issuing resp pkt %d len\n", (int)(p - response)); diff --git a/lib/server.c b/lib/server.c index 14031b28..5d177898 100644 --- a/lib/server.c +++ b/lib/server.c @@ -49,9 +49,6 @@ libwebsocket_create_new_server_wsi(struct libwebsocket_context *context) } memset(new_wsi, 0, sizeof(struct libwebsocket)); -#ifndef LWS_NO_EXTENSIONS - new_wsi->count_active_extensions = 0; -#endif new_wsi->pending_timeout = NO_PENDING_TIMEOUT; /* intialize the instance struct */