added a new handler callback to be used when the http connection is being closed

so that in case of connections closed prematurely, the user can clear resources
eventually bound to the connection using the 'connection_param' member of the
mg_connection structure
This commit is contained in:
Andrea Guzzo 2014-02-06 10:17:56 +00:00 committed by xant
parent eaa0e26a85
commit b22f7d3a49
2 changed files with 10 additions and 0 deletions

View File

@ -297,6 +297,7 @@ struct mg_server {
union socket_address lsa; // Listening socket address
struct ll active_connections;
mg_handler_t request_handler;
mg_handler_t http_close_handler;
mg_handler_t error_handler;
mg_handler_t auth_handler;
char *config_options[NUM_OPTIONS];
@ -1391,6 +1392,10 @@ static void close_conn(struct connection *conn) {
LINKED_LIST_REMOVE(&conn->link);
closesocket(conn->client_sock);
close_local_endpoint(conn);
if (conn->server->http_close_handler)
conn->server->http_close_handler(&conn->mg_conn);
DBG(("%p %d %d", conn, conn->flags, conn->endpoint_type));
free(conn->request); // It's OK to free(NULL), ditto below
free(conn->path_info);
@ -4175,6 +4180,10 @@ void mg_set_request_handler(struct mg_server *server, mg_handler_t handler) {
server->request_handler = handler;
}
void mg_set_http_close_handler(struct mg_server *server, mg_handler_t handler) {
server->http_close_handler = handler;
}
void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
server->error_handler = handler;
}

View File

@ -67,6 +67,7 @@ void mg_destroy_server(struct mg_server **);
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
unsigned int mg_poll_server(struct mg_server *, int milliseconds);
void mg_set_request_handler(struct mg_server *, mg_handler_t);
void mg_set_http_close_handler(struct mg_server *, mg_handler_t);
void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
void mg_set_auth_handler(struct mg_server *, mg_handler_t);
const char **mg_get_valid_option_names(void);