make protocols const require explicit context API BREAK
authorAndy Green <andy.green@linaro.org>
Fri, 11 Dec 2015 02:45:35 +0000 (10:45 +0800)
committerAndy Green <andy.green@linaro.org>
Sun, 13 Dec 2015 22:43:26 +0000 (06:43 +0800)
The user protocols struct has not been const until now.

This has been painful for a while because the semantics of the protocols
struct look like it's going to be treated as const.

At context creation, the protocols struct has been getting marked with the context,
and three apis exploited that to only need to be passed a pointer to a protocol to
get access to the context.

This patch removes the two writeable members in the context (these were never directly
used by user code), changes all pointers to protocols to be const, and adds an explicit
first argument to the three affected apis so they can have access to context.

The three affected apis are these

 LWS_VISIBLE LWS_EXTERN int
-lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol);
+lws_callback_on_writable_all_protocol(const struct lws_context *context,
+                                     const struct lws_protocols *protocol);

 LWS_VISIBLE LWS_EXTERN int
-lws_callback_all_protocol(const struct lws_protocols *protocol, int reason);
+lws_callback_all_protocol(struct lws_context *context,
+                         const struct lws_protocols *protocol, int reason);

 LWS_VISIBLE LWS_EXTERN void
-lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol);
+lws_rx_flow_allow_all_protocol(const struct lws_context *context,
+                              const struct lws_protocols *protocol);

unfortunately the original apis can no longer be emulated and users of them must update.

Signed-off-by: Andy Green <andy.green@linaro.org>
17 files changed:
lib/client.c
lib/context.c
lib/extension-deflate-frame.c
lib/extension-deflate-frame.h
lib/extension-deflate-stream.c
lib/extension-deflate-stream.h
lib/extension.c
lib/libwebsockets.c
lib/libwebsockets.h
lib/pollfd.c
lib/private-libwebsockets.h
lib/server-handshake.c
test-server/test-client.c
test-server/test-echo.c
test-server/test-server-mirror.c
test-server/test-server-pthreads.c
test-server/test-server.c

