fxa / uritemplate-js

An javascript implementation of RFC6570 Uri Templates
MIT License
97 stars 20 forks source link

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

Open pfrazee opened 11 years ago

pfrazee commented 11 years ago

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;
    }
fxa commented 11 years ago

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 11 years ago

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

fxa commented 11 years ago

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 https://github.com/fxa/uritemplate-js/issues/12#issuecomment-18464869 . https://github.com/notifications/beacon/z007K-oJ-ZnzAnsfdzRoGT9s4OxJfp3jOhB_xi0I6lw1MEyQReoufU5WMr73pUaN.gif