Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit e1ae028

Browse files
committed
Sync from next
1 parent 16aee3d commit e1ae028

File tree

24 files changed

+616
-266
lines changed

24 files changed

+616
-266
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ coroutinesVersion=1.5.0
1313

1414
# korlibs
1515
klockVersion=2.2.0
16-
kdsVersion=2.2.0
17-
kmemVersion=2.2.0
16+
kdsVersion=2.2.1
17+
kmemVersion=2.2.1
1818
kryptoVersion=2.2.0
1919
kloggerVersion=2.2.0
2020

gradle/wrapper/gradle-wrapper.jar

333 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

gradlew

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ case "`uname`" in
7272
Darwin* )
7373
darwin=true
7474
;;
75-
MINGW* )
75+
MSYS* | MINGW* )
7676
msys=true
7777
;;
7878
NONSTOP* )

korio/src/commonMain/kotlin/com/soywiz/korio/async/Signal.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.soywiz.korio.async
44

5+
import com.soywiz.kds.*
56
import com.soywiz.kds.iterators.*
67
import com.soywiz.klock.*
78
import com.soywiz.korio.lang.*
@@ -21,8 +22,8 @@ abstract class BaseSignal<T, THandler>(val onRegister: () -> Unit = {}) {
2122
}
2223
}
2324

