Skip to content

Commit ba7978c

Browse files
Copilotringabout
andauthored
Fix multipart/form-data parser breaking on empty entries (#284)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
1 parent a493d47 commit ba7978c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/prologue/core/form.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func parseFormPart*(body, contentType: string): FormPart =
5858
tail = data[pos ..< ^2] # 2 because of protocol newline after content disposition body
5959

6060
if not head.startsWith("Content-Disposition"):
61-
break
61+
continue
6262

6363
for line in head.splitLines:
6464
let header = line.parseHeader

tests/unit/tunit_core/tunit_form

378 KB
Binary file not shown.

tests/unit/tunit_core/tunit_form.nim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ../../../src/prologue/core/form
2-
import tables, strutils
2+
import tables, strutils, strtabs
33

44
block:
55
const testmime =
@@ -40,3 +40,19 @@ block:
4040
let formPart = parseFormPart(testmime, contenttype)
4141
doAssert formPart.data["upload"].body.len == testfile.len
4242
doAssert formPart.data["upload"].body == testfile
43+
44+
block:
45+
# Test for file input field (issue: multipart/form-data not working)
46+
# This tests that file inputs with filename parameter work correctly
47+
const testmime =
48+
"------WebKitFormBoundary7MA4YWxkTrZu0gW\13\10" &
49+
"Content-Disposition: form-data; name=\"myfile\"; filename=\"test.txt\"\13\10" &
50+
"Content-Type: text/plain\13\10" &
51+
"\13\10" &
52+
"Hello World\13\10" &
53+
"------WebKitFormBoundary7MA4YWxkTrZu0gW--\13\10"
54+
const contenttype = "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
55+
let formPart = parseFormPart(testmime, contenttype)
56+
doAssert formPart.data.contains("myfile"), "myfile field should be present"
57+
doAssert formPart.data["myfile"].body == "Hello World"
58+
doAssert formPart.data["myfile"].params.getOrDefault("filename", "") == "test.txt"

0 commit comments

Comments
 (0)