From 45a0c893161d7d0abd950a303f6e2fdc6df880d4 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 11 Apr 2015 16:29:11 +0200 Subject: implement #3471 --- src/mint/taler-mint-httpd_parsing.c | 90 ++++++++++++++++++++++++++++++++++--- src/mint/taler-mint-httpd_parsing.h | 17 +++++++ 2 files changed, 102 insertions(+), 5 deletions(-) (limited to 'src/mint') 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; } @@ -890,6 +886,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. * 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 @@ -322,6 +322,23 @@ TMH_PARSE_amount_json (struct MHD_Connection *connection, struct TALER_Amount *amount); +/** + * 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. * -- cgit v1.2.3