File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ public final class Parser {
30
30
/// Possible errors that may be encountered while parsing.
31
31
public enum Error : Swift . Error {
32
32
case hasAlreadyFinished
33
+ case invalidBoundary
33
34
}
34
35
35
36
/// An enum representing all possible states of the parser.
@@ -71,6 +72,13 @@ public final class Parser {
71
72
buffer = [ ]
72
73
}
73
74
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
+
74
82
// A buffer for the bytes that have been parsed.
75
83
// This allows for a reduction in the number of copies
76
84
// needed for each step as only indecies into this array
@@ -219,6 +227,14 @@ public final class Parser {
219
227
}
220
228
}
221
229
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
+
222
238
// Private flag for tracking whether `finish()`
223
239
// has been called.
224
240
private var hasFinished = false
You can’t perform that action at this time.
0 commit comments