Skip to content

Commit de12fb8

Browse files
committed
add generate content type
1 parent 297965d commit de12fb8

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

Sources/FormData/Serializer.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ public final class Serializer {
3939
part.headers["Content-Disposition"] = contentDisposition
4040
try multipart.serialize(part)
4141
}
42+
43+
/// Generates a Content-Type header value from a boundary
44+
public static func generateContentType(boundary: BytesConvertible) throws -> Bytes {
45+
let b = try boundary.makeBytes()
46+
return "multipart/form-data; boundary=".bytes + b
47+
}
4248
}

Sources/Multipart/Parser.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Core
22
import HTTP
3+
import Foundation
34

45
/**
56
Parses preamble, Parts, and epilogue from a Multipart
@@ -72,6 +73,15 @@ public final class Parser {
7273
buffer = []
7374
}
7475

76+
/// Extracts the boundary from a multipart Content-Type header
77+
public static func extractBoundary(contentType: BytesConvertible) throws -> Bytes {
78+
let boundaryPieces = try contentType.makeBytes().string.components(separatedBy: "boundary=")
79+
guard boundaryPieces.count == 2 else {
80+
throw Error.invalidBoundary
81+
}
82+
return boundaryPieces[1].bytes
83+
}
84+
7585
/// Create a new multipart parser from a
7686
/// Content-Type header value.
7787
public convenience init(contentType: BytesConvertible) throws {
@@ -227,15 +237,6 @@ public final class Parser {
227237
}
228238
}
229239

230-
/// Extracts the boundary from a multipart Content-Type header
231-
public static func extractBoundary(contentType: BytesConvertible) throws -> Bytes {
232-
let boundaryPieces = try contentType.makeBytes().string.components(separatedBy: "boundary=")
233-
guard boundaryPieces.count == 2 else {
234-
throw Error.invalidBoundary
235-
}
236-
return boundaryPieces[1].bytes
237-
}
238-
239240
// Private flag for tracking whether `finish()`
240241
// has been called.
241242
private var hasFinished = false

0 commit comments

Comments
 (0)