This commit is contained in:
@@ -41,6 +41,7 @@ typedef struct restd_hook_s restd_hook_t;
|
||||
typedef struct restd_resp_s restd_resp_t;
|
||||
|
||||
typedef int (*restd_callback)(restd_resp_t *response, void *arg);
|
||||
typedef int (*restd__error_callback)(restd_resp_t *response, int reason, void *arg);
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
@@ -50,9 +51,12 @@ extern int restd_server_start(restd_server_t *server);
|
||||
extern int restd_server_attach_event_loop(restd_server_t *server, struct event_base *ev_base);
|
||||
|
||||
extern void restd_server_set_option(restd_server_t *server, const char *key, const char *value);
|
||||
|
||||
extern void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_type method, const char *path,
|
||||
restd_callback cb, void *userdata);
|
||||
|
||||
extern void restd_server_register_error_handler(restd_server_t *server, restd__error_callback cb, void *userdata);
|
||||
|
||||
extern void restd_http_response(restd_resp_t *response, int code, const char *contenttype, const char *data);
|
||||
extern char *restd_http_get_body(restd_resp_t *response);
|
||||
|
||||
@@ -81,6 +85,9 @@ enum restd_log_e
|
||||
#define RESTD_OK (0) /*!< I'm done with this request. Escalate to other hooks. */
|
||||
#define RESTD_FAILED (1) /*!< I'm done with this request. But the Process failed. */
|
||||
|
||||
#define RESTD_ERROR_METHOD_NOT_ALLOWED (2)
|
||||
#define RESTD_ERROR_PATH_NOT_FOUND (3)
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
| DATA STRUCTURES |
|
||||
\*---------------------------------------------------------------------------*/
|
||||
@@ -100,7 +107,8 @@ struct restd_server_s
|
||||
|
||||
struct bufferevent *notify_buffer; /*!< internal notification channel */
|
||||
|
||||
restd_callback error_handler;
|
||||
restd__error_callback error_handler;
|
||||
void *error_userdata;
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user