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

update base url in active client after 301 redirect

reduce validation retry count
This commit is contained in:
Christian Schabesberger 2021-09-16 14:20:57 +02:00
parent ce761aaec2
commit 5ca99a0e69
5 changed files with 43 additions and 15 deletions

View File

@ -32,7 +32,7 @@ class ConnectionValidator (
client.account = baseClient.account client.account = baseClient.account
client.credentials = baseClient.credentials client.credentials = baseClient.credentials
while (validationRetryCount < 5) { while (validationRetryCount < VALIDATION_RETRY_COUNT) {
Timber.d("+++++++++++++++++++++++++++++++++++++ validationRetryCout %d", validationRetryCount) Timber.d("+++++++++++++++++++++++++++++++++++++ validationRetryCout %d", validationRetryCount)
var successCounter = 0 var successCounter = 0
var failCounter = 0 var failCounter = 0
@ -180,4 +180,8 @@ class ConnectionValidator (
} }
return credentialsWereRefreshed return credentialsWereRefreshed
} }
companion object {
val VALIDATION_RETRY_COUNT = 3
}
} }

View File

@ -48,6 +48,7 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER; import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
import static com.owncloud.android.lib.common.http.HttpConstants.HTTP_MOVED_PERMANENTLY;
import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID; import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID;
public class OwnCloudClient extends HttpClient { public class OwnCloudClient extends HttpClient {
@ -77,7 +78,10 @@ public class OwnCloudClient extends HttpClient {
private boolean mFollowRedirects = false; private boolean mFollowRedirects = false;
public OwnCloudClient(Uri baseUri, ConnectionValidator connectionValidator, boolean synchronizeRequests, SingleSessionManager singleSessionManager) { public OwnCloudClient(Uri baseUri,
ConnectionValidator connectionValidator,
boolean synchronizeRequests,
SingleSessionManager singleSessionManager) {
if (baseUri == null) { if (baseUri == null) {
throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL"); throw new IllegalArgumentException("Parameter 'baseUri' cannot be NULL");
} }
@ -113,6 +117,10 @@ public class OwnCloudClient extends HttpClient {
int repeatCounter = 0; int repeatCounter = 0;
int status; int status;
if(mFollowRedirects) {
method.setFollowRedirects(true);
}
boolean retry; boolean retry;
do { do {
repeatCounter++; repeatCounter++;
@ -128,16 +136,19 @@ public class OwnCloudClient extends HttpClient {
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
} }
method.setFollowRedirects(mFollowRedirects);
status = method.execute(); status = method.execute();
stacklog(status, method); stacklog(status, method);
if (!mFollowRedirects && if (!mFollowRedirects &&
!method.getFollowRedirects() &&
mConnectionValidator != null && mConnectionValidator != null &&
(status == HttpConstants.HTTP_MOVED_TEMPORARILY || (status == HttpConstants.HTTP_MOVED_TEMPORARILY ||
(!(mCredentials instanceof OwnCloudAnonymousCredentials) && (!(mCredentials instanceof OwnCloudAnonymousCredentials) &&
status == HttpConstants.HTTP_UNAUTHORIZED))) { status == HttpConstants.HTTP_UNAUTHORIZED))) {
retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success
} else if(method.getFollowPermanentRedirects() && status == HTTP_MOVED_PERMANENTLY) {
retry = true;
method.setFollowRedirects(true);
} }
} while (retry && repeatCounter < MAX_RETRY_COUNT); } while (retry && repeatCounter < MAX_RETRY_COUNT);

View File

@ -41,6 +41,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
var okHttpClient: OkHttpClient var okHttpClient: OkHttpClient
var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException() var httpUrl: HttpUrl = url.toHttpUrlOrNull() ?: throw MalformedURLException()
var request: Request var request: Request
private var _followPermanentRedirects = false
abstract var response: Response abstract var response: Response
var call: Call? = null var call: Call? = null
@ -123,6 +124,11 @@ abstract class HttpBaseMethod constructor(url: URL) {
return response.body?.byteStream() return response.body?.byteStream()
} }
/**
* returns the final url after following the last redirect.
*/
open fun getFinalUrl() = response.request.url
/************************* /*************************
*** Connection Params *** *** Connection Params ***
*************************/ *************************/
@ -158,6 +164,15 @@ abstract class HttpBaseMethod constructor(url: URL) {
.build() .build()
} }
open fun getFollowRedirects() = okHttpClient.followRedirects
open fun setFollPermanentRedirects(followRedirects: Boolean) {
_followPermanentRedirects = followRedirects
}
open fun getFollowPermanentRedirects() = _followPermanentRedirects
/************ /************
*** Call *** *** Call ***
************/ ************/

View File

@ -48,17 +48,11 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
if(!usesHttpOrHttps(client.baseUri)) { if(!usesHttpOrHttps(client.baseUri)) {
client.baseUri = buildFullHttpsUrl(client.baseUri) client.baseUri = buildFullHttpsUrl(client.baseUri)
} }
return tryToConnect(client)
var result = tryToConnect(client)
/*
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
Timber.d("Establishing secure connection failed, trying non secure connection")
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
result = tryToConnect(client)
} }
*/
return result private fun updateClientBaseUrl(client:OwnCloudClient, newBaseUrl:String) {
client.baseUri = Uri.parse(newBaseUrl)
} }
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> { private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
@ -66,7 +60,9 @@ class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
return try { return try {
val requester = StatusRequester() val requester = StatusRequester()
val requestResult = requester.request(baseUrl, client) val requestResult = requester.request(baseUrl, client)
requester.handleRequestResult(requestResult, baseUrl) val result = requester.handleRequestResult(requestResult, baseUrl)
updateClientBaseUrl(client, result.data.baseUrl)
return result
} catch (e: JSONException) { } catch (e: JSONException) {
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED) RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
} catch (e: Exception) { } catch (e: Exception) {

View File

@ -83,8 +83,10 @@ internal class StatusRequester {
var status: Int var status: Int
val getMethod = getGetMethod(currentLocation) val getMethod = getGetMethod(currentLocation)
getMethod.setFollPermanentRedirects(true)
status = client.executeHttpMethod(getMethod) status = client.executeHttpMethod(getMethod)
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
return RequestResult(getMethod, status, redirectedToUnsecureLocation, getMethod.getFinalUrl().toString())
} }
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK private fun Int.isSuccess() = this == HttpConstants.HTTP_OK