1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

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>
This commit is contained in:
Andy Green 2015-12-15 21:15:58 +08:00
parent e59908e7fc
commit 6b5de70f4f
18 changed files with 179 additions and 216 deletions

View file

@ -1,13 +1,14 @@
#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;
@ -15,7 +16,7 @@ lws_client_connect_2(struct lws_context *context, struct lws *wsi)
int n, plen = 0;
const char *ads;
lwsl_client("lws_client_connect_2\n");
lwsl_client("%s\n", __func__);
/* proxy? */
@ -310,7 +311,7 @@ oom4:
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);

View file

@ -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 @@ some_wait:
} 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 @@ some_wait:
/* 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 @@ some_wait:
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 @@ some_wait:
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 @@ some_wait:
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 @@ some_wait:
* 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 @@ bail2:
/* 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;
}

View file

@ -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--;
}

View file

@ -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 */

View file

@ -217,7 +217,7 @@ http_complete:
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;
}

View file

@ -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 @@ pre_data:
if (lws_frag_append(wsi, c1))
return 1;
} else { /* name */
if (lws_parse(context, wsi, c1))
if (lws_parse(wsi, c1))
return 1;
}

View file

@ -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:

View file

@ -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 @@ just_kill_connection:
* 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

View file

@ -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);
}

View file

@ -157,7 +157,7 @@ lws_plat_service(struct lws_context *context, int timeout_ms)
* at the end of the service, he'll get put back on the
* list then.
*/
lws_ssl_remove_wsi_from_buffered_list(context, wsi);
lws_ssl_remove_wsi_from_buffered_list(wsi);
}
wsi = wsi_next;
}

View file

