Skip to content

Commit

Permalink
hawkbit-client: drop action ID URL regex matching in process_deployme…
Browse files Browse the repository at this point in the history
…nt()

It is neither required nor a good idea to get the action ID from an
URL. It is also a bad idea to rely on the "c" GET parameter in
deployment base links. This is an implementation detail which can
change any time in hawkBit. It is not part of the DDI API, rather an
artifact from an example in the API's documentation.

So simply use the action ID from the deployment JSON response and drop
all corresponding regex matching code.

Signed-off-by: Bastian Krause <bst@pengutronix.de>
  • Loading branch information
Bastian-Krause committed Feb 2, 2021
1 parent cbafe65 commit 8624e71
Showing 1 changed file with 9 additions and 34 deletions.
43 changes: 9 additions & 34 deletions src/hawkbit-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,24 +430,6 @@ static long json_get_sleeptime(JsonNode *root)
return poll_sleep_time;
}

/**
* @brief
*/
static gchar** regex_groups(const gchar* pattern, const gchar *str, GError **error)
{
gchar **result = NULL;
GMatchInfo *match_info;
GRegex *regex = g_regex_new(pattern, 0, 0, error);
g_regex_match(regex, str, 0, &match_info);
if (g_match_info_matches(match_info))
{
result = g_match_info_fetch_all(match_info);
}
g_match_info_free(match_info);
g_regex_unref(regex);
return result;
}

/**
* @brief Build API URL
*
Expand Down Expand Up @@ -618,30 +600,23 @@ static gboolean process_deployment(JsonNode *req_root, GError **error)
return FALSE;
}

// get resource id and action id from url
gchar** groups = regex_groups("/deploymentBase/(.+)[?]c=(.+)$", deployment, NULL);
if (groups == NULL) {
g_set_error(error,1,2,"Failed to parse deployment base response.");
return FALSE;
}
action_id = g_strdup(groups[1]);
g_autofree gchar *resource_id = g_strdup(groups[2]);
g_strfreev(groups);

// build urls for deployment resource info
g_autofree gchar *get_resource_url = build_api_url(
"deploymentBase/%s?c=%s", action_id, resource_id);
gchar *feedback_url = build_api_url("deploymentBase/%s/feedback", action_id);

// get deployment resource
JsonParser *json_response_parser = NULL;
int status = rest_request(GET, get_resource_url, NULL, &json_response_parser, error);
int status = rest_request(GET, deployment, NULL, &json_response_parser, error);
if (status != 200 || json_response_parser == NULL) {
g_debug("Failed to get resource from hawkbit server. Status: %d", status);
return FALSE;
}
JsonNode *resp_root = json_parser_get_root(json_response_parser);

action_id = json_get_string(resp_root, "$.id", NULL);
if (!action_id) {
g_set_error(error,1,1,"Failed to parse deployment base response.");
return FALSE;
}

gchar *feedback_url = build_api_url("deploymentBase/%s/feedback", action_id);

JsonArray *json_chunks = json_get_array(resp_root, "$.deployment.chunks", NULL);
if (json_chunks == NULL || json_array_get_length(json_chunks) == 0) {
feedback(feedback_url, action_id, "Failed to parse deployment resource.", "failure", "closed", NULL);
Expand Down

0 comments on commit 8624e71

Please sign in to comment.