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

remove redirect code from owncloudclient use okhttp instead

This commit is contained in:
Christian Schabesberger 2021-09-14 17:35:05 +02:00
parent c59d1540c7
commit ce761aaec2
3 changed files with 6 additions and 101 deletions

View File

@ -128,16 +128,16 @@ 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 (mConnectionValidator != null && if (!mFollowRedirects &&
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 (mFollowRedirects) {
status = followRedirection(method).getLastStatus();
} }
} while (retry && repeatCounter < MAX_RETRY_COUNT); } 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. * Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
* *

View File

@ -66,7 +66,7 @@ public class RemoteOperationResult<T>
private String mHttpPhrase = null; private String mHttpPhrase = null;
private Exception mException = null; private Exception mException = null;
private ResultCode mCode = ResultCode.UNKNOWN_ERROR; private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
private String mRedirectedLocation; private String mRedirectedLocation = "";
private List<String> mAuthenticate = new ArrayList<>(); private List<String> mAuthenticate = new ArrayList<>();
private String mLastPermanentLocation = null; private String mLastPermanentLocation = null;
private T mData = null; private T mData = null;
@ -257,11 +257,11 @@ public class RemoteOperationResult<T>
this(httpCode, httpPhrase); this(httpCode, httpPhrase);
if (headers != null) { if (headers != null) {
for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) { for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) {
if ("location".equals(header.getKey().toLowerCase())) { if ("location".equalsIgnoreCase(header.getKey())) {
mRedirectedLocation = header.getValue().get(0); mRedirectedLocation = header.getValue().get(0);
continue; continue;
} }
if ("www-authenticate".equals(header.getKey().toLowerCase())) { if ("www-authenticate".equalsIgnoreCase(header.getKey())) {
for (String value: header.getValue()) { for (String value: header.getValue()) {
mAuthenticate.add(value.toLowerCase()); mAuthenticate.add(value.toLowerCase());
} }

View File

@ -51,16 +51,8 @@ class CheckPathExistenceRemoteOperation(
val remotePath: String? = "", val remotePath: String? = "",
val isUserLogged: Boolean val isUserLogged: Boolean
) : RemoteOperation<Boolean>() { ) : RemoteOperation<Boolean>() {
/**
* 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<Boolean> { override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> {
val previousFollowRedirects = client.getFollowRedirects()
return try { return try {
val stringUrl = val stringUrl =
if (isUserLogged) client.baseFilesWebDavUri.toString() if (isUserLogged) client.baseFilesWebDavUri.toString()
@ -72,10 +64,6 @@ class CheckPathExistenceRemoteOperation(
} }
var status = client.executeHttpMethod(propFindMethod) var status = client.executeHttpMethod(propFindMethod)
if (previousFollowRedirects) {
redirectionPath = client.followRedirection(propFindMethod)
status = redirectionPath?.lastStatus!!
}
/* PROPFIND method /* PROPFIND method
* 404 NOT FOUND: path doesn't exist, * 404 NOT FOUND: path doesn't exist,
* 207 MULTI_STATUS: path exists. * 207 MULTI_STATUS: path exists.
@ -93,8 +81,6 @@ class CheckPathExistenceRemoteOperation(
"Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}" "Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}"
) )
result result
} finally {
client.setFollowRedirects(previousFollowRedirects)
} }
} }