mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-10 16:31:44 +00:00
Implemented mg_set_http_error_handler()
This commit is contained in:
1 parent
f0571f9fd6
commit
56f128514e
2 files changed
+37
-1
No files matched your search
+15
-1
@@ -285,6 +285,7 @@ struct mg_server {
|
||||
union socket_address lsa; // Listening socket address
|
||||
struct ll active_connections;
|
||||
struct ll uri_handlers;
|
||||
mg_handler_t error_handler;
|
||||
char *config_options[NUM_OPTIONS];
|
||||
void *server_data;
|
||||
void *ssl_ctx; // SSL context
|
||||
@@ -653,6 +654,15 @@ static void send_http_error(struct connection *conn, int code,
|
||||
va_list ap;
|
||||
int body_len, headers_len, match_code;
|
||||
|
||||
conn->mg_conn.status_code = code;
|
||||
|
||||
// Invoke error handler if it is set
|
||||
if (conn->server->error_handler != NULL &&
|
||||
conn->server->error_handler(&conn->mg_conn)) {
|
||||
close_local_endpoint(conn);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle error code rewrites
|
||||
while ((rewrites = next_option(rewrites, &a, &b)) != NULL) {
|
||||
if ((match_code = atoi(a.ptr)) > 0 && match_code == code) {
|
||||
@@ -666,7 +676,6 @@ static void send_http_error(struct connection *conn, int code,
|
||||
}
|
||||
}
|
||||
|
||||
conn->mg_conn.status_code = code;
|
||||
body_len = mg_snprintf(body, sizeof(body), "%d %s\n", code, message);
|
||||
if (fmt != NULL) {
|
||||
body[body_len++] = '\n';
|
||||
@@ -3876,6 +3885,11 @@ const char *mg_set_option(struct mg_server *server, const char *name,
|
||||
return error_msg;
|
||||
}
|
||||
|
||||
|
||||
void mg_set_http_error_handler(struct mg_server *server, mg_handler_t handler) {
|
||||
server->error_handler = handler;
|
||||
}
|
||||
|
||||
void mg_set_listening_socket(struct mg_server *server, int sock) {
|
||||
if (server->listening_sock != INVALID_SOCKET) {
|
||||
closesocket(server->listening_sock);
|
||||
|
||||
Reference in new issue
Block a user