From 4f2655b59a323f1936a72b2622e693c589f18fbb Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Fri, 14 Oct 2016 13:30:01 -0700 Subject: HttpUrlConnection doesn't auto follow http -> https redirects --- .../daneren2005/dsub/service/RESTMusicService.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java index b05a569d..cb8871a0 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/RESTMusicService.java @@ -1875,12 +1875,29 @@ public class RESTMusicService implements MusicService { if(connection.getResponseCode() >= 500) { throw new IOException("Error code: " + connection.getResponseCode()); } - detectRedirect(context, urlObj, connection); + if(detectRedirect(context, urlObj, connection)) { + String rewrittenUrl = rewriteUrlWithRedirect(context, url); + if(!rewrittenUrl.equals(url)) { + connection.disconnect(); + return getConnectionDirect(context, rewrittenUrl, headers, minNetworkTimeout); + } + } + return connection; } - private void detectRedirect(Context context, URL originalUrl, HttpURLConnection connection) throws Exception { + // Returns true when we should immediately retry with the redirect + private boolean detectRedirect(Context context, URL originalUrl, HttpURLConnection connection) throws Exception { + if(connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP || connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) { + String redirectLocation = connection.getHeaderField("Location"); + if(redirectLocation != null) { + detectRedirect(context, originalUrl.toExternalForm(), redirectLocation); + return true; + } + } + detectRedirect(context, originalUrl, connection.getURL()); + return false; } private void detectRedirect(Context context, URL originalUrl, URL redirectedUrl) throws Exception { detectRedirect(context, originalUrl.toExternalForm(), redirectedUrl.toExternalForm()); -- cgit v1.2.3