mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-06 23:46:30 +00:00
remove redirect code from status requestor
This commit is contained in:
parent
7a2c61d3bf
commit
409e37b913
@ -13,6 +13,7 @@ buildscript {
|
|||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
|
||||||
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
|
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
|
@ -48,4 +48,8 @@ android {
|
|||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* Client used to perform network operations
|
* Client used to perform network operations
|
||||||
*
|
*
|
||||||
* @author David González Verdugo
|
* @author David González Verdugo
|
||||||
|
* @author Christian Schabesberger
|
||||||
*/
|
*/
|
||||||
public class HttpClient {
|
public class HttpClient {
|
||||||
private static OkHttpClient sOkHttpClient;
|
private static OkHttpClient sOkHttpClient;
|
||||||
@ -69,9 +70,7 @@ public class HttpClient {
|
|||||||
|
|
||||||
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
|
||||||
|
|
||||||
// Automatic cookie handling, NOT PERSISTENT
|
sOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager);
|
||||||
final CookieJar cookieJar = getNewCookieJar();
|
|
||||||
sOkHttpClient = buildOkHttpClient(cookieJar, sslSocketFactory, trustManager);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Timber.e(e, "Could not setup SSL system.");
|
Timber.e(e, "Could not setup SSL system.");
|
||||||
@ -110,29 +109,27 @@ public class HttpClient {
|
|||||||
List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet);
|
List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet);
|
||||||
|
|
||||||
sCookieStore.put(url.host(), nonDuplicatedCookiesList);
|
sCookieStore.put(url.host(), nonDuplicatedCookiesList);
|
||||||
System.out.println("set cookiestore size " + url.toString() + " " + sCookieStore.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Cookie> loadForRequest(HttpUrl url) {
|
public List<Cookie> loadForRequest(HttpUrl url) {
|
||||||
System.out.println("get cookiestore size " + url.toString() + " " + sCookieStore.size());
|
|
||||||
List<Cookie> cookies = sCookieStore.get(url.host());
|
List<Cookie> cookies = sCookieStore.get(url.host());
|
||||||
return cookies != null ? cookies : new ArrayList<>();
|
return cookies != null ? cookies : new ArrayList<>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static OkHttpClient buildOkHttpClient(CookieJar cookieJar, SSLSocketFactory sslSocketFactory,
|
private static OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory,
|
||||||
X509TrustManager trustManager) {
|
X509TrustManager trustManager) {
|
||||||
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
|
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
|
||||||
.protocols(Arrays.asList(Protocol.HTTP_1_1))
|
.protocols(Arrays.asList(Protocol.HTTP_1_1))
|
||||||
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
|
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||||
.writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
|
.writeTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||||
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
|
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||||
.followRedirects(false)
|
.followRedirects(true)
|
||||||
.sslSocketFactory(sslSocketFactory, trustManager)
|
.sslSocketFactory(sslSocketFactory, trustManager)
|
||||||
.hostnameVerifier((placeholder1, placeholder2) -> true)
|
.hostnameVerifier((placeholder1, placeholder2) -> true)
|
||||||
.cookieJar(cookieJar);
|
.cookieJar(getNewCookieJar());
|
||||||
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
|
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
|
||||||
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
|
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
|
||||||
return clientBuilder.build();
|
return clientBuilder.build();
|
||||||
|
@ -134,6 +134,12 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addRedirectChainHandler(redirectChainHandler: RedirectChainHandler) {
|
||||||
|
okHttpClient = okHttpClient.newBuilder()
|
||||||
|
.addNetworkInterceptor(redirectChainHandler)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
/************
|
/************
|
||||||
*** Call ***
|
*** Call ***
|
||||||
************/
|
************/
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.owncloud.android.lib.common.http.methods
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.resources.status.HttpScheme
|
||||||
|
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
|
||||||
|
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import okhttp3.Response
|
||||||
|
|
||||||
|
class RedirectChainHandler : Interceptor {
|
||||||
|
private val _redirectChain = arrayListOf<String>()
|
||||||
|
|
||||||
|
val redirectChain: List<String>
|
||||||
|
get() = _redirectChain
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is only true if http and https requests where in the request chain.
|
||||||
|
*/
|
||||||
|
val hasBeenRedirectedUnsecureLocation: Boolean
|
||||||
|
get() {
|
||||||
|
var containsHttpsRequests = false
|
||||||
|
var containsHttpRequests = false
|
||||||
|
for(url in _redirectChain) {
|
||||||
|
when {
|
||||||
|
url.startsWith(HTTPS_SCHEME) -> containsHttpsRequests = true
|
||||||
|
url.startsWith("$HTTP_SCHEME://") -> containsHttpRequests = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return containsHttpRequests && containsHttpsRequests
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
|
_redirectChain.add(chain.request().url.toString())
|
||||||
|
return chain.proceed(chain.request())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -90,10 +90,13 @@ class CheckPathExistenceRemoteOperation(
|
|||||||
|
|
||||||
client.setFollowRedirects(false)
|
client.setFollowRedirects(false)
|
||||||
var status = client.executeHttpMethod(propFindMethod)
|
var status = client.executeHttpMethod(propFindMethod)
|
||||||
|
/*
|
||||||
if (previousFollowRedirects) {
|
if (previousFollowRedirects) {
|
||||||
redirectionPath = client.followRedirection(propFindMethod)
|
redirectionPath = client.followRedirection(propFindMethod)
|
||||||
status = redirectionPath?.lastStatus!!
|
status = redirectionPath?.lastStatus!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
handleResult(requestUrl, status, propFindMethod)
|
handleResult(requestUrl, status, propFindMethod)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
val result = RemoteOperationResult<Boolean>(e)
|
val result = RemoteOperationResult<Boolean>(e)
|
||||||
|
@ -44,11 +44,15 @@ import timber.log.Timber
|
|||||||
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
|
class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
|
override fun run(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
|
||||||
|
//try to connect with ssl
|
||||||
if (client.baseUri.scheme.isNullOrEmpty())
|
if (client.baseUri.scheme.isNullOrEmpty())
|
||||||
client.baseUri = Uri.parse("$HTTPS_SCHEME://${client.baseUri}")
|
client.baseUri = Uri.parse("$HTTPS_SCHEME://${client.baseUri}")
|
||||||
|
|
||||||
var result = tryToConnect(client)
|
var result = tryToConnect(client)
|
||||||
if (result.code != ResultCode.OK_SSL && !result.isSslRecoverableException) {
|
|
||||||
|
//try to connect without ssl
|
||||||
|
if (result.code != ResultCode.OK_SSL
|
||||||
|
&& !result.isSslRecoverableException
|
||||||
|
&& !result.isRedirectToNonSecureConnection) {
|
||||||
Timber.d("Establishing secure connection failed, trying non secure connection")
|
Timber.d("Establishing secure connection failed, trying non secure connection")
|
||||||
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
|
client.baseUri = client.baseUri.buildUpon().scheme(HTTP_SCHEME).build()
|
||||||
result = tryToConnect(client)
|
result = tryToConnect(client)
|
||||||
@ -59,10 +63,9 @@ class GetRemoteStatusOperation : RemoteOperation<OwnCloudVersion>() {
|
|||||||
|
|
||||||
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
|
private fun tryToConnect(client: OwnCloudClient): RemoteOperationResult<OwnCloudVersion> {
|
||||||
val baseUrl = client.baseUri.toString()
|
val baseUrl = client.baseUri.toString()
|
||||||
client.setFollowRedirects(false)
|
|
||||||
return try {
|
return try {
|
||||||
val requester = StatusRequester()
|
val requester = StatusRequester()
|
||||||
val requestResult = requester.requestAndFollowRedirects(baseUrl, client)
|
val requestResult = requester.requestStatus(baseUrl, client)
|
||||||
requester.handleRequestResult(requestResult, baseUrl)
|
requester.handleRequestResult(requestResult, baseUrl)
|
||||||
} catch (e: JSONException) {
|
} catch (e: JSONException) {
|
||||||
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
RemoteOperationResult(ResultCode.INSTANCE_NOT_CONFIGURED)
|
||||||
|
@ -26,32 +26,15 @@ package com.owncloud.android.lib.resources.status
|
|||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
import com.owncloud.android.lib.common.http.HttpConstants
|
||||||
|
import com.owncloud.android.lib.common.http.methods.RedirectChainHandler
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
|
import com.owncloud.android.lib.resources.status.HttpScheme.HTTPS_SCHEME
|
||||||
import com.owncloud.android.lib.resources.status.HttpScheme.HTTP_SCHEME
|
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
internal class StatusRequester {
|
internal class StatusRequester {
|
||||||
|
|
||||||
private fun checkIfConnectionIsRedirectedToNoneSecure(
|
|
||||||
isConnectionSecure: Boolean,
|
|
||||||
baseUrl: String,
|
|
||||||
redirectedUrl: String
|
|
||||||
): Boolean {
|
|
||||||
return isConnectionSecure ||
|
|
||||||
(baseUrl.startsWith(HTTPS_SCHEME) && redirectedUrl.startsWith(HTTP_SCHEME))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateLocationWithRedirectPath(oldLocation: String, redirectedLocation: String): String {
|
|
||||||
if (!redirectedLocation.startsWith("/"))
|
|
||||||
return redirectedLocation
|
|
||||||
val oldLocationURL = URL(oldLocation)
|
|
||||||
return URL(oldLocationURL.protocol, oldLocationURL.host, oldLocationURL.port, redirectedLocation).toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getGetMethod(url: String): GetMethod {
|
private fun getGetMethod(url: String): GetMethod {
|
||||||
return GetMethod(URL(url)).apply {
|
return GetMethod(URL(url)).apply {
|
||||||
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
setReadTimeout(TRY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
||||||
@ -66,32 +49,20 @@ internal class StatusRequester {
|
|||||||
val redirectedToUnsecureLocation: Boolean
|
val redirectedToUnsecureLocation: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
fun requestAndFollowRedirects(baseLocation: String, client: OwnCloudClient): RequestResult {
|
fun requestStatus(baseLocation: String, client: OwnCloudClient): RequestResult {
|
||||||
var currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
|
var currentLocation = baseLocation + OwnCloudClient.STATUS_PATH
|
||||||
var redirectedToUnsecureLocation = false
|
|
||||||
var status: Int
|
var status: Int
|
||||||
|
|
||||||
while (true) {
|
val getMethod = getGetMethod(currentLocation)
|
||||||
val getMethod = getGetMethod(currentLocation)
|
val redirectChainHandler = RedirectChainHandler()
|
||||||
|
getMethod.addRedirectChainHandler(redirectChainHandler)
|
||||||
|
|
||||||
status = client.executeHttpMethod(getMethod)
|
status = client.executeHttpMethod(getMethod)
|
||||||
val result =
|
val result =
|
||||||
if (status.isSuccess()) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
|
if (status.isSuccess()) RemoteOperationResult<OwnCloudVersion>(RemoteOperationResult.ResultCode.OK)
|
||||||
else RemoteOperationResult(getMethod)
|
else RemoteOperationResult(getMethod)
|
||||||
|
|
||||||
if (result.redirectedLocation.isNullOrEmpty() || result.isSuccess) {
|
return RequestResult(getMethod, status, result, redirectChainHandler.hasBeenRedirectedUnsecureLocation)
|
||||||
return RequestResult(getMethod, status, result, redirectedToUnsecureLocation)
|
|
||||||
} else {
|
|
||||||
val nextLocation = updateLocationWithRedirectPath(currentLocation, result.redirectedLocation)
|
|
||||||
redirectedToUnsecureLocation =
|
|
||||||
checkIfConnectionIsRedirectedToNoneSecure(
|
|
||||||
redirectedToUnsecureLocation,
|
|
||||||
currentLocation,
|
|
||||||
nextLocation
|
|
||||||
)
|
|
||||||
currentLocation = nextLocation
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK
|
private fun Int.isSuccess() = this == HttpConstants.HTTP_OK
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 ownCloud GmbH.
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
||||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.owncloud.android.lib
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.resources.status.StatusRequester
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
class StatusRequestorTest {
|
|
||||||
private val requestor = StatusRequester()
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `update location with an absolute path`() {
|
|
||||||
val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, "$TEST_DOMAIN$SUB_PATH")
|
|
||||||
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
|
|
||||||
fun `update location with a smaller absolute path`() {
|
|
||||||
val newLocation = requestor.updateLocationWithRedirectPath("$TEST_DOMAIN$SUB_PATH", TEST_DOMAIN)
|
|
||||||
assertEquals(TEST_DOMAIN, newLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `update location with a relative path`() {
|
|
||||||
val newLocation = requestor.updateLocationWithRedirectPath(TEST_DOMAIN, SUB_PATH)
|
|
||||||
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `update location by replacing the relative path`() {
|
|
||||||
val newLocation = requestor.updateLocationWithRedirectPath(
|
|
||||||
"$TEST_DOMAIN/some/other/subdir", SUB_PATH
|
|
||||||
)
|
|
||||||
assertEquals("$TEST_DOMAIN$SUB_PATH", newLocation)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val TEST_DOMAIN = "https://cloud.somewhere.com"
|
|
||||||
const val SUB_PATH = "/subdir"
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user