From ce761aaec21222e2de4e9d0d0a17d3746ce77e94 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Tue, 14 Sep 2021 17:35:05 +0200 Subject: [PATCH] remove redirect code from owncloudclient use okhttp instead --- .../android/lib/common/OwnCloudClient.java | 87 +------------------ .../operations/RemoteOperationResult.java | 6 +- .../CheckPathExistenceRemoteOperation.kt | 14 --- 3 files changed, 6 insertions(+), 101 deletions(-) diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java index fc58cfef..65e2dada 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java @@ -128,16 +128,16 @@ public class OwnCloudClient extends HttpClient { method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); } + method.setFollowRedirects(mFollowRedirects); status = method.execute(); stacklog(status, method); - if (mConnectionValidator != null && + if (!mFollowRedirects && + mConnectionValidator != null && (status == HttpConstants.HTTP_MOVED_TEMPORARILY || (!(mCredentials instanceof OwnCloudAnonymousCredentials) && status == HttpConstants.HTTP_UNAUTHORIZED))) { retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success - } else if (mFollowRedirects) { - status = followRedirection(method).getLastStatus(); } } while (retry && repeatCounter < MAX_RETRY_COUNT); @@ -167,87 +167,6 @@ public class OwnCloudClient extends HttpClient { } } - private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception { - int status; - String requestId = RandomUtils.generateRandomUUID(); - - // Header to allow tracing requests in apache and ownCloud logs - Timber.d("Executing in request with id %s", requestId); - method.setRequestHeader(OC_X_REQUEST_ID, requestId); - method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent()); - method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY); - if (mCredentials.getHeaderAuth() != null) { - method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth()); - } - status = method.execute(); - return status; - } - - public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception { - int redirectionsCount = 0; - int status = method.getStatusCode(); - RedirectionPath redirectionPath = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT); - - while (redirectionsCount < MAX_REDIRECTIONS_COUNT && - (status == HttpConstants.HTTP_MOVED_PERMANENTLY || - status == HttpConstants.HTTP_MOVED_TEMPORARILY || - status == HttpConstants.HTTP_TEMPORARY_REDIRECT) - ) { - - final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER) != null - ? method.getResponseHeader(HttpConstants.LOCATION_HEADER) - : method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER); - - if (location != null) { - Timber.d("#" + mInstanceNumber + "Location to redirect: " + location); - - redirectionPath.addLocation(location); - - // Release the connection to avoid reach the max number of connections per hostClientManager - // due to it will be set a different url - exhaustResponse(method.getResponseBodyAsStream()); - - Timber.d("+++++++++++++++++++++++++++++++++++++++ %s", getFullUrl(location)); - method.setUrl(getFullUrl(location)); - final String destination = method.getRequestHeader("Destination") != null - ? method.getRequestHeader("Destination") - : method.getRequestHeader("destination"); - - if (destination != null) { - final int suffixIndex = location.lastIndexOf(getUserFilesWebDavUri().toString()); - final String redirectionBase = location.substring(0, suffixIndex); - final String destinationPath = destination.substring(mBaseUri.toString().length()); - - method.setRequestHeader("destination", redirectionBase + destinationPath); - } - try { - status = executeRedirectedHttpMethod(method); - } catch (HttpException e) { - if (e.getMessage().contains(Integer.toString(HttpConstants.HTTP_MOVED_TEMPORARILY))) { - status = HttpConstants.HTTP_MOVED_TEMPORARILY; - } else { - throw e; - } - } - redirectionPath.addStatus(status); - redirectionsCount++; - - } else { - Timber.d(" #" + mInstanceNumber + "No location to redirect!"); - status = HttpConstants.HTTP_NOT_FOUND; - } - } - return redirectionPath; - } - - private HttpUrl getFullUrl(String redirection) { - if(redirection.startsWith("/")) { - return HttpUrl.parse(mBaseUri.toString() + redirection); - } else { - return HttpUrl.parse(redirection); - } - } - /** * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation. * diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java index 4f707821..a30a43a5 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java @@ -66,7 +66,7 @@ public class RemoteOperationResult private String mHttpPhrase = null; private Exception mException = null; private ResultCode mCode = ResultCode.UNKNOWN_ERROR; - private String mRedirectedLocation; + private String mRedirectedLocation = ""; private List mAuthenticate = new ArrayList<>(); private String mLastPermanentLocation = null; private T mData = null; @@ -257,11 +257,11 @@ public class RemoteOperationResult this(httpCode, httpPhrase); if (headers != null) { for (Map.Entry> header : headers.toMultimap().entrySet()) { - if ("location".equals(header.getKey().toLowerCase())) { + if ("location".equalsIgnoreCase(header.getKey())) { mRedirectedLocation = header.getValue().get(0); continue; } - if ("www-authenticate".equals(header.getKey().toLowerCase())) { + if ("www-authenticate".equalsIgnoreCase(header.getKey())) { for (String value: header.getValue()) { mAuthenticate.add(value.toLowerCase()); } diff --git a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt index 5e0d974c..94fb7227 100644 --- a/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt +++ b/owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt @@ -51,16 +51,8 @@ class CheckPathExistenceRemoteOperation( val remotePath: String? = "", val isUserLogged: Boolean ) : RemoteOperation() { - /** - * Gets the sequence of redirections followed during the execution of the operation. - * - * @return Sequence of redirections followed, if any, or NULL if the operation was not executed. - */ - var redirectionPath: RedirectionPath? = null - private set override fun run(client: OwnCloudClient): RemoteOperationResult { - val previousFollowRedirects = client.getFollowRedirects() return try { val stringUrl = if (isUserLogged) client.baseFilesWebDavUri.toString() @@ -72,10 +64,6 @@ class CheckPathExistenceRemoteOperation( } var status = client.executeHttpMethod(propFindMethod) - if (previousFollowRedirects) { - redirectionPath = client.followRedirection(propFindMethod) - status = redirectionPath?.lastStatus!! - } /* PROPFIND method * 404 NOT FOUND: path doesn't exist, * 207 MULTI_STATUS: path exists. @@ -93,8 +81,6 @@ class CheckPathExistenceRemoteOperation( "Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}" ) result - } finally { - client.setFollowRedirects(previousFollowRedirects) } }