From 0c9563bbd255f4d98bd463f71f967f8bf4be7040 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Mon, 10 Jun 2013 22:54:40 +0800 Subject: [PATCH 1/7] add LWS_CALLBACK_CLOSED_HTTP Adds a callback to allow handling HTTP protocol connections closing. Signed-off-by: Andy Green --- changelog | 3 +++ lib/libwebsockets.c | 2 ++ lib/libwebsockets.h | 3 +++ 3 files changed, 8 insertions(+) diff --git a/changelog b/changelog index 8b79f121a9..f6b85d94cb 100644 --- a/changelog +++ b/changelog @@ -28,6 +28,9 @@ User api additions then does not have to try to track while connections it choked, this will free up all of them in one call. + - there's a new, optional callback LWS_CALLBACK_CLOSED_HTTP which gets + called when an HTTP protocol socket closes + User api changes ---------------- diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index d312390d4c..e75a51009e 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -210,6 +210,8 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, lwsl_debug("closing http fd %d\n", wsi->u.http.fd); close(wsi->u.http.fd); wsi->u.http.fd = 0; + context->protocols[0].callback(context, wsi, + LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); } #ifndef LWS_NO_EXTENSIONS diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 65bae14534..9f686bd879 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -138,6 +138,7 @@ enum libwebsocket_callback_reasons { LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH, LWS_CALLBACK_CLIENT_ESTABLISHED, LWS_CALLBACK_CLOSED, + LWS_CALLBACK_CLOSED_HTTP, LWS_CALLBACK_RECEIVE, LWS_CALLBACK_CLIENT_RECEIVE, LWS_CALLBACK_CLIENT_RECEIVE_PONG, @@ -419,6 +420,8 @@ struct libwebsocket_extension; * * LWS_CALLBACK_CLOSED: when the websocket session ends * + * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends + * * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a * remote client, it can be found at *in and is * len bytes long From d8b8542d59736bbf7c2cc1fbceb14310e5630a03 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 19 Jun 2013 15:10:26 +0200 Subject: [PATCH 2/7] Declare "sha1_init" statically to avoid naming conflicts if linked statically with other libraries providing a method with the same name (e.g. OpenSSL). --- lib/sha-1.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sha-1.c b/lib/sha-1.c index 6020c227e0..1491502d2f 100644 --- a/lib/sha-1.c +++ b/lib/sha-1.c @@ -215,7 +215,7 @@ sha1_step(struct sha1_ctxt *ctxt) /*------------------------------------------------------------*/ -void +static void sha1_init(struct sha1_ctxt *ctxt) { bzero(ctxt, sizeof(struct sha1_ctxt)); From d727e9f698e91e1832a53e088ad54f2fda69dba8 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sun, 23 Jun 2013 14:47:26 +0800 Subject: [PATCH 3/7] Support empty ping and pong packets. --- lib/output.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/output.c b/lib/output.c index ef823c3e9f..37502dd278 100644 --- a/lib/output.c +++ b/lib/output.c @@ -311,7 +311,7 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, int m; #endif - if (len == 0 && protocol != LWS_WRITE_CLOSE) { + if (len == 0 && protocol != LWS_WRITE_CLOSE && protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) { lwsl_warn("zero length libwebsocket_write attempt\n"); return 0; } From db761be972dbeb8a9d5f96d7181860c887bd668b Mon Sep 17 00:00:00 2001 From: "Niall T. Davidson" Date: Sat, 29 Jun 2013 10:16:18 +0800 Subject: [PATCH 4/7] Added additional LWS_CALLBACK_CLOSED_HTTP callback to just_kill_connection branch of close_and_free_session --- lib/libwebsockets.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index e75a51009e..661876edac 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -370,6 +370,10 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context, lwsl_debug("calling back CLOSED\n"); wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, wsi->user_space, NULL, 0); + } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) { + lwsl_debug("calling back CLOSED_HTTP\n"); + context->protocols[0].callback(context, wsi, + LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 ); } else lwsl_debug("not calling back closed\n"); From eb51a7a6efc3c9941d3ccb862936395966deba7c Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Sat, 29 Jun 2013 10:18:52 +0800 Subject: [PATCH 5/7] add CMake find function for libwebsockets Signed-off-by: Jiri Hnidek --- scripts/FindLibWebSockets.cmake | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 scripts/FindLibWebSockets.cmake diff --git a/scripts/FindLibWebSockets.cmake b/scripts/FindLibWebSockets.cmake new file mode 100644 index 0000000000..e7d283932a --- /dev/null +++ b/scripts/FindLibWebSockets.cmake @@ -0,0 +1,33 @@ +# This module tries to find libWebsockets library and include files +# +# LIBWEBSOCKETS_INCLUDE_DIR, path where to find libwebsockets.h +# LIBWEBSOCKETS_LIBRARY_DIR, path where to find libwebsockets.so +# LIBWEBSOCKETS_LIBRARIES, the library to link against +# LIBWEBSOCKETS_FOUND, If false, do not try to use libWebSockets +# +# This currently works probably only for Linux + +FIND_PATH ( LIBWEBSOCKETS_INCLUDE_DIR libwebsockets.h + /usr/local/include + /usr/include +) + +FIND_LIBRARY ( LIBWEBSOCKETS_LIBRARIES websockets + /usr/local/lib + /usr/lib +) + +GET_FILENAME_COMPONENT( LIBWEBSOCKETS_LIBRARY_DIR ${LIBWEBSOCKETS_LIBRARIES} PATH ) + +SET ( LIBWEBSOCKETS_FOUND "NO" ) +IF ( LIBWEBSOCKETS_INCLUDE_DIR ) + IF ( LIBWEBSOCKETS_LIBRARIES ) + SET ( LIBWEBSOCKETS_FOUND "YES" ) + ENDIF ( LIBWEBSOCKETS_LIBRARIES ) +ENDIF ( LIBWEBSOCKETS_INCLUDE_DIR ) + +MARK_AS_ADVANCED( + LIBWEBSOCKETS_LIBRARY_DIR + LIBWEBSOCKETS_INCLUDE_DIR + LIBWEBSOCKETS_LIBRARIES +) From 8294c1fa4f6485fa6c5f15078d609f62b3026d98 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sat, 29 Jun 2013 10:22:09 +0800 Subject: [PATCH 6/7] Request "send" not to trigger SIGPIPE on errors. --- lib/client-handshake.c | 2 +- lib/client.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client-handshake.c b/lib/client-handshake.c index de99a208eb..2dd810c561 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -85,7 +85,7 @@ struct libwebsocket *__libwebsocket_client_connect_2( if (context->http_proxy_port) { - n = send(wsi->sock, context->service_buffer, plen, 0); + n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL); if (n < 0) { compatible_close(wsi->sock); lwsl_debug("ERROR writing to proxy socket\n"); diff --git a/lib/client.c b/lib/client.c index b6236a2b80..94a6694714 100644 --- a/lib/client.c +++ b/lib/client.c @@ -229,7 +229,7 @@ int lws_client_socket_service(struct libwebsocket_context *context, else #endif n = send(wsi->sock, context->service_buffer, - p - (char *)context->service_buffer, 0); + p - (char *)context->service_buffer, MSG_NOSIGNAL); lws_latency(context, wsi, "send or SSL_write LWS_CONNMODE...HANDSHAKE", n, n >= 0); From 6c58228577306c023b072b5c7c7a2b044a94f12a Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Sat, 29 Jun 2013 10:24:16 +0800 Subject: [PATCH 7/7] fix http incomplete send handling Signed-off-by: David Gauchard --- lib/output.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/output.c b/lib/output.c index 37502dd278..224a0f27a5 100644 --- a/lib/output.c +++ b/lib/output.c @@ -504,7 +504,6 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, LWS_VISIBLE int libwebsockets_serve_http_file_fragment( struct libwebsocket_context *context, struct libwebsocket *wsi) { - int ret = 0; int n, m; while (!lws_send_pipe_choked(wsi)) { @@ -516,7 +515,7 @@ LWS_VISIBLE int libwebsockets_serve_http_file_fragment( if (m < 0) return -1; - wsi->u.http.filepos += n; + wsi->u.http.filepos += m; if (m != n) /* adjust for what was not sent */ lseek(wsi->u.http.fd, m - n, SEEK_CUR); @@ -525,23 +524,23 @@ LWS_VISIBLE int libwebsockets_serve_http_file_fragment( if (n < 0) return -1; /* caller will close */ - if (n < sizeof(context->service_buffer) || - wsi->u.http.filepos == wsi->u.http.filelen) { + if (wsi->u.http.filepos == wsi->u.http.filelen) { wsi->state = WSI_STATE_HTTP; if (wsi->protocol->callback) - ret = user_callback_handle_rxflow( + /* ignore callback returned value */ + user_callback_handle_rxflow( wsi->protocol->callback, context, wsi, LWS_CALLBACK_HTTP_FILE_COMPLETION, wsi->user_space, NULL, 0); - return ret; + return 1; /* >0 indicates completed */ } } lwsl_notice("choked before able to send whole file (post)\n"); libwebsocket_callback_on_writable(context, wsi); - return ret; + return 0; /* indicates further processing must be done */ } /**