4

I'm writing a component that integrate between two 3rd-party libraries.

Gets a URL from the first library, and pass it to the second library.

The URL i get is in the format of:

https://xxxxx.s3.amazonaws.com/xxxx_xx_xx/xxxx_xx_xx/xxxxxxxx/g_19743dff-0931-40c4-ac5a-10fd1150bda4/x/xxxxxxxx_xxxx_xx_xx_xx_xx_xx_xxxx_xxx_xxx_xxxxxxxxxx.jpg?AWSAccessKeyId=AKIAJFACFRGCGV7RPFKQ&Expires=1445259633&Signature=RD35q%2BlEc4Lkn5ppfM2QTKsDCTo%3D

meaning it include signs like "=" and "+" in encoded format.

When I pass it to the second library as-is, the library failed to download the file. From the source of that library I see that it tries to perform 'encode(url)' before downloading the file, which cause it to be encoded again with no need.

I tried decoding the URL before transferring it to the second library (with decodeURI or decodeURIComponent), so it will then encode it and successfully download the file, but encode() doesn't encode all chars, it has some reserved-char (like '+' and '='), so the result url wasn't end up the same as the original.

To sum-up my question:

Assuming i don't have any control over the code of the 2 libraries, given 'encodedUrl', how can I implement 'myFunc(str)' such that:

encodeURI(myFunc(encodedUrl)) === encodedUrl

for every url possible.

avivr
  • 2,573
  • 3
  • 22
  • 34
  • Could you post the original string as well. – Asons Oct 20 '15 at 08:27
  • @LGSon I did, it's the url in the question. just replaced some alphabet chars to 'x' to hide private data – avivr Oct 20 '15 at 08:29
  • The major issue with this is described here: http://stackoverflow.com/questions/17010119/decodeuri-decodes-space-as-symbol – Asons Oct 20 '15 at 08:48

1 Answers1

3

Since the first library gives you encoded urls, you can't. For example, myFunc could only turn "%2B" into "+", because "+" is the only thing encodeURI would turn into "%2B". That makes myFunc === decodeURIComponent, and you know what that means.

Luka Žitnik
  • 1,160
  • 8
  • 15