diff options
| author | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2009-04-22 23:49:19 +0000 |
|---|---|---|
| committer | Benjamin Dauvergne <bdauvergne@entrouvert.com> | 2009-04-22 23:49:19 +0000 |
| commit | ed5e0fce1583d3e73ce0dcb7187c6aa541fbf97d (patch) | |
| tree | 6500bee501c53c1984e83daf35828483b9c1cfb2 | |
| parent | 50ea06e0f769418c189469d7f164117c9d1db366 (diff) | |
Add a new internal API for parameters building
* xml/tools.c:
add lasso_url_add_parameter that concat the string &key=value to an
existing URL where key and value are url-encoded.
* xml/private.h:
declare lasso_url_add_parameter.
| -rw-r--r-- | lasso/xml/private.h | 1 | ||||
| -rw-r--r-- | lasso/xml/tools.c | 64 |
2 files changed, 65 insertions, 0 deletions
diff --git a/lasso/xml/private.h b/lasso/xml/private.h index de67fb7c..d96da366 100644 --- a/lasso/xml/private.h +++ b/lasso/xml/private.h @@ -213,6 +213,7 @@ static inline void message(GLogLevelFlags level, const char *format, ...) LASSO_PROTOCOL_SAML_2_0) char * lasso_get_relaystate_from_query(const char *query); +char * lasso_url_add_parameters(char *url, gboolean free, ...); #ifdef __cplusplus } diff --git a/lasso/xml/tools.c b/lasso/xml/tools.c index adc6ce54..2b18ff40 100644 --- a/lasso/xml/tools.c +++ b/lasso/xml/tools.c @@ -26,6 +26,7 @@ #include <string.h> #include <time.h> #include <ctype.h> +#include <stdarg.h> #include <libxml/uri.h> #include <libxml/parser.h> @@ -1491,3 +1492,66 @@ lasso_get_relaystate_from_query(const char *query) { } return result; } + +/** + * lasso_url_add_parameters: + * @url: the original URL + * @free: whether to free the URL parameter + * @...: pairs of strings, key, value, followed by NULL + * + * Iterate over all pairs of key,value, and concatenate them to @url encoded as "&key=value", where + * key and value are url-encoded. + * If free is true and at least one pair was given, url is freed. If url is NULL, the first + * ampersand is omitted. + * + * Return value: a newly allocated string, or url. + */ +char* +lasso_url_add_parameters(char *url, + gboolean free, ...) +{ + char *old_url = url, *new_url; + xmlChar *encoded_key, *encoded_value; + int rc = 0; + va_list ap; + + va_start(ap, free); + + while (1) { + char *key; + char *value; + + key = va_arg(ap, char*); + if (! key) { + break; + } + encoded_key = xmlURIEscapeStr((xmlChar*)key, NULL); + goto_exit_if_fail(encoded_key, 0); + + value = va_arg(ap, char*); + if (! value) { + message(G_LOG_LEVEL_CRITICAL, "lasso_url_add_parameter: key without a value !!"); + break; + } + encoded_value = xmlURIEscapeStr((xmlChar*)value, NULL); + goto_exit_if_fail(encoded_value, 0); + + if (old_url) { + new_url = g_strdup_printf("%s&%s=%s", old_url, (char*)encoded_key, (char*)encoded_value); + } else { + new_url = g_strdup_printf("%s=%s", (char*)encoded_key, (char*)encoded_value); + } + old_url = new_url; + + lasso_release_xml_string(encoded_key); + lasso_release_xml_string(encoded_value); + } +exit: + va_end(ap); + if (free && new_url != url) { + lasso_release(url); + } + lasso_release_xml_string(encoded_key); + + return new_url; +} |
