From b7c7db964a96f5405199bdd2f2ae386ac83cc740 Mon Sep 17 00:00:00 2001 From: olme04 Date: Thu, 10 Mar 2022 13:51:34 +0300 Subject: [PATCH] mime/auth type improvements --- .../kotlin/io/rsocket/kotlin/CompactType.kt | 129 ++++++++++++++++++ .../kotlin/io/rsocket/kotlin/MimeType.kt | 89 ++++++++++++ .../configuration/MimeTypeConfiguration.kt | 22 +-- .../configuration/PayloadConfiguration.kt | 6 +- .../configuration/RSocketConfiguration.kt | 6 +- .../rsocket/kotlin/connect/RSocketContext.kt | 5 +- .../rsocket/kotlin/connect/RSocketServer.kt | 7 +- .../kotlin/io/rsocket/kotlin/core/MimeType.kt | 46 ------- .../rsocket/kotlin/core/WellKnownMimeType.kt | 91 ------------ .../io/rsocket/kotlin/frame/io/mimeType.kt | 76 ----------- .../kotlin/metadata/CompositeMetadata.kt | 7 +- .../metadata/CompositeMetadataBuilder.kt | 1 - .../metadata/CompositeMetadataExtensions.kt | 1 - .../io/rsocket/kotlin/metadata/Metadata.kt | 1 - ...erStreamAcceptableDataMimeTypesMetadata.kt | 8 +- .../metadata/PerStreamDataMimeTypeMetadata.kt | 8 +- .../io/rsocket/kotlin/metadata/RawMetadata.kt | 1 - .../kotlin/metadata/RoutingMetadata.kt | 3 +- .../kotlin/metadata/ZipkinTracingMetadata.kt | 3 +- .../kotlin/metadata/security/AuthMetadata.kt | 10 +- .../kotlin/metadata/security/AuthType.kt | 37 +++-- .../metadata/security/BearerAuthMetadata.kt | 4 +- .../metadata/security/SimpleAuthMetadata.kt | 4 +- .../metadata/security/WellKnowAuthType.kt | 43 ------ .../rsocket/kotlin/RSocketCustomErrorTest.kt | 2 +- .../io/rsocket/kotlin/frame/SetupFrameTest.kt | 4 +- .../kotlin/metadata/CompositeMetadataTest.kt | 108 +++++++-------- ...reamAcceptableDataMimeTypesMetadataTest.kt | 28 ++-- .../PerStreamDataMimeTypeMetadataTest.kt | 24 ++-- .../metadata/security/AuthMetadataTest.kt | 10 +- 30 files changed, 365 insertions(+), 419 deletions(-) create mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/CompactType.kt create mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/MimeType.kt delete mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/MimeType.kt delete mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/WellKnownMimeType.kt delete mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/mimeType.kt delete mode 100644 rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/WellKnowAuthType.kt diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/CompactType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/CompactType.kt new file mode 100644 index 000000000..e58c30c72 --- /dev/null +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/CompactType.kt @@ -0,0 +1,129 @@ +package io.rsocket.kotlin + +import io.ktor.utils.io.core.* +import io.rsocket.kotlin.frame.io.* +import kotlin.experimental.* + +//used for MimeType, AuthType and later in LeaseStrategy +public interface CompactType { + public interface WithId : CompactType { + public val identifier: Byte + } + + public interface WithName : CompactType { + public val text: String + } + + public interface WellKnown : CompactType, WithId, WithName +} + +@Suppress("UNCHECKED_CAST") +//TODO: type relations +public abstract class CompactTypeFactory< + Type : CompactType, + WithId : CompactType.WithId, + WithName : CompactType.WithName, + WellKnown, + > internal constructor( + private val withIdConstructor: (identifier: Byte) -> WithId, + private val withNameConstructor: (text: String) -> WithName, + wellKnownValues: Array, +) where WellKnown : CompactType.WellKnown, WellKnown : Enum { + private val wellKnownByIdentifier: Array = arrayOfNulls(128) + private val wellKnownByName: MutableMap = HashMap(128) + + init { + wellKnownValues.forEach { + wellKnownByIdentifier[it.identifier.toInt()] = it + wellKnownByName[it.text] = it + } + } + + //TODO is it needed? + public fun WellKnown(text: String): WellKnown? = wellKnownByName[text] + public fun WellKnown(identifier: Byte): WellKnown? = wellKnownByIdentifier[identifier.toInt()] as WellKnown? + public fun WellKnown(identifier: Int): WellKnown? = wellKnownByIdentifier[identifier] as WellKnown? + + public fun WithName(text: String): WithName = (WellKnown(text) ?: withNameConstructor(text)) as WithName + public fun WithId(identifier: Byte): WithId = (WellKnown(identifier) ?: withIdConstructor(identifier)) as WithId + public fun WithId(identifier: Int): WithId = WithId(identifier.toByte()) + + public operator fun invoke(text: String): WithName = WithName(text) + public operator fun invoke(identifier: Byte): WithId = WithId(identifier) + public operator fun invoke(identifier: Int): WithId = WithId(identifier) +} + +internal fun CompactType.WellKnown.toString(typeName: String): String = "$typeName(id=$identifier, text=$text)" +internal fun CompactType.WithId.toString(typeName: String): String = "$typeName(id=$identifier)" +internal fun CompactType.WithName.toString(typeName: String): String = "$typeName(text=$text)" + +internal abstract class AbstractCompactTypeWithId(final override val identifier: Byte) : CompactType.WithId { + init { + require(identifier > 0) { "Mime-type identifier must be positive but was '${identifier}'" } + } + + final override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as AbstractCompactTypeWithId + + if (identifier != other.identifier) return false + + return true + } + + final override fun hashCode(): Int { + return identifier.hashCode() + } + + abstract override fun toString(): String +} + +internal abstract class AbstractCompactTypeWithName(final override val text: String) : CompactType.WithName { + init { + require(text.all { it.code <= 0x7f }) { "String should be an ASCII encodded string" } + require(text.length in 1..128) { "Mime-type text length must be in range 1..128 but was '${text.length}'" } + } + + final override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as AbstractCompactTypeWithName + + if (text != other.text) return false + + return true + } + + final override fun hashCode(): Int { + return text.hashCode() + } + + abstract override fun toString(): String +} + +private const val KnownTypeFlag: Byte = Byte.MIN_VALUE + +internal fun BytePacketBuilder.writeCompactType(type: CompactType) { + when (type) { + is CompactType.WithId -> writeByte(type.identifier or KnownTypeFlag) + is CompactType.WithName -> { + val typeBytes = type.text.encodeToByteArray() + writeByte(typeBytes.size.toByte()) //write length + writeFully(typeBytes) //write type + } + } +} + +internal fun ByteReadPacket.readCompactType(factory: CompactTypeFactory): T { + val byte = readByte() + return if (byte check KnownTypeFlag) { + val identifier = byte xor KnownTypeFlag + factory(identifier) + } else { + val stringType = readTextExactBytes(byte.toInt()) + factory(stringType) + } as T +} diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/MimeType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/MimeType.kt new file mode 100644 index 000000000..18d718518 --- /dev/null +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/MimeType.kt @@ -0,0 +1,89 @@ +/* + * Copyright 2015-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.rsocket.kotlin + +public sealed interface MimeType : CompactType { + public companion object : CompactTypeFactory(::WithIdImpl, ::WithNameImpl, enumValues()) + + public sealed interface WithId : MimeType, CompactType.WithId + public sealed interface WithName : MimeType, CompactType.WithName + public enum class WellKnown( + public override val text: String, + public override val identifier: Byte, + ) : MimeType, CompactType.WellKnown, WithId, WithName { + ApplicationAvro("application/avro", 0x00), + ApplicationCbor("application/cbor", 0x01), + ApplicationGraphql("application/graphql", 0x02), + ApplicationGzip("application/gzip", 0x03), + ApplicationJavascript("application/javascript", 0x04), + ApplicationJson("application/json", 0x05), + ApplicationOctetStream("application/octet-stream", 0x06), + ApplicationPdf("application/pdf", 0x07), + ApplicationThrift("application/vnd.apache.thrift.binary", 0x08), + ApplicationProtoBuf("application/vnd.google.protobuf", 0x09), + ApplicationXml("application/xml", 0x0A), + ApplicationZip("application/zip", 0x0B), + AudioAac("audio/aac", 0x0C), + AudioMp3("audio/mp3", 0x0D), + AudioMp4("audio/mp4", 0x0E), + AudioMpeg3("audio/mpeg3", 0x0F), + AudioMpeg("audio/mpeg", 0x10), + AudioOgg("audio/ogg", 0x11), + AudioOpus("audio/opus", 0x12), + AudioVorbis("audio/vorbis", 0x13), + ImageBmp("image/bmp", 0x14), + ImageGif("image/gif", 0x15), + ImageHeicSequence("image/heic-sequence", 0x16), + ImageHeic("image/heic", 0x17), + ImageHeifSequence("image/heif-sequence", 0x18), + ImageHeif("image/heif", 0x19), + ImageJpeg("image/jpeg", 0x1A), + ImagePng("image/png", 0x1B), + ImageTiff("image/tiff", 0x1C), + MultipartMixed("multipart/mixed", 0x1D), + TextCss("text/css", 0x1E), + TextCsv("text/csv", 0x1F), + TextHtml("text/html", 0x20), + TextPlain("text/plain", 0x21), + TextXml("text/xml", 0x22), + VideoH264("video/H264", 0x23), + VideoH265("video/H265", 0x24), + VideoVp8("video/VP8", 0x25), + ApplicationHessian("application/x-hessian", 0x26), + ApplicationJavaObject("application/x-java-object", 0x27), + ApplicationCloudeventsJson("application/cloudevents+json", 0x28), + ApplicationCapnProto("application/x-capnp", 0x29), + ApplicationFlatBuffers("application/x-flatbuffers", 0x2A), + + MessageRSocketMimeType("message/x.rsocket.mime-type.v0", 0x7A), + MessageRSocketAcceptMimeTypes("message/x.rsocket.accept-mime-types.v0", 0x7b), + MessageRSocketAuthentication("message/x.rsocket.authentication.v0", 0x7C), + MessageRSocketTracingZipkin("message/x.rsocket.tracing-zipkin.v0", 0x7D), + MessageRSocketRouting("message/x.rsocket.routing.v0", 0x7E), + MessageRSocketCompositeMetadata("message/x.rsocket.composite-metadata.v0", 0x7F); + + override fun toString(): String = toString("MimeType") + } + + private class WithIdImpl(identifier: Byte) : WithId, AbstractCompactTypeWithId(identifier) { + override fun toString(): String = toString("MimeType") + } + + private class WithNameImpl(text: String) : WithName, AbstractCompactTypeWithName(text) { + override fun toString(): String = toString("MimeType") + } +} diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/MimeTypeConfiguration.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/MimeTypeConfiguration.kt index f45eb7465..149f35637 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/MimeTypeConfiguration.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/MimeTypeConfiguration.kt @@ -1,17 +1,17 @@ package io.rsocket.kotlin.configuration -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* public sealed interface MimeTypeConfiguration { - public val metadata: MimeTypeWithName - public val data: MimeTypeWithName + public val metadata: MimeType.WithName + public val data: MimeType.WithName } public sealed interface MimeTypeConnectConfiguration : MimeTypeConfiguration, ConnectConfiguration public sealed interface MimeTypeClientConnectConfiguration : MimeTypeConnectConfiguration { - public fun metadata(mimeType: MimeTypeWithName) - public fun data(mimeType: MimeTypeWithName) + public fun metadata(mimeType: MimeType.WithName) + public fun data(mimeType: MimeType.WithName) } public sealed interface MimeTypeServerConnectConfiguration : MimeTypeConnectConfiguration @@ -19,23 +19,23 @@ public sealed interface MimeTypeServerConnectConfiguration : MimeTypeConnectConf internal class MimeTypeClientConnectConfigurationImpl( private val configurationState: ConfigurationState, ) : MimeTypeClientConnectConfiguration { - override var metadata: MimeTypeWithName = WellKnownMimeType.ApplicationOctetStream + override var metadata: MimeType.WithName = MimeType.WellKnown.ApplicationOctetStream private set - override var data: MimeTypeWithName = WellKnownMimeType.ApplicationOctetStream + override var data: MimeType.WithName = MimeType.WellKnown.ApplicationOctetStream private set - override fun metadata(mimeType: MimeTypeWithName) { + override fun metadata(mimeType: MimeType.WithName) { configurationState.checkNotConfigured() metadata = mimeType } - override fun data(mimeType: MimeTypeWithName) { + override fun data(mimeType: MimeType.WithName) { configurationState.checkNotConfigured() data = mimeType } } internal class MimeTypeServerConnectConfigurationImpl( - override val metadata: MimeTypeWithName, - override val data: MimeTypeWithName, + override val metadata: MimeType.WithName, + override val data: MimeType.WithName, ) : MimeTypeServerConnectConfiguration diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/PayloadConfiguration.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/PayloadConfiguration.kt index 157a0d158..f5ddca66d 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/PayloadConfiguration.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/PayloadConfiguration.kt @@ -1,6 +1,6 @@ package io.rsocket.kotlin.configuration -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* public sealed interface PayloadConfiguration { //TODO: Long? @@ -55,8 +55,8 @@ internal class PayloadClientConnectConfigurationImpl( internal class PayloadServerConnectConfigurationImpl( configurationState: ConfigurationState, - metadataMimeType: MimeTypeWithName, - dataMimeType: MimeTypeWithName, + metadataMimeType: MimeType.WithName, + dataMimeType: MimeType.WithName, ) : PayloadServerConnectConfiguration, PayloadConnectConfigurationImpl(configurationState) { override val mimeType: MimeTypeServerConnectConfigurationImpl = MimeTypeServerConnectConfigurationImpl(metadataMimeType, dataMimeType) diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/RSocketConfiguration.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/RSocketConfiguration.kt index f1d927342..ae6d1e504 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/RSocketConfiguration.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/configuration/RSocketConfiguration.kt @@ -1,7 +1,7 @@ package io.rsocket.kotlin.configuration import io.ktor.utils.io.core.* -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* import io.rsocket.kotlin.payload.* import kotlin.time.* @@ -52,8 +52,8 @@ internal class RSocketServerConnectConfigurationImpl( configurationState: ConfigurationState, keepAliveInterval: Duration, keepAliveMaxLifetime: Duration, - metadataMimeType: MimeTypeWithName, - dataMimeType: MimeTypeWithName, + metadataMimeType: MimeType.WithName, + dataMimeType: MimeType.WithName, setupPayload: Payload, ) : RSocketServerConnectConfiguration, RSocketConnectConfigurationImpl() { override val setup: SetupServerConnectConfigurationImpl = diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketContext.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketContext.kt index 5eac551ef..6cc2d2c9b 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketContext.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketContext.kt @@ -4,7 +4,6 @@ package io.rsocket.kotlin.connect import io.rsocket.kotlin.* import io.rsocket.kotlin.configuration.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.* import io.rsocket.kotlin.frame.io.* import io.rsocket.kotlin.internal.* @@ -214,8 +213,8 @@ internal class RSocketServerConnectContextImpl( deferredRequester: Deferred, keepAliveInterval: Duration, keepAliveMaxLifetime: Duration, - metadataMimeType: MimeTypeWithName, - dataMimeType: MimeTypeWithName, + metadataMimeType: MimeType.WithName, + dataMimeType: MimeType.WithName, setupPayload: Payload, ) : RSocketServerConnectContext, RSocketConnectContextImpl(session, deferredRequester) { override val isServer: Boolean get() = true diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketServer.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketServer.kt index 901ea4366..b17dd44ce 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketServer.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/connect/RSocketServer.kt @@ -19,7 +19,6 @@ package io.rsocket.kotlin.connect import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.* import io.rsocket.kotlin.frame.io.* import io.rsocket.kotlin.logging.* @@ -131,10 +130,8 @@ private class RSocketServerImpl( deferred, keepAliveInterval = setupFrame.keepAliveIntervalMillis.milliseconds, keepAliveMaxLifetime = setupFrame.keepAliveMaxLifetimeMillis.milliseconds, - metadataMimeType = WellKnownMimeType(setupFrame.metadataMimeTypeText) - ?: CustomMimeType(setupFrame.metadataMimeTypeText), - dataMimeType = WellKnownMimeType(setupFrame.dataMimeTypeText) - ?: CustomMimeType(setupFrame.dataMimeTypeText), + metadataMimeType = MimeType(setupFrame.metadataMimeTypeText), + dataMimeType = MimeType(setupFrame.dataMimeTypeText), setupPayload = setupFrame.payload, ) connectContext.configure(beforeConfigurators, afterConfigurators, peerConfigurator) diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/MimeType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/MimeType.kt deleted file mode 100644 index 7522de1c2..000000000 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/MimeType.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2015-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.rsocket.kotlin.core - -import io.rsocket.kotlin.frame.io.* - -public sealed interface MimeType - -public sealed interface MimeTypeWithName : MimeType { - public val text: String -} - -public sealed interface MimeTypeWithId : MimeType { - public val identifier: Byte -} - -public data class CustomMimeType(override val text: String) : MimeTypeWithName { - init { - text.requireAscii() - require(text.length in 1..128) { "Mime-type text length must be in range 1..128 but was '${text.length}'" } - } - - override fun toString(): String = text -} - -public data class ReservedMimeType(override val identifier: Byte) : MimeTypeWithId { - init { - require(identifier in 1..128) { "Mime-type identifier must be in range 1..128 but was '${identifier}'" } - } - - override fun toString(): String = "ID: $identifier" -} diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/WellKnownMimeType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/WellKnownMimeType.kt deleted file mode 100644 index be6aade89..000000000 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/core/WellKnownMimeType.kt +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2015-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.rsocket.kotlin.core - -public enum class WellKnownMimeType( - public override val text: String, - public override val identifier: Byte, -) : MimeTypeWithName, MimeTypeWithId { - ApplicationAvro("application/avro", 0x00), - ApplicationCbor("application/cbor", 0x01), - ApplicationGraphql("application/graphql", 0x02), - ApplicationGzip("application/gzip", 0x03), - ApplicationJavascript("application/javascript", 0x04), - ApplicationJson("application/json", 0x05), - ApplicationOctetStream("application/octet-stream", 0x06), - ApplicationPdf("application/pdf", 0x07), - ApplicationThrift("application/vnd.apache.thrift.binary", 0x08), - ApplicationProtoBuf("application/vnd.google.protobuf", 0x09), - ApplicationXml("application/xml", 0x0A), - ApplicationZip("application/zip", 0x0B), - AudioAac("audio/aac", 0x0C), - AudioMp3("audio/mp3", 0x0D), - AudioMp4("audio/mp4", 0x0E), - AudioMpeg3("audio/mpeg3", 0x0F), - AudioMpeg("audio/mpeg", 0x10), - AudioOgg("audio/ogg", 0x11), - AudioOpus("audio/opus", 0x12), - AudioVorbis("audio/vorbis", 0x13), - ImageBmp("image/bmp", 0x14), - ImageGif("image/gif", 0x15), - ImageHeicSequence("image/heic-sequence", 0x16), - ImageHeic("image/heic", 0x17), - ImageHeifSequence("image/heif-sequence", 0x18), - ImageHeif("image/heif", 0x19), - ImageJpeg("image/jpeg", 0x1A), - ImagePng("image/png", 0x1B), - ImageTiff("image/tiff", 0x1C), - MultipartMixed("multipart/mixed", 0x1D), - TextCss("text/css", 0x1E), - TextCsv("text/csv", 0x1F), - TextHtml("text/html", 0x20), - TextPlain("text/plain", 0x21), - TextXml("text/xml", 0x22), - VideoH264("video/H264", 0x23), - VideoH265("video/H265", 0x24), - VideoVp8("video/VP8", 0x25), - ApplicationHessian("application/x-hessian", 0x26), - ApplicationJavaObject("application/x-java-object", 0x27), - ApplicationCloudeventsJson("application/cloudevents+json", 0x28), - ApplicationCapnProto("application/x-capnp", 0x29), - ApplicationFlatBuffers("application/x-flatbuffers", 0x2A), - - MessageRSocketMimeType("message/x.rsocket.mime-type.v0", 0x7A), - MessageRSocketAcceptMimeTypes("message/x.rsocket.accept-mime-types.v0", 0x7b), - MessageRSocketAuthentication("message/x.rsocket.authentication.v0", 0x7C), - MessageRSocketTracingZipkin("message/x.rsocket.tracing-zipkin.v0", 0x7D), - MessageRSocketRouting("message/x.rsocket.routing.v0", 0x7E), - MessageRSocketCompositeMetadata("message/x.rsocket.composite-metadata.v0", 0x7F); - - override fun toString(): String = text - - public companion object { - private val byIdentifier: Array = arrayOfNulls(128) - private val byName: MutableMap = HashMap(128) - - init { - values().forEach { - byIdentifier[it.identifier.toInt()] = it - byName[it.text] = it - } - } - - public operator fun invoke(identifier: Byte): WellKnownMimeType? = byIdentifier[identifier.toInt()] - - public operator fun invoke(text: String): WellKnownMimeType? = byName[text] - } -} diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/mimeType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/mimeType.kt deleted file mode 100644 index 416fa7231..000000000 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/mimeType.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2015-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.rsocket.kotlin.frame.io - -import io.ktor.utils.io.core.* -import io.rsocket.kotlin.core.* -import io.rsocket.kotlin.metadata.security.* -import kotlin.experimental.* - -internal fun BytePacketBuilder.writeMimeType(type: MimeType) { - when (type) { - is MimeTypeWithId -> writeIdentifier(type.identifier) - is MimeTypeWithName -> writeTextWithLength(type.text) - } -} - -internal fun ByteReadPacket.readMimeType(): MimeType = readType( - { WellKnownMimeType(it) ?: ReservedMimeType(it) }, - { WellKnownMimeType(it) ?: CustomMimeType(it) } -) - -internal fun BytePacketBuilder.writeAuthType(type: AuthType) { - when (type) { - is AuthTypeWithId -> writeIdentifier(type.identifier) - is AuthTypeWithName -> writeTextWithLength(type.text) - } -} - -internal fun ByteReadPacket.readAuthType(): AuthType = readType( - { WellKnowAuthType(it) ?: ReservedAuthType(it) }, - { WellKnowAuthType(it) ?: CustomAuthType(it) } -) - -private fun BytePacketBuilder.writeTextWithLength(text: String) { - val typeBytes = text.encodeToByteArray() - writeByte(typeBytes.size.toByte()) //write length - writeFully(typeBytes) //write mime type -} - -private const val KnownTypeFlag: Byte = Byte.MIN_VALUE - -private fun BytePacketBuilder.writeIdentifier(identifier: Byte) { - writeByte(identifier or KnownTypeFlag) -} - -private inline fun ByteReadPacket.readType( - fromIdentifier: (Byte) -> T, - fromText: (String) -> T, -): T { - val byte = readByte() - return if (byte check KnownTypeFlag) { - val identifier = byte xor KnownTypeFlag - fromIdentifier(identifier) - } else { - val stringType = readTextExactBytes(byte.toInt()) - fromText(stringType) - } -} - -internal fun String.requireAscii() { - require(all { it.code <= 0x7f }) { "String should be an ASCII encodded string" } -} diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadata.kt index 7cfd9b9b3..52274f53a 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadata.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.io.* @ExperimentalMetadataApi @@ -38,7 +37,7 @@ public sealed interface CompositeMetadata : Metadata { override fun BytePacketBuilder.writeSelf() { entries.forEach { - writeMimeType(it.mimeType) + writeCompactType(it.mimeType) writeLength(it.content.remaining.toInt()) //write metadata length writePacket(it.content) //write metadata content } @@ -53,11 +52,11 @@ public sealed interface CompositeMetadata : Metadata { } public companion object Reader : MetadataReader { - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketCompositeMetadata + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketCompositeMetadata override fun ByteReadPacket.read(pool: ObjectPool): CompositeMetadata { val list = mutableListOf() while (isNotEmpty) { - val type = readMimeType() + val type = readCompactType(MimeType) val length = readLength() val packet = readPacket(pool, length) list.add(Entry(type, packet)) diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataBuilder.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataBuilder.kt index 2086a18ec..3f079978b 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataBuilder.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataBuilder.kt @@ -18,7 +18,6 @@ package io.rsocket.kotlin.metadata import io.ktor.utils.io.core.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.payload.* @ExperimentalMetadataApi diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataExtensions.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataExtensions.kt index ddb18e9c7..afded5302 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataExtensions.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataExtensions.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* @ExperimentalMetadataApi public fun CompositeMetadata.Entry.hasMimeTypeOf(reader: MetadataReader<*>): Boolean = mimeType == reader.mimeType diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/Metadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/Metadata.kt index 05d922943..7179d0384 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/Metadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/Metadata.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.io.* import io.rsocket.kotlin.payload.* diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadata.kt index 15d04b762..89a4e3921 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadata.kt @@ -20,8 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* -import io.rsocket.kotlin.frame.io.* @ExperimentalMetadataApi public fun PerStreamAcceptableDataMimeTypesMetadata(vararg tags: MimeType): PerStreamAcceptableDataMimeTypesMetadata = @@ -33,17 +31,17 @@ public class PerStreamAcceptableDataMimeTypesMetadata(public val types: List { - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketAcceptMimeTypes + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketAcceptMimeTypes override fun ByteReadPacket.read(pool: ObjectPool): PerStreamAcceptableDataMimeTypesMetadata { val list = mutableListOf() - while (isNotEmpty) list.add(readMimeType()) + while (isNotEmpty) list.add(readCompactType(MimeType)) return PerStreamAcceptableDataMimeTypesMetadata(list.toList()) } } diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadata.kt index a3aa1a2ea..505c90090 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadata.kt @@ -20,22 +20,20 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* -import io.rsocket.kotlin.frame.io.* @ExperimentalMetadataApi public class PerStreamDataMimeTypeMetadata(public val type: MimeType) : Metadata { override val mimeType: MimeType get() = Reader.mimeType override fun BytePacketBuilder.writeSelf() { - writeMimeType(type) + writeCompactType(type) } override fun close(): Unit = Unit public companion object Reader : MetadataReader { - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketMimeType + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketMimeType override fun ByteReadPacket.read(pool: ObjectPool): PerStreamDataMimeTypeMetadata = - PerStreamDataMimeTypeMetadata(readMimeType()) + PerStreamDataMimeTypeMetadata(readCompactType(MimeType)) } } diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RawMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RawMetadata.kt index fec283598..f2d5638b2 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RawMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RawMetadata.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.io.* @ExperimentalMetadataApi diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RoutingMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RoutingMetadata.kt index 04c1125b4..abefbe7da 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RoutingMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/RoutingMetadata.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* @ExperimentalMetadataApi public fun RoutingMetadata(vararg tags: String): RoutingMetadata = RoutingMetadata(tags.toList()) @@ -46,7 +45,7 @@ public class RoutingMetadata(public val tags: List) : Metadata { override fun close(): Unit = Unit public companion object Reader : MetadataReader { - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketRouting + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketRouting override fun ByteReadPacket.read(pool: ObjectPool): RoutingMetadata { val list = mutableListOf() while (isNotEmpty) { diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/ZipkinTracingMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/ZipkinTracingMetadata.kt index 0182d69ca..38b19f6f7 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/ZipkinTracingMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/ZipkinTracingMetadata.kt @@ -20,7 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* import io.rsocket.kotlin.frame.io.* import kotlin.experimental.* @@ -70,7 +69,7 @@ public class ZipkinTracingMetadata internal constructor( override fun close(): Unit = Unit public companion object Reader : MetadataReader { - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketTracingZipkin + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketTracingZipkin override fun ByteReadPacket.read(pool: ObjectPool): ZipkinTracingMetadata { val flags = readByte() diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadata.kt index 16f76f3b7..b9faba40b 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadata.kt @@ -20,8 +20,6 @@ import io.ktor.utils.io.core.* import io.ktor.utils.io.core.internal.* import io.ktor.utils.io.pool.* import io.rsocket.kotlin.* -import io.rsocket.kotlin.core.* -import io.rsocket.kotlin.frame.io.* import io.rsocket.kotlin.metadata.* @ExperimentalMetadataApi @@ -29,10 +27,10 @@ public sealed interface AuthMetadata : Metadata { public val type: AuthType public fun BytePacketBuilder.writeContent() - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketAuthentication + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketAuthentication override fun BytePacketBuilder.writeSelf() { - writeAuthType(type) + writeCompactType(type) writeContent() } } @@ -41,9 +39,9 @@ public sealed interface AuthMetadata : Metadata { public sealed interface AuthMetadataReader : MetadataReader { public fun ByteReadPacket.readContent(type: AuthType, pool: ObjectPool): AM - override val mimeType: MimeType get() = WellKnownMimeType.MessageRSocketAuthentication + override val mimeType: MimeType get() = MimeType.WellKnown.MessageRSocketAuthentication override fun ByteReadPacket.read(pool: ObjectPool): AM { - val type = readAuthType() + val type = readCompactType(AuthType) return readContent(type, pool) } } diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthType.kt index 17f5e8e57..8c28c8c1b 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthType.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/AuthType.kt @@ -16,31 +16,28 @@ package io.rsocket.kotlin.metadata.security -import io.rsocket.kotlin.frame.io.* +import io.rsocket.kotlin.* -public sealed interface AuthType +public sealed interface AuthType : CompactType { + public companion object : CompactTypeFactory(::WithIdImpl, ::WithNameImpl, enumValues()) -public sealed interface AuthTypeWithName : AuthType { - public val text: String -} - -public sealed interface AuthTypeWithId : AuthType { - public val identifier: Byte -} + public sealed interface WithId : AuthType, CompactType.WithId + public sealed interface WithName : AuthType, CompactType.WithName + public enum class WellKnown( + public override val text: String, + public override val identifier: Byte, + ) : AuthType, CompactType.WellKnown, WithId, WithName { + Simple("simple", 0x00), + Bearer("bearer", 0x01); -public data class CustomAuthType(override val text: String) : AuthTypeWithName { - init { - text.requireAscii() - require(text.length in 1..128) { "Mime-type length must be in range 1..128 but was '${text.length}'" } + override fun toString(): String = toString("AuthType") } - override fun toString(): String = text -} - -public data class ReservedAuthType(override val identifier: Byte) : AuthTypeWithId { - init { - require(identifier in 1..128) { "Mime-type identifier must be in range 1..128 but was '${identifier}'" } + private class WithIdImpl(identifier: Byte) : WithId, AbstractCompactTypeWithId(identifier) { + override fun toString(): String = toString("AuthType") } - override fun toString(): String = "ID: $identifier" + private class WithNameImpl(text: String) : WithName, AbstractCompactTypeWithName(text) { + override fun toString(): String = toString("AuthType") + } } diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/BearerAuthMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/BearerAuthMetadata.kt index f8f6b9c27..fb35343f7 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/BearerAuthMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/BearerAuthMetadata.kt @@ -25,7 +25,7 @@ import io.rsocket.kotlin.* public class BearerAuthMetadata( public val token: String, ) : AuthMetadata { - override val type: AuthType get() = WellKnowAuthType.Bearer + override val type: AuthType get() = AuthType.WellKnown.Bearer override fun BytePacketBuilder.writeContent() { writeText(token) } @@ -34,7 +34,7 @@ public class BearerAuthMetadata( public companion object Reader : AuthMetadataReader { override fun ByteReadPacket.readContent(type: AuthType, pool: ObjectPool): BearerAuthMetadata { - require(type == WellKnowAuthType.Bearer) { "Metadata auth type should be 'bearer'" } + require(type == AuthType.WellKnown.Bearer) { "Metadata auth type should be 'bearer'" } val token = readText() return BearerAuthMetadata(token) } diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/SimpleAuthMetadata.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/SimpleAuthMetadata.kt index 9998a2675..a5a56932e 100644 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/SimpleAuthMetadata.kt +++ b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/SimpleAuthMetadata.kt @@ -31,7 +31,7 @@ public class SimpleAuthMetadata( require(username.length < 65535) { "Username length must be in range 1..65535 but was '${username.length}'" } } - override val type: AuthType get() = WellKnowAuthType.Simple + override val type: AuthType get() = AuthType.WellKnown.Simple override fun BytePacketBuilder.writeContent() { val length = username.encodeToByteArray() @@ -44,7 +44,7 @@ public class SimpleAuthMetadata( public companion object Reader : AuthMetadataReader { override fun ByteReadPacket.readContent(type: AuthType, pool: ObjectPool): SimpleAuthMetadata { - require(type == WellKnowAuthType.Simple) { "Metadata auth type should be 'simple'" } + require(type == AuthType.WellKnown.Simple) { "Metadata auth type should be 'simple'" } val length = readShort().toInt() val username = readTextExactBytes(length) val password = readText() diff --git a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/WellKnowAuthType.kt b/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/WellKnowAuthType.kt deleted file mode 100644 index b1704660b..000000000 --- a/rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/security/WellKnowAuthType.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2015-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.rsocket.kotlin.metadata.security - -public enum class WellKnowAuthType( - public override val text: String, - public override val identifier: Byte, -) : AuthTypeWithName, AuthTypeWithId { - Simple("simple", 0x00), - Bearer("bearer", 0x01); - - override fun toString(): String = text - - public companion object { - private val byIdentifier: Array = arrayOfNulls(128) - private val byName: MutableMap = HashMap(128) - - init { - values().forEach { - byIdentifier[it.identifier.toInt()] = it - byName[it.text] = it - } - } - - public operator fun invoke(identifier: Byte): WellKnowAuthType? = byIdentifier[identifier.toInt()] - - public operator fun invoke(text: String): WellKnowAuthType? = byName[text] - } -} diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/RSocketCustomErrorTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/RSocketCustomErrorTest.kt index cd80282e1..567d550e8 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/RSocketCustomErrorTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/RSocketCustomErrorTest.kt @@ -35,4 +35,4 @@ class RSocketCustomErrorTest { RSocketError.Custom(errorCode, "custom") } } -} \ No newline at end of file +} diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/frame/SetupFrameTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/frame/SetupFrameTest.kt index 7c51e5e82..4adf71fee 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/frame/SetupFrameTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/frame/SetupFrameTest.kt @@ -17,7 +17,7 @@ package io.rsocket.kotlin.frame import io.ktor.utils.io.core.* -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* import io.rsocket.kotlin.frame.io.* import io.rsocket.kotlin.payload.* import io.rsocket.kotlin.test.* @@ -31,7 +31,7 @@ class SetupFrameTest : TestWithLeakCheck { private val keepAliveIntervalMillis = 10.seconds.toInt(DurationUnit.MILLISECONDS) private val keepAliveMaxLifetimeMillis = 500.seconds.toInt(DurationUnit.MILLISECONDS) private val metadataMimeTypeText = "mime" - private val dataMimeTypeText = WellKnownMimeType.ApplicationOctetStream.text + private val dataMimeTypeText = MimeType.WellKnown.ApplicationOctetStream.text @Test fun testNoResumeEmptyPayload() { diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataTest.kt index d861811c0..6c9291852 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/CompositeMetadataTest.kt @@ -17,7 +17,7 @@ package io.rsocket.kotlin.metadata import io.ktor.utils.io.core.* -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* import io.rsocket.kotlin.test.* import kotlin.test.* @@ -26,37 +26,37 @@ class CompositeMetadataTest : TestWithLeakCheck { @Test fun decodeEntryHasNoContent() { val cm = buildCompositeMetadata { - add(CustomMimeType("w"), ByteReadPacket.Empty) + add(MimeType("w"), ByteReadPacket.Empty) } val decoded = cm.readLoop(CompositeMetadata) assertEquals(1, decoded.entries.size) val entry = decoded.entries.first() - assertEquals(CustomMimeType("w"), entry.mimeType) + assertEquals(MimeType("w"), entry.mimeType) assertEquals(0, entry.content.remaining) } @Test fun decodeMultiple() { val cm = buildCompositeMetadata { - add(CustomMimeType("custom"), packet("custom metadata")) - add(ReservedMimeType(120), packet("reserved metadata")) - add(WellKnownMimeType.ApplicationAvro, packet("avro metadata")) + add(MimeType("custom"), packet("custom metadata")) + add(MimeType(120), packet("reserved metadata")) + add(MimeType.WellKnown.ApplicationAvro, packet("avro metadata")) } val decoded = cm.readLoop(CompositeMetadata) assertEquals(3, decoded.entries.size) decoded.entries[0].let { custom -> - assertEquals(CustomMimeType("custom"), custom.mimeType) + assertEquals(MimeType("custom"), custom.mimeType) assertEquals("custom metadata", custom.content.readText()) } decoded.entries[1].let { reserved -> - assertEquals(ReservedMimeType(120), reserved.mimeType) + assertEquals(MimeType(120), reserved.mimeType) assertEquals("reserved metadata", reserved.content.readText()) } decoded.entries[2].let { known -> - assertEquals(WellKnownMimeType.ApplicationAvro, known.mimeType) + assertEquals(MimeType.WellKnown.ApplicationAvro, known.mimeType) assertEquals("avro metadata", known.content.readText()) } } @@ -74,20 +74,20 @@ class CompositeMetadataTest : TestWithLeakCheck { @Test fun testContains() { val cm = buildCompositeMetadata { - add(CustomMimeType("custom"), packet("custom metadata")) - add(ReservedMimeType(120), packet("reserved metadata")) - add(WellKnownMimeType.ApplicationAvro, packet("avro metadata")) + add(MimeType("custom"), packet("custom metadata")) + add(MimeType(120), packet("reserved metadata")) + add(MimeType.WellKnown.ApplicationAvro, packet("avro metadata")) } val decoded = cm.readLoop(CompositeMetadata) - assertTrue(CustomMimeType("custom") in decoded) - assertTrue(CustomMimeType("custom2") !in decoded) + assertTrue(MimeType("custom") in decoded) + assertTrue(MimeType("custom2") !in decoded) - assertTrue(ReservedMimeType(120) in decoded) - assertTrue(ReservedMimeType(110) !in decoded) + assertTrue(MimeType(120) in decoded) + assertTrue(MimeType(110) !in decoded) - assertTrue(WellKnownMimeType.ApplicationAvro in decoded) - assertTrue(WellKnownMimeType.MessageRSocketRouting !in decoded) + assertTrue(MimeType.WellKnown.ApplicationAvro in decoded) + assertTrue(MimeType.WellKnown.MessageRSocketRouting !in decoded) decoded.entries.forEach { it.content.close() } } @@ -95,72 +95,72 @@ class CompositeMetadataTest : TestWithLeakCheck { @Test fun testGet() { val cm = buildCompositeMetadata { - add(CustomMimeType("custom"), packet("custom metadata")) - add(ReservedMimeType(120), packet("reserved metadata")) - add(WellKnownMimeType.ApplicationAvro, packet("avro metadata")) + add(MimeType("custom"), packet("custom metadata")) + add(MimeType(120), packet("reserved metadata")) + add(MimeType.WellKnown.ApplicationAvro, packet("avro metadata")) } val decoded = cm.readLoop(CompositeMetadata) - assertEquals("custom metadata", decoded[CustomMimeType("custom")].readText()) - assertEquals("reserved metadata", decoded[ReservedMimeType(120)].readText()) - assertEquals("avro metadata", decoded[WellKnownMimeType.ApplicationAvro].readText()) + assertEquals("custom metadata", decoded[MimeType("custom")].readText()) + assertEquals("reserved metadata", decoded[MimeType(120)].readText()) + assertEquals("avro metadata", decoded[MimeType.WellKnown.ApplicationAvro].readText()) } @Test fun testGetOrNull() { val cm = buildCompositeMetadata { - add(CustomMimeType("custom"), packet("custom metadata")) - add(ReservedMimeType(120), packet("reserved metadata")) - add(WellKnownMimeType.ApplicationAvro, packet("avro metadata")) + add(MimeType("custom"), packet("custom metadata")) + add(MimeType(120), packet("reserved metadata")) + add(MimeType.WellKnown.ApplicationAvro, packet("avro metadata")) } val decoded = cm.readLoop(CompositeMetadata) - assertNull(decoded.getOrNull(ReservedMimeType(121))) - assertNull(decoded.getOrNull(CustomMimeType("custom2"))) - assertNull(decoded.getOrNull(WellKnownMimeType.MessageRSocketRouting)) + assertNull(decoded.getOrNull(MimeType(121))) + assertNull(decoded.getOrNull(MimeType("custom2"))) + assertNull(decoded.getOrNull(MimeType.WellKnown.MessageRSocketRouting)) - assertEquals("custom metadata", decoded.getOrNull(CustomMimeType("custom"))?.readText()) - assertEquals("reserved metadata", decoded.getOrNull(ReservedMimeType(120))?.readText()) - assertEquals("avro metadata", decoded.getOrNull(WellKnownMimeType.ApplicationAvro)?.readText()) + assertEquals("custom metadata", decoded.getOrNull(MimeType("custom"))?.readText()) + assertEquals("reserved metadata", decoded.getOrNull(MimeType(120))?.readText()) + assertEquals("avro metadata", decoded.getOrNull(MimeType.WellKnown.ApplicationAvro)?.readText()) } @Test fun testList() { val cm = buildCompositeMetadata { - add(CustomMimeType("custom"), packet("custom metadata - 1")) - add(ReservedMimeType(120), packet("reserved metadata - 1")) - add(ReservedMimeType(120), packet("reserved metadata - 2")) - add(WellKnownMimeType.MessageRSocketRouting, packet("routing metadata")) - add(CustomMimeType("custom"), packet("custom metadata - 2")) + add(MimeType("custom"), packet("custom metadata - 1")) + add(MimeType(120), packet("reserved metadata - 1")) + add(MimeType(120), packet("reserved metadata - 2")) + add(MimeType.WellKnown.MessageRSocketRouting, packet("routing metadata")) + add(MimeType("custom"), packet("custom metadata - 2")) } val decoded = cm.readLoop(CompositeMetadata) assertEquals(5, decoded.entries.size) - decoded.list(WellKnownMimeType.ApplicationAvro).let { + decoded.list(MimeType.WellKnown.ApplicationAvro).let { assertEquals(0, it.size) } - decoded.list(CustomMimeType("custom2")).let { + decoded.list(MimeType("custom2")).let { assertEquals(0, it.size) } - decoded.list(ReservedMimeType(110)).let { + decoded.list(MimeType(110)).let { assertEquals(0, it.size) } - decoded.list(WellKnownMimeType.MessageRSocketRouting).let { + decoded.list(MimeType.WellKnown.MessageRSocketRouting).let { assertEquals(1, it.size) assertEquals("routing metadata", it[0].readText()) } - decoded.list(CustomMimeType("custom")).let { + decoded.list(MimeType("custom")).let { assertEquals(2, it.size) assertEquals("custom metadata - 1", it[0].readText()) assertEquals("custom metadata - 2", it[1].readText()) } - decoded.list(ReservedMimeType(120)).let { + decoded.list(MimeType(120)).let { assertEquals(2, it.size) assertEquals("reserved metadata - 1", it[0].readText()) assertEquals("reserved metadata - 2", it[1].readText()) @@ -172,7 +172,7 @@ class CompositeMetadataTest : TestWithLeakCheck { val packet = packet("string") assertFails { buildCompositeMetadata { - add(WellKnownMimeType.ApplicationAvro, packet) + add(MimeType.WellKnown.ApplicationAvro, packet) error("") } } @@ -185,12 +185,12 @@ class CompositeMetadataTest : TestWithLeakCheck { add(RoutingMetadata("tag1", "tag2")) add( PerStreamAcceptableDataMimeTypesMetadata( - WellKnownMimeType.ApplicationAvro, - CustomMimeType("application/custom"), - ReservedMimeType(120) + MimeType.WellKnown.ApplicationAvro, + MimeType("application/custom"), + MimeType(120) ) ) - add(WellKnownMimeType.ApplicationJson, packet("{}")) + add(MimeType.WellKnown.ApplicationJson, packet("{}")) } val decoded = cm.readLoop(CompositeMetadata) @@ -199,19 +199,19 @@ class CompositeMetadataTest : TestWithLeakCheck { assertEquals(listOf("tag1", "tag2"), decoded[RoutingMetadata].tags) assertEquals( listOf( - WellKnownMimeType.ApplicationAvro, - CustomMimeType("application/custom"), - ReservedMimeType(120) + MimeType.WellKnown.ApplicationAvro, + MimeType("application/custom"), + MimeType(120) ), decoded[PerStreamAcceptableDataMimeTypesMetadata].types ) - assertEquals("{}", decoded[WellKnownMimeType.ApplicationJson].readText()) + assertEquals("{}", decoded[MimeType.WellKnown.ApplicationJson].readText()) } @Test fun failOnNonAscii() { assertFailsWith(IllegalArgumentException::class) { - CustomMimeType("1234567#4? 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎") + MimeType("1234567#4? 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎") } } diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadataTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadataTest.kt index 6890add80..75aef7711 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadataTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamAcceptableDataMimeTypesMetadataTest.kt @@ -16,7 +16,7 @@ package io.rsocket.kotlin.metadata -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* import io.rsocket.kotlin.test.* import kotlin.test.* @@ -24,24 +24,24 @@ class PerStreamAcceptableDataMimeTypesMetadataTest : TestWithLeakCheck { @Test fun encodeMetadata() { val metadata = PerStreamAcceptableDataMimeTypesMetadata( - ReservedMimeType(110), - WellKnownMimeType.ApplicationAvro, - CustomMimeType("custom"), - WellKnownMimeType.ApplicationCbor, - ReservedMimeType(120), - CustomMimeType("custom2"), + MimeType(110), + MimeType.WellKnown.ApplicationAvro, + MimeType("custom"), + MimeType.WellKnown.ApplicationCbor, + MimeType(120), + MimeType("custom2"), ) val decoded = metadata.readLoop(PerStreamAcceptableDataMimeTypesMetadata) - assertEquals(WellKnownMimeType.MessageRSocketAcceptMimeTypes, decoded.mimeType) + assertEquals(MimeType.WellKnown.MessageRSocketAcceptMimeTypes, decoded.mimeType) assertEquals(6, decoded.types.size) assertEquals( listOf( - ReservedMimeType(110), - WellKnownMimeType.ApplicationAvro, - CustomMimeType("custom"), - WellKnownMimeType.ApplicationCbor, - ReservedMimeType(120), - CustomMimeType("custom2"), + MimeType(110), + MimeType.WellKnown.ApplicationAvro, + MimeType("custom"), + MimeType.WellKnown.ApplicationCbor, + MimeType(120), + MimeType("custom2"), ), decoded.types ) diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadataTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadataTest.kt index 1b19b5b39..ba34edfb7 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadataTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/PerStreamDataMimeTypeMetadataTest.kt @@ -16,32 +16,36 @@ package io.rsocket.kotlin.metadata -import io.rsocket.kotlin.core.* +import io.rsocket.kotlin.* import io.rsocket.kotlin.test.* import kotlin.test.* class PerStreamDataMimeTypeMetadataTest : TestWithLeakCheck { @Test fun encodeReserved() { - val metadata = PerStreamDataMimeTypeMetadata(ReservedMimeType(110)) + val metadata = PerStreamDataMimeTypeMetadata(MimeType(110)) val decoded = metadata.readLoop(PerStreamDataMimeTypeMetadata) - assertEquals(WellKnownMimeType.MessageRSocketMimeType, decoded.mimeType) - assertEquals(ReservedMimeType(110), decoded.type) + assertEquals(MimeType.WellKnown.MessageRSocketMimeType, decoded.mimeType) + assertEquals(MimeType(110), decoded.type) + val type = assertIs(decoded.type) + assertEquals(110, type.identifier) } @Test fun encodeCustom() { - val metadata = PerStreamDataMimeTypeMetadata(CustomMimeType("custom-2")) + val metadata = PerStreamDataMimeTypeMetadata(MimeType("custom-2")) val decoded = metadata.readLoop(PerStreamDataMimeTypeMetadata) - assertEquals(WellKnownMimeType.MessageRSocketMimeType, decoded.mimeType) - assertEquals(CustomMimeType("custom-2"), decoded.type) + assertEquals(MimeType.WellKnown.MessageRSocketMimeType, decoded.mimeType) + assertEquals(MimeType("custom-2"), decoded.type) + val type = assertIs(decoded.type) + assertEquals("custom-2", type.text) } @Test fun encodeWellKnown() { - val metadata = PerStreamDataMimeTypeMetadata(WellKnownMimeType.ApplicationGraphql) + val metadata = PerStreamDataMimeTypeMetadata(MimeType.WellKnown.ApplicationGraphql) val decoded = metadata.readLoop(PerStreamDataMimeTypeMetadata) - assertEquals(WellKnownMimeType.MessageRSocketMimeType, decoded.mimeType) - assertEquals(WellKnownMimeType.ApplicationGraphql, decoded.type) + assertEquals(MimeType.WellKnown.MessageRSocketMimeType, decoded.mimeType) + assertEquals(MimeType.WellKnown.ApplicationGraphql, decoded.type) } } diff --git a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadataTest.kt b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadataTest.kt index 0007fdc6c..651077360 100644 --- a/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadataTest.kt +++ b/rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/metadata/security/AuthMetadataTest.kt @@ -69,31 +69,31 @@ class AuthMetadataTest : TestWithLeakCheck { @Test fun encodeCustomAuth() { - val metadata = RawAuthMetadata(CustomAuthType("custom/auth"), packet("hello world auth data")) + val metadata = RawAuthMetadata(AuthType("custom/auth"), packet("hello world auth data")) val decoded = metadata.readLoop(RawAuthMetadata) - assertEquals(CustomAuthType("custom/auth"), decoded.type) + assertEquals(AuthType("custom/auth"), decoded.type) assertEquals("hello world auth data", decoded.content.readText()) } @Test fun failOnNonAscii() { assertFailsWith(IllegalArgumentException::class) { - CustomAuthType("1234567#4? 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎") + AuthType("1234567#4? 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎") } } @Test fun failOnLongMimeType() { assertFailsWith(IllegalArgumentException::class) { - CustomAuthType("1234567890".repeat(13)) + AuthType("1234567890".repeat(13)) } } @Test fun failOnEmptyMimeType() { assertFailsWith(IllegalArgumentException::class) { - CustomAuthType("") + AuthType("") } }