Example #1
0
static char *
uri_normalized_copy (const char *part, int length,
		     const char *unescape_extra)
{
	unsigned char *s, *d, c;
	char *normalized = g_strndup (part, length);
	gboolean need_fixup = FALSE;

	s = d = (unsigned char *)normalized;
	do {
		if (*s == '%') {
			if (!g_ascii_isxdigit (s[1]) ||
			    !g_ascii_isxdigit (s[2])) {
				*d++ = *s;
				continue;
			}

			c = HEXCHAR (s);
			if (soup_char_is_uri_unreserved (c) ||
			    (unescape_extra && strchr (unescape_extra, c))) {
				*d++ = c;
				s += 2;
			} else {
				/* We leave it unchanged. We used to uppercase percent-encoded
				 * triplets but we do not do it any more as RFC3986 Section 6.2.2.1
				 * says that they only SHOULD be case normalized.
				 */
				*d++ = *s++;
				*d++ = *s++;
				*d++ = *s;
			}
		} else {
			if (*s == ' ')
				need_fixup = TRUE;
			*d++ = *s;
		}
	} while (*s++);

	if (need_fixup) {
		GString *fixed;
		char *sp, *p;

		fixed = g_string_new (NULL);
		p = normalized;
		while ((sp = strchr (p, ' '))) {
			g_string_append_len (fixed, p, sp - p);
			g_string_append (fixed, "%20");
			p = sp + 1;
		}
		g_string_append (fixed, p);
		g_free (normalized);
		normalized = g_string_free (fixed, FALSE);
	}

	return normalized;
}
Example #2
0
static char *
uri_normalized_copy (const char *part, int length,
                     const char *unescape_extra)
{
    unsigned char *s, *d, c;
    char *normalized = g_strndup (part, length);
    gboolean need_fixup = FALSE;

    if (!unescape_extra)
        unescape_extra = "";

    s = d = (unsigned char *)normalized;
    while (*s) {
        if (*s == '%') {
            if (!g_ascii_isxdigit (s[1]) ||
                    !g_ascii_isxdigit (s[2])) {
                *d++ = *s++;
                continue;
            }

            c = HEXCHAR (s);
            if (soup_char_is_uri_unreserved (c) ||
                    (c && strchr (unescape_extra, c))) {
                *d++ = c;
                s += 3;
            } else {
                /* We leave it unchanged. We used to uppercase percent-encoded
                 * triplets but we do not do it any more as RFC3986 Section 6.2.2.1
                 * says that they only SHOULD be case normalized.
                 */
                *d++ = *s++;
                *d++ = *s++;
                *d++ = *s++;
            }
        } else {
            if (!g_ascii_isgraph (*s) &&
                    !strchr (unescape_extra, *s))
                need_fixup = TRUE;
            *d++ = *s++;
        }
    }
    *d = '\0';

    if (need_fixup) {
        GString *fixed;

        fixed = g_string_new (NULL);
        s = (guchar *)normalized;
        while (*s) {
            if (g_ascii_isgraph (*s) ||
                    strchr (unescape_extra, *s))
                g_string_append_c (fixed, *s);
            else
                g_string_append_printf (fixed, "%%%02X", (int)*s);
            s++;
        }
        g_free (normalized);
        normalized = g_string_free (fixed, FALSE);
    }

    return normalized;
}