Skip to content
Permalink
Browse files

refactor needless context with wsi paramater passing

Now we bit the bullet and gave each wsi an lws_context *, many
internal apis that take both a context and wsi parameter only
need the wsi.

Also simplify parser code by making a temp var for
allocated_headers * instead of the longwinded
dereference chain everywhere.

Signed-off-by: Andy Green <andy.green@linaro.org>
  • Loading branch information...
Andy Green
Andy Green committed Dec 15, 2015
1 parent e59908e commit 6b5de70f4fb1eadac6730f3b4ecfe156bd38567a
Showing with 180 additions and 217 deletions.
  1. +5 −4 lib/client-handshake.c
  2. +20 −27 lib/client.c
  3. +1 −2 lib/context.c
  4. +2 −2 lib/extension.c
  5. +1 −1 lib/handshake.c
  6. +8 −8 lib/hpack.c
  7. +5 −5 lib/http2.c
  8. +6 −7 lib/libwebsockets.c
  9. +2 −5 lib/lws-plat-mbed3.cpp
  10. +1 −1 lib/lws-plat-unix.c
  11. +1 −4 lib/output.c
  12. +69 −80 lib/parsers.c
  13. +2 −2 lib/pollfd.c
  14. +21 −29 lib/private-libwebsockets.h
  15. +6 −7 lib/server-handshake.c
  16. +7 −8 lib/server.c
  17. +13 −17 lib/service.c
  18. +10 −8 lib/ssl.c
@@ -1,21 +1,22 @@
#include "private-libwebsockets.h"

struct lws *
lws_client_connect_2(struct lws_context *context, struct lws *wsi)
lws_client_connect_2(struct lws *wsi)
{
#ifdef LWS_USE_IPV6
struct sockaddr_in6 server_addr6;
struct sockaddr_in6 client_addr6;
struct addrinfo hints, *result;
#endif
struct lws_context *context = wsi->context;
struct sockaddr_in server_addr4;
struct sockaddr_in client_addr4;
struct lws_pollfd pfd;
struct sockaddr *v;
int n, plen = 0;
const char *ads;

lwsl_client("lws_client_connect_2\n");
lwsl_client("%s\n", __func__);

/* proxy? */

@@ -310,7 +311,7 @@ lws_client_connect_2(struct lws_context *context, struct lws *wsi)
return NULL;

failed:
lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);

return NULL;
}
@@ -424,7 +425,7 @@ lws_client_connect(struct lws_context *context, const char *address,
}
lwsl_client("lws_client_connect: direct conn\n");

return lws_client_connect_2(context, wsi);
return lws_client_connect_2(wsi);

bail1:
lws_free(wsi->u.hdr.ah);
@@ -59,7 +59,7 @@ int lws_client_socket_service(struct lws_context *context,
* timeout protection set in client-handshake.c
*/

if (lws_client_connect_2(context, wsi) == NULL) {
if (lws_client_connect_2(wsi) == NULL) {
/* closed */
lwsl_client("closed\n");
return -1;
@@ -75,10 +75,9 @@ int lws_client_socket_service(struct lws_context *context,
if (pollfd->revents & LWS_POLLHUP) {

lwsl_warn("Proxy connection %p (fd=%d) dead\n",
(void *)wsi, pollfd->fd);
(void *)wsi, pollfd->fd);

lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return 0;
}

@@ -91,8 +90,7 @@ int lws_client_socket_service(struct lws_context *context,
return 0;
}

lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
lwsl_err("ERROR reading from proxy socket\n");
return 0;
}
@@ -101,8 +99,7 @@ int lws_client_socket_service(struct lws_context *context,
if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ") &&
strcmp((char *)context->service_buffer, "HTTP/1.1 200 ")
) {
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
lwsl_err("ERROR proxy: %s\n", context->service_buffer);
return 0;
}
@@ -334,8 +331,8 @@ int lws_client_socket_service(struct lws_context *context,
} else {
lwsl_err("server's cert didn't look good, X509_V_ERR = %d: %s\n",
n, ERR_error_string(n, (char *)context->service_buffer));
lws_close_and_free_session(context,
wsi, LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi,
LWS_CLOSE_STATUS_NOSTATUS);
return 0;
}
}
@@ -351,11 +348,10 @@ int lws_client_socket_service(struct lws_context *context,
/* fallthru */

case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE2:
p = lws_generate_client_handshake(context, wsi, p);
p = lws_generate_client_handshake(wsi, p);
if (p == NULL) {
lwsl_err("Failed to generate handshake for client\n");
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return 0;
}

@@ -370,8 +366,7 @@ int lws_client_socket_service(struct lws_context *context,
switch (n) {
case LWS_SSL_CAPABLE_ERROR:
lwsl_debug("ERROR writing to client socket\n");
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return 0;
case LWS_SSL_CAPABLE_MORE_SERVICE:
lws_callback_on_writable(context, wsi);
@@ -422,7 +417,7 @@ int lws_client_socket_service(struct lws_context *context,
len = 1;
while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
len > 0) {
n = lws_ssl_capable_read(context, wsi, &c, 1);
n = lws_ssl_capable_read(wsi, &c, 1);
lws_latency(context, wsi, "send lws_issue_raw", n,
n == 1);
switch (n) {
@@ -432,7 +427,7 @@ int lws_client_socket_service(struct lws_context *context,
return 0;
}

if (lws_parse(context, wsi, c)) {
if (lws_parse(wsi, c)) {
lwsl_warn("problems parsing header\n");
goto bail3;
}
@@ -453,12 +448,11 @@ int lws_client_socket_service(struct lws_context *context,
* right away and deal with it that way
*/

return lws_client_interpret_server_handshake(context, wsi);
return lws_client_interpret_server_handshake(wsi);

bail3:
lwsl_info("closing conn at LWS_CONNMODE...SERVER_REPLY\n");
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return -1;

case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
@@ -490,9 +484,9 @@ strtolower(char *s)
}

int
lws_client_interpret_server_handshake(struct lws_context *context,
struct lws *wsi)
lws_client_interpret_server_handshake(struct lws *wsi)
{
struct lws_context *context = wsi->context;
int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR;
int n, len, okay = 0, isErrorCodeReceived = 0;
const char *pc;
@@ -815,17 +809,17 @@ lws_client_interpret_server_handshake(struct lws_context *context,

/* free up his parsing allocations */
lws_free2(wsi->u.hdr.ah);
lws_close_and_free_session(context, wsi, close_reason);
lws_close_free_wsi(wsi, close_reason);

return 1;
}


char *
lws_generate_client_handshake(struct lws_context *context,
struct lws *wsi, char *pkt)
lws_generate_client_handshake(struct lws *wsi, char *pkt)
{
char buf[128], hash[20], key_b64[40], *p = pkt;
struct lws_context *context = wsi->context;
int n;
#ifndef LWS_NO_EXTENSIONS
const struct lws_extension *ext;
@@ -839,8 +833,7 @@ lws_generate_client_handshake(struct lws_context *context,
if (n != 16) {
lwsl_err("Unable to read from random dev %s\n",
SYSTEM_RANDOM_FILEPATH);
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return NULL;
}

@@ -262,8 +262,7 @@ lws_context_destroy(struct lws_context *context)
struct lws *wsi = wsi_from_fd(context, context->fds[n].fd);
if (!wsi)
continue;
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
/* no protocol close */);
n--;
}
@@ -185,13 +185,13 @@ lws_issue_raw_ext_access(struct lws *wsi,
}

int
lws_any_extension_handled(struct lws_context *context,
struct lws *wsi,
lws_any_extension_handled(struct lws *wsi,
enum lws_extension_callback_reasons r,
void *v, size_t len)
{
int n;
int handled = 0;
struct lws_context *context = wsi->context;

/* maybe an extension will take care of it for us */

@@ -217,7 +217,7 @@ lws_read(struct lws_context *context, struct lws *wsi, unsigned char *buf,

bail:
lwsl_debug("closing connection at lws_read bail:\n");
lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);

return -1;
}
@@ -202,14 +202,14 @@ static int lws_frag_start(struct lws *wsi, int hdr_token_idx)
if (!hdr_token_idx)
return 1;

if (ah->next_frag_index >= ARRAY_SIZE(ah->frag_index))
if (ah->nfrag >= ARRAY_SIZE(ah->frag_index))
return 1;

ah->frags[ah->next_frag_index].offset = ah->pos;
ah->frags[ah->next_frag_index].len = 0;
ah->frags[ah->next_frag_index].next_frag_index = 0;
ah->frags[ah->nfrag].offset = ah->pos;
ah->frags[ah->nfrag].len = 0;
ah->frags[ah->nfrag].nfrag = 0;

ah->frag_index[hdr_token_idx] = ah->next_frag_index;
ah->frag_index[hdr_token_idx] = ah->nfrag;

return 0;
}
@@ -219,7 +219,7 @@ static int lws_frag_append(struct lws *wsi, unsigned char c)
struct allocated_headers * ah = wsi->u.http2.http.ah;

ah->data[ah->pos++] = c;
ah->frags[ah->next_frag_index].len++;
ah->frags[ah->nfrag].len++;

return ah->pos >= sizeof(ah->data);
}
@@ -229,7 +229,7 @@ static int lws_frag_end(struct lws *wsi)
if (lws_frag_append(wsi, 0))
return 1;

wsi->u.http2.http.ah->next_frag_index++;
wsi->u.http2.http.ah->nfrag++;
return 0;
}

@@ -543,7 +543,7 @@ int lws_hpack_interpret(struct lws_context *context,
if (lws_frag_append(wsi, c1))
return 1;
} else { /* name */
if (lws_parse(context, wsi, c1))
if (lws_parse(wsi, c1))
return 1;

}
@@ -219,7 +219,7 @@ lws_http2_parser(struct lws_context *context, struct lws *wsi, unsigned char c)
* and the peer must send a SETTINGS with ACK flag...
*/

lws_set_protocol_write_pending(context, wsi,
lws_set_protocol_write_pending(wsi,
LWS_PPS_HTTP2_MY_SETTINGS);
}
break;
@@ -295,14 +295,14 @@ lws_http2_parser(struct lws_context *context, struct lws *wsi, unsigned char c)
case LWS_HTTP2_FRAME_TYPE_HEADERS:
/* service the http request itself */
lwsl_info("servicing initial http request, wsi=%p, stream wsi=%p\n", wsi, wsi->u.http2.stream_wsi);
n = lws_http_action(context, swsi);
n = lws_http_action(swsi);
(void)n;
lwsl_info(" action result %d\n", n);
break;
case LWS_HTTP2_FRAME_TYPE_PING:
if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
} else { /* they're sending us a ping request */
lws_set_protocol_write_pending(context, wsi, LWS_PPS_HTTP2_PONG);
lws_set_protocol_write_pending(wsi, LWS_PPS_HTTP2_PONG);
}
break;
case LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE:
@@ -360,7 +360,7 @@ lws_http2_parser(struct lws_context *context, struct lws *wsi, unsigned char c)
if (wsi->u.http2.flags & LWS_HTTP2_FLAG_SETTINGS_ACK) { // ack
} else
/* non-ACK coming in means we must ACK it */
lws_set_protocol_write_pending(context, wsi, LWS_PPS_HTTP2_ACK_SETTINGS);
lws_set_protocol_write_pending(wsi, LWS_PPS_HTTP2_ACK_SETTINGS);
break;
case LWS_HTTP2_FRAME_TYPE_PING:
if (wsi->u.http2.stream_id)
@@ -486,7 +486,7 @@ int lws_http2_do_pps_send(struct lws_context *context, struct lws *wsi)
/* demanded by HTTP2 */
swsi->u.http2.END_STREAM = 1;
lwsl_info("servicing initial http request\n");
return lws_http_action(context, swsi);
return lws_http_action(swsi);
}
break;
case LWS_PPS_HTTP2_PONG:
@@ -57,9 +57,9 @@ lws_free_wsi(struct lws *wsi)
}

void
lws_close_and_free_session(struct lws_context *context,
struct lws *wsi, enum lws_close_status reason)
lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
{
struct lws_context *context = wsi->context;
int n, m, ret, old_state;
struct lws_tokens eff_buf;
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
@@ -216,10 +216,10 @@ lws_close_and_free_session(struct lws_context *context,
* we won't be servicing or receiving anything further from this guy
* delete socket from the internal poll list if still present
*/
lws_ssl_remove_wsi_from_buffered_list(context, wsi);
lws_ssl_remove_wsi_from_buffered_list(wsi);

/* checking return redundant since we anyway close */
remove_wsi_socket_from_fds(context, wsi);
remove_wsi_socket_from_fds(wsi);

wsi->state = WSI_STATE_DEAD_SOCKET;

@@ -889,8 +889,7 @@ lws_partial_buffered(struct lws *wsi)
return !!wsi->truncated_send_len;
}

void lws_set_protocol_write_pending(struct lws_context *context,
struct lws *wsi,
void lws_set_protocol_write_pending(struct lws *wsi,
enum lws_pending_protocol_send pend)
{
lwsl_info("setting pps %d\n", pend);
@@ -899,7 +898,7 @@ void lws_set_protocol_write_pending(struct lws_context *context,
lwsl_err("pps overwrite\n");
wsi->pps = pend;
lws_rx_flow_control(wsi, 0);
lws_callback_on_writable(context, wsi);
lws_callback_on_writable(wsi->context, wsi);
}

LWS_VISIBLE size_t
@@ -82,15 +82,13 @@ lws_plat_change_pollfd(struct lws_context *context,
}

extern "C" LWS_VISIBLE int
lws_ssl_capable_read_no_ssl(struct lws_context *context,
struct lws *wsi, unsigned char *buf, int len)
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
{
socket_error_t err;
size_t _len = len;

lwsl_debug("%s\r\n", __func__);

(void)context;
err = ((lws_conn *)wsi->sock)->ts->recv((char *)buf, &_len);
if (err == SOCKET_ERROR_NONE) {
lwsl_info("%s: got %d bytes\n", __func__, _len);
@@ -298,8 +296,7 @@ void lws_conn::onDisconnect(TCPStream *s)
{
lwsl_notice("%s:\r\n", __func__);
(void)s;
lws_close_and_free_session(lws_get_ctx(wsi), wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
}


0 comments on commit 6b5de70

Please sign in to comment.
You can’t perform that action at this time.