Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom MIME type serialization incompatibility (#260) #261

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ internal fun ByteReadPacket.readAuthType(): AuthType = readType(

private fun BytePacketBuilder.writeTextWithLength(text: String) {
val typeBytes = text.encodeToByteArray()
writeByte(typeBytes.size.toByte()) //write length
writeFully(typeBytes) //write mime type
// The first byte indicates MIME type encoding:
// - Values 0x00 to 0x7F represent predefined "well-known" MIME types, directly using the byte as the type ID.
// - Values 0x80 to 0xFF denote custom MIME types, where the byte represents the length of the MIME type name that follows.
// For custom types, the length stored (byte value minus 0x80) is adjusted by -1 when writing, and adjusted by +1 when reading,
// mapping to an effective range of 1 to 128 characters for the MIME type name length.
writeByte((typeBytes.size - 1).toByte())
whyoleg marked this conversation as resolved.
Show resolved Hide resolved
writeFully(typeBytes)
}

private const val KnownTypeFlag: Byte = Byte.MIN_VALUE
Expand All @@ -66,7 +71,12 @@ private inline fun <T> ByteReadPacket.readType(
val identifier = byte xor KnownTypeFlag
fromIdentifier(identifier)
} else {
val stringType = readTextExactBytes(byte.toInt())
// The first byte indicates MIME type encoding:
// - Values 0x00 to 0x7F represent predefined "well-known" MIME types, directly using the byte as the type ID.
// - Values 0x80 to 0xFF denote custom MIME types, where the byte represents the length of the MIME type name that follows.
// For custom types, the length stored (byte value minus 0x80) is adjusted by -1 when writing, and adjusted by +1 when reading,
// mapping to an effective range of 1 to 128 characters for the MIME type name length.
val stringType = readTextExactBytes(byte.toInt() + 1)
whyoleg marked this conversation as resolved.
Show resolved Hide resolved
fromText(stringType)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2015-2024 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.rsocket.kotlin.core.*
import io.rsocket.kotlin.test.*
import kotlin.test.*

class MimeTypeTest : TestWithLeakCheck {

@Test
fun customMimeTypeSerialization() {
testCustomMimeType("message/x.foo")
testCustomMimeType(randomAsciiString(1))
testCustomMimeType(randomAsciiString(127))
testCustomMimeType(randomAsciiString(128))
}

private fun testCustomMimeType(name: String) {
val mimeType = CustomMimeType(name)
val packet = packet {
writeMimeType(mimeType)
}
assertEquals(name.length - 1, packet.tryPeek())
assertEquals(mimeType, packet.readMimeType())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2015-2024 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


private val asciiChars = '!'..'~'

internal fun randomAsciiString(length: Int): String {
return (1..length).joinToString(separator = "") { asciiChars.random().toString() }
}
Loading