Skip to content

Commit

Permalink
Merge pull request #6 from vapor/issue-vapor-938
Browse files Browse the repository at this point in the history
Fix for parsing content with -- inside
  • Loading branch information
tanner0101 authored Apr 5, 2017
2 parents 99f8704 + 10acbcf commit af678f0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Sources/Multipart/BoundaryParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ final class BoundaryParser {
let match = [.hyphen, .hyphen] + boundary

if
(buffer.count <= 1 && byte == .hyphen) ||
(buffer.count > 1 && buffer.count < match.count)
buffer.count < match.count && match[buffer.count] == byte
{
state = .parsing(buffer: buffer + [byte], trailingHyphenCount: trailingHyphenCount)
break main
Expand Down
28 changes: 27 additions & 1 deletion Tests/FormDataTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class ParserTests: XCTestCase {
("testFormData", testFormData),
("testWebkit", testWebkit),
("testForm", testForm),
("testFormManyFields", testFormManyFields)
("testFormManyFields", testFormManyFields),
("testBoundaryLikeContent", testBoundaryLikeContent)
]

func testFormData() throws {
Expand Down Expand Up @@ -139,4 +140,29 @@ class ParserTests: XCTestCase {
XCTAssertEqual(fields["field\(i)"]?.part.body.makeString(), "The Quick Brown Fox Jumps Over The Lazy Dog", "Field 'field\(i)' was parsed incorrectly!")
}
}

func testBoundaryLikeContent() throws {
var message = ""

message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ\r\n"
message += "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n"
message += "Content-Type: text/plain\r\n"
message += "\r\n"
message += "---this is a test\r\n"
message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ--\r\n"

let multipart = try Multipart.Parser(boundary: "----WebKitFormBoundaryezkRLRyEVe1aMUVZ")
let parser = FormData.Parser(multipart: multipart)

var fields: [String: Field] = [:]

parser.onField = { field in
fields[field.name] = field
}

try parser.multipart.parse(message)

XCTAssertEqual(fields["file"]?.filename, "test.txt")
XCTAssertEqual("---this is a test", fields["file"]?.part.body.makeString())
}
}

0 comments on commit af678f0

Please sign in to comment.