Percent encoding needs 0-padding if less than 2 digits #12

Open
pfrazee opened this Issue May 21, 2013 · 3 comments

Comments

Projects
None yet
2 participants

pfrazee commented May 21, 2013

Discovered on Chrome 26.0.1410.65, OSX 10.8.

Some character codes (eg "\n") produce hex values which are less than "F" (eg "%A") which creates an error when decoded by the browser (it expects "%0A").

To reproduce this issue:

decodeURIComponent(UriTemplate.parse('http://whatever.com{?q}').expand({q:"foo\nbar"}));

A simple fix:

    function pad0(v) {
      if (v.length > 1) return v;
        return '0'+v;
    }

    /**
     * encodes a character, if needed or not.
     * @param chr
     * @return pct-encoded character
     */
    function encodeCharacter (chr) {
        var
            result = '',
            octets = utf8.encode(chr),
            octet,
            index;
        for (index = 0; index < octets.length; index += 1) {
            octet = octets.charCodeAt(index);
            result += '%' + pad0(octet.toString(16).toUpperCase());
        }
        return result;
    }
Owner

fxa commented May 26, 2013

of course you are right. I will add a test case and your fix
sorry, as a freelancer I normally can only fix things on weekend

pfrazee commented May 26, 2013

No problem! This has been a very useful library for me - I appreciate it.

Owner

fxa commented May 26, 2013

Thank you!

I just fixed your issue.

And yes, the “extract” function will come.

I have a 0.0.1 version and will check in, but the bug fixes have higher priority

Franz

Von: Paul Frazee [mailto:notifications@github.com]
Gesendet: Sonntag, 26. Mai 2013 17:40
An: fxa/uritemplate-js
Cc: Franz X Antesberger
Betreff: Re: [uritemplate-js] Percent encoding needs 0-padding if less than 2 digits (#12)

No problem! This has been a very useful library for me - I appreciate it.


Reply to this email directly or view it on GitHub #12 (comment) . https://github.com/notifications/beacon/z007K-oJ-ZnzAnsfdzRoGT9s4OxJfp3jOhB_xi0I6lw1MEyQReoufU5WMr73pUaN.gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment