connection: add api: close

close low level TCP connection

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-27 13:18:27 +08:00
parent 2f951e5166
commit 6e84f2a738
3 changed files with 33 additions and 1 deletions

View File

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

View File

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

View File

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