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

Commit ce761aa

Browse files
committed
remove redirect code from owncloudclient use okhttp instead
1 parent c59d154 commit ce761aa

File tree

3 files changed

+6
-101
lines changed

3 files changed

+6
-101
lines changed

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

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
128128
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
129129
}
130130

131+
method.setFollowRedirects(mFollowRedirects);
131132
status = method.execute();
132133
stacklog(status, method);
133134

134-
if (mConnectionValidator != null &&
135+
if (!mFollowRedirects &&
136+
mConnectionValidator != null &&
135137
(status == HttpConstants.HTTP_MOVED_TEMPORARILY ||
136138
(!(mCredentials instanceof OwnCloudAnonymousCredentials) &&
137139
status == HttpConstants.HTTP_UNAUTHORIZED))) {
138140
retry = mConnectionValidator.validate(this, mSingleSessionManager); // retry on success fail on no success
139-
} else if (mFollowRedirects) {
140-
status = followRedirection(method).getLastStatus();
141141
}
142142

143143
} while (retry && repeatCounter < MAX_RETRY_COUNT);
@@ -167,87 +167,6 @@ private void stacklog(int status, HttpBaseMethod method) {
167167
}
168168
}
169169

170-
private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception {
171-
int status;
172-
String requestId = RandomUtils.generateRandomUUID();
173-
174-
// Header to allow tracing requests in apache and ownCloud logs
175-
Timber.d("Executing in request with id %s", requestId);
176-
method.setRequestHeader(OC_X_REQUEST_ID, requestId);
177-
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
178-
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
179-
if (mCredentials.getHeaderAuth() != null) {
180-
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
181-
}
182-
status = method.execute();
183-
return status;
184-
}
185-
186-
public RedirectionPath followRedirection(HttpBaseMethod method) throws Exception {
187-
int redirectionsCount = 0;
188-
int status = method.getStatusCode();
189-
RedirectionPath redirectionPath = new RedirectionPath(status, MAX_REDIRECTIONS_COUNT);
190-
191-
while (redirectionsCount < MAX_REDIRECTIONS_COUNT &&
192-
(status == HttpConstants.HTTP_MOVED_PERMANENTLY ||
193-
status == HttpConstants.HTTP_MOVED_TEMPORARILY ||
194-
status == HttpConstants.HTTP_TEMPORARY_REDIRECT)
195-
) {
196-
197-
final String location = method.getResponseHeader(HttpConstants.LOCATION_HEADER) != null
198-
? method.getResponseHeader(HttpConstants.LOCATION_HEADER)
199-
: method.getResponseHeader(HttpConstants.LOCATION_HEADER_LOWER);
200-
201-
if (location != null) {
202-
Timber.d("#" + mInstanceNumber + "Location to redirect: " + location);
203-
204-
redirectionPath.addLocation(location);
205-
206-
// Release the connection to avoid reach the max number of connections per hostClientManager
207-
// due to it will be set a different url
208-
exhaustResponse(method.getResponseBodyAsStream());
209-
210-
Timber.d("+++++++++++++++++++++++++++++++++++++++ %s", getFullUrl(location));
211-
method.setUrl(getFullUrl(location));
212-
final String destination = method.getRequestHeader("Destination") != null
213-
? method.getRequestHeader("Destination")
214-
: method.getRequestHeader("destination");
215-
216-
if (destination != null) {
217-
final int suffixIndex = location.lastIndexOf(getUserFilesWebDavUri().toString());
218-
final String redirectionBase = location.substring(0, suffixIndex);
219-
final String destinationPath = destination.substring(mBaseUri.toString().length());
220-
221-
method.setRequestHeader("destination", redirectionBase + destinationPath);
222-
}
223-
try {
224-
status = executeRedirectedHttpMethod(method);
225-
} catch (HttpException e) {
226-
if (e.getMessage().contains(Integer.toString(HttpConstants.HTTP_MOVED_TEMPORARILY))) {
227-
status = HttpConstants.HTTP_MOVED_TEMPORARILY;
228-
} else {
229-
throw e;
230-
}
231-
}
232-
redirectionPath.addStatus(status);
233-
redirectionsCount++;
234-
235-
} else {
236-
Timber.d(" #" + mInstanceNumber + "No location to redirect!");
237-
status = HttpConstants.HTTP_NOT_FOUND;
238-
}
239-
}
240-
return redirectionPath;
241-
}
242-
243-
private HttpUrl getFullUrl(String redirection) {
244-
if(redirection.startsWith("/")) {
245-
return HttpUrl.parse(mBaseUri.toString() + redirection);
246-
} else {
247-
return HttpUrl.parse(redirection);
248-
}
249-
}
250-
251170
/**
252171
* Exhausts a not interesting HTTP response. Encouraged by HttpClient documentation.
253172
*

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/operations/RemoteOperationResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class RemoteOperationResult<T>
6666
private String mHttpPhrase = null;
6767
private Exception mException = null;
6868
private ResultCode mCode = ResultCode.UNKNOWN_ERROR;
69-
private String mRedirectedLocation;
69+
private String mRedirectedLocation = "";
7070
private List<String> mAuthenticate = new ArrayList<>();
7171
private String mLastPermanentLocation = null;
7272
private T mData = null;
@@ -257,11 +257,11 @@ public RemoteOperationResult(int httpCode, String httpPhrase, Headers headers) {
257257
this(httpCode, httpPhrase);
258258
if (headers != null) {
259259
for (Map.Entry<String, List<String>> header : headers.toMultimap().entrySet()) {
260-
if ("location".equals(header.getKey().toLowerCase())) {
260+
if ("location".equalsIgnoreCase(header.getKey())) {
261261
mRedirectedLocation = header.getValue().get(0);
262262
continue;
263263
}
264-
if ("www-authenticate".equals(header.getKey().toLowerCase())) {
264+
if ("www-authenticate".equalsIgnoreCase(header.getKey())) {
265265
for (String value: header.getValue()) {
266266
mAuthenticate.add(value.toLowerCase());
267267
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/CheckPathExistenceRemoteOperation.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,8 @@ class CheckPathExistenceRemoteOperation(
5151
val remotePath: String? = "",
5252
val isUserLogged: Boolean
5353
) : RemoteOperation<Boolean>() {
54-
/**
55-
* Gets the sequence of redirections followed during the execution of the operation.
56-
*
57-
* @return Sequence of redirections followed, if any, or NULL if the operation was not executed.
58-
*/
59-
var redirectionPath: RedirectionPath? = null
60-
private set
6154

6255
override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> {
63-
val previousFollowRedirects = client.getFollowRedirects()
6456
return try {
6557
val stringUrl =
6658
if (isUserLogged) client.baseFilesWebDavUri.toString()
@@ -72,10 +64,6 @@ class CheckPathExistenceRemoteOperation(
7264
}
7365

7466
var status = client.executeHttpMethod(propFindMethod)
75-
if (previousFollowRedirects) {
76-
redirectionPath = client.followRedirection(propFindMethod)
77-
status = redirectionPath?.lastStatus!!
78-
}
7967
/* PROPFIND method
8068
* 404 NOT FOUND: path doesn't exist,
8169
* 207 MULTI_STATUS: path exists.
@@ -93,8 +81,6 @@ class CheckPathExistenceRemoteOperation(
9381
"Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}"
9482
)
9583
result
96-
} finally {
97-
client.setFollowRedirects(previousFollowRedirects)
9884
}
9985
}
10086

0 commit comments

Comments
 (0)