Skip to content

Commit

Permalink
Update KTOR to 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Jun 14, 2022
1 parent d2f75a9 commit 9e4a896
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "info.dvkr.screenstream"
minSdkVersion(21)
targetSdkVersion(32)
versionCode = 30809
versionName = "3.8.9"
versionCode = 30900
versionName = "3.9.0"
resConfigs 'en', 'ru', 'pt-rBR', 'zh-rTW', 'fr-rFR', 'fa', 'it', 'pl', 'hi', 'de', 'sk', 'es', 'ar', 'ja', 'gl', 'ca', 'uk', 'nl'

vectorDrawables.useSupportLibrary = true
Expand Down
5 changes: 4 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
-keep class com.google.firebase.crashlytics.** { *; }

#KTOR
-keep class io.ktor.application.Application
-keep class io.ktor.server.application.Application
-keep class kotlin.reflect.jvm.internal.**
-keep class kotlin.text.RegexOption
-dontwarn java.lang.management.ManagementFactory
-dontwarn java.lang.management.RuntimeMXBean
-dontwarn org.slf4j.impl.StaticLoggerBinder

#datastore-preferences
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ val baseKoinModule = module {
single<com.ironz.binaryprefs.Preferences> {
BinaryPreferencesBuilder(androidApplication())
.supportInterProcess(true)
.memoryCacheMode(BinaryPreferencesBuilder.MemoryCacheMode.EAGER)
.exceptionHandler { ex -> XLog.e(ex) }
.build()
}
Expand Down
4 changes: 3 additions & 1 deletion data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ dependencies {
implementation("androidx.core:core:1.8.0")
implementation("androidx.window:window:1.0.0")

implementation("io.ktor:ktor-server-cio:1.6.8")
implementation("io.ktor:ktor-server-cio:2.0.2")
implementation("io.ktor:ktor-server-default-headers:2.0.2")
implementation("io.ktor:ktor-server-status-pages:2.0.2")

implementation("com.github.iamironz:binaryprefs:1.0.1") //Migrating to datastore
implementation("androidx.datastore:datastore-preferences:1.0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
Expand All @@ -40,6 +41,7 @@ import java.io.IOException
import java.net.BindException
import java.util.concurrent.atomic.AtomicReference

@OptIn(ExperimentalCoroutinesApi::class)
internal class HttpServer(
applicationContext: Context,
private val parentCoroutineScope: CoroutineScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@ import com.elvishew.xlog.XLog
import info.dvkr.screenstream.data.model.FatalError
import info.dvkr.screenstream.data.other.getLog
import info.dvkr.screenstream.data.other.randomString
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.http.cio.*
import io.ktor.http.content.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.application.*
import io.ktor.server.cio.*
import io.ktor.util.*
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.utils.io.*
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import java.net.InetSocketAddress
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference

@OptIn(InternalAPI::class)
internal fun Application.appModule(
httpServerFiles: HttpServerFiles,
clientData: ClientData,
Expand Down Expand Up @@ -66,16 +72,17 @@ internal fun Application.appModule(
install(DefaultHeaders) { header(HttpHeaders.CacheControl, "no-cache") }

install(StatusPages) {
status(HttpStatusCode.NotFound) {
status(HttpStatusCode.NotFound) { call, _ ->
call.respondRedirect(HttpServerFiles.ROOT_ADDRESS, permanent = true)
}
status(HttpStatusCode.Forbidden) {
status(HttpStatusCode.Forbidden) { call, _ ->
call.respondRedirect(HttpServerFiles.CLIENT_BLOCKED_ADDRESS)
}
status(HttpStatusCode.Unauthorized) {
status(HttpStatusCode.Unauthorized) { call, _ ->
call.respondRedirect(HttpServerFiles.PIN_REQUEST_ADDRESS)
}
exception<Throwable> { cause ->
exception<Throwable> { call, cause ->
if (cause is CancellationException) return@exception
val headers = CIOHeadersResearch.getHeadersAsString(call.request.headers as CIOHeaders)
XLog.e(this@appModule.getLog("exception<Throwable>", headers))
XLog.e(this@appModule.getLog("exception"), cause)
Expand All @@ -84,7 +91,7 @@ internal fun Application.appModule(
}
}

install(Routing) {
routing {
route(HttpServerFiles.ROOT_ADDRESS) {

handle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.lang.reflect.Field;
import java.net.InetSocketAddress;

import io.ktor.request.ApplicationRequest;
import io.ktor.routing.RoutingApplicationRequest;
import io.ktor.server.request.ApplicationRequest;
import io.ktor.server.routing.RoutingApplicationRequest;

// Waiting for https://youtrack.jetbrains.com/issue/KTOR-430 to get port also
// Tested on ktor-server-cio:1.5.3. May broke on other versions
// Tested on ktor-server-cio:2.0.2. May broke on other versions
public class ClientAddressWorkAround {
public static InetSocketAddress getInetSocketAddress(ApplicationRequest request) {
try {
Expand Down

0 comments on commit 9e4a896

Please sign in to comment.