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;
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;
}
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;
}
read_ok:
- /* Nothing more to do for now. */
+ /* Nothing more to do for now */
lwsl_debug("lws_read: read_ok\n");
return 0;
bail:
lwsl_debug("closing connection at lws_read bail:\n");
-
lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
return -1;
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) {
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
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;
}
/* 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;
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);
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);
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
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;
}
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))
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;