@ -577,13 +577,10 @@ all_sent:
#if LWS_POSIX
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)
{
int n;
(void)context;
n = recv(wsi->sock, (char *)buf, len, 0);
if (n >= 0)
return n;

View file

@ -29,7 +29,6 @@ unsigned char lextable[] = {
int lextable_decode(int pos, char c)
{
c = tolower(c);
while (1) {
@ -69,7 +68,7 @@ int lws_allocate_header_table(struct lws *wsi)
return -1;
}
memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index));
wsi->u.hdr.ah->next_frag_index = 0;
wsi->u.hdr.ah->nfrag = 0;
wsi->u.hdr.ah->pos = 0;
return 0;
@ -92,13 +91,13 @@ LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
return 0;
do {
len += wsi->u.hdr.ah->frags[n].len;
n = wsi->u.hdr.ah->frags[n].next_frag_index;
n = wsi->u.hdr.ah->frags[n].nfrag;
} while (n);
return len;
}
LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dest, int len,
LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
enum lws_token_indexes h)
{
int toklen = lws_hdr_total_length(wsi, h);
@ -112,10 +111,9 @@ LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dest, int len,
return 0;
do {
strcpy(dest,
&wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
dest += wsi->u.hdr.ah->frags[n].len;
n = wsi->u.hdr.ah->frags[n].next_frag_index;
strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
dst += wsi->u.hdr.ah->frags[n].len;
n = wsi->u.hdr.ah->frags[n].nfrag;
} while (n);
return toklen;
@ -132,23 +130,20 @@ char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset];
}
int lws_hdr_simple_create(struct lws *wsi,
enum lws_token_indexes h, const char *s)
int lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h,
const char *s)
{
wsi->u.hdr.ah->next_frag_index++;
if (wsi->u.hdr.ah->next_frag_index ==
sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) {
wsi->u.hdr.ah->nfrag++;
if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) {
lwsl_warn("More hdr frags than we can deal with, dropping\n");
return -1;
}
wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index;
wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
wsi->u.hdr.ah->pos;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index =
0;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0;
do {
if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
@ -157,8 +152,7 @@ int lws_hdr_simple_create(struct lws *wsi,
}
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
if (*s)
wsi->u.hdr.ah->frags[
wsi->u.hdr.ah->next_frag_index].len++;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
} while (*s++);
return 0;
@ -181,32 +175,35 @@ static signed char char_to_hex(const char c)
static int issue_char(struct lws *wsi, unsigned char c)
{
unsigned short frag_len;
if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
lwsl_warn("excessive header content\n");
return -1;
}
frag_len = \
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len;
/* If we haven't hit the token limit, just copy the character into
* the header: */
if( frag_len < wsi->u.hdr.current_token_limit ) {
frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len;
/*
* If we haven't hit the token limit, just copy the character into
* the header
*/
if (frag_len < wsi->u.hdr.current_token_limit) {
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
if (c)
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
return 0;
}
else {
/* Insert a null character when we *hit* the limit: */
if( frag_len == wsi->u.hdr.current_token_limit ) {
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
lwsl_warn("header %i exceeds limit\n", wsi->u.hdr.parser_state);
};
};
/* Insert a null character when we *hit* the limit: */
if (frag_len == wsi->u.hdr.current_token_limit) {
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
lwsl_warn("header %i exceeds limit\n",
wsi->u.hdr.parser_state);
}
return 1;
}
int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
int lws_parse(struct lws *wsi, unsigned char c)
{
static const unsigned char methods[] = {
WSI_TOKEN_GET_URI,
@ -216,6 +213,8 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
WSI_TOKEN_PATCH_URI,
WSI_TOKEN_DELETE_URI,
};
struct allocated_headers *ah = wsi->u.hdr.ah;
struct lws_context *context = wsi->context;
unsigned int n, m, enc = 0;
switch (wsi->u.hdr.parser_state) {
@ -225,7 +224,7 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
/* collect into malloc'd buffers */
/* optional initial space swallow */
if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[
if (!ah->frags[ah->frag_index[
wsi->u.hdr.parser_state]].len && c == ' ')
break;
@ -240,7 +239,7 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
if (c == ' ') {
/* enforce starting with / */
if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len)
if (!ah->frags[ah->nfrag].len)
if (issue_char(wsi, '/') < 0)
return -1;
@ -279,7 +278,7 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
issue_char(wsi, '%');
wsi->u.hdr.ues = URIES_IDLE;
/* regurgitate + assess */
if (lws_parse(context, wsi, wsi->u.hdr.esc_stash) < 0)
if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0)
return -1;
/* continue on to assess c */
break;
@ -324,14 +323,14 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
* safe against header fragmentation because
* the method URI can only be in 1 fragment
*/
if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) {
wsi->u.hdr.ah->pos--;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
if (ah->frags[ah->nfrag].len > 2) {
ah->pos--;
ah->frags[ah->nfrag].len--;
do {
wsi->u.hdr.ah->pos--;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
} while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 &&
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/');
ah->pos--;
ah->frags[ah->nfrag].len--;
} while (ah->frags[ah->nfrag].len > 1 &&
ah->data[ah->pos] != '/');
}
wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
goto swallow;
@ -363,20 +362,15 @@ int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
if (c == '?' && !enc) { /* start of URI arguments */
/* seal off uri header */
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
ah->data[ah->pos++] = '\0';
/* move to using WSI_TOKEN_HTTP_URI_ARGS */
wsi->u.hdr.ah->next_frag_index++;
wsi->u.hdr.ah->frags[
wsi->u.hdr.ah->next_frag_index].offset =
wsi->u.hdr.ah->pos;
wsi->u.hdr.ah->frags[
wsi->u.hdr.ah->next_frag_index].len = 0;
wsi->u.hdr.ah->frags[
wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
ah->nfrag++;
ah->frags[ah->nfrag].offset = ah->pos;
ah->frags[ah->nfrag].len = 0;
ah->frags[ah->nfrag].nfrag = 0;
wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] =
wsi->u.hdr.ah->next_frag_index;
ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
/* defeat normal uri path processing */
wsi->u.hdr.ups = URIPS_ARGUMENTS;
@ -419,7 +413,7 @@ swallow:
wsi->mode == LWS_CONNMODE_HTTP_SERVING) {
/* this is not a header we know about */
for (m = 0; m < ARRAY_SIZE(methods); m++)
if (wsi->u.hdr.ah->frag_index[methods[m]]) {
if (ah->frag_index[methods[m]]) {
/*
* already had the method, no idea what
* this crap from the client is, ignore
@ -455,7 +449,7 @@ swallow:
lwsl_parser("known hdr %d\n", n);
for (m = 0; m < ARRAY_SIZE(methods); m++)
if (n == methods[m] &&
wsi->u.hdr.ah->frag_index[
ah->frag_index[
methods[m]]) {
lwsl_warn("Duplicated method\n");
return -1;
@ -473,9 +467,10 @@ swallow:
if (context->token_limits)
wsi->u.hdr.current_token_limit =
context->token_limits->token_limit[wsi->u.hdr.parser_state];
context->token_limits->token_limit[
wsi->u.hdr.parser_state];
else
wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data);
wsi->u.hdr.current_token_limit = sizeof(ah->data);
if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
goto set_parsing_complete;
@ -485,39 +480,33 @@ swallow:
break;
start_fragment:
wsi->u.hdr.ah->next_frag_index++;
if (wsi->u.hdr.ah->next_frag_index ==
sizeof(wsi->u.hdr.ah->frags) /
sizeof(wsi->u.hdr.ah->frags[0])) {
ah->nfrag++;
if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
lwsl_warn("More hdr frags than we can deal with\n");
return -1;
}
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
wsi->u.hdr.ah->pos;
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
wsi->u.hdr.ah->frags[
wsi->u.hdr.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;
n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state];
n = ah->frag_index[wsi->u.hdr.parser_state];
if (!n) { /* first fragment */
wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] =
wsi->u.hdr.ah->next_frag_index;
ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
break;
}
/* continuation */
while (wsi->u.hdr.ah->frags[n].next_frag_index)
n = wsi->u.hdr.ah->frags[n].next_frag_index;
wsi->u.hdr.ah->frags[n].next_frag_index =
wsi->u.hdr.ah->next_frag_index;
while (ah->frags[n].nfrag)
n = ah->frags[n].nfrag;
ah->frags[n].nfrag = ah->nfrag;
if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
if (ah->pos == sizeof(ah->data)) {
lwsl_warn("excessive header content\n");
return -1;
}
wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' ';
wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
ah->data[ah->pos++] = ' ';
ah->frags[ah->nfrag].len++;
break;
/* skipping arg part of a name we didn't recognize */

View file

@ -70,11 +70,11 @@ insert_wsi_socket_into_fds(struct lws_context *context,
}
int
remove_wsi_socket_from_fds(struct lws_context *context,
struct lws *wsi)
remove_wsi_socket_from_fds(struct lws *wsi)
{
int m;
struct lws_pollargs pa = { wsi->sock, 0, 0 };
struct lws_context *context = wsi->context;
lws_libev_io(context, wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);

View file

@ -600,7 +600,7 @@ enum uri_esc_states {
struct lws_fragments {
unsigned short offset;
unsigned short len;
unsigned char next_frag_index;
unsigned char nfrag;
};
/* notice that these union members:
@ -616,7 +616,7 @@ struct lws_fragments {
*/
struct allocated_headers {
unsigned char next_frag_index;
unsigned char nfrag;
unsigned short pos;
unsigned char frag_index[WSI_TOKEN_COUNT];
struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
@ -905,11 +905,10 @@ struct lws {
LWS_EXTERN int log_level;
LWS_EXTERN void
lws_close_and_free_session(struct lws_context *context,
struct lws *wsi, enum lws_close_status);
lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
LWS_EXTERN int
remove_wsi_socket_from_fds(struct lws_context *context, struct lws *wsi);
remove_wsi_socket_from_fds(struct lws *wsi);
LWS_EXTERN int
lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
@ -934,16 +933,16 @@ lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
#endif
LWS_EXTERN void
lws_set_protocol_write_pending(struct lws_context *context, struct lws *wsi,
lws_set_protocol_write_pending(struct lws *wsi,
enum lws_pending_protocol_send pend);
LWS_EXTERN int
lws_client_rx_sm(struct lws *wsi, unsigned char c);
LWS_EXTERN int
lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c);
lws_parse(struct lws *wsi, unsigned char c);
LWS_EXTERN int
lws_http_action(struct lws_context *context, struct lws *wsi);
lws_http_action(struct lws *wsi);
LWS_EXTERN int
lws_b64_selftest(void);
@ -971,22 +970,19 @@ lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
LWS_EXTERN int
lws_service_timeout_check(struct lws_context *context,
struct lws *wsi, unsigned int sec);
lws_service_timeout_check(struct lws *wsi, unsigned int sec);
LWS_EXTERN struct lws *
lws_client_connect_2(struct lws_context *context, struct lws *wsi);
lws_client_connect_2(struct lws *wsi);
LWS_EXTERN struct lws *
lws_create_new_server_wsi(struct lws_context *context);
LWS_EXTERN char *
lws_generate_client_handshake(struct lws_context *context, struct lws *wsi,
char *pkt);
lws_generate_client_handshake(struct lws *wsi, char *pkt);
LWS_EXTERN int
lws_handle_POLLOUT_event(struct lws_context *context, struct lws *wsi,
struct lws_pollfd *pollfd);
lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
/*
* EXTENSIONS
@ -997,7 +993,7 @@ LWS_VISIBLE void
lws_context_init_extensions(struct lws_context_creation_info *info,
struct lws_context *context);
LWS_EXTERN 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);
@ -1009,7 +1005,7 @@ lws_ext_callback_for_each_extension_type(struct lws_context *context,
struct lws *wsi, int reason,
void *arg, int len);
#else
#define lws_any_extension_handled(_a, _b, _c, _d, _e) (0)
#define lws_any_extension_handled(_a, _b, _c, _d) (0)
#define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0)
#define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0)
#define lws_issue_raw_ext_access lws_issue_raw
@ -1017,8 +1013,7 @@ lws_ext_callback_for_each_extension_type(struct lws_context *context,
#endif
LWS_EXTERN int
lws_client_interpret_server_handshake(struct lws_context *context,
struct lws *wsi);
lws_client_interpret_server_handshake(struct lws *wsi);
LWS_EXTERN int
lws_rx_sm(struct lws *wsi, unsigned char c);
@ -1140,31 +1135,29 @@ enum lws_ssl_capable_status {
#define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
#define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
#define lws_ssl_pending lws_ssl_pending_no_ssl
#define lws_server_socket_service_ssl(_a, _b, _c, _d, _e) (0)
#define lws_server_socket_service_ssl(_a, _b, _c, _d) (0)
#define lws_ssl_close(_a) (0)
#define lws_ssl_context_destroy(_a)
#define lws_ssl_remove_wsi_from_buffered_list(_a, _b)
#define lws_ssl_remove_wsi_from_buffered_list(_a)
#else
#define LWS_SSL_ENABLED(context) (context->use_ssl)
LWS_EXTERN int openssl_websocket_private_data_index;
LWS_EXTERN int
lws_ssl_capable_read(struct lws_context *context,
struct lws *wsi, unsigned char *buf, int len);
lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
LWS_EXTERN int
lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
LWS_EXTERN int
lws_ssl_pending(struct lws *wsi);
LWS_EXTERN int
lws_server_socket_service_ssl(struct lws_context *context, struct lws **wsi,
struct lws *new_wsi, lws_sockfd_type accept_fd,
lws_server_socket_service_ssl(struct lws **wsi, struct lws *new_wsi,
lws_sockfd_type accept_fd,
struct lws_pollfd *pollfd);
LWS_EXTERN int
lws_ssl_close(struct lws *wsi);
LWS_EXTERN void
lws_ssl_context_destroy(struct lws_context *context);
LWS_VISIBLE void
lws_ssl_remove_wsi_from_buffered_list(struct lws_context *context,
struct lws *wsi);
lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
#ifndef LWS_NO_SERVER
LWS_EXTERN int
lws_context_init_server_ssl(struct lws_context_creation_info *info,
@ -1186,8 +1179,7 @@ lws_context_init_http2_ssl(struct lws_context *context);
#endif
LWS_EXTERN 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);
LWS_EXTERN int
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);

View file

@ -24,13 +24,13 @@
#define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
#ifndef LWS_NO_EXTENSIONS
LWS_VISIBLE int
lws_extension_server_handshake(struct lws_context *context,
struct lws *wsi, char **p)
lws_extension_server_handshake(struct lws *wsi, char **p)
{
int n;
char *c;
char ext_name[128];
const struct lws_extension *ext;
struct lws_context *context = wsi->context;
int ext_count = 0;
int more = 1;
@ -223,7 +223,7 @@ handshake_0405(struct lws_context *context, struct lws *wsi)
* Figure out which extensions the client has that we want to
* enable on this connection, and give him back the list
*/
if (lws_extension_server_handshake(context, wsi, &p))
if (lws_extension_server_handshake(wsi, &p))
goto bail;
#endif
@ -233,9 +233,8 @@ handshake_0405(struct lws_context *context, struct lws *wsi)
LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
if (!lws_any_extension_handled(context, wsi,
LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
response, p - response)) {
if (!lws_any_extension_handled(wsi, LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX,
response, p - response)) {
/* okay send the handshake response accepting the connection */
@ -244,7 +243,7 @@ handshake_0405(struct lws_context *context, struct lws *wsi)
fwrite(response, 1, p - response, stderr);
#endif
n = lws_write(wsi, (unsigned char *)response,
p - response, LWS_WRITE_HTTP_HEADERS);
p - response, LWS_WRITE_HTTP_HEADERS);
if (n != (p - response)) {
lwsl_debug("handshake_0405: ERROR writing to socket\n");
goto bail;

View file

@ -186,8 +186,9 @@ _lws_rx_flow_control(struct lws *wsi)
return 0;
}
int lws_http_action(struct lws_context *context, struct lws *wsi)
int lws_http_action(struct lws *wsi)
{
struct lws_context *context = wsi->context;
enum http_connection_type connection_type;
enum http_version request_version;
char content_length_str[32];
@ -350,7 +351,7 @@ int lws_handshake_server(struct lws_context *context, struct lws *wsi,
/* LWS_CONNMODE_WS_SERVING */
while (len--) {
if (lws_parse(context, wsi, *(*buf)++)) {
if (lws_parse(wsi, *(*buf)++)) {
lwsl_info("lws_parse failed\n");
goto bail_nuke_ah;
}
@ -377,7 +378,7 @@ int lws_handshake_server(struct lws_context *context, struct lws *wsi,
/* expose it at the same offset as u.hdr */
wsi->u.http.ah = ah;
n = lws_http_action(context, wsi);
n = lws_http_action(wsi);
return n;
}
@ -721,8 +722,7 @@ int lws_server_socket_service(struct lws_context *context,
/* any incoming data ready? */
if (pollfd->revents & LWS_POLLIN) {
len = lws_ssl_capable_read(context, wsi,
context->service_buffer,
len = lws_ssl_capable_read(wsi, context->service_buffer,
sizeof(context->service_buffer));
lwsl_debug("%s: read %d\r\n", __func__, len);
switch (len) {
@ -874,12 +874,11 @@ try_pollout:
break;
}
if (!lws_server_socket_service_ssl(context, &wsi, new_wsi, accept_fd,
pollfd))
if (!lws_server_socket_service_ssl(&wsi, new_wsi, accept_fd, pollfd))
return 0;
fail:
lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return 1;
}

View file

@ -44,9 +44,9 @@ lws_calllback_as_writeable(struct lws_context *context, struct lws *wsi)
}
int
lws_handle_POLLOUT_event(struct lws_context *context, struct lws *wsi,
struct lws_pollfd *pollfd)
lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd)
{
struct lws_context *context = wsi->context;
int write_type = LWS_WRITE_PONG;
struct lws_tokens eff_buf;
#ifdef LWS_USE_HTTP2
@ -60,16 +60,15 @@ lws_handle_POLLOUT_event(struct lws_context *context, struct lws *wsi,
if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
wsi->truncated_send_offset,
wsi->truncated_send_len) < 0) {
lwsl_info("lws_handle_POLLOUT_event signalling to close\n");
lwsl_info("%s signalling to close\n", __func__);
return -1;
}
/* leave POLLOUT active either way */
return 0;
} else
if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
lwsl_info("***** %x signalling to close in POLLOUT handler\n", wsi);
if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE)
return -1; /* retry closing now */
}
#ifdef LWS_USE_HTTP2
/* protocol packets are next */
if (wsi->pps) {
@ -258,8 +257,7 @@ user_service:
wsi2->u.http2.requested_POLLOUT = 0;
if (lws_calllback_as_writeable(context, wsi2)) {
lwsl_debug("Closing POLLOUT child\n");
lws_close_and_free_session(context, wsi2,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi2, LWS_CLOSE_STATUS_NOSTATUS);
}
wsi2 = wsi;
} while (wsi2 != NULL && !lws_send_pipe_choked(wsi));
@ -275,8 +273,7 @@ notify:
int
lws_service_timeout_check(struct lws_context *context,
struct lws *wsi, unsigned int sec)
lws_service_timeout_check(struct lws *wsi, unsigned int sec)
{
/*
* if extensions want in on it (eg, we are a mux parent)
@ -304,8 +301,8 @@ lws_service_timeout_check(struct lws_context *context,
* cleanup like flush partials.
*/
wsi->socket_is_permanently_unusable = 1;
lws_close_and_free_session(context, wsi,
LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
return 1;
}
@ -401,8 +398,7 @@ lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
if (!wsi)
continue;
if (lws_service_timeout_check(context, wsi,
(unsigned int)now))
if (lws_service_timeout_check(wsi, (unsigned int)now))
/* he did time out... */
if (mfd == our_fd)
/* it was the guy we came to service! */
@ -512,7 +508,7 @@ lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
wsi->state == WSI_STATE_HTTP2_ESTABLISHED_PRE_SETTINGS ||
wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY ||
wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) &&
lws_handle_POLLOUT_event(context, wsi, pollfd)) {
lws_handle_POLLOUT_event(wsi, pollfd)) {
if (wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY)
wsi->state = WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE;
lwsl_info("lws_service_fd: closing\n");
@ -536,7 +532,7 @@ lws_service_fd(struct lws_context *context, struct lws_pollfd *pollfd)
break;
read:
eff_buf.token_len = lws_ssl_capable_read(context, wsi,
eff_buf.token_len = lws_ssl_capable_read(wsi,
context->service_buffer,
pending ? pending :
sizeof(context->service_buffer));
@ -631,7 +627,7 @@ handle_pending:
close_and_handled:
lwsl_debug("Close and handled\n");
lws_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
/*
* pollfd may point to something else after the close
* due to pollfd swapping scheme on delete on some platforms

View file

@ -408,9 +408,10 @@ int lws_context_init_client_ssl(struct lws_context_creation_info *info,
#endif
LWS_VISIBLE void
lws_ssl_remove_wsi_from_buffered_list(struct lws_context *context,
struct lws *wsi)
lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
{
struct lws_context *context = wsi->context;
if (!wsi->pending_read_list_prev &&
!wsi->pending_read_list_next &&
context->pending_read_list != wsi)
@ -434,13 +435,13 @@ lws_ssl_remove_wsi_from_buffered_list(struct lws_context *context,
}
LWS_VISIBLE int
lws_ssl_capable_read(struct lws_context *context,
struct lws *wsi, unsigned char *buf, int len)
lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
{
struct lws_context *context = wsi->context;
int n;
if (!wsi->ssl)
return lws_ssl_capable_read_no_ssl(context, wsi, buf, len);
return lws_ssl_capable_read_no_ssl(wsi, buf, len);
n = SSL_read(wsi->ssl, buf, len);
/* manpage: returning 0 means connection shut down */
@ -467,7 +468,7 @@ lws_ssl_capable_read(struct lws_context *context,
}
}
} else
lws_ssl_remove_wsi_from_buffered_list(context, wsi);
lws_ssl_remove_wsi_from_buffered_list(wsi);
return n;
}
@ -529,11 +530,12 @@ lws_ssl_close(struct lws *wsi)
/* 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, lws_sockfd_type accept_fd,
lws_server_socket_service_ssl(struct lws **pwsi, struct lws *new_wsi,
lws_sockfd_type accept_fd,
struct lws_pollfd *pollfd)
{
struct lws *wsi = *pwsi;
struct lws_context *context = wsi->context;
int n, m;
#ifndef USE_WOLFSSL
BIO *bio;