diff options
Diffstat (limited to 'src/util/url.c')
-rw-r--r-- | src/util/url.c | 161 |
1 files changed, 74 insertions, 87 deletions
diff --git a/src/util/url.c b/src/util/url.c index f2298c0f1..bf59ba6ec 100644 --- a/src/util/url.c +++ b/src/util/url.c @@ -27,8 +27,8 @@ * be urlencoded. * * @param c character to look at - * @return #GNUNET_YES if @a c needs to be urlencoded, - * #GNUNET_NO otherwise + * @return true if @a c needs to be urlencoded, + * false otherwise */ static bool is_reserved (char c) @@ -48,11 +48,11 @@ is_reserved (char c) case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '-': case '.': case '_': case '~': - return GNUNET_NO; + return false; default: break; } - return GNUNET_YES; + return true; } @@ -68,7 +68,7 @@ urlencode_len (const char *s) { size_t len = 0; for (; *s != '\0'; len++, s++) - if (GNUNET_YES == is_reserved (*s)) + if (is_reserved (*s)) len += 2; return len; } @@ -102,12 +102,6 @@ buffer_write_urlencode (struct GNUNET_Buffer *buf, } -/** - * URL-encode a string according to rfc3986. - * - * @param s string to encode - * @returns the urlencoded string, the caller must free it with #GNUNET_free() - */ char * TALER_urlencode (const char *s) { @@ -212,89 +206,65 @@ serialize_arguments (struct GNUNET_Buffer *buf, } -/** - * Make an absolute URL with query parameters. - * - * If a 'value' is given as NULL, both the key and the value are skipped. Note - * that a NULL value does not terminate the list, only a NULL key signals the - * end of the list of arguments. - * - * @param base_url absolute base URL to use - * @param path path of the url - * @param ... NULL-terminated key-value pairs (char *) for query parameters, - * the value will be url-encoded - * @returns the URL (must be freed with #GNUNET_free) or - * NULL if an error occurred. - */ char * TALER_url_join (const char *base_url, const char *path, ...) { struct GNUNET_Buffer buf = { 0 }; - va_list args; - size_t len; GNUNET_assert (NULL != base_url); GNUNET_assert (NULL != path); if (0 == strlen (base_url)) { /* base URL can't be empty */ - GNUNET_break (0); - return NULL; - } - if ('/' != base_url[strlen (base_url) - 1]) - { - /* Must be an actual base URL! */ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Base URL `%s' does not end with '/'\n", - base_url); + "Empty base URL specified\n"); return NULL; } - if ('/' == path[0]) + if ('\0' != path[0]) { - /* The path must be relative. */ - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Path `%s' is not relative\n", - path); - return NULL; + if ('/' != base_url[strlen (base_url) - 1]) + { + /* Must be an actual base URL! */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Base URL `%s' does not end with '/', cannot join with `%s'\n", + base_url, + path); + return NULL; + } + if ('/' == path[0]) + { + /* The path must be relative. */ + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Path `%s' is not relative\n", + path); + return NULL; + } } - va_start (args, - path); - - len = strlen (base_url) + strlen (path) + 1; - len += calculate_argument_length (args); - - GNUNET_buffer_prealloc (&buf, - len); - GNUNET_buffer_write_str (&buf, - base_url); - GNUNET_buffer_write_str (&buf, - path); - serialize_arguments (&buf, - args); - va_end (args); - + { + va_list args; + size_t len; + + va_start (args, + path); + len = strlen (base_url) + strlen (path) + 1; + len += calculate_argument_length (args); + GNUNET_buffer_prealloc (&buf, + len); + GNUNET_buffer_write_str (&buf, + base_url); + GNUNET_buffer_write_str (&buf, + path); + serialize_arguments (&buf, + args); + va_end (args); + } return GNUNET_buffer_reap_str (&buf); } -/** - * Make an absolute URL for the given parameters. - * - * If a 'value' is given as NULL, both the key and the value are skipped. Note - * that a NULL value does not terminate the list, only a NULL key signals the - * end of the list of arguments. - * - * @param proto protocol for the URL (typically https) - * @param host hostname for the URL - * @param prefix prefix for the URL - * @param path path for the URL - * @param args NULL-terminated key-value pairs (char *) for query parameters, - * the value will be url-encoded - * @returns the URL, must be freed with #GNUNET_free - */ char * TALER_url_absolute_raw_va (const char *proto, const char *host, @@ -327,21 +297,6 @@ TALER_url_absolute_raw_va (const char *proto, } -/** - * Make an absolute URL for the given parameters. - * - * If a 'value' is given as NULL, both the key and the value are skipped. Note - * that a NULL value does not terminate the list, only a NULL key signals the - * end of the list of arguments. - * - * @param proto protocol for the URL (typically https) - * @param host hostname for the URL - * @param prefix prefix for the URL - * @param path path for the URL - * @param ... NULL-terminated key-value pairs (char *) for query parameters, - * the value will be url-encoded - * @return the URL, must be freed with #GNUNET_free - */ char * TALER_url_absolute_raw (const char *proto, const char *host, @@ -364,4 +319,36 @@ TALER_url_absolute_raw (const char *proto, } +bool +TALER_url_valid_charset (const char *url) +{ + for (unsigned int i = 0; '\0' != url[i]; i++) + { +#define ALLOWED_CHARACTERS \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/:;&?-.,=_~%+#" + if (NULL == strchr (ALLOWED_CHARACTERS, + (int) url[i])) + return false; +#undef ALLOWED_CHARACTERS + } + return true; +} + + +bool +TALER_is_web_url (const char *url) +{ + if ( (0 != strncasecmp (url, + "https://", + strlen ("https://"))) && + (0 != strncasecmp (url, + "http://", + strlen ("http://"))) ) + return false; + if (! TALER_url_valid_charset (url) ) + return false; + return true; +} + + /* end of url.c */ |