imgix / imgix-java

A Java client library for generating URLs with imgix
https://www.imgix.com
BSD 2-Clause "Simplified" License
19 stars 8 forks source link

URLHelper is calling platform dependent String.getBytes() #35

Closed spand closed 4 years ago

spand commented 4 years ago

URLHelper is calling platform dependent String.getBytes() here:

private String encodeBase64(String str) {
    byte[] stringBytes = str.getBytes();

But as far as I know base64 encoding for urls require UTF-8 to be used and should be specified explicitly here.

Running this example program:

public class ImgixExample {
    public static void main(String[] args) {
        System.out.println("Default charset: " + Charset.defaultCharset());
        URLBuilder builder = new URLBuilder("demos.imgix.net");
        Map<String, String> params = new HashMap<String, String>();
        params.put("m64", "http://demos.imgix.net/bridge.png?ixlib=java-2.1.0");
        System.out.println(builder.createURL("bridge.png", params));
    }
}

with -Dfile.encoding=UTF-8 and -Dfile.encoding=UTF-32 respectively demonstrates the problem:

Default charset: UTF-8
http://demos.imgix.net/bridge.png?ixlib=java-2.1.0&m64=aHR0cDovL2RlbW9zLmltZ2l4Lm5ldC9icmlkZ2UucG5nP2l4bGliPWphdmEtMi4xLjA
Default charset: UTF-32
http://demos.imgix.net/bridge.png?ixlib=java-2.1.0&m64=�������������������������������������������������������������������
sherwinski commented 4 years ago

Good catch @spand! This looks like a quick fix but I'm scratching my head trying to figure out how to write a unit test for this. To your knowledge, is there anyway to programmatically change the default charset for a single test case or is that only something that can be set before the JVM starts up?

I'd also gladly accept a PR for this if you/anyone else is feeling up for it.

spand commented 4 years ago

It is not possible to set programmatically to my knowledge (internet would seem to agree) other than maybe doing some crazy Classloader magic.