diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2019-04-07 23:46:33 +0200 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2019-04-08 02:37:45 +0200 |
commit | 3c66bb30b9ad56e934c364379095ec040b9e36e6 (patch) | |
tree | bd72711e9eb5e71cf96639936e08cdeef9932410 | |
parent | eca27d9d913378e2356a6fdc51e95e6ff3f4d7bb (diff) |
Gather /history implementation(s) in one point.
-rw-r--r-- | src/bank-lib/fakebank.c | 520 |
1 files changed, 267 insertions, 253 deletions
diff --git a/src/bank-lib/fakebank.c b/src/bank-lib/fakebank.c index 0ade934a5..9d73b7f04 100644 --- a/src/bank-lib/fakebank.c +++ b/src/bank-lib/fakebank.c @@ -763,10 +763,11 @@ handle_reject (struct TALER_FAKEBANK_Handle *h, json_decref (json); if (GNUNET_OK != found) - return create_bank_error (connection, - MHD_HTTP_NOT_FOUND, - TALER_EC_BANK_REJECT_TRANSACTION_NOT_FOUND, - "transaction unknown"); + return create_bank_error + (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_BANK_REJECT_TRANSACTION_NOT_FOUND, + "transaction unknown"); /* finally build regular response */ resp = MHD_create_response_from_buffer (0, NULL, @@ -778,253 +779,9 @@ handle_reject (struct TALER_FAKEBANK_Handle *h, return ret; } - -/** - * Parse URL history arguments, of _both_ APIs: - * /history and /history-range. - * - * @param connection MHD connection. - * @param function_name name of the caller. - * @param ha[out] will contain the parsed values. - * @return GNUNET_OK only if the parsing succeedes. - */ -static int -parse_history_common_args (struct MHD_Connection *connection, - struct HistoryArgs *ha) -{ - /** - * @variable - * Just check if given and == "basic", no need to keep around. - */ - const char *auth; - - /** - * All those will go into the structure, after parsing. - */ - const char *direction; - const char *cancelled; - const char *ordering; - const char *account_number; - - - auth = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "auth"); - direction = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "direction"); - cancelled = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "cancelled"); - ordering = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "ordering"); - account_number = MHD_lookup_connection_value - (connection, - MHD_GET_ARGUMENT_KIND, - "account_number"); - - /* Fail if one of the above failed. */ - if ( (NULL == direction) || - (NULL == cancelled) || - ( (0 != strcasecmp (cancelled, - "OMIT")) && - (0 != strcasecmp (cancelled, - "SHOW")) ) || - ( (0 != strcasecmp (direction, - "BOTH")) && - (0 != strcasecmp (direction, - "CREDIT")) && - (0 != strcasecmp (direction, - "DEBIT")) ) || - (1 != sscanf (account_number, - "%llu", - &ha->account_number)) || - ( (NULL == auth) || (0 != strcasecmp (auth, - "basic")) ) ) - { - /* Invalid request, given that this is fakebank we impolitely - * just kill the connection instead of returning a nice error. - */ - GNUNET_break (0); - return GNUNET_NO; - } - - if (0 == strcasecmp (direction, - "CREDIT")) - { - ha->direction = TALER_BANK_DIRECTION_CREDIT; - } - else if (0 == strcasecmp (direction, - "DEBIT")) - { - ha->direction = TALER_BANK_DIRECTION_DEBIT; - } - else if (0 == strcasecmp (direction, - "BOTH")) - { - ha->direction = TALER_BANK_DIRECTION_BOTH; - } - - /* Direction is invalid. */ - else - { - GNUNET_break (0); - return GNUNET_NO; - } - - if (0 == strcasecmp (cancelled, - "OMIT")) - { - /* nothing */ - } else if (0 == strcasecmp (cancelled, - "SHOW")) - { - ha->direction |= TALER_BANK_DIRECTION_CANCEL; - } - - /* Cancel-showing policy is invalid. */ - else - { - GNUNET_break (0); - return GNUNET_NO; - } - - if ((NULL != ordering) - && 0 == strcmp ("ascending", - ordering)) - ha->ascending = GNUNET_YES; - else - ha->ascending = GNUNET_NO; - - return GNUNET_OK; -} - -/** - * Handle incoming HTTP request for /history - * - * @param h the fakebank handle - * @param connection the connection - * @param con_cls place to store state, not used - * @return MHD result code - */ -static int -handle_history_new (struct TALER_FAKEBANK_Handle *h, - struct MHD_Connection *connection, - void **con_cls) -{ - struct HistoryArgs ha; - struct HistoryRangeIds hri; - const char *start; - const char *delta; - struct Transaction *pos; - - if (GNUNET_OK != parse_history_common_args (connection, - &ha)) - { - GNUNET_break (0); - return MHD_NO; - } - - start = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "start"); - delta = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "delta"); - if ( ((NULL != start) && (1 != sscanf (start, - "%llu", - &hri.start))) || - (NULL == delta) || (1 != sscanf (delta, - "%lld", - &hri.count)) ) - { - GNUNET_break (0); - return MHD_NO; - } - - if (NULL == start) - pos = 0 > hri.count ? - h->transactions_tail : h->transactions_head; - - else if (NULL != h->transactions_head) - { - for (pos = h->transactions_head; - NULL != pos; - pos = pos->next) - if (pos->row_id == hri.start) - break; - if (NULL == pos) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Invalid range specified," - " transaction %llu not known!\n", - (unsigned long long) hri.start); - return MHD_NO; - } - /* range is exclusive, skip the matching entry */ - if (hri.count > 0) - pos = pos->next; - if (hri.count < 0) - pos = pos->prev; - } - else - { - /* list is empty */ - pos = NULL; - } - - /* Loop starts here. */ -} - -/** - * Handle incoming HTTP request for /history-range. - * - * @param h the fakebank handle - * @param connection the connection - * @param con_cls place to store state, not used - * @return MHD result code - */ -static int -handle_history_range (struct TALER_FAKEBANK_Handle *h, - struct MHD_Connection *connection, - void **con_cls) -{ - - struct HistoryArgs ha; - struct HistoryRangeDates hrd; - const char *start; - const char *end; - long long unsigned int start_stamp; - long long unsigned int end_stamp; - - if (GNUNET_OK != parse_history_common_args (connection, - &ha)) - { - GNUNET_break (0); - return MHD_NO; - } - start = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "start"); - end = MHD_lookup_connection_value (connection, - MHD_GET_ARGUMENT_KIND, - "end"); - - if ( (NULL == start) || (1 != sscanf (start, - "%llu", - &start_stamp)) || - (NULL == end) || (1 != sscanf (end, - "%lld", - &end_stamp)) ) - { - GNUNET_break (0); - return GNUNET_NO; - } - - hrd.start.abs_value_us = start_stamp * 1000LL * 1000LL; - hrd.end.abs_value_us = end_stamp * 1000LL * 1000LL; -} +/*********************************** + * Serving "/history" starts here. * + ***********************************/ /** @@ -1075,7 +832,7 @@ handle_history_skip (const struct HistoryArgs *ha, * @return the next element to be processed. */ static struct Transaction * -handle_history_step (struct HistoryArgs *ha, +handle_history_step (const struct HistoryArgs *ha, const struct Transaction *pos) { struct HistoryRangeIds *hri = ha->range; @@ -1136,6 +893,7 @@ handle_history_range_step (const struct HistoryArgs *ha) const struct HistoryRangeDates *hrd = ha->range; } + /** * Actual history response builder. * @@ -1145,7 +903,7 @@ handle_history_range_step (const struct HistoryArgs *ha) * @return MHD_YES / MHD_NO, after having enqueued the response * object into MHD. */ -static struct MHD_response * +static int build_history_response (struct MHD_Connection *connection, struct Transaction *pos, struct HistoryArgs *ha, @@ -1297,6 +1055,259 @@ build_history_response (struct MHD_Connection *connection, } + +/** + * Parse URL history arguments, of _both_ APIs: + * /history and /history-range. + * + * @param connection MHD connection. + * @param function_name name of the caller. + * @param ha[out] will contain the parsed values. + * @return GNUNET_OK only if the parsing succeedes. + */ +static int +parse_history_common_args (struct MHD_Connection *connection, + struct HistoryArgs *ha) +{ + /** + * @variable + * Just check if given and == "basic", no need to keep around. + */ + const char *auth; + + /** + * All those will go into the structure, after parsing. + */ + const char *direction; + const char *cancelled; + const char *ordering; + const char *account_number; + + + auth = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "auth"); + direction = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "direction"); + cancelled = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "cancelled"); + ordering = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "ordering"); + account_number = MHD_lookup_connection_value + (connection, + MHD_GET_ARGUMENT_KIND, + "account_number"); + + /* Fail if one of the above failed. */ + if ( (NULL == direction) || + (NULL == cancelled) || + ( (0 != strcasecmp (cancelled, + "OMIT")) && + (0 != strcasecmp (cancelled, + "SHOW")) ) || + ( (0 != strcasecmp (direction, + "BOTH")) && + (0 != strcasecmp (direction, + "CREDIT")) && + (0 != strcasecmp (direction, + "DEBIT")) ) || + (1 != sscanf (account_number, + "%llu", + &ha->account_number)) || + ( (NULL == auth) || (0 != strcasecmp (auth, + "basic")) ) ) + { + /* Invalid request, given that this is fakebank we impolitely + * just kill the connection instead of returning a nice error. + */ + GNUNET_break (0); + return GNUNET_NO; + } + + if (0 == strcasecmp (direction, + "CREDIT")) + { + ha->direction = TALER_BANK_DIRECTION_CREDIT; + } + else if (0 == strcasecmp (direction, + "DEBIT")) + { + ha->direction = TALER_BANK_DIRECTION_DEBIT; + } + else if (0 == strcasecmp (direction, + "BOTH")) + { + ha->direction = TALER_BANK_DIRECTION_BOTH; + } + + /* Direction is invalid. */ + else + { + GNUNET_break (0); + return GNUNET_NO; + } + + if (0 == strcasecmp (cancelled, + "OMIT")) + { + /* nothing */ + } else if (0 == strcasecmp (cancelled, + "SHOW")) + { + ha->direction |= TALER_BANK_DIRECTION_CANCEL; + } + + /* Cancel-showing policy is invalid. */ + else + { + GNUNET_break (0); + return GNUNET_NO; + } + + if ((NULL != ordering) + && 0 == strcmp ("ascending", + ordering)) + ha->ascending = GNUNET_YES; + else + ha->ascending = GNUNET_NO; + + return GNUNET_OK; +} + +/** + * Handle incoming HTTP request for /history + * + * @param h the fakebank handle + * @param connection the connection + * @param con_cls place to store state, not used + * @return MHD result code + */ +static int +handle_history_new (struct TALER_FAKEBANK_Handle *h, + struct MHD_Connection *connection, + void **con_cls) +{ + struct HistoryArgs ha; + struct HistoryRangeIds hri; + const char *start; + const char *delta; + struct Transaction *pos; + + if (GNUNET_OK != parse_history_common_args (connection, + &ha)) + { + GNUNET_break (0); + return MHD_NO; + } + + start = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "start"); + delta = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "delta"); + if ( ((NULL != start) && (1 != sscanf (start, + "%llu", + &hri.start))) || + (NULL == delta) || (1 != sscanf (delta, + "%lld", + &hri.count)) ) + { + GNUNET_break (0); + return MHD_NO; + } + + if (NULL == start) + pos = 0 > hri.count ? + h->transactions_tail : h->transactions_head; + + else if (NULL != h->transactions_head) + { + for (pos = h->transactions_head; + NULL != pos; + pos = pos->next) + if (pos->row_id == hri.start) + break; + if (NULL == pos) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Invalid range specified," + " transaction %llu not known!\n", + (unsigned long long) hri.start); + return MHD_NO; + } + /* range is exclusive, skip the matching entry */ + if (hri.count > 0) + pos = pos->next; + if (hri.count < 0) + pos = pos->prev; + } + else + { + /* list is empty */ + pos = NULL; + } + + return build_history_response (connection, + pos, + &ha, + &handle_history_skip, + &handle_history_step, + &handle_history_advance); +} + +/** + * Handle incoming HTTP request for /history-range. + * + * @param h the fakebank handle + * @param connection the connection + * @param con_cls place to store state, not used + * @return MHD result code + */ +static int +handle_history_range (struct TALER_FAKEBANK_Handle *h, + struct MHD_Connection *connection, + void **con_cls) +{ + + struct HistoryArgs ha; + struct HistoryRangeDates hrd; + const char *start; + const char *end; + long long unsigned int start_stamp; + long long unsigned int end_stamp; + + if (GNUNET_OK != parse_history_common_args (connection, + &ha)) + { + GNUNET_break (0); + return MHD_NO; + } + start = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "start"); + end = MHD_lookup_connection_value (connection, + MHD_GET_ARGUMENT_KIND, + "end"); + + if ( (NULL == start) || (1 != sscanf (start, + "%llu", + &start_stamp)) || + (NULL == end) || (1 != sscanf (end, + "%lld", + &end_stamp)) ) + { + GNUNET_break (0); + return GNUNET_NO; + } + + hrd.start.abs_value_us = start_stamp * 1000LL * 1000LL; + hrd.end.abs_value_us = end_stamp * 1000LL * 1000LL; +} + /** * Handle incoming HTTP request for /history * @@ -1615,6 +1626,9 @@ handle_history (struct TALER_FAKEBANK_Handle *h, return ret; } +/*********************************** + * End of /history implementation. * + ***********************************/ /** * Handle incoming HTTP request. |