Skip to content

Commit 65fc85f

Browse files
authored
Fix bug with repeating slashes in URL (#40)
1 parent 986a1a4 commit 65fc85f

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed

core/common/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ dependencies {
1313
implementation di.koinCore
1414
implementation reactive.rxjava
1515
implementation log.timber
16+
testImplementation test.junit
1617
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.shifthackz.aisdv1.core.common.extensions
22

33
private const val PROTOCOL_DELIMITER = "://"
4+
private const val PROTOCOL_HOLDER = "[[_PROTOCOL_]]"
45

56
fun String.withoutUrlProtocol(): String {
67
if (!this.contains(PROTOCOL_DELIMITER)) return this
78
val decomposed = this.split(PROTOCOL_DELIMITER)
89
if (decomposed.size < 2) return this
910
return decomposed.last()
1011
}
12+
13+
fun String.fixUrlSlashes(): String = this
14+
.replace(PROTOCOL_DELIMITER, PROTOCOL_HOLDER)
15+
.replace(Regex("/{2,}"), "/")
16+
.let { str -> if (str.last() == '/') str.substring(0, str.lastIndex) else str }
17+
.replace(PROTOCOL_HOLDER, PROTOCOL_DELIMITER)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.shifthackz.aisdv1.core.common.extensions
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class StringExtensionsTest {
7+
8+
@Test
9+
fun `given string with slash at and, expected no slash at end`() {
10+
val actual = "http://192.168.228.9:7860/".fixUrlSlashes()
11+
val expected = "http://192.168.228.9:7860"
12+
Assert.assertEquals(expected, actual)
13+
}
14+
15+
@Test
16+
fun `given string with 3 slashes at and, expected no slashes at end`() {
17+
val actual = "http://192.168.228.9:7860///".fixUrlSlashes()
18+
val expected = "http://192.168.228.9:7860"
19+
Assert.assertEquals(expected, actual)
20+
}
21+
22+
@Test
23+
fun `given string with even and odd duplicates and slash at end, expected no duplicate slashes and slash at end`() {
24+
val actual = "http://192.168.228.9:7860///path1////path2/".fixUrlSlashes()
25+
val expected = "http://192.168.228.9:7860/path1/path2"
26+
Assert.assertEquals(expected, actual)
27+
}
28+
}

data/src/main/java/com/shifthackz/aisdv1/data/di/RemoteDataSourceModule.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.shifthackz.aisdv1.data.di
22

3+
import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
34
import com.shifthackz.aisdv1.core.common.links.LinksProvider
45
import com.shifthackz.aisdv1.data.gateway.ServerConnectivityGatewayImpl
56
import com.shifthackz.aisdv1.data.provider.ServerUrlProvider
@@ -33,7 +34,10 @@ val remoteDataSourceModule = module {
3334
val links = get<LinksProvider>()
3435
val chain = if (prefs.useSdAiCloud) Single.fromCallable(links::cloudUrl)
3536
else Single.fromCallable(prefs::serverUrl)
36-
chain.map { baseUrl -> "$baseUrl/$endpoint" }
37+
38+
chain
39+
.map(String::fixUrlSlashes)
40+
.map { baseUrl -> "$baseUrl/$endpoint" }
3741
}
3842
}
3943

data/src/main/java/com/shifthackz/aisdv1/data/preference/PreferenceManagerImpl.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.shifthackz.aisdv1.data.preference
22

33
import android.content.SharedPreferences
4+
import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
45
import com.shifthackz.aisdv1.domain.entity.Settings
56
import com.shifthackz.aisdv1.domain.preference.PreferenceManager
67
import io.reactivex.rxjava3.core.BackpressureStrategy
@@ -15,10 +16,10 @@ class PreferenceManagerImpl(
1516
BehaviorSubject.createDefault(Unit)
1617

1718
override var serverUrl: String
18-
get() = if (useSdAiCloud) ""
19-
else preferences.getString(KEY_SERVER_URL, "") ?: ""
19+
get() = (if (useSdAiCloud) "" else preferences.getString(KEY_SERVER_URL, "")
20+
?: "").fixUrlSlashes()
2021
set(value) = preferences.edit()
21-
.putString(KEY_SERVER_URL, if (useSdAiCloud) "" else value)
22+
.putString(KEY_SERVER_URL, (if (useSdAiCloud) "" else value).fixUrlSlashes())
2223
.apply()
2324
.also { onPreferencesChanged() }
2425

data/src/main/java/com/shifthackz/aisdv1/data/remote/StableDiffusionGenerationRemoteDataSource.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.shifthackz.aisdv1.data.remote
22

3+
import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
34
import com.shifthackz.aisdv1.data.mappers.mapToAiGenResult
45
import com.shifthackz.aisdv1.data.mappers.mapToRequest
56
import com.shifthackz.aisdv1.data.provider.ServerUrlProvider
@@ -20,7 +21,7 @@ internal class StableDiffusionGenerationRemoteDataSource(
2021
override fun checkAvailability() = serverUrlProvider("/")
2122
.flatMapCompletable(api::healthCheck)
2223

23-
override fun checkAvailability(url: String) = api.healthCheck(url)
24+
override fun checkAvailability(url: String) = api.healthCheck(url.fixUrlSlashes())
2425

2526
override fun textToImage(payload: TextToImagePayload) = serverUrlProvider(PATH_TXT_TO_IMG)
2627
.flatMap { url -> api.textToImage(url, payload.mapToRequest()) }

0 commit comments

Comments
 (0)