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

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jul 10, 2021
1 parent 16aee3d commit e1ae028
Show file tree
Hide file tree
Showing 24 changed files with 616 additions and 266 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ coroutinesVersion=1.5.0

# korlibs
klockVersion=2.2.0
kdsVersion=2.2.0
kmemVersion=2.2.0
kdsVersion=2.2.1
kmemVersion=2.2.1
kryptoVersion=2.2.0
kloggerVersion=2.2.0

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ case "`uname`" in
Darwin* )
darwin=true
;;
MINGW* )
MSYS* | MINGW* )
msys=true
;;
NONSTOP* )
Expand Down
7 changes: 4 additions & 3 deletions korio/src/commonMain/kotlin/com/soywiz/korio/async/Signal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.soywiz.korio.async

import com.soywiz.kds.*
import com.soywiz.kds.iterators.*
import com.soywiz.klock.*
import com.soywiz.korio.lang.*
Expand All @@ -21,8 +22,8 @@ abstract class BaseSignal<T, THandler>(val onRegister: () -> Unit = {}) {
}
}

protected var handlers = ArrayList<Node>()
protected var handlersToRemove = ArrayList<Node>()
protected var handlers = FastArrayList<Node>()
protected var handlersToRemove = FastArrayList<Node>()
val listenerCount: Int get() = handlers.size
val hasListeners get() = listenerCount > 0
fun clear() = handlers.clear()
Expand Down Expand Up @@ -126,7 +127,7 @@ fun <TI, TO> Signal<TI>.mapSignal(transform: (TI) -> TO): Signal<TO> {
operator fun Signal<Unit>.invoke() = invoke(Unit)

suspend fun Iterable<Signal<*>>.waitOne(): Any? = suspendCancellableCoroutine { c ->
val closes = arrayListOf<Closeable>()
val closes = FastArrayList<Closeable>()
for (signal in this) {
closes += signal.once {
closes.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ interface CompressionMethod {
}

@UseExperimental(KorioExperimentalApi::class)
suspend fun CompressionMethod.uncompress(i: AsyncInputStreamWithLength, o: AsyncOutputStream): Unit = uncompress(BitReader(i), o)
suspend fun CompressionMethod.uncompress(i: AsyncInputStream, o: AsyncOutputStream): Unit = uncompress(BitReader(i), o)
@UseExperimental(KorioExperimentalApi::class)
suspend fun CompressionMethod.compress(i: AsyncInputStreamWithLength, o: AsyncOutputStream, context: CompressionContext = CompressionContext()): Unit = compress(BitReader(i), o, context)
suspend fun CompressionMethod.compress(i: AsyncInputStream, o: AsyncOutputStream, context: CompressionContext = CompressionContext()): Unit = compress(BitReader(i), o, context)

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

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

suspend fun AsyncInputStreamWithLength.uncompressed(method: CompressionMethod): AsyncInputStream = method.uncompressStream(this)
suspend fun AsyncInputStreamWithLength.compressed(method: CompressionMethod, context: CompressionContext = CompressionContext()): AsyncInputStream = method.compressStream(this, context)
suspend fun AsyncInputStream.uncompressed(method: CompressionMethod): AsyncInputStream = method.uncompressStream(this)
suspend fun AsyncInputStream.compressed(method: CompressionMethod, context: CompressionContext = CompressionContext()): AsyncInputStream = method.compressStream(this, context)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.soywiz.korio.stream.*
import kotlin.math.*

@KorioExperimentalApi
open class BitReader(val s: AsyncInputStreamWithLength) : AsyncInputStreamWithLength {
open class BitReader(val s: AsyncInputStream) : AsyncInputStreamWithLength {
@PublishedApi
internal var bitdata = 0
@PublishedApi
Expand Down Expand Up @@ -112,6 +112,6 @@ open class BitReader(val s: AsyncInputStreamWithLength) : AsyncInputStreamWithLe
//suspend fun readBytesExact(count: Int): ByteArray = abytes(count)

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

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class ObjectMapper {
fun <T : Any> toTyped(clazz: KClass<T>, obj: Any?): T {
val typer = _typers[clazz]
return when {
typer != null -> typer(typeCtx, obj) as T
fallbackTyper != null && obj != null -> fallbackTyper!!(clazz, obj) as T
typer != null -> typer(typeCtx, obj).fastCastTo<T>()
fallbackTyper != null && obj != null -> fallbackTyper!!(clazz, obj).fastCastTo<T>()
else -> invalidArg("Unregistered $clazz")
}
}
Expand Down Expand Up @@ -139,4 +139,4 @@ class ObjectMapper {
}
}

val Mapper = ObjectMapper()
val Mapper = ObjectMapper()
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ interface AsyncServer: AsyncCloseable {
suspend fun accept(): AsyncClient

suspend fun listen(handler: suspend (AsyncClient) -> Unit): Closeable {
val job = async(coroutineContext) { while (true) handler(accept()) }
val job = async(coroutineContext) {
while (true) {
val client = accept()
launchImmediately(coroutineContext) {
handler(client)
}
}
}
return Closeable { job.cancel() }
}

Expand Down
11 changes: 10 additions & 1 deletion korio/src/commonMain/kotlin/com/soywiz/korio/net/http/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,22 @@ interface Http {

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

fun toHttpHeaderString(extraLine: Boolean = true) = buildString {
for ((key, value) in items) {
append("$key: $value\r\n")
}
if (extraLine) append("\r\n")
}

class Builder {
private val items = arrayListOf<Pair<String, String>>()
fun put(key: String, value: String) = run { items += key to value }
fun put(key: String, value: String) { items += key to value }
fun build() = Headers(items)
}

companion object {
operator fun invoke(block: Builder.() -> Unit): Headers = Builder().apply(block).build()

fun build(block: Builder.() -> Unit): Headers = Builder().apply(block).build()

fun fromListMap(map: Map<String?, List<String>>): Headers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.soywiz.korio.net.http

import com.soywiz.kds.*
import com.soywiz.korio.async.*
import com.soywiz.korio.compression.deflate.GZIP
import com.soywiz.korio.compression.uncompressed
import com.soywiz.korio.concurrent.atomic.*
import com.soywiz.korio.lang.*
import com.soywiz.korio.net.*
Expand All @@ -23,9 +25,33 @@ abstract class HttpClient protected constructor() {
val status: Int,
val statusText: String,
val headers: Http.Headers,
val content: AsyncInputStream
val rawContent: AsyncInputStream,
val content: AsyncInputStream
) {
val success = status < 400

companion object {
suspend operator fun invoke(
status: Int,
statusText: String,
headers: Http.Headers,
rawContent: AsyncInputStream,
): Response {
val contentEncoding = (headers["Content-Encoding"] ?: "").lowercase().trim()
return Response(
status,
statusText,
headers,
rawContent,
when (contentEncoding) {
"" -> rawContent
"gzip" -> rawContent.uncompressed(GZIP)
else -> error("Unsupported Content-Encoding '$contentEncoding'")
}
)
}
}

suspend fun readAllBytes(): ByteArray {
//println(content)
val allContent = content.readAll()
Expand Down Expand Up @@ -180,8 +206,9 @@ fun HttpClient.delayed(ms: Long) = DelayedHttpClient(ms, this)

class FakeHttpClient(val redirect: HttpClient? = null) : HttpClient() {
val log = arrayListOf<String>()
private val defaultContent = "LogHttpClient.response".toByteArray(UTF8).openAsync()
var defaultResponse =
HttpClient.Response(200, "OK", Http.Headers(), "LogHttpClient.response".toByteArray(UTF8).openAsync())
HttpClient.Response(200, "OK", Http.Headers(), defaultContent, defaultContent)
private val rules = LinkedHashMap<Rule, ArrayList<ResponseBuilder>>()

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

internal fun buildResponse() = HttpClient.Response(
internal suspend fun buildResponse() = HttpClient.Response(
responseCode,
HttpStatusMessage(responseCode),
responseHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class FakeHttpClientEndpoint(val defaultMessage: String = "{}") : HttpClientEndp
private var responsePointer = 0
private val responses = arrayListOf<HttpClient.Response>()

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

fun addResponse(code: Int, content: String) {
responses += getResponse(code, content)
Expand Down
Loading

0 comments on commit e1ae028

Please sign in to comment.