0

I am writing something like an image proxy where I receive URLs from my site front-end, and I download images , re-size them, and return smaller images for the front end and client to download from the "proxy". This means I need to take care of all-sorts of url patterns, this is why I chose to decode the given url and than encode it using URIUtils.decode:

private String fixUrl(String fromUrl) throws URIException {
    fromUrl = URIUtil.decode(fromUrl);
    fromUrl = URIUtil.encodeQuery(fromUrl);
    return fromUrl;
}

This should help me take care of urls that are already encoded. My problem is that some of the urls are double encoded, and from what I saw, URIUtils.decode, performs recursive decode and this means that in cases of double encoded urls I will get a bad url that does not work.

Is there a simple way to decode only once?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

2 Answers2

0

I'd try to check that URL still contains character %. If it does not contain any % it is not encoded and you can stop your decoding procedure.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • What about the "not-quite-official" encoding where a space encodes to a '+'? I think you need to check for % _or_ +. (Depends if you are strictly encoding or not) – user949300 Aug 26 '12 at 21:30
0

Easiest option I know is to use the built-in

java.net.URLDecoder.decode

In order to decode automatically only once.

If like me you have the case that sometimes URL's are double / triple encoded - you can use this recursive function in order to decode again and again until there are no "%" or "+" :

private static String completeDecode(String url) {
    if(url.contains("%") || url.contains("+"))
    {
        try
        {
            return(completeDecode(java.net.URLDecoder.decode(url, "UTF-8")));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
    }
    return url;
}

Cheers

dor7dev
  • 31
  • 4