Skip to content

Commit

Permalink
Fix: Treat unrecognized MIME types as binary in MediaType
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Nov 9, 2024
1 parent c51db7f commit 6883910
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/MediaTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ object MediaTypeSpec extends ZIOHttpSpec {
),
)
},
test("application/x-zip-compressed should be binary") {
val mediaType = MediaType.forContentType("application/x-zip-compressed")
assertTrue(mediaType.exists(_.binary))
},
test("text/plain should not be binary") {
val mediaType = MediaType.forContentType("text/plain")
assertTrue(mediaType.exists(!_.binary))
},
)
}
5 changes: 5 additions & 0 deletions zio-http/shared/src/main/scala/zio/http/MediaType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ object MediaType extends MediaTypes {
val contentTypeParts = customMediaType.split('/')
if (contentTypeParts.length == 2) {
val subtypeParts = contentTypeParts(1).split(';')
// Default binary to true for unknown types unless they belong to text families
val isBinary = !customMediaType.startsWith("text/") &&
!customMediaType.startsWith("application/json") &&
!customMediaType.startsWith("application/xml")
if (subtypeParts.length >= 1) {
Some(
MediaType(
mainType = contentTypeParts.head,
subType = subtypeParts.head,
binary = isBinary,
parameters = if (subtypeParts.length >= 2) parseOptionalParameters(subtypeParts.tail) else Map.empty,
),
)
Expand Down

0 comments on commit 6883910

Please sign in to comment.