Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit e6bdfab

Browse files
theScrabidavigonz
authored andcommitted
make redirect more or less work
1 parent 0b9a794 commit e6bdfab

3 files changed

Lines changed: 40 additions & 50 deletions

File tree

src/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package com.owncloud.android.lib.common;
2727

28-
import android.accounts.Account;
2928
import android.accounts.AccountManager;
3029
import android.accounts.AccountsException;
3130
import android.content.Context;
@@ -37,17 +36,16 @@
3736
import com.owncloud.android.lib.common.http.HttpClient;
3837
import com.owncloud.android.lib.common.http.HttpConstants;
3938
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
40-
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod;
4139
import com.owncloud.android.lib.common.network.RedirectionPath;
4240
import com.owncloud.android.lib.common.utils.Log_OC;
4341
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
4442

4543

4644
import java.io.IOException;
4745
import java.io.InputStream;
48-
import java.util.ArrayList;
4946
import java.util.List;
5047

48+
import at.bitfire.dav4android.exception.HttpException;
5149
import okhttp3.Cookie;
5250
import okhttp3.Headers;
5351
import okhttp3.HttpUrl;
@@ -136,9 +134,16 @@ public int executeHttpMethod (HttpBaseMethod method) throws Exception {
136134
int status;
137135

138136
do {
139-
//TODO Dav4Android doesn't allow follow redirections right now
140-
// method.setFollowRedirects(mFollowRedirects);
141-
status = method.execute();
137+
try {
138+
status = method.execute();
139+
} catch (HttpException e) {
140+
if(e.getMessage().contains(Integer.toString(HttpConstants.HTTP_MOVED_TEMPORARILY))) {
141+
status = followRedirection(method).getLastStatus();
142+
} else {
143+
throw e;
144+
}
145+
}
146+
142147
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
143148
if (repeatWithFreshCredentials) {
144149
repeatCounter++;
@@ -148,41 +153,26 @@ public int executeHttpMethod (HttpBaseMethod method) throws Exception {
148153
return status;
149154
}
150155

151-
private void checkFirstRedirection(HttpMethod method) {
152-
final String location = method.getResponseHeaders()
153-
.get("location");
156+
private int executeRedirectedHttpMethod (HttpBaseMethod method) throws Exception {
157+
boolean repeatWithFreshCredentials;
158+
int repeatCounter = 0;
159+
int status;
154160

155-
if(location != null && !location.isEmpty()) {
156-
mRedirectedLocation = location;
157-
}
158-
}
161+
do {
162+
status = method.execute();
159163

160-
/**
161-
* Fix for https://github.com/owncloud/android/issues/1847#issuecomment-267558274
162-
*
163-
* The problem: default SocketFactory in HTTPClient 3.x for HTTP connections creates a separate thread
164-
* to create the socket. When a port out of TCP bounds is passed, an exception is thrown in that
165-
* separate thread, and our original thread is not able to catch it. This is not happenning with HTTPS
166-
* connections because we had to define our own socket factory,
167-
* {@link com.owncloud.android.lib.common.network.AdvancedSslSocketFactory}, and it does not mess with
168-
* threads.
169-
*
170-
* The solution: validate the input (the port number) ourselves before let the work to HTTPClient 3.x.
171-
*
172-
* @param method HTTP method to run.
173-
* @throws IllegalArgumentException If 'method' targets an invalid port in an HTTP URI.
174-
*/
175-
private void preventCrashDueToInvalidPort(HttpMethod method) {
176-
final int port = method.getUrl().port();
177-
String scheme = method.getUrl().scheme().toLowerCase();
178-
if ("http".equals(scheme) && port > 0xFFFF) {
179-
// < 0 is not tested because -1 is used when no port number is specified in the URL;
180-
// no problem, the network library will convert that in the default HTTP port
181-
throw new IllegalArgumentException("Invalid port number " + port);
182-
}
164+
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
165+
if (repeatWithFreshCredentials) {
166+
repeatCounter++;
167+
}
168+
} while (repeatWithFreshCredentials);
169+
170+
return status;
183171
}
184172

185-
public RedirectionPath followRedirection(HttpMethod method) throws Exception {
173+
174+
175+
public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception {
186176
int redirectionsCount = 0;
187177
int status = method.getStatusCode();
188178
RedirectionPath result = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT);
@@ -215,13 +205,19 @@ public RedirectionPath followRedirection(HttpMethod method) throws Exception {
215205
if (destination != null) {
216206
final int suffixIndex = location.lastIndexOf(WEBDAV_PATH_4_0);
217207
final String redirectionBase = location.substring(0, suffixIndex);
218-
219208
final String destinationPath = destination.substring(mBaseUri.toString().length());
220-
final String redirectedDestination = redirectionBase + destinationPath;
221209

222-
method.setRequestHeader("destination", destination);
210+
method.setRequestHeader("destination", redirectionBase + destinationPath);
211+
}
212+
try {
213+
status = executeRedirectedHttpMethod(method);
214+
} catch (HttpException e) {
215+
if(e.getMessage().contains(Integer.toString(HttpConstants.HTTP_MOVED_TEMPORARILY))) {
216+
status = HttpConstants.HTTP_MOVED_TEMPORARILY;
217+
} else {
218+
throw e;
219+
}
223220
}
224-
status = executeHttpMethod(method);
225221
result.addStatus(status);
226222
redirectionsCount++;
227223

src/com/owncloud/android/lib/common/authentication/OwnCloudSamlSsoCredentials.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
*/
2424
package com.owncloud.android.lib.common.authentication;
2525

26-
27-
28-
import android.net.Uri;
29-
3026
import com.owncloud.android.lib.common.OwnCloudClient;
3127
import com.owncloud.android.lib.common.http.HttpClient;
3228
import com.owncloud.android.lib.common.http.interceptors.BarearAuthInterceptor;
@@ -36,7 +32,6 @@
3632

3733
import java.util.ArrayList;
3834

39-
import okhttp3.Cookie;
4035

4136
public class OwnCloudSamlSsoCredentials implements OwnCloudCredentials {
4237

src/com/owncloud/android/lib/common/http/HttpClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ public class HttpClient {
5858
private static HttpInterceptor sOkHttpInterceptor;
5959
private static Context sContext;
6060

61-
public HttpClient() {
62-
63-
}
6461

6562
public static void setContext(Context context) {
6663
sContext = context;
@@ -78,7 +75,9 @@ public static OkHttpClient getOkHttpClient() {
7875
.protocols(Arrays.asList(Protocol.HTTP_1_1))
7976
.followRedirects(false)
8077
.sslSocketFactory(sslContext.getSocketFactory(), trustManager)
81-
.hostnameVerifier(new BrowserCompatHostnameVerifier());
78+
.hostnameVerifier((asdf, usdf) -> true);
79+
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
80+
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
8281
if(BuildConfig.DEBUG) {
8382
clientBuilder.addNetworkInterceptor(new StethoInterceptor());
8483
}

0 commit comments

Comments
 (0)