Skip to content

Commit

Permalink
fix(closing connections): connections are closed after use.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinopresnik authored and jkuri committed Apr 3, 2018
1 parent 56c26e7 commit 17d6a78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ typedef struct conn_t
uv_stream_t *handle;
boolean ws_handshake_sent;
uv_stream_t *proxy_handle;
enum
{
TYPE_REQUEST,
TYPE_WEBSOCKET
} type;
} conn_t;

int message_begin_cb(http_parser *p);
Expand Down
31 changes: 21 additions & 10 deletions src/bproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void conn_init(uv_stream_t *handle)
http_parser_init(conn->parser, HTTP_REQUEST);
conn->parser->data = conn;
conn->ws_handshake_sent = false;
conn->type = TYPE_REQUEST;
}

void conn_free(uv_handle_t *handle)
Expand Down Expand Up @@ -170,9 +171,10 @@ void proxy_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
{
log_error("could not read from socket!");
}
if (uv_is_closing((const uv_handle_t *)handle))
if (!uv_is_closing((const uv_handle_t *)handle))
{
uv_close((uv_handle_t *)handle, proxy_close_cb);
conn->proxy_handle = NULL;
}
}

Expand All @@ -184,11 +186,15 @@ void proxy_connect_cb(uv_connect_t *req, int status)
proxy_t *proxy_conn = req->handle->data;
proxy_conn->connect_req = *req;

if (proxy_conn->conn->ws_handshake_sent)
if (status < 0)
{
proxy_conn->conn->proxy_handle = req->handle;
uv_close((uv_handle_t *)proxy_conn->conn->handle, close_cb);
// TODO: write meaningful responses
return;
}

proxy_conn->conn->proxy_handle = req->handle;

http_parser_init(&proxy_conn->response.parser, HTTP_RESPONSE);
proxy_conn->response.parser.data = &proxy_conn->response;

Expand All @@ -207,10 +213,10 @@ void proxy_http_request(char *ip, unsigned short port, conn_t *conn)
proxy_conn->tcp.data = proxy_conn;
proxy_conn->gzip_state = NULL;
proxy_conn->response.processed_data_len = 0;

if (conn->request->upgrade)
{
conn->ws_handshake_sent = true;
conn->type = TYPE_WEBSOCKET;
}

uv_tcp_init(server->loop, &proxy_conn->tcp);
Expand All @@ -225,7 +231,11 @@ void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)

if (nread >= 0)
{
if (!conn->ws_handshake_sent)
if (conn->proxy_handle != NULL)
{
write_buf(conn->proxy_handle, buf->base, nread);
}
else
{
conn->request->raw = malloc(nread + 1);
memcpy(conn->request->raw, buf->base, nread);
Expand Down Expand Up @@ -261,19 +271,20 @@ void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)

proxy_http_request(ip_port.ip, ip_port.port, conn);
}
else if (conn->proxy_handle)
{
write_buf(conn->proxy_handle, buf->base, nread);
}
}
else
{
if (nread != UV_EOF)
{
log_error("could not read from socket!");
}
if (uv_is_closing((const uv_handle_t *)handle))
if (!uv_is_closing((const uv_handle_t *)handle))
{
if (conn->proxy_handle && !uv_is_closing((uv_handle_t *)conn->proxy_handle))
{
uv_close((uv_handle_t *)conn->proxy_handle, proxy_close_cb);
conn->proxy_handle = NULL;
}
uv_close((uv_handle_t *)handle, close_cb);
}
}
Expand Down

0 comments on commit 17d6a78

Please sign in to comment.