Skip to content

Commit 873e42d

Browse files
committed
add extract boundary
1 parent 0e8f3b3 commit 873e42d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/Multipart/Parser.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public final class Parser {
3030
/// Possible errors that may be encountered while parsing.
3131
public enum Error: Swift.Error {
3232
case hasAlreadyFinished
33+
case invalidBoundary
3334
}
3435

3536
/// An enum representing all possible states of the parser.
@@ -71,6 +72,13 @@ public final class Parser {
7172
buffer = []
7273
}
7374

75+
/// Create a new multipart parser from a
76+
/// Content-Type header value.
77+
public convenience init(contentType: BytesConvertible) throws {
78+
let boundary = try Parser.extractBoundary(contentType: contentType)
79+
self.init(boundary: boundary)
80+
}
81+
7482
// A buffer for the bytes that have been parsed.
7583
// This allows for a reduction in the number of copies
7684
// needed for each step as only indecies into this array
@@ -219,6 +227,14 @@ public final class Parser {
219227
}
220228
}
221229

230+
public static func extractBoundary(contentType: BytesConvertible) throws -> Bytes {
231+
let boundaryPieces = try contentType.makeBytes().string.components(separatedBy: "boundary=")
232+
guard boundaryPieces.count == 2 else {
233+
throw Error.invalidBoundary
234+
}
235+
return boundaryPieces[1].bytes
236+
}
237+
222238
// Private flag for tracking whether `finish()`
223239
// has been called.
224240
private var hasFinished = false

0 commit comments

Comments
 (0)