guri: new URI parsing and generating functions

Add a set of new URI parsing and generating functions, including a new
parsed-URI type GUri. Move all the code from gurifuncs.c into guri.c,
reimplementing some of those functions (and
g_string_append_uri_encoded()) in terms of the new code.

Fixes:
https://gitlab.gnome.org/GNOME/glib/issues/110

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Dan Winship
2019-01-17 10:38:20 +04:00
committed by Marc-André Lureau
parent 167b950122
commit d83d68d64c
16 changed files with 3715 additions and 443 deletions

View File

@@ -35,7 +35,7 @@
#include <ctype.h>
#include "gstring.h"
#include "guriprivate.h"
#include "gprintf.h"
@@ -506,34 +506,6 @@ g_string_insert_len (GString *string,
return string;
}
#define SUB_DELIM_CHARS "!$&'()*+,;="
static gboolean
is_valid (char c,
const char *reserved_chars_allowed)
{
if (g_ascii_isalnum (c) ||
c == '-' ||
c == '.' ||
c == '_' ||
c == '~')
return TRUE;
if (reserved_chars_allowed &&
strchr (reserved_chars_allowed, c) != NULL)
return TRUE;
return FALSE;
}
static gboolean
gunichar_ok (gunichar c)
{
return
(c != (gunichar) -2) &&
(c != (gunichar) -1);
}
/**
* g_string_append_uri_escaped:
* @string: a #GString
@@ -542,7 +514,7 @@ gunichar_ok (gunichar c)
* to be used, or %NULL
* @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
*
* Appends @unescaped to @string, escaped any characters that
* Appends @unescaped to @string, escaping any characters that
* are reserved in URIs using URI-style escape sequences.
*
* Returns: (transfer none): @string
@@ -555,38 +527,8 @@ g_string_append_uri_escaped (GString *string,
const gchar *reserved_chars_allowed,
gboolean allow_utf8)
{
unsigned char c;
const gchar *end;
static const gchar hex[16] = "0123456789ABCDEF";
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (unescaped != NULL, NULL);
end = unescaped + strlen (unescaped);
while ((c = *unescaped) != 0)
{
if (c >= 0x80 && allow_utf8 &&
gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
{
int len = g_utf8_skip [c];
g_string_append_len (string, unescaped, len);
unescaped += len;
}
else if (is_valid (c, reserved_chars_allowed))
{
g_string_append_c (string, c);
unescaped++;
}
else
{
g_string_append_c (string, '%');
g_string_append_c (string, hex[((guchar)c) >> 4]);
g_string_append_c (string, hex[((guchar)c) & 0xf]);
unescaped++;
}
}
_uri_encoder (string, (const guchar *) unescaped, strlen (unescaped),
reserved_chars_allowed, allow_utf8);
return string;
}