1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Fix follow redirections

This commit is contained in:
davigonz 2018-08-30 13:31:53 +02:00
parent f3eb0c4431
commit 8a04392bfc
5 changed files with 43 additions and 31 deletions

View File

@ -178,7 +178,7 @@ public class OwnCloudClient extends HttpClient {
public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception { public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception {
int redirectionsCount = 0; int redirectionsCount = 0;
int status = method.getStatusCode(); int status = method.getStatusCode();
RedirectionPath result = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT); RedirectionPath redirectionPath = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT);
while (redirectionsCount < MAX_REDIRECTIONS_COUNT && while (redirectionsCount < MAX_REDIRECTIONS_COUNT &&
(status == HttpConstants.HTTP_MOVED_PERMANENTLY || (status == HttpConstants.HTTP_MOVED_PERMANENTLY ||
@ -189,12 +189,12 @@ public class OwnCloudClient extends HttpClient {
final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER) != null final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER) != null
? method.getResponseHeader(HttpConstants.LOCATION_HEADER) ? method.getResponseHeader(HttpConstants.LOCATION_HEADER)
: method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER); : method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER);
if (location != null) {
if (location != null) {
Log_OC.d(TAG + " #" + mInstanceNumber, Log_OC.d(TAG + " #" + mInstanceNumber,
"Location to redirect: " + location); "Location to redirect: " + location);
result.addLocation(location); redirectionPath.addLocation(location);
// Release the connection to avoid reach the max number of connections per host // Release the connection to avoid reach the max number of connections per host
// due to it will be set a different url // due to it will be set a different url
@ -221,7 +221,7 @@ public class OwnCloudClient extends HttpClient {
throw e; throw e;
} }
} }
result.addStatus(status); redirectionPath.addStatus(status);
redirectionsCount++; redirectionsCount++;
} else { } else {
@ -229,7 +229,7 @@ public class OwnCloudClient extends HttpClient {
status = HttpConstants.HTTP_NOT_FOUND; status = HttpConstants.HTTP_NOT_FOUND;
} }
} }
return result; return redirectionPath;
} }
/** /**

View File

@ -52,11 +52,9 @@ public abstract class HttpBaseMethod {
protected RequestBody mRequestBody; protected RequestBody mRequestBody;
protected Response mResponse; protected Response mResponse;
protected Call mCall; protected Call mCall;
protected URL mUrl;
protected HttpBaseMethod(URL url) { protected HttpBaseMethod(URL url) {
mOkHttpClient = HttpClient.getOkHttpClient(); mOkHttpClient = HttpClient.getOkHttpClient();
mUrl = url;
mRequest = new Request.Builder() mRequest = new Request.Builder()
.url(HttpUrl.parse(url.toString())) .url(HttpUrl.parse(url.toString()))
.build(); .build();
@ -94,10 +92,6 @@ public abstract class HttpBaseMethod {
return mRequest.header(name); return mRequest.header(name);
} }
public HttpUrl getUrl() {
return mRequest.url();
}
// Response // Response
public int getStatusCode() { public int getStatusCode() {
@ -109,11 +103,17 @@ public abstract class HttpBaseMethod {
} }
public String getResponseBodyAsString() throws IOException { public String getResponseBodyAsString() throws IOException {
return mResponse.body().string(); if (mResponse.body() != null) {
return mResponse.body().string();
}
return null;
} }
public InputStream getResponseBodyAsStream() { public InputStream getResponseBodyAsStream() {
return mResponse.body().byteStream(); if (mResponse.body() != null) {
return mResponse.body().byteStream();
}
return null;
} }
public Headers getResponseHeaders() { public Headers getResponseHeaders() {

View File

@ -51,7 +51,7 @@ public abstract class DavMethod extends HttpBaseMethod {
super(url); super(url);
mDavResource = new DavOCResource( mDavResource = new DavOCResource(
mOkHttpClient, mOkHttpClient,
HttpUrl.parse(mUrl.toString()), HttpUrl.parse(url.toString()),
Constants.INSTANCE.getLog()); Constants.INSTANCE.getLog());
} }
@ -102,7 +102,7 @@ public abstract class DavMethod extends HttpBaseMethod {
super.setReadTimeout(readTimeout, timeUnit); super.setReadTimeout(readTimeout, timeUnit);
mDavResource = new DavOCResource( mDavResource = new DavOCResource(
mOkHttpClient, mOkHttpClient,
HttpUrl.parse(mUrl.toString()), HttpUrl.parse(mRequest.url().toString()),
Constants.INSTANCE.getLog()); Constants.INSTANCE.getLog());
} }
@ -111,7 +111,7 @@ public abstract class DavMethod extends HttpBaseMethod {
super.setConnectionTimeout(connectionTimeout, timeUnit); super.setConnectionTimeout(connectionTimeout, timeUnit);
mDavResource = new DavOCResource( mDavResource = new DavOCResource(
mOkHttpClient, mOkHttpClient,
HttpUrl.parse(mUrl.toString()), HttpUrl.parse(mRequest.url().toString()),
Constants.INSTANCE.getLog()); Constants.INSTANCE.getLog());
} }
@ -120,7 +120,7 @@ public abstract class DavMethod extends HttpBaseMethod {
super.setFollowRedirects(followRedirects); super.setFollowRedirects(followRedirects);
mDavResource = new DavOCResource( mDavResource = new DavOCResource(
mOkHttpClient, mOkHttpClient,
HttpUrl.parse(mUrl.toString()), HttpUrl.parse(mRequest.url().toString()),
Constants.INSTANCE.getLog()); Constants.INSTANCE.getLog());
} }
@ -129,7 +129,16 @@ public abstract class DavMethod extends HttpBaseMethod {
super.setRetryOnConnectionFailure(retryOnConnectionFailure); super.setRetryOnConnectionFailure(retryOnConnectionFailure);
mDavResource = new DavOCResource( mDavResource = new DavOCResource(
mOkHttpClient, mOkHttpClient,
HttpUrl.parse(mUrl.toString()), HttpUrl.parse(mRequest.url().toString()),
Constants.INSTANCE.getLog());
}
@Override
public void setUrl(HttpUrl url){
super.setUrl(url);
mDavResource = new DavOCResource(
mOkHttpClient,
HttpUrl.parse(mRequest.url().toString()),
Constants.INSTANCE.getLog()); Constants.INSTANCE.getLog());
} }
@ -146,12 +155,4 @@ public abstract class DavMethod extends HttpBaseMethod {
public boolean isAborted() { public boolean isAborted() {
return mDavResource.isCallAborted(); return mDavResource.isCallAborted();
} }
public void setUrl(URL url) {
mUrl = url;
mDavResource = new DavOCResource(
mOkHttpClient,
HttpUrl.parse(mUrl.toString()),
Constants.INSTANCE.getLog());
}
} }

View File

@ -77,8 +77,9 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
boolean previousFollowRedirects = client.followRedirects();
try { try {
client.setFollowRedirects(true);
PropfindMethod propfindMethod = new PropfindMethod( PropfindMethod propfindMethod = new PropfindMethod(
new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)), new URL(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mPath)),
0, 0,
@ -87,7 +88,13 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
propfindMethod.setReadTimeout(TIMEOUT, TimeUnit.SECONDS); propfindMethod.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
propfindMethod.setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS); propfindMethod.setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS);
final int status = client.executeHttpMethod(propfindMethod); client.setFollowRedirects(false);
int status = client.executeHttpMethod(propfindMethod);
if (previousFollowRedirects) {
mRedirectionPath = client.followRedirection(propfindMethod);
status = mRedirectionPath.getLastStatus();
}
/** /**
* PROPFIND method * PROPFIND method
@ -111,6 +118,8 @@ public class ExistenceCheckRemoteOperation extends RemoteOperation {
(mSuccessIfAbsent ? " absence " : " existence ") + ": " + (mSuccessIfAbsent ? " absence " : " existence ") + ": " +
result.getLogMessage(), result.getException()); result.getLogMessage(), result.getException());
return result; return result;
} finally {
client.setFollowRedirects(previousFollowRedirects);
} }
} }

View File

@ -83,17 +83,19 @@ public class GetRemoteStatusOperation extends RemoteOperation<OwnCloudVersion> {
getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); getMethod.setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS); getMethod.setConnectionTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
client.setFollowRedirects(false);
boolean isRedirectToNonSecureConnection = false;
int status; int status;
try { try {
status = client.executeHttpMethod(getMethod); status = client.executeHttpMethod(getMethod);
mLatestResult = new RemoteOperationResult(OK); mLatestResult = isSuccess(status)
? new RemoteOperationResult<>(OK)
: new RemoteOperationResult<>(getMethod);
} catch (SSLException sslE) { } catch (SSLException sslE) {
mLatestResult = new RemoteOperationResult(sslE); mLatestResult = new RemoteOperationResult(sslE);
return false; return false;
} }
client.setFollowRedirects(false);
boolean isRedirectToNonSecureConnection = false;
String redirectedLocation = mLatestResult.getRedirectedLocation(); String redirectedLocation = mLatestResult.getRedirectedLocation();
while (redirectedLocation != null && redirectedLocation.length() > 0 while (redirectedLocation != null && redirectedLocation.length() > 0
&& !mLatestResult.isSuccess()) { && !mLatestResult.isSuccess()) {