Skip to content

Commit

Permalink
Handle content new lines in fromFormAST (zio#2976)
Browse files Browse the repository at this point in the history
Signed-off-by: Nabil Abdel-Hafeez <[email protected]>
  • Loading branch information
987Nabil committed Jul 30, 2024
1 parent 12f0c26 commit 176b797
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/FormSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ object FormSpec extends ZIOHttpSpec {
.runCollect
.map { c => assertTrue(c == expected) }
},
test("crlf not stripped") {
val content = "1,2\r\n3,4\r\n"
val form = Form(
Chunk(
FormField.textField("csv", content, MediaType.text.`csv`),
),
)
val boundary = Boundary("X-INSOMNIA-BOUNDARY")
val formByteStream = form.multipartBytes(boundary)
val streamingForm = StreamingForm(formByteStream, boundary)
streamingForm.fields.mapZIO { field =>
field.asChunk
}.runCollect.map { chunks =>
val s = chunks.headOption.map(_.asString)
assertTrue(s.contains(content))
}
},
) @@ sequential

def spec =
Expand Down
4 changes: 3 additions & 1 deletion zio-http/shared/src/main/scala/zio/http/FormField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ object FormField {
defaultCharset: Charset = StandardCharsets.UTF_8,
): Either[FormDecodingError, FormField] = {
val extract =
ast.foldLeft(
ast.init.foldLeft(
(
Option.empty[FormAST.Header],
Option.empty[FormAST.Header],
Expand All @@ -159,6 +159,8 @@ object FormField {
) {
case (accum, header: FormAST.Header) if header.name.equalsIgnoreCase("Content-Disposition") =>
(Some(header), accum._2, accum._3, accum._4)
case (accum, FormAST.EoL) =>
(accum._1, accum._2, accum._3, accum._4 :+ FormAST.Content(Chunk[Byte](13, 10)))
case (accum, content: FormAST.Content) =>
(accum._1, accum._2, accum._3, accum._4 :+ content)
case (accum, header: FormAST.Header) if header.name.equalsIgnoreCase("Content-Type") =>
Expand Down

0 comments on commit 176b797

Please sign in to comment.