-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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-7470 receiveMultipart throw UnsupportedMediaTypeException #4339
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e8d2d61
KTOR-7470 fix typo
58d1714
KTOR-7470 Remove unused import statements
5735df8
KTOR-7470 Rethrow UnsupportedMediaTypeException if content type heade…
3eba61e
KTOR-7470 generalize UnsupportedMediaException message when content-t…
51d9c34
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado 28c4a2e
KTOR-7470 throw UnsupportedMediaTypeException only if message contain…
f64059a
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado 2831967
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado fc2a59e
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado 0ccca30
KTOR-7470 add UnsupportedMediaTypeException for CIO module
4e022e4
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado 814cea1
fix codestyle, run apiDump, add test, add @InternalAPI
marychatte 1dfb333
Merge branch 'ktorio:main' into issue/KTOR-7470
stokado 1a9ef52
run apiDump
marychatte 7423a44
Merge remote-tracking branch 'origin/3.1.0-eap' into fork/stokado/iss…
marychatte 601efac
move Errors.kt from jvm to common
marychatte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
ktor-http/ktor-http-cio/jvm/src/io/ktor/http/cio/Errors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* 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 | ||
|
||
import io.ktor.utils.io.* | ||
import kotlinx.io.IOException | ||
|
||
@InternalAPI | ||
public class UnsupportedMediaTypeExceptionCIO(message: String) : IOException(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import io.ktor.http.* | |||||
import io.ktor.http.cio.* | ||||||
import io.ktor.http.content.* | ||||||
import io.ktor.server.application.* | ||||||
import io.ktor.server.plugins.UnsupportedMediaTypeException | ||||||
import io.ktor.server.request.* | ||||||
import io.ktor.util.pipeline.* | ||||||
import io.ktor.utils.io.* | ||||||
|
@@ -33,16 +34,21 @@ internal actual suspend fun PipelineContext<Any, PipelineCall>.defaultPlatformTr | |||||
@OptIn(InternalAPI::class) | ||||||
internal actual fun PipelineContext<*, PipelineCall>.multiPartData(rc: ByteReadChannel): MultiPartData { | ||||||
val contentType = call.request.header(HttpHeaders.ContentType) | ||||||
?: throw IllegalStateException("Content-Type header is required for multipart processing") | ||||||
?: throw UnsupportedMediaTypeException(null) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also one more place, where exception is thrown - CIO:
2)
Can we please fix it too? |
||||||
|
||||||
val contentLength = call.request.header(HttpHeaders.ContentLength)?.toLong() | ||||||
return CIOMultipartDataBase( | ||||||
coroutineContext + Dispatchers.Unconfined, | ||||||
rc, | ||||||
contentType, | ||||||
contentLength, | ||||||
call.formFieldLimit | ||||||
) | ||||||
|
||||||
try { | ||||||
return CIOMultipartDataBase( | ||||||
coroutineContext + Dispatchers.Unconfined, | ||||||
rc, | ||||||
contentType, | ||||||
contentLength, | ||||||
call.formFieldLimit | ||||||
) | ||||||
} catch (_: UnsupportedMediaTypeExceptionCIO) { | ||||||
throw UnsupportedMediaTypeException(ContentType.parse(contentType)) | ||||||
} | ||||||
} | ||||||
|
||||||
internal actual fun Source.readTextWithCustomCharset(charset: Charset): String = | ||||||
|
78 changes: 78 additions & 0 deletions
78
ktor-server/ktor-server-core/jvm/test/io/ktor/tests/server/engine/MultiPartDataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.server.engine | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.plugins.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.testing.* | ||
import io.ktor.util.pipeline.* | ||
import io.ktor.utils.io.* | ||
import io.mockk.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.* | ||
import kotlin.test.* | ||
|
||
class MultiPartDataTest { | ||
private val mockContext = mockk<PipelineContext<*, PipelineCall>>(relaxed = true) | ||
private val mockRequest = mockk<PipelineRequest>(relaxed = true) | ||
private val testScope = TestScope() | ||
|
||
@Test | ||
fun givenRequest_whenNoContentTypeHeaderPresent_thenUnsupportedMediaTypeException() { | ||
// Given | ||
every { mockContext.call.request } returns mockRequest | ||
every { mockRequest.header(HttpHeaders.ContentType) } returns null | ||
|
||
// When & Then | ||
assertFailsWith<UnsupportedMediaTypeException> { | ||
runBlocking { mockContext.multiPartData(ByteReadChannel("sample data")) } | ||
} | ||
} | ||
|
||
@Test | ||
fun givenWrongContentType_whenProcessMultiPart_thenUnsupportedMediaTypeException() { | ||
// Given | ||
val rc = ByteReadChannel("sample data") | ||
val contentType = "test/plain; boundary=test" | ||
val contentLength = "123" | ||
every { mockContext.call.request } returns mockRequest | ||
every { mockContext.call.attributes.getOrNull<Long>(any()) } returns 0L | ||
every { mockRequest.header(HttpHeaders.ContentType) } returns contentType | ||
every { mockRequest.header(HttpHeaders.ContentLength) } returns contentLength | ||
|
||
// When & Then | ||
testScope.runTest { | ||
assertFailsWith<UnsupportedMediaTypeException> { | ||
mockContext.multiPartData(rc) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testUnsupportedMediaTypeStatusCode() = testApplication { | ||
routing { | ||
post { | ||
call.receiveMultipart() | ||
call.respond(HttpStatusCode.OK) | ||
} | ||
} | ||
|
||
client.post { | ||
accept(ContentType.Text.Plain) | ||
}.apply { | ||
assertEquals(HttpStatusCode.UnsupportedMediaType, status) | ||
} | ||
|
||
client.post {}.apply { | ||
assertEquals(HttpStatusCode.UnsupportedMediaType, status) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this change is not binary compatible. Could you add a constructor with an old signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added constructor with an old signature
How can I run an apiDump task now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not run
apiDump
task on my Windows PC because ios subtasks are failingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed it with @osipxd and it's binary compatible, I reverted the last commit and will merge the PR