/** * soup_uri_to_string: * @uri: a #SoupURI * @just_path_and_query: if %TRUE, output just the path and query portions * * Returns a string representing @uri. * * If @just_path_and_query is %TRUE, this concatenates the path and query * together. That is, it constructs the string that would be needed in * the Request-Line of an HTTP request for @uri. * * Return value: a string representing @uri, which the caller must free. **/ char * soup_uri_to_string (SoupURI *uri, gboolean just_path_and_query) { GString *str; char *return_result; g_return_val_if_fail (uri != NULL, NULL); g_warn_if_fail (SOUP_URI_IS_VALID (uri)); str = g_string_sized_new (20); if (uri->scheme && !just_path_and_query) g_string_append_printf (str, "%s:", uri->scheme); if (uri->host && !just_path_and_query) { g_string_append (str, "//"); if (uri->user) { append_uri_encoded (str, uri->user, ":;@?/"); g_string_append_c (str, '@'); } if (strchr (uri->host, ':')) { g_string_append_c (str, '['); g_string_append (str, uri->host); g_string_append_c (str, ']'); } else append_uri_encoded (str, uri->host, ":/"); if (uri->port && uri->port != soup_scheme_default_port (uri->scheme)) g_string_append_printf (str, ":%u", uri->port); if (!uri->path && (uri->query || uri->fragment)) g_string_append_c (str, '/'); else if ((!uri->path || !*uri->path) && (uri->scheme == SOUP_URI_SCHEME_HTTP || uri->scheme == SOUP_URI_SCHEME_HTTPS)) g_string_append_c (str, '/'); } if (uri->path && *uri->path) g_string_append (str, uri->path); else if (just_path_and_query) g_string_append_c (str, '/'); if (uri->query) { g_string_append_c (str, '?'); g_string_append (str, uri->query); } if (uri->fragment && !just_path_and_query) { g_string_append_c (str, '#'); g_string_append (str, uri->fragment); } return_result = str->str; g_string_free (str, FALSE); return return_result; }
/** * soup_uri_to_string: * @uri: a #SoupURI * @just_path_and_query: if %TRUE, output just the path and query portions * * Returns a string representing @uri. * * If @just_path_and_query is %TRUE, this concatenates the path and query * together. That is, it constructs the string that would be needed in * the Request-Line of an HTTP request for @uri. * * Return value: a string representing @uri, which the caller must free. **/ char * soup_uri_to_string (SoupURI *uri, gboolean just_path_and_query) { GString *str; char *return_result; g_return_val_if_fail (uri != NULL, NULL); /* IF YOU CHANGE ANYTHING IN THIS FUNCTION, RUN * tests/uri-parsing AFTERWARD. */ str = g_string_sized_new (20); if (uri->scheme && !just_path_and_query) g_string_append_printf (str, "%s:", uri->scheme); if (uri->host && !just_path_and_query) { g_string_append (str, "//"); if (uri->user) { append_uri_encoded (str, uri->user, ":;@?/"); g_string_append_c (str, '@'); } if (strchr (uri->host, ':')) { g_string_append_c (str, '['); g_string_append (str, uri->host); g_string_append_c (str, ']'); } else append_uri_encoded (str, uri->host, ":/"); if (uri->port && uri->port != soup_scheme_default_port (uri->scheme)) g_string_append_printf (str, ":%d", uri->port); if (!uri->path && (uri->query || uri->fragment)) g_string_append_c (str, '/'); } if (uri->path && *uri->path) g_string_append (str, uri->path); if (uri->query) { g_string_append_c (str, '?'); g_string_append (str, uri->query); } if (uri->fragment && !just_path_and_query) { g_string_append_c (str, '#'); g_string_append (str, uri->fragment); } return_result = str->str; g_string_free (str, FALSE); return return_result; }
/** * soup_uri_encode: * @part: a URI part * @escape_extra: additional reserved characters to escape (or %NULL) * * This %<!-- -->-encodes the given URI part and returns the escaped * version in allocated memory, which the caller must free when it is * done. * * Return value: the encoded URI part **/ char * soup_uri_encode (const char *part, const char *escape_extra) { GString *str; char *encoded; str = g_string_new (NULL); append_uri_encoded (str, part, escape_extra); encoded = str->str; g_string_free (str, FALSE); return encoded; }
char * soup_uri_to_string_internal (SoupURI *uri, gboolean just_path_and_query, gboolean include_password, gboolean force_port) { GString *str; char *return_result; g_return_val_if_fail (uri != NULL, NULL); g_warn_if_fail (SOUP_URI_IS_VALID (uri)); str = g_string_sized_new (40); if (uri->scheme && !just_path_and_query) g_string_append_printf (str, "%s:", uri->scheme); if (uri->host && !just_path_and_query) { g_string_append (str, "//"); if (uri->user) { append_uri_encoded (str, uri->user, ":;@?/"); if (uri->password && include_password) { g_string_append_c (str, ':'); append_uri_encoded (str, uri->password, ";@?/"); } g_string_append_c (str, '@'); } if (strchr (uri->host, ':')) { const char *pct; g_string_append_c (str, '['); pct = strchr (uri->host, '%'); if (pct) { g_string_append_printf (str, "%.*s%%25%s", (int) (pct - uri->host), uri->host, pct + 1); } else g_string_append (str, uri->host); g_string_append_c (str, ']'); } else append_uri_encoded (str, uri->host, ":/"); if (uri->port && (force_port || uri->port != soup_scheme_default_port (uri->scheme))) g_string_append_printf (str, ":%u", uri->port); if (!uri->path && (uri->query || uri->fragment)) g_string_append_c (str, '/'); else if ((!uri->path || !*uri->path) && (uri->scheme == SOUP_URI_SCHEME_HTTP || uri->scheme == SOUP_URI_SCHEME_HTTPS)) g_string_append_c (str, '/'); } if (uri->path && *uri->path) g_string_append (str, uri->path); else if (just_path_and_query) g_string_append_c (str, '/'); if (uri->query) { g_string_append_c (str, '?'); g_string_append (str, uri->query); } if (uri->fragment && !just_path_and_query) { g_string_append_c (str, '#'); g_string_append (str, uri->fragment); } return_result = str->str; g_string_free (str, FALSE); return return_result; }