From 9fb3e147db68150d33a4075621c274afd81615ef Mon Sep 17 00:00:00 2001 From: Olivier ROLAND Date: Tue, 3 Nov 2015 13:45:10 +0100 Subject: [PATCH] =htp fix multipart/form-data unmarshaller when no Content-Type field is present MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RFC 2046: … multipart/* … If no Content-Type field is present it is assumed to be "message/rfc822" in a "multipart/digest" and "text/plain" otherwise RFC 2388: As with all multipart MIME types, each part has an optional "Content-Type", which defaults to text/plain. --- .../unmarshalling/MultipartUnmarshallersSpec.scala | 11 +++++++++++ .../unmarshalling/MultipartUnmarshallers.scala | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallersSpec.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallersSpec.scala index 6011ef9dca..3a597b303e 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallersSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallersSpec.scala @@ -236,10 +236,20 @@ class MultipartUnmarshallersSpec extends FreeSpec with Matchers with BeforeAndAf } "multipartFormDataUnmarshaller should correctly unmarshal 'multipart/form-data' content" - { + "with one element and no explicit content-type" in { + Unmarshal(HttpEntity(`multipart/form-data` withBoundary "XYZABC" withCharset `UTF-8`, + """--XYZABC + |content-disposition: form-data; name=email + | + |test@there.com + |--XYZABC--""".stripMarginWithNewline("\r\n"))).to[Multipart.FormData] should haveParts( + Multipart.FormData.BodyPart.Strict("email", HttpEntity(ContentTypes.`text/plain(UTF-8)`, "test@there.com"))) + } "with one element" in { Unmarshal(HttpEntity(`multipart/form-data` withBoundary "XYZABC" withCharset `UTF-8`, """--XYZABC |content-disposition: form-data; name=email + |Content-Type: application/octet-stream | |test@there.com |--XYZABC--""".stripMarginWithNewline("\r\n"))).to[Multipart.FormData] should haveParts( @@ -255,6 +265,7 @@ class MultipartUnmarshallersSpec extends FreeSpec with Matchers with BeforeAndAf ByteString { """--XYZABC |Content-Disposition: form-data; name="email" + |Content-Type: application/octet-stream | |test@there.com |--XYZABC diff --git a/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala b/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala index 7d522bcae7..4e42b1c084 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/unmarshalling/MultipartUnmarshallers.scala @@ -44,7 +44,7 @@ trait MultipartUnmarshallers { implicit def multipartFormDataUnmarshaller(implicit log: LoggingAdapter = NoLogging, parserSettings: ParserSettings = null): FromEntityUnmarshaller[Multipart.FormData] = multipartUnmarshaller[Multipart.FormData, Multipart.FormData.BodyPart, Multipart.FormData.BodyPart.Strict]( mediaRange = `multipart/form-data`, - defaultContentType = ContentTypes.`application/octet-stream`, + defaultContentType = ContentTypes.`text/plain(UTF-8)`, createBodyPart = (entity, headers) ⇒ Multipart.General.BodyPart(entity, headers).toFormDataBodyPart.get, createStreamed = (_, parts) ⇒ Multipart.FormData(parts), createStrictBodyPart = (entity, headers) ⇒ Multipart.General.BodyPart.Strict(entity, headers).toFormDataBodyPart.get,