From 6ab6ee2c0fa62c1ed7f388ed0bf0bf25e7c4fe26 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sun, 6 Dec 2015 06:39:51 +0800 Subject: [PATCH] more server close processing error handling precisions Signed-off-by: Andy Green --- lib/handshake.c | 74 +++++++++++++++++++++++++-------------------------------- lib/server.c | 33 ++++++++++--------------- lib/ssl.c | 17 +++++++------ 3 files changed, 52 insertions(+), 72 deletions(-) diff --git a/lib/handshake.c b/lib/handshake.c index 1c783a6..67eb41b 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -58,7 +58,7 @@ LWS_VISIBLE int lws_read(struct lws_context *context, - struct lws *wsi, unsigned char *buf, size_t len) + struct lws *wsi, unsigned char *buf, size_t len) { size_t n; int body_chunk_len; @@ -124,21 +124,13 @@ http_new: case WSI_STATE_HTTP_ISSUING_FILE: goto read_ok; case WSI_STATE_HTTP_BODY: - wsi->u.http.content_remain = wsi->u.http.content_length; - if (!wsi->u.http.content_remain) { - /* there is no POST content */ - lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); - if (wsi->protocol->callback) { - n = wsi->protocol->callback( - wsi->protocol->owning_server, wsi, - LWS_CALLBACK_HTTP_BODY_COMPLETION, - wsi->user_space, NULL, 0); - if (n) - goto bail; - } - goto http_complete; - } - goto http_postbody; + wsi->u.http.content_remain = + wsi->u.http.content_length; + if (wsi->u.http.content_remain) + goto http_postbody; + + /* there is no POST content */ + goto postbody_completion; default: break; } @@ -155,32 +147,31 @@ http_postbody: wsi->u.http.content_remain -= body_chunk_len; len -= body_chunk_len; - if (wsi->protocol->callback) { - n = wsi->protocol->callback( - wsi->protocol->owning_server, wsi, - LWS_CALLBACK_HTTP_BODY, wsi->user_space, - buf, body_chunk_len); - if (n) - goto bail; - } + n = wsi->protocol->callback( + wsi->protocol->owning_server, wsi, + LWS_CALLBACK_HTTP_BODY, wsi->user_space, + buf, body_chunk_len); + if (n) + goto bail; + buf += body_chunk_len; - if (!wsi->u.http.content_remain) { - /* he sent the content in time */ - lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); - if (wsi->protocol->callback) { - n = wsi->protocol->callback( - wsi->protocol->owning_server, wsi, - LWS_CALLBACK_HTTP_BODY_COMPLETION, - wsi->user_space, NULL, 0); - if (n) - goto bail; - } - goto http_complete; - } else - lws_set_timeout(wsi, - PENDING_TIMEOUT_HTTP_CONTENT, - AWAITING_TIMEOUT); + if (wsi->u.http.content_remain) { + lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, + AWAITING_TIMEOUT); + break; + } + /* he sent all the content in time */ +postbody_completion: + lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); + n = wsi->protocol->callback( + wsi->protocol->owning_server, wsi, + LWS_CALLBACK_HTTP_BODY_COMPLETION, + wsi->user_space, NULL, 0); + if (n) + goto bail; + + goto http_complete; } break; @@ -204,7 +195,7 @@ http_postbody: } read_ok: - /* Nothing more to do for now. */ + /* Nothing more to do for now */ lwsl_debug("lws_read: read_ok\n"); return 0; @@ -225,7 +216,6 @@ http_complete: bail: lwsl_debug("closing connection at lws_read bail:\n"); - lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS); return -1; diff --git a/lib/server.c b/lib/server.c index 5d998e1..d8d3846 100644 --- a/lib/server.c +++ b/lib/server.c @@ -687,14 +687,13 @@ LWS_VISIBLE int lws_server_socket_service(struct lws_context *context, struct lws *wsi, struct lws_pollfd *pollfd) { - struct lws *new_wsi = NULL; lws_sockfd_type accept_fd = LWS_SOCK_INVALID; #if LWS_POSIX - socklen_t clilen; struct sockaddr_in cli_addr; + socklen_t clilen; #endif - int n; - int len; + struct lws *new_wsi = NULL; + int n, len; switch (wsi->mode) { @@ -711,8 +710,7 @@ int lws_server_socket_service(struct lws_context *context, if (lws_issue_raw(wsi, wsi->truncated_send_malloc + wsi->truncated_send_offset, wsi->truncated_send_len) < 0) { - lwsl_info("closing from socket service\n"); - return -1; + goto fail; } /* * we can't afford to allow input processing send @@ -737,10 +735,7 @@ int lws_server_socket_service(struct lws_context *context, lws_free_header_table(wsi); /* fallthru */ case LWS_SSL_CAPABLE_ERROR: - lws_close_and_free_session( - context, wsi, - LWS_CLOSE_STATUS_NOSTATUS); - return 0; + goto fail; case LWS_SSL_CAPABLE_MORE_SERVICE: goto try_pollout; } @@ -751,9 +746,8 @@ int lws_server_socket_service(struct lws_context *context, /* hm this may want to send (via HTTP callback for example) */ n = lws_read(context, wsi, context->service_buffer, len); - if (n < 0) - /* we closed wsi */ - return 0; + if (n < 0) /* we closed wsi */ + return 1; /* hum he may have used up the writability above */ break; @@ -845,9 +839,8 @@ try_pollout: new_wsi->sock = accept_fd; /* the transport is accepted... give him time to negotiate */ - lws_set_timeout(new_wsi, - PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, - AWAITING_TIMEOUT); + lws_set_timeout(new_wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, + AWAITING_TIMEOUT); #if LWS_POSIX == 0 mbed3_tcp_stream_accept(accept_fd, new_wsi); @@ -878,11 +871,9 @@ try_pollout: break; } - if (lws_server_socket_service_ssl(context, &wsi, new_wsi, accept_fd, - pollfd)) - goto fail; - - return 0; + if (!lws_server_socket_service_ssl(context, &wsi, new_wsi, accept_fd, + pollfd)) + return 0; fail: lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS); diff --git a/lib/ssl.c b/lib/ssl.c index c15092d..f3a9ee5 100644 --- a/lib/ssl.c +++ b/lib/ssl.c @@ -526,13 +526,15 @@ lws_ssl_close(struct lws *wsi) return 1; /* handled */ } +/* leave all wsi close processing to the caller */ + LWS_VISIBLE int lws_server_socket_service_ssl(struct lws_context *context, struct lws **pwsi, struct lws *new_wsi, int accept_fd, struct lws_pollfd *pollfd) { - int n, m; struct lws *wsi = *pwsi; + int n, m; #ifndef USE_WOLFSSL BIO *bio; #endif @@ -551,10 +553,8 @@ lws_server_socket_service_ssl(struct lws_context *context, struct lws **pwsi, new_wsi->ssl = SSL_new(context->ssl_ctx); if (new_wsi->ssl == NULL) { lwsl_err("SSL_new failed: %s\n", - ERR_error_string(SSL_get_error(new_wsi->ssl, 0), NULL)); + ERR_error_string(SSL_get_error(new_wsi->ssl, 0), NULL)); lws_decode_ssl_error(); - - // TODO: Shouldn't the caller handle this? compatible_close(accept_fd); goto fail; } @@ -670,7 +670,7 @@ lws_server_socket_service_ssl(struct lws_context *context, struct lws **pwsi, m = SSL_get_error(wsi->ssl, n); lwsl_debug("SSL_accept failed %d / %s\n", - m, ERR_error_string(m, NULL)); + m, ERR_error_string(m, NULL)); go_again: if (m == SSL_ERROR_WANT_READ) { if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) @@ -689,14 +689,13 @@ go_again: break; } lwsl_debug("SSL_accept failed skt %u: %s\n", - pollfd->fd, ERR_error_string(m, NULL)); + pollfd->fd, ERR_error_string(m, NULL)); goto fail; accepted: /* OK, we are accepted... give him some time to negotiate */ - lws_set_timeout(wsi, - PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, - AWAITING_TIMEOUT); + lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, + AWAITING_TIMEOUT); wsi->mode = LWS_CONNMODE_HTTP_SERVING; -- 2.7.4