mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
refactor use unified ssl read write functions
Signed-off-by: Andy Green <andy.green@linaro.org>
This commit is contained in:
parent
02138126f2
commit
78f266a525
3 changed files with 41 additions and 76 deletions
50
lib/client.c
50
lib/client.c
|
@ -51,7 +51,7 @@ int lws_client_socket_service(struct libwebsocket_context *context,
|
|||
int n;
|
||||
char *p = (char *)&context->service_buffer[0];
|
||||
int len;
|
||||
char c;
|
||||
unsigned char c;
|
||||
|
||||
switch (wsi->mode) {
|
||||
|
||||
|
@ -324,7 +324,15 @@ int lws_client_socket_service(struct libwebsocket_context *context,
|
|||
} else
|
||||
wsi->ssl = NULL;
|
||||
#endif
|
||||
|
||||
wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2;
|
||||
libwebsocket_set_timeout(wsi,
|
||||
PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND,
|
||||
AWAITING_TIMEOUT);
|
||||
|
||||
/* fallthru */
|
||||
|
||||
case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2:
|
||||
p = libwebsockets_generate_client_handshake(context, wsi, p);
|
||||
if (p == NULL) {
|
||||
lwsl_err("Failed to generate handshake for client\n");
|
||||
|
@ -336,23 +344,18 @@ int lws_client_socket_service(struct libwebsocket_context *context,
|
|||
/* send our request to the server */
|
||||
|
||||
lws_latency_pre(context, wsi);
|
||||
#ifdef LWS_OPENSSL_SUPPORT
|
||||
if (wsi->use_ssl)
|
||||
n = SSL_write(wsi->ssl, context->service_buffer,
|
||||
p - (char *)context->service_buffer);
|
||||
else
|
||||
#endif
|
||||
n = send(wsi->sock, context->service_buffer,
|
||||
p - (char *)context->service_buffer, MSG_NOSIGNAL);
|
||||
lws_latency(context, wsi,
|
||||
"send or SSL_write LWS_CONNMODE...HANDSHAKE",
|
||||
n, n >= 0);
|
||||
|
||||
if (n < 0) {
|
||||
n = lws_ssl_capable_write(wsi, context->service_buffer, p - (char *)context->service_buffer);
|
||||
lws_latency(context, wsi, "send lws_issue_raw", n, n == p - (char *)context->service_buffer);
|
||||
switch (n) {
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
lwsl_debug("ERROR writing to client socket\n");
|
||||
libwebsocket_close_and_free_session(context, wsi,
|
||||
LWS_CLOSE_STATUS_NOSTATUS);
|
||||
return 0;
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
libwebsocket_callback_on_writable(context, wsi);
|
||||
break;
|
||||
}
|
||||
|
||||
wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
|
||||
|
@ -400,22 +403,13 @@ int lws_client_socket_service(struct libwebsocket_context *context,
|
|||
len = 1;
|
||||
while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
|
||||
len > 0) {
|
||||
#ifdef LWS_OPENSSL_SUPPORT
|
||||
if (wsi->use_ssl) {
|
||||
len = SSL_read(wsi->ssl, &c, 1);
|
||||
if (len < 0) {
|
||||
n = SSL_get_error(wsi->ssl, len);
|
||||
if (n == SSL_ERROR_WANT_READ ||
|
||||
n == SSL_ERROR_WANT_WRITE)
|
||||
return 0;
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
len = recv(wsi->sock, &c, 1, 0);
|
||||
|
||||
if (len < 0) {
|
||||
lwsl_warn("error on parsing recv\n");
|
||||
n = lws_ssl_capable_read(wsi, &c, 1);
|
||||
lws_latency(context, wsi, "send lws_issue_raw", n, n == 1);
|
||||
switch (n) {
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
goto bail3;
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (libwebsocket_parse(wsi, c)) {
|
||||
|
|
31
lib/server.c
31
lib/server.c
|
@ -520,34 +520,23 @@ int lws_server_socket_service(struct libwebsocket_context *context,
|
|||
/* any incoming data ready? */
|
||||
|
||||
if (pollfd->revents & LWS_POLLIN) {
|
||||
|
||||
#ifdef LWS_OPENSSL_SUPPORT
|
||||
if (wsi->ssl)
|
||||
len = SSL_read(wsi->ssl,
|
||||
len = lws_ssl_capable_read(wsi,
|
||||
context->service_buffer,
|
||||
sizeof(context->service_buffer));
|
||||
else
|
||||
#endif
|
||||
len = recv(pollfd->fd,
|
||||
context->service_buffer,
|
||||
sizeof(context->service_buffer), 0);
|
||||
|
||||
if (len < 0) {
|
||||
lwsl_debug("Socket read returned %d\n", len);
|
||||
if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN)
|
||||
libwebsocket_close_and_free_session(
|
||||
context, wsi,
|
||||
LWS_CLOSE_STATUS_NOSTATUS);
|
||||
return 0;
|
||||
}
|
||||
if (!len) {
|
||||
sizeof(context->service_buffer));
|
||||
switch (len) {
|
||||
case 0:
|
||||
lwsl_info("lws_server_skt_srv: read 0 len\n");
|
||||
/* lwsl_info(" state=%d\n", wsi->state); */
|
||||
if (!wsi->hdr_parsing_completed)
|
||||
free(wsi->u.hdr.ah);
|
||||
/* fallthru */
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
libwebsocket_close_and_free_session(
|
||||
context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
|
||||
context, wsi,
|
||||
LWS_CLOSE_STATUS_NOSTATUS);
|
||||
return 0;
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
break;
|
||||
}
|
||||
|
||||
/* hm this may want to send (via HTTP callback for example) */
|
||||
|
|
|
@ -371,34 +371,18 @@ libwebsocket_service_fd(struct libwebsocket_context *context,
|
|||
if (!(pollfd->revents & LWS_POLLIN))
|
||||
break;
|
||||
|
||||
#ifdef LWS_OPENSSL_SUPPORT
|
||||
read_pending:
|
||||
if (wsi->ssl) {
|
||||
eff_buf.token_len = SSL_read(wsi->ssl,
|
||||
context->service_buffer,
|
||||
sizeof(context->service_buffer));
|
||||
if (!eff_buf.token_len) {
|
||||
n = SSL_get_error(wsi->ssl, eff_buf.token_len);
|
||||
lwsl_err("SSL_read returned 0 with reason %s\n",
|
||||
ERR_error_string(n,
|
||||
(char *)context->service_buffer));
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
eff_buf.token_len = recv(pollfd->fd,
|
||||
eff_buf.token_len = lws_ssl_capable_read(wsi,
|
||||
context->service_buffer,
|
||||
sizeof(context->service_buffer), 0);
|
||||
|
||||
if (eff_buf.token_len < 0) {
|
||||
lwsl_debug("service_fd read ret = %d, errno = %d\n",
|
||||
eff_buf.token_len, LWS_ERRNO);
|
||||
if (LWS_ERRNO != LWS_EINTR && LWS_ERRNO != LWS_EAGAIN)
|
||||
goto close_and_handled;
|
||||
sizeof(context->service_buffer));
|
||||
switch (eff_buf.token_len) {
|
||||
case 0:
|
||||
lwsl_info("service_fd: closing due to 0 length read\n");
|
||||
goto close_and_handled;
|
||||
case LWS_SSL_CAPABLE_ERROR:
|
||||
n = 0;
|
||||
goto handled;
|
||||
}
|
||||
if (!eff_buf.token_len) {
|
||||
lwsl_info("service_fd: closing due to 0 length read\n");
|
||||
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
||||
goto close_and_handled;
|
||||
}
|
||||
|
||||
|
@ -454,10 +438,8 @@ drain:
|
|||
n = _libwebsocket_rx_flow_control(wsi); /* n ignored, needed for NO_SERVER case */
|
||||
}
|
||||
|
||||
#ifdef LWS_OPENSSL_SUPPORT
|
||||
if (wsi->ssl && SSL_pending(wsi->ssl))
|
||||
if (lws_ssl_pending(wsi))
|
||||
goto read_pending;
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
Loading…
Add table
Reference in a new issue