summaryrefslogtreecommitdiff
path: root/src/lib/merchant_api_get_webhooks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/merchant_api_get_webhooks.c')
-rw-r--r--src/lib/merchant_api_get_webhooks.c129
1 files changed, 62 insertions, 67 deletions
diff --git a/src/lib/merchant_api_get_webhooks.c b/src/lib/merchant_api_get_webhooks.c
index f5ba6f41..e702baac 100644
--- a/src/lib/merchant_api_get_webhooks.c
+++ b/src/lib/merchant_api_get_webhooks.c
@@ -32,6 +32,11 @@
/**
+ * Maximum number of webhooks we return.
+ */
+#define MAX_WEBHOOKS 1024
+
+/**
* Handle for a GET /webhooks operation.
*/
struct TALER_MERCHANT_WebhooksGetHandle
@@ -68,53 +73,52 @@ struct TALER_MERCHANT_WebhooksGetHandle
* Parse webhook information from @a ia.
*
* @param ia JSON array (or NULL!) with webhook data
+ * @param[in] wgr partially filled webhook response
* @param wgh operation handle
* @return #GNUNET_OK on success
*/
-static int
+static enum GNUNET_GenericReturnValue
parse_webhooks (const json_t *ia,
+ struct TALER_MERCHANT_WebhooksGetResponse *wgr,
struct TALER_MERCHANT_WebhooksGetHandle *wgh)
{
- unsigned int ies_len = json_array_size (ia);
- struct TALER_MERCHANT_WebhookEntry ies[ies_len];
- size_t index;
- json_t *value;
- int ret;
-
- ret = GNUNET_OK;
- json_array_foreach (ia, index, value) {
- struct TALER_MERCHANT_WebhookEntry *ie = &ies[index];
- struct GNUNET_JSON_Specification spec[] = {
- GNUNET_JSON_spec_string ("webhook_id",
- &ie->webhook_id),
- GNUNET_JSON_spec_end ()
- };
-
- if (GNUNET_OK !=
- GNUNET_JSON_parse (value,
- spec,
- NULL, NULL))
- {
- GNUNET_break_op (0);
- ret = GNUNET_SYSERR;
- continue;
- }
- if (GNUNET_SYSERR == ret)
- break;
+ unsigned int whook_len = (unsigned int) json_array_size (ia);
+
+ if ( (json_array_size (ia) != (size_t) whook_len) ||
+ (whook_len > MAX_WEBHOOKS) )
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
}
- if (GNUNET_OK == ret)
{
- struct TALER_MERCHANT_HttpResponse hr = {
- .http_status = MHD_HTTP_OK
- };
+ struct TALER_MERCHANT_WebhookEntry whook[GNUNET_NZL (whook_len)];
+ size_t index;
+ json_t *value;
+
+ json_array_foreach (ia, index, value) {
+ struct TALER_MERCHANT_WebhookEntry *ie = &whook[index];
+ struct GNUNET_JSON_Specification spec[] = {
+ GNUNET_JSON_spec_string ("webhook_id",
+ &ie->webhook_id),
+ GNUNET_JSON_spec_end ()
+ };
+ if (GNUNET_OK !=
+ GNUNET_JSON_parse (value,
+ spec,
+ NULL, NULL))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ }
+ wgr->details.ok.webhooks_length = whook_len;
+ wgr->details.ok.webhooks = whook;
wgh->cb (wgh->cb_cls,
- &hr,
- ies_len,
- ies);
+ wgr);
wgh->cb = NULL; /* just to be sure */
}
- return ret;
+ return GNUNET_OK;
}
@@ -133,9 +137,9 @@ handle_get_webhooks_finished (void *cls,
{
struct TALER_MERCHANT_WebhooksGetHandle *wgh = cls;
const json_t *json = response;
- struct TALER_MERCHANT_HttpResponse hr = {
- .http_status = (unsigned int) response_code,
- .reply = json
+ struct TALER_MERCHANT_WebhooksGetResponse wgr = {
+ .hr.http_status = (unsigned int) response_code,
+ .hr.reply = json
};
wgh->job = NULL;
@@ -146,10 +150,10 @@ handle_get_webhooks_finished (void *cls,
{
case MHD_HTTP_OK:
{
- json_t *webhooks;
+ const json_t *webhooks;
struct GNUNET_JSON_Specification spec[] = {
- GNUNET_JSON_spec_json ("webhooks",
- &webhooks),
+ GNUNET_JSON_spec_array_const ("webhooks",
+ &webhooks),
GNUNET_JSON_spec_end ()
};
@@ -158,48 +162,39 @@ handle_get_webhooks_finished (void *cls,
spec,
NULL, NULL))
{
- hr.http_status = 0;
- hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
+ wgr.hr.http_status = 0;
+ wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
+ break;
}
- else
+ if (GNUNET_OK ==
+ parse_webhooks (webhooks,
+ &wgr,
+ wgh))
{
- if ( (! json_is_array (webhooks)) ||
- (GNUNET_OK ==
- parse_webhooks (webhooks,
- wgh)) )
- {
- GNUNET_JSON_parse_free (spec);
- TALER_MERCHANT_webhooks_get_cancel (wgh);
- return;
- }
- else
- {
- hr.http_status = 0;
- hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
- }
+ TALER_MERCHANT_webhooks_get_cancel (wgh);
+ return;
}
- GNUNET_JSON_parse_free (spec);
+ wgr.hr.http_status = 0;
+ wgr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
break;
}
case MHD_HTTP_UNAUTHORIZED:
- hr.ec = TALER_JSON_get_error_code (json);
- hr.hint = TALER_JSON_get_error_hint (json);
+ wgr.hr.ec = TALER_JSON_get_error_code (json);
+ wgr.hr.hint = TALER_JSON_get_error_hint (json);
/* Nothing really to verify, merchant says we need to authenticate. */
break;
default:
/* unexpected response code */
- hr.ec = TALER_JSON_get_error_code (json);
- hr.hint = TALER_JSON_get_error_hint (json);
+ wgr.hr.ec = TALER_JSON_get_error_code (json);
+ wgr.hr.hint = TALER_JSON_get_error_hint (json);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unexpected response code %u/%d\n",
(unsigned int) response_code,
- (int) hr.ec);
+ (int) wgr.hr.ec);
break;
}
wgh->cb (wgh->cb_cls,
- &hr,
- 0,
- NULL);
+ &wgr);
TALER_MERCHANT_webhooks_get_cancel (wgh);
}