MultipartFormData.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //
  2. // MultipartFormData.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. #if os(iOS) || os(watchOS) || os(tvOS)
  26. import MobileCoreServices
  27. #elseif os(macOS)
  28. import CoreServices
  29. #endif
  30. /// Constructs `multipart/form-data` for uploads within an HTTP or HTTPS body. There are currently two ways to encode
  31. /// multipart form data. The first way is to encode the data directly in memory. This is very efficient, but can lead
  32. /// to memory issues if the dataset is too large. The second way is designed for larger datasets and will write all the
  33. /// data to a single file on disk with all the proper boundary segmentation. The second approach MUST be used for
  34. /// larger datasets such as video content, otherwise your app may run out of memory when trying to encode the dataset.
  35. ///
  36. /// For more information on `multipart/form-data` in general, please refer to the RFC-2388 and RFC-2045 specs as well
  37. /// and the w3 form documentation.
  38. ///
  39. /// - https://www.ietf.org/rfc/rfc2388.txt
  40. /// - https://www.ietf.org/rfc/rfc2045.txt
  41. /// - https://www.w3.org/TR/html401/interact/forms.html#h-17.13
  42. open class MultipartFormData {
  43. // MARK: - Helper Types
  44. enum EncodingCharacters {
  45. static let crlf = "\r\n"
  46. }
  47. enum BoundaryGenerator {
  48. enum BoundaryType {
  49. case initial, encapsulated, final
  50. }
  51. static func randomBoundary() -> String {
  52. let first = UInt32.random(in: UInt32.min...UInt32.max)
  53. let second = UInt32.random(in: UInt32.min...UInt32.max)
  54. return String(format: "alamofire.boundary.%08x%08x", first, second)
  55. }
  56. static func boundaryData(forBoundaryType boundaryType: BoundaryType, boundary: String) -> Data {
  57. let boundaryText: String
  58. switch boundaryType {
  59. case .initial:
  60. boundaryText = "--\(boundary)\(EncodingCharacters.crlf)"
  61. case .encapsulated:
  62. boundaryText = "\(EncodingCharacters.crlf)--\(boundary)\(EncodingCharacters.crlf)"
  63. case .final:
  64. boundaryText = "\(EncodingCharacters.crlf)--\(boundary)--\(EncodingCharacters.crlf)"
  65. }
  66. return Data(boundaryText.utf8)
  67. }
  68. }
  69. class BodyPart {
  70. let headers: HTTPHeaders
  71. let bodyStream: InputStream
  72. let bodyContentLength: UInt64
  73. var hasInitialBoundary = false
  74. var hasFinalBoundary = false
  75. init(headers: HTTPHeaders, bodyStream: InputStream, bodyContentLength: UInt64) {
  76. self.headers = headers
  77. self.bodyStream = bodyStream
  78. self.bodyContentLength = bodyContentLength
  79. }
  80. }
  81. // MARK: - Properties
  82. /// Default memory threshold used when encoding `MultipartFormData`, in bytes.
  83. public static let encodingMemoryThreshold: UInt64 = 10_000_000
  84. /// The `Content-Type` header value containing the boundary used to generate the `multipart/form-data`.
  85. open lazy var contentType: String = "multipart/form-data; boundary=\(self.boundary)"
  86. /// The content length of all body parts used to generate the `multipart/form-data` not including the boundaries.
  87. public var contentLength: UInt64 { bodyParts.reduce(0) { $0 + $1.bodyContentLength } }
  88. /// The boundary used to separate the body parts in the encoded form data.
  89. public let boundary: String
  90. let fileManager: FileManager
  91. private var bodyParts: [BodyPart]
  92. private var bodyPartError: AFError?
  93. private let streamBufferSize: Int
  94. // MARK: - Lifecycle
  95. /// Creates an instance.
  96. ///
  97. /// - Parameters:
  98. /// - fileManager: `FileManager` to use for file operations, if needed.
  99. /// - boundary: Boundary `String` used to separate body parts.
  100. public init(fileManager: FileManager = .default, boundary: String? = nil) {
  101. self.fileManager = fileManager
  102. self.boundary = boundary ?? BoundaryGenerator.randomBoundary()
  103. bodyParts = []
  104. //
  105. // The optimal read/write buffer size in bytes for input and output streams is 1024 (1KB). For more
  106. // information, please refer to the following article:
  107. // - https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html
  108. //
  109. streamBufferSize = 1024
  110. }
  111. // MARK: - Body Parts
  112. /// Creates a body part from the data and appends it to the instance.
  113. ///
  114. /// The body part data will be encoded using the following format:
  115. ///
  116. /// - `Content-Disposition: form-data; name=#{name}; filename=#{filename}` (HTTP Header)
  117. /// - `Content-Type: #{mimeType}` (HTTP Header)
  118. /// - Encoded file data
  119. /// - Multipart form boundary
  120. ///
  121. /// - Parameters:
  122. /// - data: `Data` to encoding into the instance.
  123. /// - name: Name to associate with the `Data` in the `Content-Disposition` HTTP header.
  124. /// - fileName: Filename to associate with the `Data` in the `Content-Disposition` HTTP header.
  125. /// - mimeType: MIME type to associate with the data in the `Content-Type` HTTP header.
  126. public func append(_ data: Data, withName name: String, fileName: String? = nil, mimeType: String? = nil) {
  127. let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
  128. let stream = InputStream(data: data)
  129. let length = UInt64(data.count)
  130. append(stream, withLength: length, headers: headers)
  131. }
  132. /// Creates a body part from the file and appends it to the instance.
  133. ///
  134. /// The body part data will be encoded using the following format:
  135. ///
  136. /// - `Content-Disposition: form-data; name=#{name}; filename=#{generated filename}` (HTTP Header)
  137. /// - `Content-Type: #{generated mimeType}` (HTTP Header)
  138. /// - Encoded file data
  139. /// - Multipart form boundary
  140. ///
  141. /// The filename in the `Content-Disposition` HTTP header is generated from the last path component of the
  142. /// `fileURL`. The `Content-Type` HTTP header MIME type is generated by mapping the `fileURL` extension to the
  143. /// system associated MIME type.
  144. ///
  145. /// - Parameters:
  146. /// - fileURL: `URL` of the file whose content will be encoded into the instance.
  147. /// - name: Name to associate with the file content in the `Content-Disposition` HTTP header.
  148. public func append(_ fileURL: URL, withName name: String) {
  149. let fileName = fileURL.lastPathComponent
  150. let pathExtension = fileURL.pathExtension
  151. if !fileName.isEmpty && !pathExtension.isEmpty {
  152. let mime = mimeType(forPathExtension: pathExtension)
  153. append(fileURL, withName: name, fileName: fileName, mimeType: mime)
  154. } else {
  155. setBodyPartError(withReason: .bodyPartFilenameInvalid(in: fileURL))
  156. }
  157. }
  158. /// Creates a body part from the file and appends it to the instance.
  159. ///
  160. /// The body part data will be encoded using the following format:
  161. ///
  162. /// - Content-Disposition: form-data; name=#{name}; filename=#{filename} (HTTP Header)
  163. /// - Content-Type: #{mimeType} (HTTP Header)
  164. /// - Encoded file data
  165. /// - Multipart form boundary
  166. ///
  167. /// - Parameters:
  168. /// - fileURL: `URL` of the file whose content will be encoded into the instance.
  169. /// - name: Name to associate with the file content in the `Content-Disposition` HTTP header.
  170. /// - fileName: Filename to associate with the file content in the `Content-Disposition` HTTP header.
  171. /// - mimeType: MIME type to associate with the file content in the `Content-Type` HTTP header.
  172. public func append(_ fileURL: URL, withName name: String, fileName: String, mimeType: String) {
  173. let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
  174. //============================================================
  175. // Check 1 - is file URL?
  176. //============================================================
  177. guard fileURL.isFileURL else {
  178. setBodyPartError(withReason: .bodyPartURLInvalid(url: fileURL))
  179. return
  180. }
  181. //============================================================
  182. // Check 2 - is file URL reachable?
  183. //============================================================
  184. do {
  185. let isReachable = try fileURL.checkPromisedItemIsReachable()
  186. guard isReachable else {
  187. setBodyPartError(withReason: .bodyPartFileNotReachable(at: fileURL))
  188. return
  189. }
  190. } catch {
  191. setBodyPartError(withReason: .bodyPartFileNotReachableWithError(atURL: fileURL, error: error))
  192. return
  193. }
  194. //============================================================
  195. // Check 3 - is file URL a directory?
  196. //============================================================
  197. var isDirectory: ObjCBool = false
  198. let path = fileURL.path
  199. guard fileManager.fileExists(atPath: path, isDirectory: &isDirectory) && !isDirectory.boolValue else {
  200. setBodyPartError(withReason: .bodyPartFileIsDirectory(at: fileURL))
  201. return
  202. }
  203. //============================================================
  204. // Check 4 - can the file size be extracted?
  205. //============================================================
  206. let bodyContentLength: UInt64
  207. do {
  208. guard let fileSize = try fileManager.attributesOfItem(atPath: path)[.size] as? NSNumber else {
  209. setBodyPartError(withReason: .bodyPartFileSizeNotAvailable(at: fileURL))
  210. return
  211. }
  212. bodyContentLength = fileSize.uint64Value
  213. } catch {
  214. setBodyPartError(withReason: .bodyPartFileSizeQueryFailedWithError(forURL: fileURL, error: error))
  215. return
  216. }
  217. //============================================================
  218. // Check 5 - can a stream be created from file URL?
  219. //============================================================
  220. guard let stream = InputStream(url: fileURL) else {
  221. setBodyPartError(withReason: .bodyPartInputStreamCreationFailed(for: fileURL))
  222. return
  223. }
  224. append(stream, withLength: bodyContentLength, headers: headers)
  225. }
  226. /// Creates a body part from the stream and appends it to the instance.
  227. ///
  228. /// The body part data will be encoded using the following format:
  229. ///
  230. /// - `Content-Disposition: form-data; name=#{name}; filename=#{filename}` (HTTP Header)
  231. /// - `Content-Type: #{mimeType}` (HTTP Header)
  232. /// - Encoded stream data
  233. /// - Multipart form boundary
  234. ///
  235. /// - Parameters:
  236. /// - stream: `InputStream` to encode into the instance.
  237. /// - length: Length, in bytes, of the stream.
  238. /// - name: Name to associate with the stream content in the `Content-Disposition` HTTP header.
  239. /// - fileName: Filename to associate with the stream content in the `Content-Disposition` HTTP header.
  240. /// - mimeType: MIME type to associate with the stream content in the `Content-Type` HTTP header.
  241. public func append(_ stream: InputStream,
  242. withLength length: UInt64,
  243. name: String,
  244. fileName: String,
  245. mimeType: String) {
  246. let headers = contentHeaders(withName: name, fileName: fileName, mimeType: mimeType)
  247. append(stream, withLength: length, headers: headers)
  248. }
  249. /// Creates a body part with the stream, length, and headers and appends it to the instance.
  250. ///
  251. /// The body part data will be encoded using the following format:
  252. ///
  253. /// - HTTP headers
  254. /// - Encoded stream data
  255. /// - Multipart form boundary
  256. ///
  257. /// - Parameters:
  258. /// - stream: `InputStream` to encode into the instance.
  259. /// - length: Length, in bytes, of the stream.
  260. /// - headers: `HTTPHeaders` for the body part.
  261. public func append(_ stream: InputStream, withLength length: UInt64, headers: HTTPHeaders) {
  262. let bodyPart = BodyPart(headers: headers, bodyStream: stream, bodyContentLength: length)
  263. bodyParts.append(bodyPart)
  264. }
  265. // MARK: - Data Encoding
  266. /// Encodes all appended body parts into a single `Data` value.
  267. ///
  268. /// - Note: This method will load all the appended body parts into memory all at the same time. This method should
  269. /// only be used when the encoded data will have a small memory footprint. For large data cases, please use
  270. /// the `writeEncodedData(to:))` method.
  271. ///
  272. /// - Returns: The encoded `Data`, if encoding is successful.
  273. /// - Throws: An `AFError` if encoding encounters an error.
  274. public func encode() throws -> Data {
  275. if let bodyPartError = bodyPartError {
  276. throw bodyPartError
  277. }
  278. var encoded = Data()
  279. bodyParts.first?.hasInitialBoundary = true
  280. bodyParts.last?.hasFinalBoundary = true
  281. for bodyPart in bodyParts {
  282. let encodedData = try encode(bodyPart)
  283. encoded.append(encodedData)
  284. }
  285. return encoded
  286. }
  287. /// Writes all appended body parts to the given file `URL`.
  288. ///
  289. /// This process is facilitated by reading and writing with input and output streams, respectively. Thus,
  290. /// this approach is very memory efficient and should be used for large body part data.
  291. ///
  292. /// - Parameter fileURL: File `URL` to which to write the form data.
  293. /// - Throws: An `AFError` if encoding encounters an error.
  294. public func writeEncodedData(to fileURL: URL) throws {
  295. if let bodyPartError = bodyPartError {
  296. throw bodyPartError
  297. }
  298. if fileManager.fileExists(atPath: fileURL.path) {
  299. throw AFError.multipartEncodingFailed(reason: .outputStreamFileAlreadyExists(at: fileURL))
  300. } else if !fileURL.isFileURL {
  301. throw AFError.multipartEncodingFailed(reason: .outputStreamURLInvalid(url: fileURL))
  302. }
  303. guard let outputStream = OutputStream(url: fileURL, append: false) else {
  304. throw AFError.multipartEncodingFailed(reason: .outputStreamCreationFailed(for: fileURL))
  305. }
  306. outputStream.open()
  307. defer { outputStream.close() }
  308. bodyParts.first?.hasInitialBoundary = true
  309. bodyParts.last?.hasFinalBoundary = true
  310. for bodyPart in bodyParts {
  311. try write(bodyPart, to: outputStream)
  312. }
  313. }
  314. // MARK: - Private - Body Part Encoding
  315. private func encode(_ bodyPart: BodyPart) throws -> Data {
  316. var encoded = Data()
  317. let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
  318. encoded.append(initialData)
  319. let headerData = encodeHeaders(for: bodyPart)
  320. encoded.append(headerData)
  321. let bodyStreamData = try encodeBodyStream(for: bodyPart)
  322. encoded.append(bodyStreamData)
  323. if bodyPart.hasFinalBoundary {
  324. encoded.append(finalBoundaryData())
  325. }
  326. return encoded
  327. }
  328. private func encodeHeaders(for bodyPart: BodyPart) -> Data {
  329. let headerText = bodyPart.headers.map { "\($0.name): \($0.value)\(EncodingCharacters.crlf)" }
  330. .joined()
  331. + EncodingCharacters.crlf
  332. return Data(headerText.utf8)
  333. }
  334. private func encodeBodyStream(for bodyPart: BodyPart) throws -> Data {
  335. let inputStream = bodyPart.bodyStream
  336. inputStream.open()
  337. defer { inputStream.close() }
  338. var encoded = Data()
  339. while inputStream.hasBytesAvailable {
  340. var buffer = [UInt8](repeating: 0, count: streamBufferSize)
  341. let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
  342. if let error = inputStream.streamError {
  343. throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: error))
  344. }
  345. if bytesRead > 0 {
  346. encoded.append(buffer, count: bytesRead)
  347. } else {
  348. break
  349. }
  350. }
  351. guard UInt64(encoded.count) == bodyPart.bodyContentLength else {
  352. let error = AFError.UnexpectedInputStreamLength(bytesExpected: bodyPart.bodyContentLength,
  353. bytesRead: UInt64(encoded.count))
  354. throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: error))
  355. }
  356. return encoded
  357. }
  358. // MARK: - Private - Writing Body Part to Output Stream
  359. private func write(_ bodyPart: BodyPart, to outputStream: OutputStream) throws {
  360. try writeInitialBoundaryData(for: bodyPart, to: outputStream)
  361. try writeHeaderData(for: bodyPart, to: outputStream)
  362. try writeBodyStream(for: bodyPart, to: outputStream)
  363. try writeFinalBoundaryData(for: bodyPart, to: outputStream)
  364. }
  365. private func writeInitialBoundaryData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
  366. let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
  367. return try write(initialData, to: outputStream)
  368. }
  369. private func writeHeaderData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
  370. let headerData = encodeHeaders(for: bodyPart)
  371. return try write(headerData, to: outputStream)
  372. }
  373. private func writeBodyStream(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
  374. let inputStream = bodyPart.bodyStream
  375. inputStream.open()
  376. defer { inputStream.close() }
  377. while inputStream.hasBytesAvailable {
  378. var buffer = [UInt8](repeating: 0, count: streamBufferSize)
  379. let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
  380. if let streamError = inputStream.streamError {
  381. throw AFError.multipartEncodingFailed(reason: .inputStreamReadFailed(error: streamError))
  382. }
  383. if bytesRead > 0 {
  384. if buffer.count != bytesRead {
  385. buffer = Array(buffer[0..<bytesRead])
  386. }
  387. try write(&buffer, to: outputStream)
  388. } else {
  389. break
  390. }
  391. }
  392. }
  393. private func writeFinalBoundaryData(for bodyPart: BodyPart, to outputStream: OutputStream) throws {
  394. if bodyPart.hasFinalBoundary {
  395. return try write(finalBoundaryData(), to: outputStream)
  396. }
  397. }
  398. // MARK: - Private - Writing Buffered Data to Output Stream
  399. private func write(_ data: Data, to outputStream: OutputStream) throws {
  400. var buffer = [UInt8](repeating: 0, count: data.count)
  401. data.copyBytes(to: &buffer, count: data.count)
  402. return try write(&buffer, to: outputStream)
  403. }
  404. private func write(_ buffer: inout [UInt8], to outputStream: OutputStream) throws {
  405. var bytesToWrite = buffer.count
  406. while bytesToWrite > 0, outputStream.hasSpaceAvailable {
  407. let bytesWritten = outputStream.write(buffer, maxLength: bytesToWrite)
  408. if let error = outputStream.streamError {
  409. throw AFError.multipartEncodingFailed(reason: .outputStreamWriteFailed(error: error))
  410. }
  411. bytesToWrite -= bytesWritten
  412. if bytesToWrite > 0 {
  413. buffer = Array(buffer[bytesWritten..<buffer.count])
  414. }
  415. }
  416. }
  417. // MARK: - Private - Mime Type
  418. private func mimeType(forPathExtension pathExtension: String) -> String {
  419. if
  420. let id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
  421. let contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue() {
  422. return contentType as String
  423. }
  424. return "application/octet-stream"
  425. }
  426. // MARK: - Private - Content Headers
  427. private func contentHeaders(withName name: String, fileName: String? = nil, mimeType: String? = nil) -> HTTPHeaders {
  428. var disposition = "form-data; name=\"\(name)\""
  429. if let fileName = fileName { disposition += "; filename=\"\(fileName)\"" }
  430. var headers: HTTPHeaders = [.contentDisposition(disposition)]
  431. if let mimeType = mimeType { headers.add(.contentType(mimeType)) }
  432. return headers
  433. }
  434. // MARK: - Private - Boundary Encoding
  435. private func initialBoundaryData() -> Data {
  436. BoundaryGenerator.boundaryData(forBoundaryType: .initial, boundary: boundary)
  437. }
  438. private func encapsulatedBoundaryData() -> Data {
  439. BoundaryGenerator.boundaryData(forBoundaryType: .encapsulated, boundary: boundary)
  440. }
  441. private func finalBoundaryData() -> Data {
  442. BoundaryGenerator.boundaryData(forBoundaryType: .final, boundary: boundary)
  443. }
  444. // MARK: - Private - Errors
  445. private func setBodyPartError(withReason reason: AFError.MultipartEncodingFailureReason) {
  446. guard bodyPartError == nil else { return }
  447. bodyPartError = AFError.multipartEncodingFailed(reason: reason)
  448. }
  449. }