From 887854150b8a156444ab140372b694d4f373f599 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 13 Jan 2020 23:29:29 +0100 Subject: [PATCH] update rest handler to support id parameter --- lib/include/restd_server.h | 1 + lib/src/restd_rest_handler.c | 50 ++++++++++++++++++++++++++++++++---- lib/src/restd_server.c | 3 +++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/include/restd_server.h b/lib/include/restd_server.h index eba4a5e..65ee867 100644 --- a/lib/include/restd_server.h +++ b/lib/include/restd_server.h @@ -125,6 +125,7 @@ struct restd_conn_s void *userdata[2]; /*!< userdata[0] for end user, userdata[1] for extra */ restd_userdata_free_cb userdata_free_cb[2]; /*!< callback to release user data */ char *method; /*!< request method. set by protocol handler */ + int id; }; /* diff --git a/lib/src/restd_rest_handler.c b/lib/src/restd_rest_handler.c index e36b315..1d2e19c 100644 --- a/lib/src/restd_rest_handler.c +++ b/lib/src/restd_rest_handler.c @@ -144,6 +144,31 @@ static const char *file_mime_lookup(const char *path) return "application/octet-stream"; } +/*--------------------------------------------------------------------------*/ + +static bool contain(const char *src, const char *dest, int len) +{ + int len_src, len_dest; + int i = 0; + + len_src = strlen(src); + if (len_src < len) + return false; + + len_dest = strlen(dest); + if (len_dest < len) + return false; + + while (i < len) + { + if (src[i] != dest[i]) + return false; + i++; + } + + return true; +} + /*! ---------------------------------------------------------------------------- * @fn restd_rest_handler * @@ -166,10 +191,6 @@ int restd_rest_handler(short event, restd_conn_t *conn, void *userdata) { // DEBUG("call_hooks: method: %s - %s \n", hook->method, conn->method); // DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); - if ((hook->path == NULL) || (http->request.path == NULL) || (strcmp(hook->path, http->request.path) != 0)) - { - continue; - } // DEBUG("HOOK FOUND !!!!\n"); if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) { @@ -178,7 +199,26 @@ int restd_rest_handler(short event, restd_conn_t *conn, void *userdata) break; } - return hook->cb(event, conn, hook->userdata); + if ((hook->path != NULL) && (http->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, http->request.path, pos)) + { + const char *buffer = &http->request.path[pos]; + // printf("buffer: <%s>\n", buffer); + conn->id = atoi(buffer); + return hook->cb(event, conn, hook->userdata); + } + else if (strcmp(hook->path, http->request.path) == 0) + return hook->cb(event, conn, hook->userdata); + } } } // No Hook Found check if it's a real file into document root. diff --git a/lib/src/restd_server.c b/lib/src/restd_server.c index fa4c4be..8f3287d 100644 --- a/lib/src/restd_server.c +++ b/lib/src/restd_server.c @@ -557,6 +557,9 @@ static restd_conn_t *conn_new(restd_server_t *server, struct bufferevent *buffer // Run callbacks with AD_EVENT_INIT event. conn->status = call_hooks(RESTD_EVENT_INIT | RESTD_EVENT_WRITE, conn); + // + conn->id = -1; + return conn; }