Add Error Handler.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-20 14:32:32 +01:00
parent c6cdd21512
commit b9beca6d72
3 changed files with 91 additions and 28 deletions

View File

@@ -450,6 +450,9 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
char *fragment;
qlist_obj_t obj;
if (server == NULL)
return;
// Init Hook.
hook = restd_hook_new();
@@ -487,6 +490,17 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
/*--------------------------------------------------------------------------*/
void restd_server_register_error_handler(restd_server_t *server, restd__error_callback cb, void *userdata)
{
if (server == NULL)
return;
server->error_handler = cb;
server->error_userdata = userdata;
}
/*--------------------------------------------------------------------------*/
void restd_http_response(restd_resp_t *response, int code, const char *contenttype, const char *data)
{
struct evbuffer *resp_buf;
@@ -794,7 +808,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
const char *request_path = evhttp_request_get_uri(req);
qlist_t *hooks = server->hooks;
//int reason = RESTD_ERROR_PATH_NOT_FOUND;
int reason = RESTD_ERROR_PATH_NOT_FOUND;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
while (hooks->getnext(hooks, &obj, false) == true)
@@ -807,8 +821,6 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
//printf("==== HOOK FOUND !!!!\n");
if (hook->method != evhttp_request_get_command(req))
{
//printf("==== Hook found but method failed -> next.\n");
//reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
continue;
}
@@ -819,7 +831,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
}
}
}
restd_resp_free(response);
// No Hook Found check if it's a real file into document root.
root_path = restd_server_get_option(server, "server.root_path");
if ((root_path != NULL) && (strlen(root_path) != 0))
@@ -831,6 +843,7 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
if (fd != -1)
{
restd_http_response_from_file(req, 200, fd, file_mime_lookup(buf));
restd_resp_free(response);
return;
}
else
@@ -838,16 +851,17 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
// TODO 404
}
}
#if 0 // TODO
if (conn->server->error_handler != NULL)
if (server->error_handler != NULL)
{
return conn->server->error_handler(reason, conn, NULL);
server->error_handler(response, reason, NULL);
}
else
#endif
{
evhttp_send_reply(req, 500, "Internal Error", NULL);
}
restd_resp_free(response);
}
/*--------------------------------------------------------------------------*/