123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- import Foundation
- #if os(iOS) || os(watchOS) || os(tvOS)
- import MobileCoreServices
- #elseif os(macOS)
- import CoreServices
- #endif
- open class MultipartFormData {
-
- enum EncodingCharacters {
- static let crlf = "\r\n"
- }
- enum BoundaryGenerator {
- enum BoundaryType {
- case initial, encapsulated, final
- }
- static func randomBoundary() -> String {
- let first = UInt32.random(in: UInt32.min...UInt32.max)
- let second = UInt32.random(in: UInt32.min...UInt32.max)
- return String(format: "alamofire.boundary.%08x%08x", first, second)
- }
- static func boundaryData(forBoundaryType boundaryType: BoundaryType, boundary: String) -> Data {
- let boundaryText: String
- switch boundaryType {
- case .initial:
- boundaryText = "--\(boundary)\(EncodingCharacters.crlf)"
- case .encapsulated:
- boundaryText = "\(EncodingCharacters.crlf)--\(boundary)\(EncodingCharacters.crlf)"
- case .final:
- boundaryText = "\(EncodingCharacters.crlf)--\(boundary)--\(EncodingCharacters.crlf)"
- }
- return Data(boundaryText.utf8)
- }
- }
- class BodyPart {
- let headers: HTTPHeaders
- let bodyStream: InputStream
- let bodyContentLength: UInt64
- var hasInitialBoundary = false
- var hasFinalBoundary = false
- init(headers: HTTPHeaders, bodyStream: InputStream, bodyContentLength: UInt64) {
- self.headers = headers
- self.bodyStream = bodyStream
- self.bodyContentLength = bodyContentLength
- }
- }
-
-
- public static let encodingMemoryThreshold: UInt64 = 10_000_000
-
- open lazy var contentType: String = "multipart/form-data; boundary=\(self.boundary)"
-
- public var contentLength: UInt64 { bodyParts.reduce(0) { $0 + $1.bodyContentLength } }
-
- public let boundary: String
- let fileManager: FileManager
- private var bodyParts: [BodyPart]
- private var bodyPartError: AFError?
- private let streamBufferSize: Int
-
-
-
-
-
-
- public init(fileManager: FileManager = .default, boundary: String? = nil) {
- self.fileManager = fileManager
- self.boundary = boundary ?? BoundaryGenerator.randomBoundary()
- bodyParts = []
-
-
-
-
-
- streamBufferSize = 1024
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func append(_ data: Data, withName name: String, fileName: String? = nil, mimeType: String? = nil) {
- let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
- let stream = InputStream(data: data)
- let length = UInt64(data.count)
- append(stream, withLength: length, headers: headers)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func append(_ fileURL: URL, withName name: String) {
- let fileName = fileURL.lastPathComponent
- let pathExtension = fileURL.pathExtension
- if !fileName.isEmpty && !pathExtension.isEmpty {
- let mime = mimeType(forPathExtension: pathExtension)
- append(fileURL, withName: name, fileName: fileName, mimeType: mime)
- } else {
- setBodyPartError(withReason: .bodyPartFilenameInvalid(in: fileURL))
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func append(_ fileURL: URL, withName name: String, fileName: String, mimeType: String) {
- let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
-
-
-
- guard fileURL.isFileURL else {
- setBodyPartError(withReason: .bodyPartURLInvalid(url: fileURL))
- return
- }
-
-
-
- #if !(os(Linux) || os(Windows))
- do {
- let isReachable = try fileURL.checkPromisedItemIsReachable()
- guard isReachable else {
- setBodyPartError(withReason: .bodyPartFileNotReachable(at: fileURL))
- return
- }
- } catch {
- setBodyPartError(withReason: .bodyPartFileNotReachableWithError(atURL: fileURL, error: error))
- return
- }
- #endif
-
-
-
- var isDirectory: ObjCBool = false
- let path = fileURL.path
- guard fileManager.fileExists(atPath: path, isDirectory: &isDirectory) && !isDirectory.boolValue else {
- setBodyPartError(withReason: .bodyPartFileIsDirectory(at: fileURL))
- return
- }
-
-
-
- let bodyContentLength: UInt64
- do {
- guard let fileSize = try fileManager.attributesOfItem(atPath: path)[.size] as? NSNumber else {
- setBodyPartError(withReason: .bodyPartFileSizeNotAvailable(at: fileURL))
- return
- }
- bodyContentLength = fileSize.uint64Value
- } catch {
- setBodyPartError(withReason: .bodyPartFileSizeQueryFailedWithError(forURL: fileURL, error: error))
- return
- }
-
-
-
- guard let stream = InputStream(url: fileURL) else {
- setBodyPartError(withReason: .bodyPartInputStreamCreationFailed(for: fileURL))
- return
- }
- append(stream, withLength: bodyContentLength, headers: headers)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func append(_ stream: InputStream,
- withLength length: UInt64,
- name: String,
- fileName: String,
- mimeType: String) {
- let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
- append(stream, withLength: length, headers: headers)
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public func append(_ stream: InputStream, withLength length: UInt64, headers: HTTPHeaders) {
- let bodyPart = BodyPart(headers: headers, bodyStream: stream, bodyContentLength: length)
- bodyParts.append(bodyPart)
- }
-
-
-
-
-
-
-
-
-
- public func encode() throws -> Data {
- if let bodyPartError = bodyPartError {
- throw bodyPartError
- }
- var encoded = Data()
- bodyParts.first?.hasInitialBoundary = true
- bodyParts.last?.hasFinalBoundary = true
- for bodyPart in bodyParts {
- let encodedData = try encode(bodyPart)
- encoded.append(encodedData)
- }
- return encoded
- }
-
-
-
-
-
-
-
- public func writeEncodedData(to fileURL: URL) throws {
- if let bodyPartError = bodyPartError {
- throw bodyPartError
- }
- if fileManager.fileExists(atPath: fileURL.path) {
- throw AFError.multipartEncodingFailed(reason: .outputStreamFileAlreadyExists(at: fileURL))
- } else if !fileURL.isFileURL {
- throw AFError.multipartEncodingFailed(reason: .outputStreamURLInvalid(url: fileURL))
- }
- guard let outputStream = OutputStream(url: fileURL, append: false) else {
- throw AFError.multipartEncodingFailed(reason: .outputStreamCreationFailed(for: fileURL))
- }
- outputStream.open()
- defer { outputStream.close() }
- bodyParts.first?.hasInitialBoundary = true
- bodyParts.last?.hasFinalBoundary = true
- for bodyPart in bodyParts {
- try write(bodyPart, to: outputStream)
- }
- }
-
- private func encode(_ bodyPart: BodyPart) throws -> Data {
- var encoded = Data()
- let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
- encoded.append(initialData)
- let headerData = encodeHeaders(for: bodyPart)
- encoded.append(headerData)
- let bodyStreamData = try encodeBodyStream(for: bodyPart)
- encoded.append(bodyStreamData)
- if bodyPart.hasFinalBoundary {
- encoded.append(finalBoundaryData())
- }
- return encoded
- }
- private func encodeHeaders(for bodyPart: BodyPart) -> Data {
- let headerText = bodyPart.headers.map { "\($0.name): \($0.value)\(EncodingCharacters.crlf)" }
- .joined()
- + EncodingCharacters.crlf
- return Data(headerText.utf8)
- }
- private func encodeBodyStream(for bodyPart: BodyPart) throws -> Data {
- let inputStream = bodyPart.bodyStream
- inputStream.open()
- defer { inputStream.close() }
- var encoded = Data()
- while inputStream.hasBytesAvailable {
- var buffer = [UInt8](repeating: 0, count: streamBufferSize)
- let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
- if let error = inputStream.streamError {
- throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: error))
- }
- if bytesRead > 0 {
- encoded.append(buffer, count: bytesRead)
- } else {
- break
- }
- }
- guard UInt64(encoded.count) == bodyPart.bodyContentLength else {
- let error = AFError.UnexpectedInputStreamLength(bytesExpected: bodyPart.bodyContentLength,
- bytesRead: UInt64(encoded.count))
- throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: error))
- }
- return encoded
- }
-
- private func write(_ bodyPart: BodyPart, to outputStream: OutputStream) throws {
- try writeInitialBoundaryData(for: bodyPart, to: outputStream)
- try writeHeaderData(for: bodyPart, to: outputStream)
- try writeBodyStream(for: bodyPart, to: outputStream)
- try writeFinalBoundaryData(for: bodyPart, to: outputStream)
- }
- private func writeInitialBoundaryData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
- let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
- return try write(initialData, to: outputStream)
- }
- private func writeHeaderData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
- let headerData = encodeHeaders(for: bodyPart)
- return try write(headerData, to: outputStream)
- }
- private func writeBodyStream(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
- let inputStream = bodyPart.bodyStream
- inputStream.open()
- defer { inputStream.close() }
- while inputStream.hasBytesAvailable {
- var buffer = [UInt8](repeating: 0, count: streamBufferSize)
- let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
- if let streamError = inputStream.streamError {
- throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: streamError))
- }
- if bytesRead > 0 {
- if buffer.count != bytesRead {
- buffer = Array(buffer[0..<bytesRead])
- }
- try write(&buffer, to: outputStream)
- } else {
- break
- }
- }
- }
- private func writeFinalBoundaryData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
- if bodyPart.hasFinalBoundary {
- return try write(finalBoundaryData(), to: outputStream)
- }
- }
-
- private func write(_ data: Data, to outputStream: OutputStream) throws {
- var buffer = [UInt8](repeating: 0, count: data.count)
- data.copyBytes(to: &buffer, count: data.count)
- return try write(&buffer, to: outputStream)
- }
- private func write(_ buffer: inout [UInt8], to outputStream: OutputStream) throws {
- var bytesToWrite = buffer.count
- while bytesToWrite > 0, outputStream.hasSpaceAvailable {
- let bytesWritten = outputStream.write(buffer, maxLength: bytesToWrite)
- if let error = outputStream.streamError {
- throw AFError.multipartEncodingFailed(reason: .outputStreamWriteFailed(error: error))
- }
- bytesToWrite -= bytesWritten
- if bytesToWrite > 0 {
- buffer = Array(buffer[bytesWritten..<buffer.count])
- }
- }
- }
-
- private func mimeType(forPathExtension pathExtension: String) -> String {
- #if !(os(Linux) || os(Windows))
- if
- let id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
- let contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue() {
- return contentType as String
- }
- #endif
- return "application/octet-stream"
- }
-
- private func contentHeaders(withName name: String, fileName: String? = nil, mimeType: String? = nil) -> HTTPHeaders {
- var disposition = "form-data; name=\"\(name)\""
- if let fileName = fileName { disposition += "; filename=\"\(fileName)\"" }
- var headers: HTTPHeaders = [.contentDisposition(disposition)]
- if let mimeType = mimeType { headers.add(.contentType(mimeType)) }
- return headers
- }
-
- private func initialBoundaryData() -> Data {
- BoundaryGenerator.boundaryData(forBoundaryType: .initial, boundary: boundary)
- }
- private func encapsulatedBoundaryData() -> Data {
- BoundaryGenerator.boundaryData(forBoundaryType: .encapsulated, boundary: boundary)
- }
- private func finalBoundaryData() -> Data {
- BoundaryGenerator.boundaryData(forBoundaryType: .final, boundary: boundary)
- }
-
- private func setBodyPartError(withReason reason: AFError.MultipartEncodingFailureReason) {
- guard bodyPartError == nil else { return }
- bodyPartError = AFError.multipartEncodingFailed(reason: reason)
- }
- }
|