index 9beb5be..073b293 100644 (file)
@@ -498,7 +498,7 @@ lws_client_interpret_server_handshake(struct lws_context *context,
        const char *pc;
        char *p;
 #ifndef LWS_NO_EXTENSIONS
-       struct lws_extension *ext;
+       const struct lws_extension *ext;
        char ext_name[128];
        const char *c;
        int more = 1;
@@ -828,7 +828,7 @@ lws_generate_client_handshake(struct lws_context *context,
        char buf[128], hash[20], key_b64[40], *p = pkt;
        int n;
 #ifndef LWS_NO_EXTENSIONS
-       struct lws_extension *ext;
+       const struct lws_extension *ext;
        int ext_count = 0;
 #endif
 
index d9c05e6..ef8414b 100644 (file)
@@ -245,7 +245,7 @@ bail:
 LWS_VISIBLE void
 lws_context_destroy(struct lws_context *context)
 {
-       struct lws_protocols *protocol = NULL;
+       const struct lws_protocols *protocol = NULL;
        int n;
 
        lwsl_notice("%s\n", __func__);
index 87eac8b..77cb660 100644 (file)
@@ -9,7 +9,7 @@
 
 int lws_extension_callback_deflate_frame(
                struct lws_context *context,
-               struct lws_extension *ext,
+               const struct lws_extension *ext,
                struct lws *wsi,
                enum lws_extension_callback_reasons reason,
                void *user, void *in, size_t len)
index 5e5b3f1..edc9de1 100644 (file)
@@ -19,7 +19,7 @@ struct lws_ext_deflate_frame_conn {
 
 extern int lws_extension_callback_deflate_frame(
                struct lws_context *context,
-               struct lws_extension *ext,
+               const struct lws_extension *ext,
                struct lws *wsi,
                enum lws_extension_callback_reasons reason,
                void *user, void *in, size_t len);
index f69b12b..5a1f158 100644 (file)
@@ -9,7 +9,7 @@
 
 int lws_extension_callback_deflate_stream(
                struct lws_context *context,
-               struct lws_extension *ext,
+               const struct lws_extension *ext,
                struct lws *wsi,
                        enum lws_extension_callback_reasons reason,
                                               void *user, void *in, size_t len)
index b417790..80e40e9 100644 (file)
@@ -14,7 +14,7 @@ struct lws_ext_deflate_stream_conn {
 
 extern int lws_extension_callback_deflate_stream(
                struct lws_context *context,
-               struct lws_extension *ext,
+               const struct lws_extension *ext,
                struct lws *wsi,
                enum lws_extension_callback_reasons reason,
                                              void *user, void *in, size_t len);
index 540be10..33fbd0c 100644 (file)
@@ -73,7 +73,7 @@ int lws_ext_callback_for_each_extension_type(
                                int reason, void *arg, int len)
 {
        int n = 0, m, handled = 0;
-       struct lws_extension *ext = context->extensions;
+       const struct lws_extension *ext = context->extensions;
 
        while (ext && ext->callback && !handled) {
                m = ext->callback(context, ext, wsi, reason,
index 54fddbc..d26e11a 100644 (file)
@@ -481,9 +481,9 @@ lws_context_user(struct lws_context *context)
  */
 
 LWS_VISIBLE int
-lws_callback_all_protocol(const struct lws_protocols *protocol, int reason)
+lws_callback_all_protocol(struct lws_context *context,
+                         const struct lws_protocols *protocol, int reason)
 {
-       struct lws_context *context = protocol->owning_server;
        struct lws *wsi;
        int n;
 
@@ -616,9 +616,9 @@ lws_rx_flow_control(struct lws *wsi, int enable)
  */
 
 LWS_VISIBLE void
-lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol)
+lws_rx_flow_allow_all_protocol(const struct lws_context *context,
+                              const struct lws_protocols *protocol)
 {
-       struct lws_context *context = protocol->owning_server;
        int n;
        struct lws *wsi;
 
@@ -936,7 +936,7 @@ lws_get_fops(struct lws_context *context)
 }
 
 LWS_VISIBLE LWS_EXTERN struct lws_context *
-lws_get_ctx(struct lws *wsi)
+lws_get_ctx(const struct lws *wsi)
 {
        return wsi->context;
 }
@@ -1087,13 +1087,6 @@ libwebsockets_get_protocol(struct lws *wsi)
        return lws_get_protocol(wsi);
 }
 
-#undef libwebsocket_callback_on_writable_all_protocol
-LWS_VISIBLE LWS_EXTERN int
-libwebsocket_callback_on_writable_all_protocol(
-                                const struct lws_protocols *protocol)
-{
-       return lws_callback_on_writable_all_protocol(protocol);
-}
 
 #undef libwebsocket_callback_on_writable
 LWS_VISIBLE LWS_EXTERN int
@@ -1103,14 +1096,6 @@ libwebsocket_callback_on_writable(struct lws_context *context,
        return lws_callback_on_writable(context, wsi);
 }
 
-#undef libwebsocket_callback_all_protocol
-LWS_VISIBLE LWS_EXTERN int
-libwebsocket_callback_all_protocol(
-               const struct lws_protocols *protocol, int reason)
-{
-       return lws_callback_all_protocol(protocol, reason);
-}
-
 #undef libwebsocket_get_socket_fd
 LWS_VISIBLE LWS_EXTERN int
 libwebsocket_get_socket_fd(struct lws *wsi)
@@ -1139,13 +1124,6 @@ libwebsocket_rx_flow_control(struct lws *wsi, int enable)
        return lws_rx_flow_control(wsi, enable);
 }
 
-#undef libwebsocket_rx_flow_allow_all_protocol
-LWS_VISIBLE LWS_EXTERN void
-libwebsocket_rx_flow_allow_all_protocol(const struct lws_protocols *protocol)
-{
-       lws_rx_flow_allow_all_protocol(protocol);
-}
-
 #undef libwebsockets_remaining_packet_payload
 LWS_VISIBLE LWS_EXTERN size_t
 libwebsockets_remaining_packet_payload(struct lws *wsi)
index 2dd55aa..6da6d09 100644 (file)
@@ -1090,10 +1090,11 @@ struct lws_extension;
  *             duration of wsi dereference from the other thread context.
  */
 LWS_VISIBLE LWS_EXTERN int
-callback(struct lws_context *context, struct lws *wsi,
+callback(const struct lws_context *context, const struct lws *wsi,
         enum lws_callback_reasons reason, void *user, void *in, size_t len);
 
-typedef int (callback_function)(struct lws_context *context, struct lws *wsi,
+typedef int (callback_function)(struct lws_context *context,
+                               struct lws *wsi,
                                enum lws_callback_reasons reason, void *user,
                                void *in, size_t len);
 
@@ -1157,12 +1158,12 @@ typedef int (callback_function)(struct lws_context *context, struct lws *wsi,
  *             set the lws_tokens token pointer to it.
  */
 LWS_VISIBLE LWS_EXTERN int
-extension_callback(struct lws_context *context, struct lws_extension *ext,
+extension_callback(struct lws_context *context, const struct lws_extension *ext,
                   struct lws *wsi, enum lws_extension_callback_reasons reason,
                   void *user, void *in, size_t len);
 
 typedef int (extension_callback_function)(struct lws_context *context,
-                       struct lws_extension *ext, struct lws *wsi,
+                       const struct lws_extension *ext, struct lws *wsi,
                        enum lws_extension_callback_reasons reason,
                        void *user, void *in, size_t len);
 #endif
@@ -1298,9 +1299,9 @@ struct lws_extension {
 struct lws_context_creation_info {
        int port;
        const char *iface;
-       struct lws_protocols *protocols;
-       struct lws_extension *extensions;
-       struct lws_token_limits *token_limits;
+       const struct lws_protocols *protocols;
+       const struct lws_extension *extensions;
+       const struct lws_token_limits *token_limits;
        const char *ssl_private_key_password;
        const char *ssl_cert_filepath;
        const char *ssl_private_key_filepath;
@@ -1516,13 +1517,15 @@ LWS_VISIBLE LWS_EXTERN const struct lws_protocols *
 lws_get_protocol(struct lws *wsi);
 
 LWS_VISIBLE LWS_EXTERN int
-lws_callback_on_writable(struct lws_context *context, struct lws *wsi);
+lws_callback_on_writable(const struct lws_context *context, struct lws *wsi);
 
 LWS_VISIBLE LWS_EXTERN int
-lws_callback_on_writable_all_protocol(const struct lws_protocols *protocol);
+lws_callback_on_writable_all_protocol(const struct lws_context *context,
+                                     const struct lws_protocols *protocol);
 
 LWS_VISIBLE LWS_EXTERN int
-lws_callback_all_protocol(const struct lws_protocols *protocol, int reason);
+lws_callback_all_protocol(struct lws_context *context,
+                         const struct lws_protocols *protocol, int reason);
 
 LWS_VISIBLE LWS_EXTERN int
 lws_get_socket_fd(struct lws *wsi);
@@ -1537,7 +1540,8 @@ LWS_VISIBLE LWS_EXTERN int
 lws_rx_flow_control(struct lws *wsi, int enable);
 
 LWS_VISIBLE LWS_EXTERN void
-lws_rx_flow_allow_all_protocol(const struct lws_protocols *protocol);
+lws_rx_flow_allow_all_protocol(const struct lws_context *context,
+                              const struct lws_protocols *protocol);
 
 LWS_VISIBLE LWS_EXTERN size_t
 lws_remaining_packet_payload(struct lws *wsi);
@@ -1630,7 +1634,7 @@ LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops *
 lws_get_fops(struct lws_context *context);
 
 LWS_VISIBLE LWS_EXTERN struct lws_context *
-lws_get_ctx(struct lws *wsi);
+lws_get_ctx(const struct lws *wsi);
 
 /*
  * File Operations access helpers
index 6e47dcc..a855bc3 100644 (file)
@@ -206,8 +206,7 @@ lws_change_pollfd(struct lws *wsi, int _and, int _or)
  */
 
 LWS_VISIBLE int
-lws_callback_on_writable(struct lws_context *context,
-                                                     struct lws *wsi)
+lws_callback_on_writable(const struct lws_context *context, struct lws *wsi)
 {
 #ifdef LWS_USE_HTTP2
        struct lws *network_wsi, *wsi2;
@@ -284,10 +283,9 @@ network_sock:
  */
 
 LWS_VISIBLE int
-lws_callback_on_writable_all_protocol(
+lws_callback_on_writable_all_protocol(const struct lws_context *context,
                                  const struct lws_protocols *protocol)
 {
-       struct lws_context *context = protocol->owning_server;
        int n;
        struct lws *wsi;
 
index 063026f..8177306 100644 (file)
@@ -525,12 +525,12 @@ struct lws_context {
 #else
 #define lws_ssl_anybody_has_buffered_read(ctx) (0)
 #endif
-       struct lws_protocols *protocols;
+       const struct lws_protocols *protocols;
        int count_protocols;
 #ifndef LWS_NO_EXTENSIONS
-       struct lws_extension *extensions;
+       const struct lws_extension *extensions;
 #endif
-       struct lws_token_limits *token_limits;
+       const struct lws_token_limits *token_limits;
        void *user_space;
        
        struct lws_plat_file_ops fops;
@@ -836,10 +836,10 @@ struct lws {
     struct lws_io_watcher w_read;
     struct lws_io_watcher w_write;
 #endif /* LWS_USE_LIBEV */
-       const struct lws_context *context;
+       struct lws_context *context;
        const struct lws_protocols *protocol;
 #ifndef LWS_NO_EXTENSIONS
-       struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
+       const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
        void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
        unsigned char count_active_extensions;
        unsigned int extension_data_pending:1;
index f562716..2d7441b 100644 (file)
@@ -30,7 +30,7 @@ lws_extension_server_handshake(struct lws_context *context,
        int n;
        char *c;
        char ext_name[128];
-       struct lws_extension *ext;
+       const struct lws_extension *ext;
        int ext_count = 0;
        int more = 1;
 
index 59e6f4f..7ab9f58 100644 (file)
@@ -72,7 +72,7 @@ enum demo_protocols {
  */
 
 static int
-callback_dumb_increment(struct lws_context *this,
+callback_dumb_increment(struct lws_context *context,
                        struct lws *wsi,
                        enum lws_callback_reasons reason,
                        void *user, void *in, size_t len)
index ccdc3c5..d9e6582 100644 (file)
@@ -416,7 +416,8 @@ int main(int argc, char **argv)
                        gettimeofday(&tv, NULL);
 
                        if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) {
-                               lws_callback_on_writable_all_protocol(&protocols[0]);
+                               lws_callback_on_writable_all_protocol(context,
+                                               &protocols[0]);
                                oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
                        }
                }
index 0e95e4f..3f7f2b2 100644 (file)
@@ -82,7 +82,7 @@ callback_lws_mirror(struct lws_context *context,
 
                        if (((ringbuffer_head - pss->ringbuffer_tail) &
                                  (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
-                               lws_rx_flow_allow_all_protocol(
+                               lws_rx_flow_allow_all_protocol(context,
                                               lws_get_protocol(wsi));
 
                        if (lws_partial_buffered(wsi) || lws_send_pipe_choked(wsi)) {
@@ -122,7 +122,7 @@ choke:
                lws_rx_flow_control(wsi, 0);
 
 done:
-               lws_callback_on_writable_all_protocol(
+               lws_callback_on_writable_all_protocol(context,
                                               lws_get_protocol(wsi));
                break;
 
index 3d3c3d6..c128eff 100644 (file)
@@ -123,7 +123,7 @@ void *thread_dumb_increment(void *threadid)
                 * them is protected by the same lock
                 */
                pthread_mutex_lock(&lock_established_conns);
-               lws_callback_on_writable_all_protocol(
+               lws_callback_on_writable_all_protocol(context,
                                &protocols[PROTOCOL_DUMB_INCREMENT]);
                pthread_mutex_unlock(&lock_established_conns);
                usleep(100000);
index 5620e4e..0f55b00 100644 (file)
@@ -314,7 +314,7 @@ int main(int argc, char **argv)
 
                ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
                if ((ms - oldms) > 50) {
-                       lws_callback_on_writable_all_protocol(
+                       lws_callback_on_writable_all_protocol(context,
                                &protocols[PROTOCOL_DUMB_INCREMENT]);
                        oldms = ms;
                }