couchr icon indicating copy to clipboard operation
couchr copied to clipboard

Possible mis-handling UTF-8 strings in HEAD requests

Open xpollen8 opened this issue 10 years ago • 0 comments

I use HEAD requests to get eTag values for caching purposes for given queries.

If my cache under that eTag is a miss, then I make a GET request.

Since the couchr.head() call does not accept arguments as GET does, I pass the arguments along as a query string to the HEAD request.

In doing so, I discovered that when "key=" strings containing UTF-8 values (emoji), the HEAD would return a different eTag value than the GET would using the same strings. Non-emoji-containing strings would work as expected.

I was able to work around this seeming limitation by performing - for HEAD requests - the encoding dance that couchr uses for GET strings.

Unless I am abusing the spec, it would seem that couchr should natively handle this string encoding.

Here is a snippet I used to take a json object containing values and returning a string properly encoded for use as the arguments to the HEAD call.

function getHeadArgs(lookup) {
  var dup = JSON.parse(JSON.stringify(lookup));
  ['key', 'keys', 'startkey', 'endkey'].forEach(function(key) {
    if (dup[key] != null) {
      dup[key] = JSON.stringify(dup[key]);
    }
  });
  return querystring.stringify(dup);
}

var encoded = getHeadArgs({ key: '💟💜💛💚💙💙!!' });

couchr.head(URL + '?' + encoded);

xpollen8 avatar Oct 20 '15 02:10 xpollen8