From 1d9049ab9963b69a6716943109ec0a7458135d1a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 14 Jan 2020 23:54:43 +0100 Subject: add payto URL test, fix payto parser implementation --- doc/prebuilt | 2 +- src/util/.gitignore | 1 + src/util/test_payto.c | 44 ++++++++++++++++++++++++- src/util/util.c | 89 ++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 115 insertions(+), 21 deletions(-) diff --git a/doc/prebuilt b/doc/prebuilt index ca53235cc..934a6a183 160000 --- a/doc/prebuilt +++ b/doc/prebuilt @@ -1 +1 @@ -Subproject commit ca53235ccfa0458ebf11c204888ca370e20ec3f5 +Subproject commit 934a6a18301e81c4fd1b3a8cda2dc13dca4741cc diff --git a/src/util/.gitignore b/src/util/.gitignore index ec61c9e0c..648b4b116 100644 --- a/src/util/.gitignore +++ b/src/util/.gitignore @@ -1 +1,2 @@ taler-config +test_payto diff --git a/src/util/test_payto.c b/src/util/test_payto.c index 80bf652e0..f966e046f 100644 --- a/src/util/test_payto.c +++ b/src/util/test_payto.c @@ -21,13 +21,55 @@ #include "platform.h" #include "taler_util.h" +#define CHECK(a,b) do { \ + if (0 != strcmp (a,b)) { \ + GNUNET_break (0); \ + fprintf (stderr, "Got %s, wanted %s\n", b, a); \ + GNUNET_free (b); \ + return 1; \ + } else { \ + GNUNET_free (b); \ + } \ +} while (0) + int main (int argc, const char *const argv[]) { + char *r; - + GNUNET_log_setup ("test-payto", + "WARNING", + NULL); + r = TALER_payto_xtalerbank_make ("https://localhost/", + "account"); + CHECK ("payto://x-taler-bank/localhost/account", + r); + r = TALER_payto_xtalerbank_make ("http://localhost:80/", + "account"); + CHECK ("payto://x-taler-bank/localhost:80/account", + r); + r = TALER_payto_xtalerbank_make ("http://localhost/", + "account"); + CHECK ("payto://x-taler-bank/localhost:80/account", + r); + r = TALER_xtalerbank_base_url_from_payto ( + "payto://x-taler-bank/localhost/bob"); + CHECK ("https://localhost/", + r); + r = TALER_xtalerbank_base_url_from_payto ( + "payto://x-taler-bank/localhost:1080/bob"); + CHECK ("http://localhost:1080/", + r); + r = TALER_xtalerbank_account_url_from_payto ( + "payto://x-taler-bank/localhost/bob"); + CHECK ("https://localhost/bob", + r); + r = TALER_xtalerbank_account_url_from_payto ( + "payto://x-taler-bank/localhost:1080/alice"); + CHECK ("http://localhost:1080/alice", + r); return 0; } diff --git a/src/util/util.c b/src/util/util.c index 62f733a17..5a06a1229 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -789,16 +789,38 @@ TALER_payto_xtalerbank_make (const char *bank_url, const char *account_name) { char *payto; - int ends_slash; + int plaintext; + const char *port; + size_t slen; - if (0 < strlen (bank_url)) - ends_slash = '/' == bank_url[strlen (bank_url) - 1]; + if (0 == strncasecmp ("https://", + bank_url, + strlen ("https://"))) + { + bank_url += strlen ("https://"); + plaintext = GNUNET_NO; + } + else if (0 == strncasecmp ("http://", + bank_url, + strlen ("http://"))) + { + bank_url += strlen ("http://"); + plaintext = GNUNET_YES; + } else - ends_slash = 0; + return NULL; + slen = strlen (bank_url); + port = memchr (bank_url, + ':', + slen); + if ( (0 < slen) && + ('/' == bank_url[slen - 1]) ) + slen--; GNUNET_asprintf (&payto, - (ends_slash) - ? "payto://x-taler-bank/%s%s" - : "payto://x-taler-bank/%s/%s", + ( (NULL == port) && (GNUNET_YES == plaintext) ) + ? "payto://x-taler-bank/%.*s:80/%s" + : "payto://x-taler-bank/%.*s/%s", + (int) slen, bank_url, account_name); return payto; @@ -817,20 +839,35 @@ TALER_xtalerbank_base_url_from_payto (const char *payto) { const char *start; const char *end; + char *ret; + int https; + const char *colon; + unsigned int port; if (0 != strncasecmp (payto, "payto://x-taler-bank/", strlen ("payto://x-taler-bank/"))) - { return NULL; - } - start = &payto [strlen ("payto://x-taler-bank/")]; + start = &payto[strlen ("payto://x-taler-bank/")]; end = strchr (start, - (unsigned char) '/'); + '/'); if (NULL == end) end = &start[strlen (start)]; - return GNUNET_strndup (start, - end - start); + colon = strrchr (start, + ':'); + https = GNUNET_YES; + if ( (NULL != colon) && + (1 == sscanf (colon + 1, + "%u", + &port)) && + (443 != port) ) + https = GNUNET_NO; + GNUNET_asprintf (&ret, + "%s://%.*s/", + (https ? "https" : "http"), + (int) (end - start), + start); + return ret; } @@ -846,20 +883,34 @@ TALER_xtalerbank_account_url_from_payto (const char *payto) { const char *start; const char *end; + char *ret; + int https; + const char *colon; + unsigned int port; if (0 != strncasecmp (payto, "payto://x-taler-bank/", strlen ("payto://x-taler-bank/"))) - { return NULL; - } - start = &payto [strlen ("payto://x-taler-bank/")]; + start = &payto[strlen ("payto://x-taler-bank/")]; end = strchr (start, - (unsigned char) '?'); + '/'); if (NULL == end) end = &start[strlen (start)]; - return GNUNET_strndup (start, - end - start); + colon = strrchr (start, + ':'); + https = GNUNET_YES; + if ( (NULL != colon) && + (1 == sscanf (colon + 1, + "%u", + &port)) && + (443 != port) ) + https = GNUNET_NO; + GNUNET_asprintf (&ret, + "%s://%s", + (https ? "https" : "http"), + start); + return ret; } -- cgit v1.2.3