parameter management
This commit is contained in:
@@ -180,6 +180,7 @@ static void notify_cb(struct bufferevent *buffer, void *userdata);
|
||||
void rest_request_cb(struct evhttp_request *req, void *arg);
|
||||
void print_request_info(struct evhttp_request *req);
|
||||
void restd_http_response_from_file(struct evhttp_request *req, int code, int fd, const char *content_type);
|
||||
bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request_path);
|
||||
|
||||
/*--------------------------- PUBLIC FUNCTIONS -------------------------------*/
|
||||
|
||||
@@ -482,13 +483,13 @@ void restd_server_register_hook_on_path(restd_server_t *server, enum evhttp_cmd_
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void restd_http_response(struct evhttp_request *req, int code, const char *contenttype, const char *data)
|
||||
void restd_http_response(restd_resp_t *response, int code, const char *contenttype, const char *data)
|
||||
{
|
||||
struct evbuffer *resp_buf;
|
||||
struct evkeyvalq *resp_headers;
|
||||
|
||||
resp_buf = evhttp_request_get_output_buffer(req);
|
||||
resp_headers = evhttp_request_get_output_headers(req);
|
||||
resp_buf = evhttp_request_get_output_buffer(response->request);
|
||||
resp_headers = evhttp_request_get_output_headers(response->request);
|
||||
|
||||
if (data != NULL)
|
||||
{
|
||||
@@ -497,17 +498,17 @@ void restd_http_response(struct evhttp_request *req, int code, const char *conte
|
||||
|
||||
evhttp_add_header(resp_headers, "Content-Type", contenttype);
|
||||
|
||||
evhttp_send_reply(req, code, NULL, resp_buf);
|
||||
evhttp_send_reply(response->request, code, NULL, resp_buf);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
char *restd_http_get_body(struct evhttp_request *req)
|
||||
char *restd_http_get_body(restd_resp_t *response)
|
||||
{
|
||||
char *body = NULL;
|
||||
struct evbuffer *buf;
|
||||
|
||||
buf = evhttp_request_get_input_buffer(req);
|
||||
buf = evhttp_request_get_input_buffer(response->request);
|
||||
size_t len = evbuffer_get_length(buf);
|
||||
|
||||
body = malloc(len + 1);
|
||||
@@ -589,7 +590,6 @@ void restd_resp_free(restd_resp_t *response)
|
||||
free(response);
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------- LOCAL FUNCTIONS -------------------------------*/
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -781,6 +781,10 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
restd_server_t *server = (restd_server_t *)arg;
|
||||
char *root_path;
|
||||
restd_resp_t *response;
|
||||
|
||||
response = restd_resp_new();
|
||||
response->request = req;
|
||||
|
||||
#if 0
|
||||
print_request_info(req);
|
||||
@@ -806,36 +810,14 @@ void rest_request_cb(struct evhttp_request *req, void *arg)
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((hook->path != NULL) && (request_path != NULL))
|
||||
if (manage_hook(hook, response, request_path) == true)
|
||||
{
|
||||
int i = 0;
|
||||
int pos = -1;
|
||||
while (hook->path[i])
|
||||
{
|
||||
if (hook->path[i] == ':')
|
||||
pos = i;
|
||||
i++;
|
||||
}
|
||||
if (pos != -1 && contain(hook->path, request_path, pos))
|
||||
{
|
||||
const char *buffer = &request_path[pos];
|
||||
// printf("buffer: <%s>\n", buffer);
|
||||
// TODO conn->id = atoi(buffer);
|
||||
hook->cb(req, hook->userdata);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
int rett = strcmp(hook->path, request_path);
|
||||
if (rett == 0)
|
||||
{
|
||||
hook->cb(req, hook->userdata);
|
||||
return;
|
||||
}
|
||||
}
|
||||
restd_resp_free(response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
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))
|
||||
@@ -939,3 +921,39 @@ void restd_http_response_from_file(struct evhttp_request *req, int code, int fd,
|
||||
|
||||
evhttp_send_reply(req, code, NULL, resp_buf);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
bool manage_hook(restd_hook_t *hook, restd_resp_t *response, const char *request_path)
|
||||
{
|
||||
if ((hook->path != NULL) && (request_path != NULL))
|
||||
{
|
||||
int i = 0;
|
||||
int pos = -1;
|
||||
while (hook->path[i])
|
||||
{
|
||||
if (hook->path[i] == ':')
|
||||
pos = i;
|
||||
i++;
|
||||
}
|
||||
if (pos != -1 && contain(hook->path, request_path, pos))
|
||||
{
|
||||
const char *buffer = &request_path[pos];
|
||||
// printf("buffer: <%s>\n", buffer);
|
||||
// TODO conn->id = atoi(buffer);
|
||||
hook->cb(response, hook->userdata);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
int rett = strcmp(hook->path, request_path);
|
||||
if (rett == 0)
|
||||
{
|
||||
hook->cb(response, hook->userdata);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ typedef struct restd_resp_s restd_resp_t;
|
||||
/**
|
||||
* User callback(hook) prototype.
|
||||
*/
|
||||
typedef int (*restd_callback)(struct evhttp_request *req, void *arg);
|
||||
typedef int (*restd_callback)(restd_resp_t *response, void *arg);
|
||||
typedef void (*restd_userdata_free_cb)(void *conn, void *userdata);
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
@@ -57,8 +57,8 @@ extern void restd_server_set_option(restd_server_t *server, const char *key, con
|
||||
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_http_response(struct evhttp_request *req, int code, const char *contenttype, const char *data);
|
||||
extern char *restd_http_get_body(struct evhttp_request *req);
|
||||
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);
|
||||
|
||||
extern restd_hook_t *restd_hook_new(void);
|
||||
extern void restd_hook_free(restd_hook_t *hook);
|
||||
|
||||
@@ -50,54 +50,54 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_error_handler(struct evhttp_request *req, void *arg)
|
||||
int my_error_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
restd_http_response(req, 200, "application/json", kerror_body);
|
||||
restd_http_response(response, 200, "application/json", kerror_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_http_handler(struct evhttp_request *req, void *arg)
|
||||
int my_success_http_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
restd_http_response(req, 200, "application/json", ksuccess_get_body);
|
||||
restd_http_response(response, 200, "application/json", ksuccess_get_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_get_handler(struct evhttp_request *req, void *arg)
|
||||
int my_success_rest_get_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
restd_http_response(req, 200, "application/json", ksuccess_get_body);
|
||||
restd_http_response(response, 200, "application/json", ksuccess_get_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_delete_handler(struct evhttp_request *req, void *arg)
|
||||
int my_success_rest_delete_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
restd_http_response(req, 200, "application/json", ksuccess_delete_body);
|
||||
restd_http_response(response, 200, "application/json", ksuccess_delete_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_put_handler(struct evhttp_request *req, void *arg)
|
||||
int my_success_rest_put_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
char *body;
|
||||
body = restd_http_get_body(req);
|
||||
restd_http_response(req, 200, "application/json", body);
|
||||
body = restd_http_get_body(response);
|
||||
restd_http_response(response, 200, "application/json", body);
|
||||
free(body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_post_handler(struct evhttp_request *req, void *arg)
|
||||
int my_success_rest_post_handler(restd_resp_t *response, void *arg)
|
||||
{
|
||||
char *body;
|
||||
body = restd_http_get_body(req);
|
||||
restd_http_response(req, 200, "application/json", body);
|
||||
body = restd_http_get_body(response);
|
||||
restd_http_response(response, 200, "application/json", body);
|
||||
free(body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user