Skip to content

Commit

Permalink
connection: add api: close
Browse files Browse the repository at this point in the history
close low level TCP connection

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Jan 27, 2021
1 parent 2f951e5 commit 6e84f2a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ static void conn_done(struct uh_connection *conn)
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
struct ev_loop *loop = conni->srv->loop;

if (conni->flags & CONN_F_CLOSED)
return;

if (!http_should_keep_alive(&conni->parser))
conni->flags |= CONN_F_SEND_AND_CLOSE;

Expand All @@ -58,6 +61,9 @@ static void conn_send(struct uh_connection *conn, const void *data, ssize_t len)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;

if (conni->flags & CONN_F_CLOSED)
return;

buffer_put_data(&conni->wb, data, len);
ev_io_start(conni->srv->loop, &conni->iow);
}
Expand All @@ -69,6 +75,9 @@ static void conn_send_file(struct uh_connection *conn, const char *path, off_t o
struct stat st;
int fd;

if (conni->flags & CONN_F_CLOSED)
return;

if (len == 0)
return;

Expand Down Expand Up @@ -109,6 +118,9 @@ static void conn_printf(struct uh_connection *conn, const char *format, ...)
struct buffer *wb = &conni->wb;
va_list arg;

if (conni->flags & CONN_F_CLOSED)
return;

va_start(arg, format);
buffer_put_vprintf(wb, format, arg);
va_end(arg);
Expand All @@ -120,6 +132,9 @@ static void conn_vprintf(struct uh_connection *conn, const char *format, va_list
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;

if (conni->flags & CONN_F_CLOSED)
return;

buffer_put_vprintf(&conni->wb, format, arg);
ev_io_start(conni->srv->loop, &conni->iow);
}
Expand Down Expand Up @@ -334,6 +349,15 @@ static struct uh_str conn_extract_body(struct uh_connection *conn)
return body;
}

static void conn_close(struct uh_connection *conn)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;

http_parser_pause(&conni->parser, true);

conni->flags |= CONN_F_CLOSED;
}

static int on_message_begin_cb(struct http_parser *parser)
{
struct uh_connection_internal *conn = (struct uh_connection_internal *)parser->data;
Expand Down Expand Up @@ -548,6 +572,10 @@ static void conn_http_parse(struct uh_connection_internal *conn)
return;

nparsed = http_parser_execute(parser, &settings, (const char *)data, length);
if (conn->flags & CONN_F_CLOSED) {
conn_free(conn);
return;
}

switch (parser->http_errno) {
case HPE_PAUSED:
Expand Down Expand Up @@ -762,6 +790,8 @@ static void conn_init_cb(struct uh_connection *conn)
conn->get_content_length = conn_get_content_length;
conn->get_body = conn_get_body;
conn->extract_body = conn_extract_body;

conn->close = conn_close;
}

struct uh_connection_internal *uh_new_connection(struct uh_server_internal *srv, int sock, struct sockaddr *addr)
Expand Down
3 changes: 2 additions & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
#define UHTTPD_MAX_HEADER_NUM 50

#define CONN_F_SEND_AND_CLOSE (1 << 0) /* Push remaining data and close */
#define CONN_F_SSL_HANDSHAKE_DONE (1 << 1) /* SSL hanshake has completed */
#define CONN_F_CLOSED (1 << 1) /* closed */
#define CONN_F_SSL_HANDSHAKE_DONE (1 << 2) /* SSL hanshake has completed */

struct uh_server_internal;

Expand Down
1 change: 1 addition & 0 deletions src/uhttpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct uh_connection {
struct uh_str (*get_body)(struct uh_connection *conn);
/* The remain body data will be discurd after this function called */
struct uh_str (*extract_body)(struct uh_connection *conn);
void (*close)(struct uh_connection *conn); /* close low level TCP connection */
void *userdata;
};

Expand Down

0 comments on commit 6e84f2a

Please sign in to comment.