diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-05-15 17:04:30 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-05-15 17:04:30 +0200 |
commit | d57677573b830950f7eaf35327d6138e1248ed4c (patch) | |
tree | af5be63fea08048b5f41eb4df66fde53effe058c /ctl-lib/ctl-action.c | |
parent | e8b0b3c3cc9b0453b88d1315822fda5963754ae7 (diff) |
Simplify action definition
Because before uri could be a path it wasn't suits
for parsing as we can't have any character chosen as
separator. As now, first part isn't a path but rather
a plugin/resource uid then it fine since the separator
isn't allowed in the uid
So # is forbidden for uid by the schema.
Change-Id: Ida3d7acb46687188c44a472c057c58301cb1d9c6
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib/ctl-action.c')
-rw-r--r-- | ctl-lib/ctl-action.c | 86 |
1 files changed, 67 insertions, 19 deletions
diff --git a/ctl-lib/ctl-action.c b/ctl-lib/ctl-action.c index b39f667..ce9b2b8 100644 --- a/ctl-lib/ctl-action.c +++ b/ctl-lib/ctl-action.c @@ -135,12 +135,41 @@ static void ActionDynRequest(AFB_ReqT request) { } #endif +void ParseURI(const char *uri, char **first, char **second) +{ + size_t first_len = 0, second_len = 0; + const char *tmp; + + if(! uri || ! first || ! second) { + return; + } + + tmp = strchr(uri, '#'); + first_len = strlen(uri); + + if (!tmp) { + *first = calloc(1, sizeof(char) * first_len); + strcpy(*first, uri); + } + else { + second_len = strlen(tmp); + first_len = first_len - second_len; + + *first = calloc(1, sizeof(char) * first_len); + *second = calloc(1, sizeof(char) * second_len); + + strncpy(*first, uri, first_len); + strncpy(*second, tmp+1, second_len); + } +} + /*** This function will fill the CtlActionT pointer given in parameters for a * given api using an 'uri' that specify the C plugin to use and the name of * the function * */ -static int BuildPluginAction(AFB_ApiT apiHandle, const char *uri, const char *function, CtlActionT *action) { +static int BuildPluginAction(AFB_ApiT apiHandle, const char *uri, CtlActionT *action) { + char *plugin = NULL, *function = NULL; json_object *callbackJ = NULL; if (!action) { @@ -148,11 +177,13 @@ static int BuildPluginAction(AFB_ApiT apiHandle, const char *uri, const char *fu return -1; } + ParseURI(uri, &plugin, &function); + action->type = CTL_TYPE_CB; - if (uri && function) { + if (plugin && function) { if (wrap_json_pack(&callbackJ, "{ss,ss,s?o*}", - "plugin", uri, + "plugin", plugin, "function", function, "args", action->argsJ)) { AFB_ApiError(apiHandle, "Error packing Callback JSON object for plugin %s and function %s", uri, function); @@ -176,15 +207,24 @@ static int BuildPluginAction(AFB_ApiT apiHandle, const char *uri, const char *fu * unexpected result. * */ -static int BuildApiAction(AFB_ApiT apiHandle, const char *uri, const char *function, CtlActionT *action) { +static int BuildApiAction(AFB_ApiT apiHandle, const char *uri, CtlActionT *action) { + char *api = NULL, *verb = NULL; + if (!action) { AFB_ApiError(apiHandle, "Action not valid"); return -1; } + ParseURI(uri, &api, &verb); + + if(!api || !verb) { + AFB_ApiError(apiHandle, "Error parsing the action string"); + return -1; + } + action->type = CTL_TYPE_API; - action->exec.subcall.api = uri; - action->exec.subcall.verb = function; + action->exec.subcall.api = api; + action->exec.subcall.verb = verb; return 0; } @@ -199,39 +239,48 @@ static int BuildApiAction(AFB_ApiT apiHandle, const char *uri, const char *funct */ #ifdef CONTROL_SUPPORT_LUA -static int BuildLuaAction(AFB_ApiT apiHandle, const char *uri, const char *function, CtlActionT *action) { +static int BuildLuaAction(AFB_ApiT apiHandle, const char *uri, CtlActionT *action) { + char *plugin = NULL, *function = NULL; + if (!action) { AFB_ApiError(apiHandle, "Action not valid"); return -1; } + ParseURI(uri, &plugin, &function); + + if(!plugin || !function) { + AFB_ApiError(apiHandle, "Error parsing the action string"); + return -1; + } + action->type = CTL_TYPE_LUA; - action->exec.lua.plugin = uri; + action->exec.lua.plugin = plugin; action->exec.lua.funcname = function; return 0; } #endif -static int BuildOneAction(AFB_ApiT apiHandle, CtlActionT *action, const char *uri, const char *function) { +static int BuildOneAction(AFB_ApiT apiHandle, CtlActionT *action, const char *uri) { size_t lua_pre_len = strlen(LUA_ACTION_PREFIX); size_t api_pre_len = strlen(API_ACTION_PREFIX); size_t plugin_pre_len = strlen(PLUGIN_ACTION_PREFIX); - if (uri && function && action) { + if (uri && action) { if (!strncasecmp(uri, LUA_ACTION_PREFIX, lua_pre_len)) { #ifdef CONTROL_SUPPORT_LUA - return BuildLuaAction(apiHandle, &uri[lua_pre_len], function, action); + return BuildLuaAction(apiHandle, &uri[lua_pre_len], action); #else AFB_ApiError(apiHandle, "LUA support not selected at build. Feature disabled"); return -1; #endif } else if (!strncasecmp(uri, API_ACTION_PREFIX, api_pre_len)) { - return BuildApiAction(apiHandle, &uri[api_pre_len], function, action); + return BuildApiAction(apiHandle, &uri[api_pre_len], action); } else if (!strncasecmp(uri, PLUGIN_ACTION_PREFIX, plugin_pre_len)) { - return BuildPluginAction(apiHandle, &uri[plugin_pre_len], function, action); + return BuildPluginAction(apiHandle, &uri[plugin_pre_len], action); } else { - AFB_ApiError(apiHandle, "Wrong uri specified. You have to specified 'lua://', 'plugin://' or 'api://'. (%s)", function); + AFB_ApiError(apiHandle, "Wrong uri specified. You have to specified 'lua://', 'plugin://' or 'api://'."); return -1; } } @@ -244,16 +293,15 @@ static int BuildOneAction(AFB_ApiT apiHandle, CtlActionT *action, const char *ur int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *actionJ, int exportApi) { int err = 0; - const char *uri = NULL, *function = NULL; + const char *uri = NULL; memset(action, 0, sizeof (CtlActionT)); if (actionJ) { - err = wrap_json_unpack(actionJ, "{ss,s?s,ss,ss,s?s,s?o !}", + err = wrap_json_unpack(actionJ, "{ss,s?s,ss,s?s,s?o !}", "uid", &action->uid, "info", &action->info, - "uri", &uri, - "function", &function, + "action", &uri, "privileges", &action->privileges, "args", &action->argsJ); if (!err) { @@ -269,7 +317,7 @@ int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *actionJ, } } #endif - err = BuildOneAction(apiHandle, action, uri, function); + err = BuildOneAction(apiHandle, action, uri); } else { AFB_ApiError(apiHandle, "Fail to parse action JSON : (%s)", json_object_to_json_string(actionJ)); err = -1; |