Skip to content

Commit

Permalink
Fix custom MIME type serialization incompatibility (rsocket#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
akowal committed May 13, 2024
1 parent 2239c5c commit 4a9458d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
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 of 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())
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 of 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)
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() }
}

0 comments on commit 4a9458d

Please sign in to comment.