Skip to content

Commit

Permalink
Merge pull request #7 from vapor/issue-vapor-938-v1
Browse files Browse the repository at this point in the history
Fix for parsing content with -- inside – v1 backport
  • Loading branch information
tanner0101 authored Apr 5, 2017
2 parents 1725590 + e13f33a commit fc15a16
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Sources/Multipart/BoundaryParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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
65 changes: 65 additions & 0 deletions Tests/FormDataTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ParserTests: XCTestCase {
static var allTests = [
("testFormData", testFormData),
("testWebkit", testWebkit),
("testForm", testForm),
("testFormManyFields", testFormManyFields),
("testBoundaryLikeContent", testBoundaryLikeContent)
]

func testFormData() throws {
Expand Down Expand Up @@ -101,4 +104,66 @@ class ParserTests: XCTestCase {

XCTAssertEqual(fields["name"]?.part.body.string, "hi")
}

func testFormManyFields() throws {
var message = ""

message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"
message += "Content-Disposition: form-data; name=\"field1\"\r\n\r\n"
message += "The Quick Brown Fox Jumps Over The Lazy Dog\r\n"
message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"
message += "Content-Disposition: form-data; name=\"field2\"\r\n\r\n"
message += "The Quick Brown Fox Jumps Over The Lazy Dog\r\n"
message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"
message += "Content-Disposition: form-data; name=\"field3\"\r\n\r\n"
message += "The Quick Brown Fox Jumps Over The Lazy Dog\r\n"
message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"
message += "Content-Disposition: form-data; name=\"field4\"\r\n\r\n"
message += "The Quick Brown Fox Jumps Over The Lazy Dog\r\n"
message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"
message += "Content-Disposition: form-data; name=\"field5\"\r\n\r\n"
message += "The Quick Brown Fox Jumps Over The Lazy Dog\r\n"
message += "------WebKitFormBoundaryMFxQS4UblUjPXRXM\r\n"

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

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

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

try parser.multipart.parse(message)

for i in 1...5 {
XCTAssertEqual(fields["field\(i)"]?.part.body.string, "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.string)
}
}

0 comments on commit fc15a16

Please sign in to comment.