Skip to content

Commit

Permalink
fix(free): remove free function for dead connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Jun 2, 2018
1 parent cd58020 commit 8f5651c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@
}
],
"version": 4
}
}
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"files.associations": {
"ostream": "c"
"ostream": "c",
"iterator": "c",
"memory": "c"
}
}
1 change: 0 additions & 1 deletion bproxy.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"src/http.c",
"src/cJSON.c",
"src/http_link.c",
"src/common.c",
"src/bproxy.c"
]
}]
Expand Down
29 changes: 27 additions & 2 deletions include/common.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
#ifndef COMMON_H
#define COMMON_H

#include "string.h"
#include <string.h>

char *strnstr(char *s, size_t n, char *find);
char *strnstr_custom(char *s, size_t n, char *find)
{
size_t find_len = strlen(find);
if (find_len == 0)
{
return s;
}
if (n < find_len)
{
return NULL;
}
char *s_end = &s[n - find_len + 1];
while (1)
{
while ((*s) != (*find) && s != s_end)
++s;
if (s == s_end)
{
return NULL;
}
if (strncmp(s++, find, find_len) == 0)
{
return --s;
}
}
}

#endif
11 changes: 6 additions & 5 deletions src/bproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void conn_close(conn_t *conn)
uv_link_close((uv_link_t *)&conn->observer, observer_connection_link_close_cb);
}
}
if (conn->handle == NULL && conn->proxy_handle == NULL)
if (!conn->handle && !conn->proxy_handle)
{
QUEUE *q;
QUEUE_FOREACH(q, &conn->http_link_context.request.raw_requests)
Expand All @@ -191,7 +191,7 @@ void conn_close(conn_t *conn)
free(bq->buf.base);
}
http_free_raw_requests_queue(&conn->http_link_context.request);
free(conn);
// free(conn);
}
}

Expand Down Expand Up @@ -251,7 +251,8 @@ void proxy_read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
int err = uv_link_write((uv_link_t *)&conn->observer, &tmp_buf, 1, NULL, http_write_link_cb, buf->base);
if (err)
{
log_error("error writing to client: %s", uv_err_name(err));
// log_error("error writing to client: %s", uv_err_name(err));
conn_close(conn);
}
}
else if (nread < 0)
Expand Down Expand Up @@ -281,8 +282,8 @@ void proxy_connect_cb(uv_connect_t *req, int status)
}

uv_read_start((uv_stream_t *)conn->proxy_handle, alloc_cb, proxy_read_cb);
http_request_t *request = &conn->http_link_context.request;
//write_buf((uv_stream_t *)conn->proxy_handle, request->raw, request->raw_len);
// http_request_t *request = &conn->http_link_context.request;
// write_buf((uv_stream_t *)conn->proxy_handle, request->raw, request->raw_len);
QUEUE *q;
QUEUE_FOREACH(q, &conn->http_link_context.request.raw_requests)
{
Expand Down
31 changes: 0 additions & 31 deletions src/common.c

This file was deleted.

7 changes: 3 additions & 4 deletions src/http_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ void alloc_cb_override(uv_link_t *link,

void http_free_raw_requests_queue(http_request_t *request)
{
QUEUE *q;
while (!QUEUE_EMPTY(&request->raw_requests))
{
buf_queue_t *bq = QUEUE_DATA(QUEUE_NEXT(&request->raw_requests), buf_queue_t, member);
Expand Down Expand Up @@ -65,7 +64,7 @@ void http_read_cb_override(uv_link_t *link, ssize_t nread, const uv_buf_t *buf)
// Parse status line
size_t status_line_len;
free(context->request.status_line);
char *status_line_end = strnstr(buf->base, nread, "\r");
char *status_line_end = strnstr_custom(buf->base, nread, "\r");
if (status_line_end)
{
status_line_len = status_line_end - buf->base;
Expand All @@ -91,7 +90,7 @@ void http_read_cb_override(uv_link_t *link, ssize_t nread, const uv_buf_t *buf)
context->response.processed_data_len = 0;
QUEUE_INIT(&context->request.raw_requests);

char *header_end = strnstr(buf->base, nread, "\r\n\r\n");
char *header_end = strnstr_custom(buf->base, nread, "\r\n\r\n");
if (header_end)
{
http_headers_len = header_end - buf->base + 4;
Expand Down Expand Up @@ -189,7 +188,7 @@ int http_link_write(uv_link_t *link, uv_link_t *source, const uv_buf_t bufs[], u

// Parse status line
size_t status_line_len;
char* status_line_end = strnstr(resp, nread, "\r");
char* status_line_end = strnstr_custom(resp, nread, "\r");
if (status_line_end)
{
status_line_len = status_line_end - resp;
Expand Down

0 comments on commit 8f5651c

Please sign in to comment.