update rest handler to support id parameter
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-01-13 23:29:29 +01:00
parent d7274d4a8d
commit 887854150b
3 changed files with 49 additions and 5 deletions

View File

@@ -125,6 +125,7 @@ struct restd_conn_s
void *userdata[2]; /*!< userdata[0] for end user, userdata[1] for extra */ 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 */ restd_userdata_free_cb userdata_free_cb[2]; /*!< callback to release user data */
char *method; /*!< request method. set by protocol handler */ char *method; /*!< request method. set by protocol handler */
int id;
}; };
/* /*

View File

@@ -144,6 +144,31 @@ static const char *file_mime_lookup(const char *path)
return "application/octet-stream"; 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 * @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: method: %s - %s \n", hook->method, conn->method);
// DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); // 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"); // DEBUG("HOOK FOUND !!!!\n");
if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) 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; 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. // No Hook Found check if it's a real file into document root.

View File

@@ -557,6 +557,9 @@ static restd_conn_t *conn_new(restd_server_t *server, struct bufferevent *buffer
// Run callbacks with AD_EVENT_INIT event. // Run callbacks with AD_EVENT_INIT event.
conn->status = call_hooks(RESTD_EVENT_INIT | RESTD_EVENT_WRITE, conn); conn->status = call_hooks(RESTD_EVENT_INIT | RESTD_EVENT_WRITE, conn);
//
conn->id = -1;
return conn; return conn;
} }