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

KTOR-7596 Make multipart Content-Type check case-insensitive #4413

Merged
merged 2 commits into from
Oct 29, 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
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.plugins.contentnegotiation
Expand All @@ -16,6 +16,6 @@ public object JsonContentTypeMatcher : ContentTypeMatcher {
}

val value = contentType.withoutParameters().toString()
return value.startsWith("application/") && value.endsWith("+json")
return value.startsWith("application/", ignoreCase = true) && value.endsWith("+json", ignoreCase = true)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.plugins.json

Expand All @@ -13,6 +13,6 @@ internal class JsonContentTypeMatcher : ContentTypeMatcher {
}

val value = contentType.withoutParameters().toString()
return value.startsWith("application/") && value.endsWith("+json")
return value.startsWith("application/", ignoreCase = true) && value.endsWith("+json", ignoreCase = true)
}
}
6 changes: 2 additions & 4 deletions ktor-http/ktor-http-cio/jvm/src/io/ktor/http/cio/Multipart.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.http.cio
Expand All @@ -11,7 +11,6 @@ import io.ktor.utils.io.core.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.io.*
import kotlinx.io.IOException
import kotlinx.io.bytestring.*
import java.io.EOFException
import java.nio.*
Expand Down Expand Up @@ -148,14 +147,13 @@ public fun CoroutineScope.parseMultipart(
/**
* Starts a multipart parser coroutine producing multipart events
*/
@Suppress("DEPRECATION_ERROR")
public fun CoroutineScope.parseMultipart(
input: ByteReadChannel,
contentType: CharSequence,
contentLength: Long?,
maxPartSize: Long = Long.MAX_VALUE,
): ReceiveChannel<MultipartEvent> {
if (!contentType.startsWith("multipart/")) {
if (!contentType.startsWith("multipart/", ignoreCase = true)) {
throw IOException("Failed to parse multipart: Content-Type should be multipart/* but it is $contentType")
}
val boundaryByteBuffer = parseBoundaryInternal(contentType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.tests.http.cio

import io.ktor.http.cio.*
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.test.*
import kotlinx.io.*
import kotlin.test.*

@OptIn(DelicateCoroutinesApi::class)
Expand Down Expand Up @@ -335,6 +337,17 @@ class MultipartTest {
}
}

@Test
fun testParseContentType() = runTest {
fun testContentType(contentType: String) {
parseMultipart(ByteReadChannel.Empty, "$contentType; boundary=A", 0L)
}

testContentType("multipart/mixed")
testContentType("Multipart/mixed")
assertFailsWith<IOException> { testContentType("multi-part/mixed") }
}

@OptIn(DelicateCoroutinesApi::class)
@Test
fun testEmptyPayload() = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.server.jetty.jakarta
Expand All @@ -8,7 +8,6 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.response.*
import io.ktor.util.*
import io.ktor.util.cio.*
import io.ktor.util.pipeline.*
import io.ktor.utils.io.*
Expand Down Expand Up @@ -72,7 +71,7 @@ internal class JettyKtorHandler(
) {
try {
val contentType = request.contentType
if (contentType != null && contentType.startsWith("multipart/")) {
if (contentType != null && contentType.startsWith("multipart/", ignoreCase = true)) {
baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multipartConfig)
// TODO someone reported auto-cleanup issues so we have to check it
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.server.jetty
Expand All @@ -8,7 +8,6 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.response.*
import io.ktor.util.*
import io.ktor.util.cio.*
import io.ktor.util.pipeline.*
import io.ktor.utils.io.*
Expand Down Expand Up @@ -72,7 +71,7 @@ internal class JettyKtorHandler(
) {
try {
val contentType = request.contentType
if (contentType != null && contentType.startsWith("multipart/")) {
if (contentType != null && contentType.startsWith("multipart/", ignoreCase = true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract constant here

Copy link
Member Author

@osipxd osipxd Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add a constant ContentType.Application.TYPE. I also thought about adding an operator function contains:

public object ContentType {
    public object Application {
        public const val TYPE: String = "application"
        // ...

        public operator fun contains(contentType: CharSequence): Boolean =
            contentType.startsWith("$TYPE/", ignoreCase = true)
    }
}

And then check the content type using in:

if (contentType in ContentType.Application) { ... }

What do you think?

Copy link
Member Author

@osipxd osipxd Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make it in a separate PR (if needed)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjhham, what do you think?

Copy link
Member Author

@osipxd osipxd Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already done some experiments locally, so here you can find a draft PR: #4433
Let's discuss there

baseRequest.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, multipartConfig)
// TODO someone reported auto-cleanup issues so we have to check it
}
Expand Down