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

Commit 3a79a86

Browse files
committed
Fix 301 redirections
1 parent 1194b9a commit 3a79a86

File tree

6 files changed

+73
-29
lines changed

6 files changed

+73
-29
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.content.Context;
2828
import android.net.Uri;
2929

30+
import com.owncloud.android.lib.common.http.HttpClient;
3031
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation;
3132

3233
public class OwnCloudClientFactory {
@@ -44,13 +45,13 @@ public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, bool
4445

4546
client.setFollowRedirects(followRedirects);
4647

47-
client.setContext(context);
48-
retriveCookisFromMiddleware(client);
48+
HttpClient.setContext(context);
49+
retrieveCookiesFromMiddleware(client);
4950

5051
return client;
5152
}
5253

53-
public static void retriveCookisFromMiddleware(OwnCloudClient client) {
54+
private static void retrieveCookiesFromMiddleware(OwnCloudClient client) {
5455
final GetRemoteStatusOperation statusOperation = new GetRemoteStatusOperation();
5556
statusOperation.run(client);
5657
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/GetRemoteStatusOperation.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperation
2929
import com.owncloud.android.lib.common.operations.RemoteOperationResult
3030
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
3131
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_PREFIX
32-
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
3332
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_PREFIX
3433
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
3534
import org.json.JSONException
@@ -43,15 +42,13 @@ import timber.log.Timber
4342
* @author David González Verdugo
4443
* @author Abel García de Prada
4544
*/
46-
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
45+
class GetRemoteStatusOperation : RemoteOperation<RemoteServerInfo>() {
4746

48-
public override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
47+
public override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
4948
client.baseUri = buildFullHttpsUrl(client.baseUri)
5049

5150
var result = tryToConnect(client)
52-
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL)
53-
&& !result.isSslRecoverableException
54-
) {
51+
if (!(result.code == ResultCode.OK || result.code == ResultCode.OK_SSL) && !result.isSslRecoverableException) {
5552
Timber.d("Establishing secure connection failed, trying non secure connection")
5653
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
5754
result = tryToConnect(client)
@@ -60,7 +57,7 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
6057
return result
6158
}
6259

63-
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
60+
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo> {
6461
val baseUrl = client.baseUri.toString()
6562
client.setFollowRedirects(false)
6663
return try {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2020 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
package com.owncloud.android.lib.resources.status
25+
26+
data class RemoteServerInfo(
27+
val ownCloudVersion: OwnCloudVersion,
28+
val baseUrl: String,
29+
val isSecureConnection: Boolean
30+
)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/StatusRequester.kt

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import com.owncloud.android.lib.common.OwnCloudClient
2828
import com.owncloud.android.lib.common.http.HttpConstants
2929
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
3030
import com.owncloud.android.lib.common.operations.RemoteOperationResult
31-
3231
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
33-
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
3432
import org.json.JSONObject
3533
import java.net.URL
3634
import java.util.concurrent.TimeUnit
@@ -44,14 +42,21 @@ internal class StatusRequester {
4442
* we assume it was a debug setup.
4543
*/
4644
fun isRedirectedToNonSecureConnection(
47-
redirectedToUnsecureLocationBefore: Boolean,
45+
redirectedToNonSecureLocationBefore: Boolean,
4846
baseUrl: String,
4947
redirectedUrl: String
50-
) = redirectedToUnsecureLocationBefore
48+
) = redirectedToNonSecureLocationBefore
5149
|| (baseUrl.startsWith(HTTPS_SCHEME)
5250
&& !redirectedUrl.startsWith(HTTPS_SCHEME))
5351

5452
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
53+
/** Redirection with different endpoint.
54+
* When asking for server.com/status.php and redirected to different.one/, we need to ask different.one/status.php
55+
*/
56+
if (redirectedLocation.endsWith('/')) {
57+
return redirectedLocation.trimEnd('/') + OwnCloudClient.STATUS_PATH
58+
}
59+
5560
if (!redirectedLocation.startsWith("/"))
5661
return redirectedLocation
5762
val oldLocationURL = URL(oldLocation)
@@ -68,7 +73,8 @@ internal class StatusRequester {
6873
data class RequestResult(
6974
val getMethod: GetMethod,
7075
val status: Int,
71-
val redirectedToUnsecureLocation: Boolean
76+
val redirectedToUnsecureLocation: Boolean,
77+
val lastLocation: String
7278
)
7379

7480
fun requestAndFollowRedirects(baseLocation: String, client: OwnCloudClient): RequestResult {
@@ -85,7 +91,7 @@ internal class StatusRequester {
8591
else RemoteOperationResult(getMethod)
8692

8793
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
88-
return RequestResult(getMethod, status, redirectedToUnsecureLocation)
94+
return RequestResult(getMethod, status, redirectedToUnsecureLocation, currentLocation)
8995
} else {
9096
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
9197
redirectedToUnsecureLocation =
@@ -104,7 +110,7 @@ internal class StatusRequester {
104110
fun handleRequestResult(
105111
requestResult: RequestResult,
106112
baseUrl: String
107-
): RemoteOperationResult<OwnCloudVersion> {
113+
): RemoteOperationResult<RemoteServerInfo> {
108114
if (!requestResult.status.isSuccess())
109115
return RemoteOperationResult(requestResult.getMethod)
110116

@@ -115,25 +121,35 @@ internal class StatusRequester {
115121
val ocVersion = OwnCloudVersion(respJSON.getString(NODE_VERSION))
116122
// the version object will be returned even if the version is invalid, no error code;
117123
// every app will decide how to act if (ocVersion.isVersionValid() == false)
118-
val result =
124+
val result: RemoteOperationResult<RemoteServerInfo> =
119125
if (requestResult.redirectedToUnsecureLocation) {
120-
RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
126+
RemoteOperationResult(RemoteOperationResult.ResultCode.OK_REDIRECT_TO_NON_SECURE_CONNECTION)
121127
} else {
122-
if (baseUrl.startsWith(HTTPS_SCHEME)) RemoteOperationResult(
123-
RemoteOperationResult.ResultCode.OK_SSL
124-
)
128+
if (baseUrl.startsWith(HTTPS_SCHEME)) RemoteOperationResult(RemoteOperationResult.ResultCode.OK_SSL)
125129
else RemoteOperationResult(RemoteOperationResult.ResultCode.OK_NO_SSL)
126130
}
127-
result.data = ocVersion
131+
val finalUrl = URL(requestResult.lastLocation)
132+
val finalBaseUrl = URL(
133+
finalUrl.protocol,
134+
finalUrl.host,
135+
finalUrl.port,
136+
finalUrl.file.dropLastWhile { it != '/' }.trimEnd('/')
137+
)
138+
139+
result.data = RemoteServerInfo(
140+
ownCloudVersion = ocVersion,
141+
baseUrl = finalBaseUrl.toString(),
142+
isSecureConnection = finalBaseUrl.protocol.startsWith(HTTPS_SCHEME)
143+
)
128144
return result
129145
}
130146

131147
companion object {
132148
/**
133149
* Maximum time to wait for a response from the server when the connection is being tested,
134-
* in MILLISECONDs.
150+
* in milliseconds.
135151
*/
136-
private const val TRY_CONNECTION_TIMEOUT: Long = 5000
152+
private const val TRY_CONNECTION_TIMEOUT = 5_000L
137153
private const val NODE_INSTALLED = "installed"
138154
private const val NODE_VERSION = "version"
139155
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/ServerInfoService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ package com.owncloud.android.lib.resources.status.services
2525

2626
import com.owncloud.android.lib.common.OwnCloudClient
2727
import com.owncloud.android.lib.common.operations.RemoteOperationResult
28-
import com.owncloud.android.lib.resources.status.OwnCloudVersion
28+
import com.owncloud.android.lib.resources.status.RemoteServerInfo
2929

3030
interface ServerInfoService {
3131
fun checkPathExistence(path: String, isUserLogged: Boolean, client: OwnCloudClient): RemoteOperationResult<Boolean>
3232

33-
fun getRemoteStatus(path: String, client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion>
33+
fun getRemoteStatus(path: String, client: OwnCloudClient): RemoteOperationResult<RemoteServerInfo>
3434
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/status/services/implementation/OCServerInfoService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import com.owncloud.android.lib.common.OwnCloudClient
2323
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2424
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
2525
import com.owncloud.android.lib.resources.status.GetRemoteStatusOperation
26-
import com.owncloud.android.lib.resources.status.OwnCloudVersion
26+
import com.owncloud.android.lib.resources.status.RemoteServerInfo
2727
import com.owncloud.android.lib.resources.status.services.ServerInfoService
2828

2929
class OCServerInfoService : ServerInfoService {
@@ -41,6 +41,6 @@ class OCServerInfoService : ServerInfoService {
4141
override fun getRemoteStatus(
4242
path: String,
4343
client: OwnCloudClient
44-
): RemoteOperationResult<OwnCloudVersion> =
44+
): RemoteOperationResult<RemoteServerInfo> =
4545
GetRemoteStatusOperation().execute(client)
4646
}

0 commit comments

Comments
 (0)