@@ -28,9 +28,7 @@ import com.owncloud.android.lib.common.OwnCloudClient
28
28
import com.owncloud.android.lib.common.http.HttpConstants
29
29
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
30
30
import com.owncloud.android.lib.common.operations.RemoteOperationResult
31
-
32
31
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
33
- import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
34
32
import org.json.JSONObject
35
33
import java.net.URL
36
34
import java.util.concurrent.TimeUnit
@@ -44,14 +42,21 @@ internal class StatusRequester {
44
42
* we assume it was a debug setup.
45
43
*/
46
44
fun isRedirectedToNonSecureConnection (
47
- redirectedToUnsecureLocationBefore : Boolean ,
45
+ redirectedToNonSecureLocationBefore : Boolean ,
48
46
baseUrl : String ,
49
47
redirectedUrl : String
50
- ) = redirectedToUnsecureLocationBefore
48
+ ) = redirectedToNonSecureLocationBefore
51
49
|| (baseUrl.startsWith(HTTPS_SCHEME )
52
50
&& ! redirectedUrl.startsWith(HTTPS_SCHEME ))
53
51
54
52
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
+
55
60
if (! redirectedLocation.startsWith(" /" ))
56
61
return redirectedLocation
57
62
val oldLocationURL = URL (oldLocation)
@@ -68,7 +73,8 @@ internal class StatusRequester {
68
73
data class RequestResult (
69
74
val getMethod : GetMethod ,
70
75
val status : Int ,
71
- val redirectedToUnsecureLocation : Boolean
76
+ val redirectedToUnsecureLocation : Boolean ,
77
+ val lastLocation : String
72
78
)
73
79
74
80
fun requestAndFollowRedirects (baseLocation : String , client : OwnCloudClient ): RequestResult {
@@ -85,7 +91,7 @@ internal class StatusRequester {
85
91
else RemoteOperationResult (getMethod)
86
92
87
93
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
88
- return RequestResult (getMethod, status, redirectedToUnsecureLocation)
94
+ return RequestResult (getMethod, status, redirectedToUnsecureLocation, currentLocation )
89
95
} else {
90
96
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
91
97
redirectedToUnsecureLocation =
@@ -104,7 +110,7 @@ internal class StatusRequester {
104
110
fun handleRequestResult (
105
111
requestResult : RequestResult ,
106
112
baseUrl : String
107
- ): RemoteOperationResult <OwnCloudVersion > {
113
+ ): RemoteOperationResult <RemoteServerInfo > {
108
114
if (! requestResult.status.isSuccess())
109
115
return RemoteOperationResult (requestResult.getMethod)
110
116
@@ -115,25 +121,35 @@ internal class StatusRequester {
115
121
val ocVersion = OwnCloudVersion (respJSON.getString(NODE_VERSION ))
116
122
// the version object will be returned even if the version is invalid, no error code;
117
123
// every app will decide how to act if (ocVersion.isVersionValid() == false)
118
- val result =
124
+ val result: RemoteOperationResult < RemoteServerInfo > =
119
125
if (requestResult.redirectedToUnsecureLocation) {
120
- RemoteOperationResult < OwnCloudVersion > (RemoteOperationResult .ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
126
+ RemoteOperationResult (RemoteOperationResult .ResultCode .OK_REDIRECT_TO_NON_SECURE_CONNECTION )
121
127
} 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 )
125
129
else RemoteOperationResult (RemoteOperationResult .ResultCode .OK_NO_SSL )
126
130
}
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
+ )
128
144
return result
129
145
}
130
146
131
147
companion object {
132
148
/* *
133
149
* Maximum time to wait for a response from the server when the connection is being tested,
134
- * in MILLISECONDs .
150
+ * in milliseconds .
135
151
*/
136
- private const val TRY_CONNECTION_TIMEOUT : Long = 5000
152
+ private const val TRY_CONNECTION_TIMEOUT = 5_000L
137
153
private const val NODE_INSTALLED = " installed"
138
154
private const val NODE_VERSION = " version"
139
155
}
0 commit comments