diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 0d6d5f22..a66f7d04 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -5,6 +5,7 @@ Release Notes v1.7 (Under Development) Changes ------- +- URI specific callbacks for websockets - Add chunked transfer support - Update LuaFileSystem - Update Lua to 5.2.4 diff --git a/docs/Embedding.md b/docs/Embedding.md index fce43b1c..fc5acff3 100644 --- a/docs/Embedding.md +++ b/docs/Embedding.md @@ -52,7 +52,7 @@ By default, the server will automatically serve up files like a normal HTTP serv Lua Support ------ -Lua is a server side include functionality. Files ending in .la will be processed with Lua. +Lua is a server side include functionality. Files ending in .lua will be processed with Lua. ##### Add the following CFLAGS diff --git a/examples/websocket/WebSockCallbacks.c b/examples/websocket/WebSockCallbacks.c index 1619a22f..2ba3aee2 100644 --- a/examples/websocket/WebSockCallbacks.c +++ b/examples/websocket/WebSockCallbacks.c @@ -1,3 +1,8 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + #include #include #include diff --git a/examples/websocket/websocket.c b/examples/websocket/websocket.c index 40ef05c7..efd6cf60 100644 --- a/examples/websocket/websocket.c +++ b/examples/websocket/websocket.c @@ -1,3 +1,8 @@ +/* This example uses deprecated interfaces: global websocket callbacks. + They have been superseeded by URI specific callbacks. + See examples/embedded_c for an up to date example. + */ + #include #include #include diff --git a/examples/websocket_client/websocket_client.c b/examples/websocket_client/websocket_client.c index 6d175c4b..cc6e4b5c 100644 --- a/examples/websocket_client/websocket_client.c +++ b/examples/websocket_client/websocket_client.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2014 the Civetweb developers +* Copyright (c) 2014-2015 the Civetweb developers * Copyright (c) 2014 Jordan Shelley * https://github.com/jshelley * License http://opensource.org/licenses/mit-license.php MIT License diff --git a/src/civetweb.c b/src/civetweb.c index f92b732d..0d8c3320 100755 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -5865,19 +5865,11 @@ static void read_websocket(struct mg_connection *conn, mg_websocket_data_handler } } - /* Exit the loop if callback signalled to exit, - or "connection close" opcode received. */ + /* Exit the loop if callback signals to exit (server side), + or "connection close" opcode received (client side). */ exit_by_callback = 0; - if ((ws_data_handler != NULL && -#ifdef USE_LUA - (conn->lua_websocket_state == NULL) && -#endif - !ws_data_handler(conn, mop, data, data_len, callback_data)) || -#ifdef USE_LUA - (conn->lua_websocket_state && - !lua_websocket_data(conn, conn->lua_websocket_state, mop, data, data_len)) || -#endif - 0) { + if ((ws_data_handler != NULL) && + !ws_data_handler(conn, mop, data, data_len, callback_data)) { exit_by_callback = 1; } @@ -6029,8 +6021,10 @@ static void handle_websocket_request(struct mg_connection *conn, /* Step 7: Enter the read loop */ if (is_callback_resource) { read_websocket(conn, ws_data_handler, cbData); +#if defined(USE_LUA) } else if (lua_websock) { - read_websocket(conn, NULL, NULL); + read_websocket(conn, lua_websocket_data, conn->lua_websocket_state); +#endif } /* Step 8: Call the close handler */ @@ -6704,7 +6698,8 @@ static void handle_request(struct mg_connection *conn) /* 8. handle websocket requests */ #if defined(USE_WEBSOCKET) if (is_websocket_request) { - handle_websocket_request(conn, path, is_callback_resource, + handle_websocket_request(conn, path, + !is_script_resource /* could be deprecated global callback */, deprecated_websocket_connect_wrapper, deprecated_websocket_ready_wrapper, deprecated_websocket_data_wrapper, @@ -7739,12 +7734,16 @@ struct mg_connection *mg_connect_websocket_client(const char *host, int port, in conn = mg_download(host, port, use_ssl, error_buffer, error_buffer_size, handshake_req, path, host, magic, origin); - + ä /* Connection object will be null if something goes wrong */ if (conn == NULL || (strcmp(conn->request_info.uri, "101") != 0)) { + if (!*error_buffer) { + /* if there is a connection, but it did not return 101, error_buffer is not yet set */ + mg_snprintf(conn, error_buffer, error_buffer_size, "Unexpected server reply"); + } DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer); - if(conn != NULL) { mg_free(conn); conn = NULL; } + if (conn != NULL) { mg_free(conn); conn = NULL; } return conn; } diff --git a/src/mod_lua.inl b/src/mod_lua.inl index 0620652e..bdfab621 100644 --- a/src/mod_lua.inl +++ b/src/mod_lua.inl @@ -1306,7 +1306,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn) return ok ? (void*)ws : NULL; } -static int lua_websocket_data(struct mg_connection * conn, void *ws_arg, int bits, char *data, size_t data_len) +static int lua_websocket_data(struct mg_connection * conn, int bits, char *data, size_t data_len, void *ws_arg) { struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg); int err, ok = 0;