Skip to content

Commit af678f0

Browse files
authored
Merge pull request #6 from vapor/issue-vapor-938
Fix for parsing content with -- inside
2 parents 99f8704 + 10acbcf commit af678f0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Sources/Multipart/BoundaryParser.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ final class BoundaryParser {
4343
let match = [.hyphen, .hyphen] + boundary
4444

4545
if
46-
(buffer.count <= 1 && byte == .hyphen) ||
47-
(buffer.count > 1 && buffer.count < match.count)
46+
buffer.count < match.count && match[buffer.count] == byte
4847
{
4948
state = .parsing(buffer: buffer + [byte], trailingHyphenCount: trailingHyphenCount)
5049
break main

Tests/FormDataTests/ParserTests.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class ParserTests: XCTestCase {
88
("testFormData", testFormData),
99
("testWebkit", testWebkit),
1010
("testForm", testForm),
11-
("testFormManyFields", testFormManyFields)
11+
("testFormManyFields", testFormManyFields),
12+
("testBoundaryLikeContent", testBoundaryLikeContent)
1213
]
1314

1415
func testFormData() throws {
@@ -139,4 +140,29 @@ class ParserTests: XCTestCase {
139140
XCTAssertEqual(fields["field\(i)"]?.part.body.makeString(), "The Quick Brown Fox Jumps Over The Lazy Dog", "Field 'field\(i)' was parsed incorrectly!")
140141
}
141142
}
143+
144+
func testBoundaryLikeContent() throws {
145+
var message = ""
146+
147+
message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ\r\n"
148+
message += "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n"
149+
message += "Content-Type: text/plain\r\n"
150+
message += "\r\n"
151+
message += "---this is a test\r\n"
152+
message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ--\r\n"
153+
154+
let multipart = try Multipart.Parser(boundary: "----WebKitFormBoundaryezkRLRyEVe1aMUVZ")
155+
let parser = FormData.Parser(multipart: multipart)
156+
157+
var fields: [String: Field] = [:]
158+
159+
parser.onField = { field in
160+
fields[field.name] = field
161+
}
162+
163+
try parser.multipart.parse(message)
164+
165+
XCTAssertEqual(fields["file"]?.filename, "test.txt")
166+
XCTAssertEqual("---this is a test", fields["file"]?.part.body.makeString())
167+
}
142168
}

0 commit comments

Comments
 (0)