Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to make call to TimeZoneApi.getTimeZone() through proxy #160

Closed
tazmaniax opened this issue Jun 12, 2016 · 9 comments
Closed

Unable to make call to TimeZoneApi.getTimeZone() through proxy #160

tazmaniax opened this issue Jun 12, 2016 · 9 comments

Comments

@tazmaniax
Copy link

Hi, After spending much effort I still have not been able to successfully make a call to TimeZoneApi.getTimeZone() through a proxy - a proxy is required as I'm making the request from a server running on Heroku and so does not have a fixed IP address. I'm not sure if I should be posting this issue here or to OkHttp or to usefixie (also see fixie@heroku) the proxy provider, but I'm going to start here.

I'm testing on OS X (10.11.6 Beta) and Java 1.8.0_40-b27 and using google-maps-services-0.1.15.jar

Firstly I'm able to successfully use the proxy using curl...

$ curl "https://maps.googleapis.com/maps/api/timezone/json?key=[key]&location=[latitude]%2C[longitude]&timestamp=0" --proxy http://[username]:[password]@velodrome.usefixie.com:80
{
   "dstOffset" : 0,
   "rawOffset" : 3600,
   "status" : "OK",
   "timeZoneId" : "Europe/London",
   "timeZoneName" : "GMT+01:00"
}

In my Java code I'm configuring the proxy as follows based on the guidance...

  1. #141 How to use authentication proxy in GeoApiContext
  2. StackOverflow: Authenticated HTTP proxy with Java
URL url = new URL("http://[username]:[password]@velodrome.usefixie.com:80");
String userInfo = url.getUserInfo();
final String user = userInfo.substring(0, userInfo.indexOf(':'));
final String password = userInfo.substring(userInfo.indexOf(':') + 1);
String host = url.getHost();
int port = url.getPort();

Authenticator.setDefault(
    new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password.toCharArray());
        }
    }
);

System.setProperty("http.proxyUser", user);
System.setProperty("http.proxyPassword", password);

GeoApiContext context = new GeoApiContext().setApiKey(GoogleAPIHelper.getServerApiKey());
context.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)));

Here is the exception I'm getting...

Jun 12, 2016 9:45:48 PM com.google.maps.OkHttpRequestHandler handle
INFO: Request: https://maps.googleapis.com/maps/api/timezone/json?key=[key]&location=[latitude]%2C[longitude]&timestamp=0
java.io.IOException: unexpected end of stream on null
    at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:201)
    at com.squareup.okhttp.internal.io.RealConnection.createTunnel(RealConnection.java:270)
    at com.squareup.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:172)
    at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149)
    at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
    at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
    at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.squareup.okhttp.Call.getResponse(Call.java:286)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
    at com.squareup.okhttp.Call.access$100(Call.java:35)
    at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:171)
    at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException: \n not found: size=0 content=...
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
    at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
    ... 18 more

There is a related issue OkHttp #1114 but I couldn't quite understand what the resolution was.

thx

@tazmaniax
Copy link
Author

tazmaniax commented Jun 14, 2016

I've just had some feedback from Stephen@fixie...

I'm sorry you're having problems using Fixie to connect to Google map services. Java's support for making HTTPS requests over a proxy is quite poor compared to other languages. The issue is that, as far as I can tell, httpsUrlConnection does not have an option to include a Proxy-Authorization header by default.

But, this being Java, it's possible to implement it yourself. http://www.javaworld.com/article/2077475/core-java/java-tip-111--implement-https-tunneling-with-jsse.html is a good reference for this. If you need help debugging it, something like Wireshark would likely be helpful - it makes it possible to easily compare the request your application code is making versus the request curl makes.

I'm going chase this and see I can follow the suggested example but I'm wondering if somebody else has already faced this and managed to get Google Maps Services Java (and by association OkHttp) to use https tunneling?

thx

@tazmaniax
Copy link
Author

Just another response from Stephen@fixie...

I looked through the repo for OkHttp, and I have some mixed news. The bad news is that https proxy authentication is not supported on master. However, there is an open pull request which adds preemptive proxy authentication, which should fix this and bring it to parity with the standard request libraries for every other major library.

That pull request is square/okhttp#2458

Are there plans to update the OkHttp dependency from v2.7.5 to a more recent version, e.g. v3.3.1?

@domesticmouse
Copy link
Contributor

Upgrading to OkHttp 3 is on the todo list. Did I mention that we gladly accept PRs? =)

@tazmaniax
Copy link
Author

@domesticmouse I hear you :) I'll see what I can muster. In any case, it looks as though the issue is with OkHttp and still waiting for the release of pull request square/okhttp#2458

@a-k-g
Copy link

a-k-g commented Jul 7, 2016

It seems like pre-emptive proxy auth may not be necessary. This square/okhttp#2467 (already merged) could also fix the issue for most people.

@shebson
Copy link

shebson commented Nov 26, 2016

Hi, this is Stephen with Fixie. We've updated our proxy to play nicely with OkHttp without preemptive authentication, so you should be safe to close this ticket as long as you're on a version of OkHttp released after they merged #2467.

@tazmaniax
Copy link
Author

@shebson great news, thx!

@domesticmouse
Copy link
Contributor

I believe this is now dependent on migration to OkHttp3 #148

@domesticmouse
Copy link
Contributor

I'm going to close this out as a dupe. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants