diff --git a/lib/client-handshake.c b/lib/client-handshake.c index 05959a7d..74f894d6 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -64,6 +64,7 @@ libwebsocket_client_connect(struct libwebsocket_context *context, wsi->pings_vs_pongs = 0; wsi->protocol = NULL; wsi->pending_timeout = NO_PENDING_TIMEOUT; + wsi->count_active_extensions = 0; #ifdef LWS_OPENSSL_SUPPORT wsi->use_ssl = ssl_connection; #endif diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index c00a8ea5..c59bbdd9 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -516,6 +516,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, 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; #ifdef LWS_OPENSSL_SUPPORT @@ -644,6 +645,7 @@ libwebsocket_service_fd(struct libwebsocket_context *context, new_wsi->sock = accept_fd; new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY; new_wsi->state = WSI_STATE_ESTABLISHED; + new_wsi->count_active_extensions = 0; /* note which protocol we are proxying */ new_wsi->protocol_index_for_broadcast_proxy = wsi->protocol_index_for_broadcast_proxy; @@ -1769,6 +1771,7 @@ OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) struct libwebsocket_context * libwebsocket_create_context(int port, const char *interf, struct libwebsocket_protocols *protocols, + struct libwebsocket_extension *extensions, const char *ssl_cert_filepath, const char *ssl_private_key_filepath, int gid, int uid, unsigned int options) @@ -1822,6 +1825,7 @@ libwebsocket_create_context(int port, const char *interf, context->http_proxy_address[0] = '\0'; context->options = options; context->fds_count = 0; + context->extensions = extensions; #ifdef WIN32 context->fd_random = 0; @@ -2067,6 +2071,7 @@ libwebsocket_create_context(int port, const char *interf, wsi = malloc(sizeof(struct libwebsocket)); memset(wsi, 0, sizeof (struct libwebsocket)); wsi->sock = sockfd; + wsi->count_active_extensions = 0; wsi->mode = LWS_CONNMODE_SERVER_LISTENER; insert_wsi(context, wsi); @@ -2146,6 +2151,7 @@ libwebsocket_create_context(int port, const char *interf, memset(wsi, 0, sizeof (struct libwebsocket)); wsi->sock = fd; wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER; + wsi->count_active_extensions = 0; /* note which protocol we are proxying */ wsi->protocol_index_for_broadcast_proxy = context->count_protocols; diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 1bf43bf8..5402ee1e 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -401,9 +401,31 @@ struct libwebsocket_protocols { int protocol_index; }; +/** + * struct libwebsocket_extension - An extension we know how to cope with + * + * @name: Formal extension name, eg, "deflate-stream" + * @callback: Service callback + * @per_session_data_size: Libwebsockets will auto-malloc this much + * memory for the use of the extension, a pointer + * to it comes in the @user callback parameter + */ + +struct libwebsocket_extension { + const char *name; + int (*callback)(struct libwebsocket_context *context, + struct libwebsocket *wsi, + enum libwebsocket_callback_reasons reason, void *user, + void *in, size_t len); + size_t per_session_data_size; +}; + + + extern struct libwebsocket_context * libwebsocket_create_context(int port, const char * interf, struct libwebsocket_protocols *protocols, + struct libwebsocket_extension *extensions, const char *ssl_cert_filepath, const char *ssl_private_key_filepath, int gid, int uid, unsigned int options); diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 0504c07d..94802ae9 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -107,6 +107,7 @@ static inline void debug(const char *format, ...) #define MAX_USER_RX_BUFFER 4096 #define MAX_BROADCAST_PAYLOAD 2048 #define LWS_MAX_PROTOCOLS 10 +#define LWS_MAX_EXTENSIONS_ACTIVE 10 #define SPEC_LATEST_SUPPORTED 6 #define MAX_WEBSOCKET_04_KEY_LEN 128 @@ -203,6 +204,7 @@ struct libwebsocket_context { #endif struct libwebsocket_protocols *protocols; int count_protocols; + struct libwebsocket_extension *extensions; }; @@ -223,6 +225,10 @@ enum pending_timeout { struct libwebsocket { const struct libwebsocket_protocols *protocol; + const struct libwebsocket_extension * + active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; + void * active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; + int count_active_extensions; enum lws_connection_states state; diff --git a/libwebsockets-api-doc.html b/libwebsockets-api-doc.html index 70bf4f2f..239f3934 100644 --- a/libwebsockets-api-doc.html +++ b/libwebsockets-api-doc.html @@ -221,6 +221,7 @@ has been created. (int port, const char * interf, struct libwebsocket_protocols * protocols, +struct libwebsocket_extension * extensions, const char * ssl_cert_filepath, const char * ssl_private_key_filepath, int gid, @@ -740,3 +741,21 @@ array of these structures is passed to libwebsocket_create_server allows as many protocols as you like to be handled by one server.