24-
protected var handlers = ArrayList<Node>()
25-
protected var handlersToRemove = ArrayList<Node>()
25+
protected var handlers = FastArrayList<Node>()
26+
protected var handlersToRemove = FastArrayList<Node>()
2627
val listenerCount: Int get() = handlers.size
2728
val hasListeners get() = listenerCount > 0
2829
fun clear() = handlers.clear()
@@ -126,7 +127,7 @@ fun <TI, TO> Signal<TI>.mapSignal(transform: (TI) -> TO): Signal<TO> {
126127
operator fun Signal<Unit>.invoke() = invoke(Unit)
127128

128129
suspend fun Iterable<Signal<*>>.waitOne(): Any? = suspendCancellableCoroutine { c ->
129-
val closes = arrayListOf<Closeable>()
130+
val closes = FastArrayList<Closeable>()
130131
for (signal in this) {
131132
closes += signal.once {
132133
closes.close()

korio/src/commonMain/kotlin/com/soywiz/korio/compression/CompressionMethod.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ interface CompressionMethod {
3131
}
3232

3333
@UseExperimental(KorioExperimentalApi::class)
34-
suspend fun CompressionMethod.uncompress(i: AsyncInputStreamWithLength, o: AsyncOutputStream): Unit = uncompress(BitReader(i), o)
34+
suspend fun CompressionMethod.uncompress(i: AsyncInputStream, o: AsyncOutputStream): Unit = uncompress(BitReader(i), o)
3535
@UseExperimental(KorioExperimentalApi::class)
36-
suspend fun CompressionMethod.compress(i: AsyncInputStreamWithLength, o: AsyncOutputStream, context: CompressionContext = CompressionContext()): Unit = compress(BitReader(i), o, context)
36+
suspend fun CompressionMethod.compress(i: AsyncInputStream, o: AsyncOutputStream, context: CompressionContext = CompressionContext()): Unit = compress(BitReader(i), o, context)
3737

38-
suspend fun CompressionMethod.uncompressStream(input: AsyncInputStreamWithLength): AsyncInputStream = asyncStreamWriter { output -> uncompress(input, output) }
39-
suspend fun CompressionMethod.compressStream(input: AsyncInputStreamWithLength, context: CompressionContext = CompressionContext()): AsyncInputStream = asyncStreamWriter { output -> compress(input, output, context) }
38+
suspend fun CompressionMethod.uncompressStream(input: AsyncInputStream): AsyncInputStream = asyncStreamWriter { output -> uncompress(input, output) }
39+
suspend fun CompressionMethod.compressStream(input: AsyncInputStream, context: CompressionContext = CompressionContext()): AsyncInputStream = asyncStreamWriter { output -> compress(input, output, context) }
4040

4141
fun CompressionMethod.uncompress(i: SyncInputStream, o: SyncOutputStream) = runBlockingNoSuspensions {
4242
uncompress(i.toAsyncInputStream(), o.toAsyncOutputStream())
@@ -50,5 +50,5 @@ fun ByteArray.uncompress(method: CompressionMethod, outputSizeHint: Int = this.s
5050
fun ByteArray.compress(method: CompressionMethod, context: CompressionContext = CompressionContext(), outputSizeHint: Int = (this.size * 1.1).toInt()): ByteArray =
5151
MemorySyncStreamToByteArray(outputSizeHint) { method.compress(this@compress.openSync(), this, context) }
5252

53-
suspend fun AsyncInputStreamWithLength.uncompressed(method: CompressionMethod): AsyncInputStream = method.uncompressStream(this)
54-
suspend fun AsyncInputStreamWithLength.compressed(method: CompressionMethod, context: CompressionContext = CompressionContext()): AsyncInputStream = method.compressStream(this, context)
53+
suspend fun AsyncInputStream.uncompressed(method: CompressionMethod): AsyncInputStream = method.uncompressStream(this)
54+
suspend fun AsyncInputStream.compressed(method: CompressionMethod, context: CompressionContext = CompressionContext()): AsyncInputStream = method.compressStream(this, context)

korio/src/commonMain/kotlin/com/soywiz/korio/compression/util/BitReader.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.soywiz.korio.stream.*
88
import kotlin.math.*
99

1010
@KorioExperimentalApi
11-
open class BitReader(val s: AsyncInputStreamWithLength) : AsyncInputStreamWithLength {
11+
open class BitReader(val s: AsyncInputStream) : AsyncInputStreamWithLength {
1212
@PublishedApi
1313
internal var bitdata = 0
1414
@PublishedApi
@@ -112,6 +112,6 @@ open class BitReader(val s: AsyncInputStreamWithLength) : AsyncInputStreamWithLe
112112
//suspend fun readBytesExact(count: Int): ByteArray = abytes(count)
113113

114114
override suspend fun getPosition(): Long = sbuffers.read
115-
override suspend fun getLength(): Long = s.getLength()
115+
override suspend fun getLength(): Long = (s as? AsyncGetLengthStream)?.getLength() ?: error("Length not available on Stream")
116116
}
117117

korio/src/commonMain/kotlin/com/soywiz/korio/dynamic/mapper/ObjectMapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class ObjectMapper {
5252
fun <T : Any> toTyped(clazz: KClass<T>, obj: Any?): T {
5353
val typer = _typers[clazz]
5454
return when {
55-
typer != null -> typer(typeCtx, obj) as T
56-
fallbackTyper != null && obj != null -> fallbackTyper!!(clazz, obj) as T
55+
typer != null -> typer(typeCtx, obj).fastCastTo<T>()
56+
fallbackTyper != null && obj != null -> fallbackTyper!!(clazz, obj).fastCastTo<T>()
5757
else -> invalidArg("Unregistered $clazz")
5858
}
5959
}
@@ -139,4 +139,4 @@ class ObjectMapper {
139139
}
140140
}
141141

142-
val Mapper = ObjectMapper()
142+
val Mapper = ObjectMapper()

korio/src/commonMain/kotlin/com/soywiz/korio/net/AsyncSocketFactory.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ interface AsyncServer: AsyncCloseable {
8585
suspend fun accept(): AsyncClient
8686

8787
suspend fun listen(handler: suspend (AsyncClient) -> Unit): Closeable {
88-
val job = async(coroutineContext) { while (true) handler(accept()) }
88+
val job = async(coroutineContext) {
89+
while (true) {
90+
val client = accept()
91+
launchImmediately(coroutineContext) {
92+
handler(client)
93+
}
94+
}
95+
}
8996
return Closeable { job.cancel() }
9097
}
9198

korio/src/commonMain/kotlin/com/soywiz/korio/net/http/Http.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,22 @@ interface Http {
170170

171171
override fun toString(): String = "Headers(${toListGrouped().joinToString(", ")})"
172172

173+
fun toHttpHeaderString(extraLine: Boolean = true) = buildString {
174+
for ((key, value) in items) {
175+
append("$key: $value\r\n")
176+
}
177+
if (extraLine) append("\r\n")
178+
}
179+
173180
class Builder {
174181
private val items = arrayListOf<Pair<String, String>>()
175-
fun put(key: String, value: String) = run { items += key to value }
182+
fun put(key: String, value: String) { items += key to value }
176183
fun build() = Headers(items)
177184
}
178185

179186
companion object {
187+
operator fun invoke(block: Builder.() -> Unit): Headers = Builder().apply(block).build()
188+
180189
fun build(block: Builder.() -> Unit): Headers = Builder().apply(block).build()
181190

182191
fun fromListMap(map: Map<String?, List<String>>): Headers {

korio/src/commonMain/kotlin/com/soywiz/korio/net/http/HttpClient.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.soywiz.korio.net.http
22

33
import com.soywiz.kds.*
44
import com.soywiz.korio.async.*
5+
import com.soywiz.korio.compression.deflate.GZIP
6+
import com.soywiz.korio.compression.uncompressed
57
import com.soywiz.korio.concurrent.atomic.*
68
import com.soywiz.korio.lang.*
79
import com.soywiz.korio.net.*
@@ -23,9 +25,33 @@ abstract class HttpClient protected constructor() {
2325
val status: Int,
2426
val statusText: String,
2527
val headers: Http.Headers,
26-
val content: AsyncInputStream
28+
val rawContent: AsyncInputStream,
29+
val content: AsyncInputStream
2730
) {
2831
val success = status < 400
32+
33+
companion object {
34+
suspend operator fun invoke(
35+
status: Int,
36+
statusText: String,
37+
headers: Http.Headers,
38+
rawContent: AsyncInputStream,
39+
): Response {
40+
val contentEncoding = (headers["Content-Encoding"] ?: "").lowercase().trim()
41+
return Response(
42+
status,
43+
statusText,
44+
headers,
45+
rawContent,
46+
when (contentEncoding) {
47+
"" -> rawContent
48+
"gzip" -> rawContent.uncompressed(GZIP)
49+
else -> error("Unsupported Content-Encoding '$contentEncoding'")
50+
}
51+
)
52+
}
53+
}
54+
2955
suspend fun readAllBytes(): ByteArray {
3056
//println(content)
3157
val allContent = content.readAll()
@@ -180,8 +206,9 @@ fun HttpClient.delayed(ms: Long) = DelayedHttpClient(ms, this)
180206

181207
class FakeHttpClient(val redirect: HttpClient? = null) : HttpClient() {
182208
val log = arrayListOf<String>()
209+
private val defaultContent = "LogHttpClient.response".toByteArray(UTF8).openAsync()
183210
var defaultResponse =
184-
HttpClient.Response(200, "OK", Http.Headers(), "LogHttpClient.response".toByteArray(UTF8).openAsync())
211+
HttpClient.Response(200, "OK", Http.Headers(), defaultContent, defaultContent)
185212
private val rules = LinkedHashMap<Rule, ArrayList<ResponseBuilder>>()
186213

187214
override suspend fun requestInternal(
@@ -233,7 +260,7 @@ class FakeHttpClient(val redirect: HttpClient? = null) : HttpClient() {
233260
fun notFound(content: String = "404 - Not Found") = response(content, code = 404)
234261
fun internalServerError(content: String = "500 - Internal Server Error") = response(content, code = 500)
235262

236-
internal fun buildResponse() = HttpClient.Response(
263+
internal suspend fun buildResponse() = HttpClient.Response(
237264
responseCode,
238265
HttpStatusMessage(responseCode),
239266
responseHeaders,

korio/src/commonMain/kotlin/com/soywiz/korio/net/http/HttpClientEndpoint.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class FakeHttpClientEndpoint(val defaultMessage: String = "{}") : HttpClientEndp
4343
private var responsePointer = 0
4444
private val responses = arrayListOf<HttpClient.Response>()
4545

46-
private fun getResponse(code: Int, content: String) =
47-
HttpClient.Response(code, HttpStatusMessage.CODES[code] ?: "Code$code", Http.Headers(), content.openAsync())
46+
private fun getResponse(code: Int, content: String): HttpClient.Response {
47+
val contentAsync = content.openAsync()
48+
return HttpClient.Response(code, HttpStatusMessage.CODES[code] ?: "Code$code", Http.Headers(), contentAsync, contentAsync)
49+
}
4850

4951
fun addResponse(code: Int, content: String) {
5052
responses += getResponse(code, content)

0 commit comments

Comments
 (0)