diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-04-11 16:29:11 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-04-11 16:29:11 +0200 |
commit | 45a0c893161d7d0abd950a303f6e2fdc6df880d4 (patch) | |
tree | b2ce4d0ef0bb81d4bcfb337269d389a92759b4b9 /src/mint | |
parent | 5f879c0b4bfe92968ab5c3fd38b5ba69177c0ef7 (diff) |
implement #3471
Diffstat (limited to 'src/mint')
-rw-r--r-- | src/mint/taler-mint-httpd_parsing.c | 90 | ||||
-rw-r--r-- | src/mint/taler-mint-httpd_parsing.h | 17 |
2 files changed, 102 insertions, 5 deletions
diff --git a/src/mint/taler-mint-httpd_parsing.c b/src/mint/taler-mint-httpd_parsing.c index 7f08d76e0..ad307dd45 100644 --- a/src/mint/taler-mint-httpd_parsing.c +++ b/src/mint/taler-mint-httpd_parsing.c @@ -673,14 +673,10 @@ TMH_PARSE_navigate_json (struct MHD_Connection *connection, case TMH_PARSE_JNC_RET_TIME_ABSOLUTE: { struct GNUNET_TIME_Absolute *where = va_arg (argp, void *); -#if 0 + ret = TMH_PARSE_time_abs_json (connection, (json_t *) root, where); -#endif - GNUNET_assert (0); /* FIXME: not implemented (#3741) */ - (void) where; - ret = GNUNET_SYSERR; break; } @@ -891,6 +887,90 @@ TMH_PARSE_release_data (struct TMH_PARSE_FieldSpecification *spec) /** + * Parse absolute time specified in JSON format. + * + * @param connection the MHD connection (to report errors) + * @param f json specification of the amount + * @param[out] time set to the time specified in @a f + * @return + * #GNUNET_YES if parsing was successful + * #GNUNET_NO if json is malformed, error response was generated + * #GNUNET_SYSERR on internal error, error response was not generated + */ +int +TMH_PARSE_time_abs_json (struct MHD_Connection *connection, + json_t *f, + struct GNUNET_TIME_Absolute *time) +{ + const char *val; + size_t slen; + unsigned long long int tval; + char *endp; + + val = json_string_value (f); + if (NULL == val) + { + if (MHD_YES != + TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_BAD_REQUEST, + "{s:s}", + "error", "string expected")) + return GNUNET_SYSERR; + return GNUNET_NO; + } + slen = strlen (val); + if ( (slen <= 2) || + ('/' != val[0]) || + ('/' != val[slen - 1]) ) + { + if (MHD_YES != + TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:s}", + "error", "timestamp expected", + "value", val)) + return GNUNET_SYSERR; + return GNUNET_NO; + } + if (0 == strcasecmp (val, + "/forever/")) + { + *time = GNUNET_TIME_UNIT_FOREVER_ABS; + return GNUNET_OK; + } + tval = strtoull (&val[1], + &endp, + 10); + if (&val[slen - 1] != endp) + { + if (MHD_YES != + TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:s}", + "error", "timestamp expected", + "value", val)) + return GNUNET_SYSERR; + return GNUNET_NO; + } + /* Time is in 'ms' in JSON, but in microseconds in GNUNET_TIME_Absolute */ + time->abs_value_us = tval * 1000LL; + if ( (time->abs_value_us) / 1000LL != tval) + { + /* Integer overflow */ + if (MHD_YES != + TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:s}", + "error", "timestamp outside of legal range", + "value", val)) + return GNUNET_SYSERR; + return GNUNET_NO; + } + return GNUNET_OK; +} + + +/** * Parse amount specified in JSON format. * * @param connection the MHD connection (to report errors) diff --git a/src/mint/taler-mint-httpd_parsing.h b/src/mint/taler-mint-httpd_parsing.h index feac6087d..0a61d0770 100644 --- a/src/mint/taler-mint-httpd_parsing.h +++ b/src/mint/taler-mint-httpd_parsing.h @@ -323,6 +323,23 @@ TMH_PARSE_amount_json (struct MHD_Connection *connection, /** + * Parse absolute time specified in JSON format. + * + * @param connection the MHD connection (to report errors) + * @param f json specification of the amount + * @param[out] time set to the time specified in @a f + * @return + * #GNUNET_YES if parsing was successful + * #GNUNET_NO if json is malformed, error response was generated + * #GNUNET_SYSERR on internal error, error response was not generated + */ +int +TMH_PARSE_time_abs_json (struct MHD_Connection *connection, + json_t *f, + struct GNUNET_TIME_Absolute *time); + + +/** * Extraxt fixed-size base32crockford encoded data from request. * * Queues an error response to the connection if the parameter is missing or |