diff options
author | Daiki Ueno <dueno@redhat.com> | 2018-10-15 14:57:16 +0200 |
---|---|---|
committer | Daiki Ueno <ueno@gnu.org> | 2018-11-07 17:23:53 +0100 |
commit | f758142178b4cc5c650dde75152bfb85ac992178 (patch) | |
tree | e6776c73097ae0c21444c4557983f1209f194f12 /common | |
parent | e81f6af7ed3b39b8df0bb7ce150619ea8178d47c (diff) |
url: Prefer upper-case letters in hex characters when encoding
This makes it more compliant with RFC 3986, where the use of
upper-case letters is recommended (as "SHOULD").
Suggested by Sumit Bose.
Diffstat (limited to 'common')
-rw-r--r-- | common/test-path.c | 2 | ||||
-rw-r--r-- | common/test-url.c | 28 | ||||
-rw-r--r-- | common/url.c | 24 |
3 files changed, 46 insertions, 8 deletions
diff --git a/common/test-path.c b/common/test-path.c index c77acf2..2eb5444 100644 --- a/common/test-path.c +++ b/common/test-path.c @@ -206,7 +206,7 @@ test_encode (void) char *test; test = p11_path_encode ("2309haonutb;/AOE@#$O "); - assert_str_eq (test, "2309haonutb%3b/AOE%40%23%24O%20"); + assert_str_eq (test, "2309haonutb%3B/AOE%40%23%24O%20"); free (test); } diff --git a/common/test-url.c b/common/test-url.c index 892bf3c..4f10cb4 100644 --- a/common/test-url.c +++ b/common/test-url.c @@ -150,6 +150,33 @@ test_encode_verbatim (void) p11_buffer_uninit (&buf); } +static void +test_encode_lower (void) +{ + const unsigned char *input = (unsigned char *)",.:;"; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); + + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%2C%2E%3A%3B%00", (char *)buf.data); + assert_num_eq (15, buf.len); + + if (!p11_buffer_reset (&buf, 5)) + assert_not_reached (); + + setenv ("P11_KIT_URI_LOWERCASE", "1", 1); + + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%2c%2e%3a%3b%00", (char *)buf.data); + assert_num_eq (15, buf.len); + + p11_buffer_uninit (&buf); +} + int main (int argc, char *argv[]) @@ -160,5 +187,6 @@ main (int argc, p11_test (test_encode, "/url/encode"); p11_test (test_encode_verbatim, "/url/encode-verbatim"); + p11_test (test_encode_lower, "/url/encode-lower"); return p11_test_run (argc, argv); } diff --git a/common/url.c b/common/url.c index 344d971..903c58b 100644 --- a/common/url.c +++ b/common/url.c @@ -44,7 +44,8 @@ #include <stdio.h> #include <string.h> -const static char HEX_CHARS[] = "0123456789abcdef"; +const static char HEX_CHARS_UPPER[] = "0123456789ABCDEF"; +const static char HEX_CHARS_LOWER[] = "0123456789abcdef"; unsigned char * p11_url_decode (const char *value, @@ -75,14 +76,14 @@ p11_url_decode (const char *value, free (result); return NULL; } - a = strchr (HEX_CHARS, p11_ascii_tolower (value[0])); - b = strchr (HEX_CHARS, p11_ascii_tolower (value[1])); + a = strchr (HEX_CHARS_UPPER, p11_ascii_toupper (value[0])); + b = strchr (HEX_CHARS_UPPER, p11_ascii_toupper (value[1])); if (!a || !b) { free (result); return NULL; } - *p = (a - HEX_CHARS) << 4; - *(p++) |= (b - HEX_CHARS); + *p = (a - HEX_CHARS_UPPER) << 4; + *(p++) |= (b - HEX_CHARS_UPPER); value += 2; /* Ignore whitespace characters */ @@ -110,9 +111,18 @@ p11_url_encode (const unsigned char *value, p11_buffer *buf) { char hex[3]; + const char *env; + const char *hex_chars; assert (value <= end); + /* Opt to output lowercase hex-digits for compatibility */ + env = secure_getenv ("P11_KIT_URI_LOWERCASE"); + if (env && *env != '\0') + hex_chars = HEX_CHARS_LOWER; + else + hex_chars = HEX_CHARS_UPPER; + /* Now loop through looking for escapes */ while (value != end) { @@ -123,8 +133,8 @@ p11_url_encode (const unsigned char *value, /* All others get encoded */ } else { hex[0] = '%'; - hex[1] = HEX_CHARS[((unsigned char)*value) >> 4]; - hex[2] = HEX_CHARS[((unsigned char)*value) & 0x0F]; + hex[1] = hex_chars[((unsigned char)*value) >> 4]; + hex[2] = hex_chars[((unsigned char)*value) & 0x0F]; p11_buffer_add (buf, hex, 3); } |