MultipartFormData.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. //
  2. // MultipartFormData.swift
  3. //
  4. // Copyright (c) 2014-2016 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(OSX)
  28. import CoreServices
  29. #endif
  30. /**
  31. Constructs `multipart/form-data` for uploads within an HTTP or HTTPS body. There are currently two ways to encode
  32. multipart form data. The first way is to encode the data directly in memory. This is very efficient, but can lead
  33. to memory issues if the dataset is too large. The second way is designed for larger datasets and will write all the
  34. data to a single file on disk with all the proper boundary segmentation. The second approach MUST be used for
  35. larger datasets such as video content, otherwise your app may run out of memory when trying to encode the dataset.
  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. - https://www.ietf.org/rfc/rfc2388.txt
  39. - https://www.ietf.org/rfc/rfc2045.txt
  40. - https://www.w3.org/TR/html401/interact/forms.html#h-17.13
  41. */
  42. public class MultipartFormData {
  43. // MARK: - Helper Types
  44. struct EncodingCharacters {
  45. static let CRLF = "\r\n"
  46. }
  47. struct BoundaryGenerator {
  48. enum BoundaryType {
  49. case Initial, Encapsulated, Final
  50. }
  51. static func randomBoundary() -> String {
  52. return String(format: "alamofire.boundary.%08x%08x", arc4random(), arc4random())
  53. }
  54. static func boundaryData(boundaryType boundaryType: BoundaryType, boundary: String) -> NSData {
  55. let boundaryText: String
  56. switch boundaryType {
  57. case .Initial:
  58. boundaryText = "--\(boundary)\(EncodingCharacters.CRLF)"
  59. case .Encapsulated:
  60. boundaryText = "\(EncodingCharacters.CRLF)--\(boundary)\(EncodingCharacters.CRLF)"
  61. case .Final:
  62. boundaryText = "\(EncodingCharacters.CRLF)--\(boundary)--\(EncodingCharacters.CRLF)"
  63. }
  64. return boundaryText.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  65. }
  66. }
  67. class BodyPart {
  68. let headers: [String: String]
  69. let bodyStream: NSInputStream
  70. let bodyContentLength: UInt64
  71. var hasInitialBoundary = false
  72. var hasFinalBoundary = false
  73. init(headers: [String: String], bodyStream: NSInputStream, bodyContentLength: UInt64) {
  74. self.headers = headers
  75. self.bodyStream = bodyStream
  76. self.bodyContentLength = bodyContentLength
  77. }
  78. }
  79. // MARK: - Properties
  80. /// The `Content-Type` header value containing the boundary used to generate the `multipart/form-data`.
  81. public var contentType: String { return "multipart/form-data; boundary=\(boundary)" }
  82. /// The content length of all body parts used to generate the `multipart/form-data` not including the boundaries.
  83. public var contentLength: UInt64 { return bodyParts.reduce(0) { $0 + $1.bodyContentLength } }
  84. /// The boundary used to separate the body parts in the encoded form data.
  85. public let boundary: String
  86. private var bodyParts: [BodyPart]
  87. private var bodyPartError: NSError?
  88. private let streamBufferSize: Int
  89. // MARK: - Lifecycle
  90. /**
  91. Creates a multipart form data object.
  92. - returns: The multipart form data object.
  93. */
  94. public init() {
  95. self.boundary = BoundaryGenerator.randomBoundary()
  96. self.bodyParts = []
  97. /**
  98. * The optimal read/write buffer size in bytes for input and output streams is 1024 (1KB). For more
  99. * information, please refer to the following article:
  100. * - https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html
  101. */
  102. self.streamBufferSize = 1024
  103. }
  104. // MARK: - Body Parts
  105. /**
  106. Creates a body part from the data and appends it to the multipart form data object.
  107. The body part data will be encoded using the following format:
  108. - `Content-Disposition: form-data; name=#{name}` (HTTP Header)
  109. - Encoded data
  110. - Multipart form boundary
  111. - parameter data: The data to encode into the multipart form data.
  112. - parameter name: The name to associate with the data in the `Content-Disposition` HTTP header.
  113. */
  114. public func appendBodyPart(data data: NSData, name: String) {
  115. let headers = contentHeaders(name: name)
  116. let stream = NSInputStream(data: data)
  117. let length = UInt64(data.length)
  118. appendBodyPart(stream: stream, length: length, headers: headers)
  119. }
  120. /**
  121. Creates a body part from the data and appends it to the multipart form data object.
  122. The body part data will be encoded using the following format:
  123. - `Content-Disposition: form-data; name=#{name}` (HTTP Header)
  124. - `Content-Type: #{generated mimeType}` (HTTP Header)
  125. - Encoded data
  126. - Multipart form boundary
  127. - parameter data: The data to encode into the multipart form data.
  128. - parameter name: The name to associate with the data in the `Content-Disposition` HTTP header.
  129. - parameter mimeType: The MIME type to associate with the data content type in the `Content-Type` HTTP header.
  130. */
  131. public func appendBodyPart(data data: NSData, name: String, mimeType: String) {
  132. let headers = contentHeaders(name: name, mimeType: mimeType)
  133. let stream = NSInputStream(data: data)
  134. let length = UInt64(data.length)
  135. appendBodyPart(stream: stream, length: length, headers: headers)
  136. }
  137. /**
  138. Creates a body part from the data and appends it to the multipart form data object.
  139. The body part data will be encoded using the following format:
  140. - `Content-Disposition: form-data; name=#{name}; filename=#{filename}` (HTTP Header)
  141. - `Content-Type: #{mimeType}` (HTTP Header)
  142. - Encoded file data
  143. - Multipart form boundary
  144. - parameter data: The data to encode into the multipart form data.
  145. - parameter name: The name to associate with the data in the `Content-Disposition` HTTP header.
  146. - parameter fileName: The filename to associate with the data in the `Content-Disposition` HTTP header.
  147. - parameter mimeType: The MIME type to associate with the data in the `Content-Type` HTTP header.
  148. */
  149. public func appendBodyPart(data data: NSData, name: String, fileName: String, mimeType: String) {
  150. let headers = contentHeaders(name: name, fileName: fileName, mimeType: mimeType)
  151. let stream = NSInputStream(data: data)
  152. let length = UInt64(data.length)
  153. appendBodyPart(stream: stream, length: length, headers: headers)
  154. }
  155. /**
  156. Creates a body part from the file and appends it to the multipart form data object.
  157. The body part data will be encoded using the following format:
  158. - `Content-Disposition: form-data; name=#{name}; filename=#{generated filename}` (HTTP Header)
  159. - `Content-Type: #{generated mimeType}` (HTTP Header)
  160. - Encoded file data
  161. - Multipart form boundary
  162. The filename in the `Content-Disposition` HTTP header is generated from the last path component of the
  163. `fileURL`. The `Content-Type` HTTP header MIME type is generated by mapping the `fileURL` extension to the
  164. system associated MIME type.
  165. - parameter fileURL: The URL of the file whose content will be encoded into the multipart form data.
  166. - parameter name: The name to associate with the file content in the `Content-Disposition` HTTP header.
  167. */
  168. public func appendBodyPart(fileURL fileURL: NSURL, name: String) {
  169. if let
  170. fileName = fileURL.lastPathComponent,
  171. pathExtension = fileURL.pathExtension
  172. {
  173. let mimeType = mimeTypeForPathExtension(pathExtension)
  174. appendBodyPart(fileURL: fileURL, name: name, fileName: fileName, mimeType: mimeType)
  175. } else {
  176. let failureReason = "Failed to extract the fileName of the provided URL: \(fileURL)"
  177. setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
  178. }
  179. }
  180. /**
  181. Creates a body part from the file and appends it to the multipart form data object.
  182. The body part data will be encoded using the following format:
  183. - Content-Disposition: form-data; name=#{name}; filename=#{filename} (HTTP Header)
  184. - Content-Type: #{mimeType} (HTTP Header)
  185. - Encoded file data
  186. - Multipart form boundary
  187. - parameter fileURL: The URL of the file whose content will be encoded into the multipart form data.
  188. - parameter name: The name to associate with the file content in the `Content-Disposition` HTTP header.
  189. - parameter fileName: The filename to associate with the file content in the `Content-Disposition` HTTP header.
  190. - parameter mimeType: The MIME type to associate with the file content in the `Content-Type` HTTP header.
  191. */
  192. public func appendBodyPart(fileURL fileURL: NSURL, name: String, fileName: String, mimeType: String) {
  193. let headers = contentHeaders(name: name, fileName: fileName, mimeType: mimeType)
  194. //============================================================
  195. // Check 1 - is file URL?
  196. //============================================================
  197. guard fileURL.fileURL else {
  198. let failureReason = "The file URL does not point to a file URL: \(fileURL)"
  199. setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
  200. return
  201. }
  202. //============================================================
  203. // Check 2 - is file URL reachable?
  204. //============================================================
  205. var isReachable = true
  206. if #available(OSX 10.10, *) {
  207. isReachable = fileURL.checkPromisedItemIsReachableAndReturnError(nil)
  208. }
  209. guard isReachable else {
  210. setBodyPartError(code: NSURLErrorBadURL, failureReason: "The file URL is not reachable: \(fileURL)")
  211. return
  212. }
  213. //============================================================
  214. // Check 3 - is file URL a directory?
  215. //============================================================
  216. var isDirectory: ObjCBool = false
  217. guard let
  218. path = fileURL.path
  219. where NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDirectory) && !isDirectory else
  220. {
  221. let failureReason = "The file URL is a directory, not a file: \(fileURL)"
  222. setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
  223. return
  224. }
  225. //============================================================
  226. // Check 4 - can the file size be extracted?
  227. //============================================================
  228. var bodyContentLength: UInt64?
  229. do {
  230. if let
  231. path = fileURL.path,
  232. fileSize = try NSFileManager.defaultManager().attributesOfItemAtPath(path)[NSFileSize] as? NSNumber
  233. {
  234. bodyContentLength = fileSize.unsignedLongLongValue
  235. }
  236. } catch {
  237. // No-op
  238. }
  239. guard let length = bodyContentLength else {
  240. let failureReason = "Could not fetch attributes from the file URL: \(fileURL)"
  241. setBodyPartError(code: NSURLErrorBadURL, failureReason: failureReason)
  242. return
  243. }
  244. //============================================================
  245. // Check 5 - can a stream be created from file URL?
  246. //============================================================
  247. guard let stream = NSInputStream(URL: fileURL) else {
  248. let failureReason = "Failed to create an input stream from the file URL: \(fileURL)"
  249. setBodyPartError(code: NSURLErrorCannotOpenFile, failureReason: failureReason)
  250. return
  251. }
  252. appendBodyPart(stream: stream, length: length, headers: headers)
  253. }
  254. /**
  255. Creates a body part from the stream and appends it to the multipart form data object.
  256. The body part data will be encoded using the following format:
  257. - `Content-Disposition: form-data; name=#{name}; filename=#{filename}` (HTTP Header)
  258. - `Content-Type: #{mimeType}` (HTTP Header)
  259. - Encoded stream data
  260. - Multipart form boundary
  261. - parameter stream: The input stream to encode in the multipart form data.
  262. - parameter length: The content length of the stream.
  263. - parameter name: The name to associate with the stream content in the `Content-Disposition` HTTP header.
  264. - parameter fileName: The filename to associate with the stream content in the `Content-Disposition` HTTP header.
  265. - parameter mimeType: The MIME type to associate with the stream content in the `Content-Type` HTTP header.
  266. */
  267. public func appendBodyPart(
  268. stream stream: NSInputStream,
  269. length: UInt64,
  270. name: String,
  271. fileName: String,
  272. mimeType: String)
  273. {
  274. let headers = contentHeaders(name: name, fileName: fileName, mimeType: mimeType)
  275. appendBodyPart(stream: stream, length: length, headers: headers)
  276. }
  277. /**
  278. Creates a body part with the headers, stream and length and appends it to the multipart form data object.
  279. The body part data will be encoded using the following format:
  280. - HTTP headers
  281. - Encoded stream data
  282. - Multipart form boundary
  283. - parameter stream: The input stream to encode in the multipart form data.
  284. - parameter length: The content length of the stream.
  285. - parameter headers: The HTTP headers for the body part.
  286. */
  287. public func appendBodyPart(stream stream: NSInputStream, length: UInt64, headers: [String: String]) {
  288. let bodyPart = BodyPart(headers: headers, bodyStream: stream, bodyContentLength: length)
  289. bodyParts.append(bodyPart)
  290. }
  291. // MARK: - Data Encoding
  292. /**
  293. Encodes all the appended body parts into a single `NSData` object.
  294. It is important to note that this method will load all the appended body parts into memory all at the same
  295. time. This method should only be used when the encoded data will have a small memory footprint. For large data
  296. cases, please use the `writeEncodedDataToDisk(fileURL:completionHandler:)` method.
  297. - throws: An `NSError` if encoding encounters an error.
  298. - returns: The encoded `NSData` if encoding is successful.
  299. */
  300. public func encode() throws -> NSData {
  301. if let bodyPartError = bodyPartError {
  302. throw bodyPartError
  303. }
  304. let encoded = NSMutableData()
  305. bodyParts.first?.hasInitialBoundary = true
  306. bodyParts.last?.hasFinalBoundary = true
  307. for bodyPart in bodyParts {
  308. let encodedData = try encodeBodyPart(bodyPart)
  309. encoded.appendData(encodedData)
  310. }
  311. return encoded
  312. }
  313. /**
  314. Writes the appended body parts into the given file URL.
  315. This process is facilitated by reading and writing with input and output streams, respectively. Thus,
  316. this approach is very memory efficient and should be used for large body part data.
  317. - parameter fileURL: The file URL to write the multipart form data into.
  318. - throws: An `NSError` if encoding encounters an error.
  319. */
  320. public func writeEncodedDataToDisk(fileURL: NSURL) throws {
  321. if let bodyPartError = bodyPartError {
  322. throw bodyPartError
  323. }
  324. if let path = fileURL.path where NSFileManager.defaultManager().fileExistsAtPath(path) {
  325. let failureReason = "A file already exists at the given file URL: \(fileURL)"
  326. throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorBadURL, failureReason: failureReason)
  327. } else if !fileURL.fileURL {
  328. let failureReason = "The URL does not point to a valid file: \(fileURL)"
  329. throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorBadURL, failureReason: failureReason)
  330. }
  331. let outputStream: NSOutputStream
  332. if let possibleOutputStream = NSOutputStream(URL: fileURL, append: false) {
  333. outputStream = possibleOutputStream
  334. } else {
  335. let failureReason = "Failed to create an output stream with the given URL: \(fileURL)"
  336. throw Error.error(domain: NSURLErrorDomain, code: NSURLErrorCannotOpenFile, failureReason: failureReason)
  337. }
  338. outputStream.open()
  339. self.bodyParts.first?.hasInitialBoundary = true
  340. self.bodyParts.last?.hasFinalBoundary = true
  341. for bodyPart in self.bodyParts {
  342. try writeBodyPart(bodyPart, toOutputStream: outputStream)
  343. }
  344. outputStream.close()
  345. }
  346. // MARK: - Private - Body Part Encoding
  347. private func encodeBodyPart(bodyPart: BodyPart) throws -> NSData {
  348. let encoded = NSMutableData()
  349. let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
  350. encoded.appendData(initialData)
  351. let headerData = encodeHeaderDataForBodyPart(bodyPart)
  352. encoded.appendData(headerData)
  353. let bodyStreamData = try encodeBodyStreamDataForBodyPart(bodyPart)
  354. encoded.appendData(bodyStreamData)
  355. if bodyPart.hasFinalBoundary {
  356. encoded.appendData(finalBoundaryData())
  357. }
  358. return encoded
  359. }
  360. private func encodeHeaderDataForBodyPart(bodyPart: BodyPart) -> NSData {
  361. var headerText = ""
  362. for (key, value) in bodyPart.headers {
  363. headerText += "\(key): \(value)\(EncodingCharacters.CRLF)"
  364. }
  365. headerText += EncodingCharacters.CRLF
  366. return headerText.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
  367. }
  368. private func encodeBodyStreamDataForBodyPart(bodyPart: BodyPart) throws -> NSData {
  369. let inputStream = bodyPart.bodyStream
  370. inputStream.open()
  371. var error: NSError?
  372. let encoded = NSMutableData()
  373. while inputStream.hasBytesAvailable {
  374. var buffer = [UInt8](count: streamBufferSize, repeatedValue: 0)
  375. let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
  376. if inputStream.streamError != nil {
  377. error = inputStream.streamError
  378. break
  379. }
  380. if bytesRead > 0 {
  381. encoded.appendBytes(buffer, length: bytesRead)
  382. } else if bytesRead < 0 {
  383. let failureReason = "Failed to read from input stream: \(inputStream)"
  384. error = Error.error(domain: NSURLErrorDomain, code: .InputStreamReadFailed, failureReason: failureReason)
  385. break
  386. } else {
  387. break
  388. }
  389. }
  390. inputStream.close()
  391. if let error = error {
  392. throw error
  393. }
  394. return encoded
  395. }
  396. // MARK: - Private - Writing Body Part to Output Stream
  397. private func writeBodyPart(bodyPart: BodyPart, toOutputStream outputStream: NSOutputStream) throws {
  398. try writeInitialBoundaryDataForBodyPart(bodyPart, toOutputStream: outputStream)
  399. try writeHeaderDataForBodyPart(bodyPart, toOutputStream: outputStream)
  400. try writeBodyStreamForBodyPart(bodyPart, toOutputStream: outputStream)
  401. try writeFinalBoundaryDataForBodyPart(bodyPart, toOutputStream: outputStream)
  402. }
  403. private func writeInitialBoundaryDataForBodyPart(
  404. bodyPart: BodyPart,
  405. toOutputStream outputStream: NSOutputStream)
  406. throws
  407. {
  408. let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
  409. return try writeData(initialData, toOutputStream: outputStream)
  410. }
  411. private func writeHeaderDataForBodyPart(bodyPart: BodyPart, toOutputStream outputStream: NSOutputStream) throws {
  412. let headerData = encodeHeaderDataForBodyPart(bodyPart)
  413. return try writeData(headerData, toOutputStream: outputStream)
  414. }
  415. private func writeBodyStreamForBodyPart(bodyPart: BodyPart, toOutputStream outputStream: NSOutputStream) throws {
  416. let inputStream = bodyPart.bodyStream
  417. inputStream.open()
  418. while inputStream.hasBytesAvailable {
  419. var buffer = [UInt8](count: streamBufferSize, repeatedValue: 0)
  420. let bytesRead = inputStream.read(&buffer, maxLength: streamBufferSize)
  421. if let streamError = inputStream.streamError {
  422. throw streamError
  423. }
  424. if bytesRead > 0 {
  425. if buffer.count != bytesRead {
  426. buffer = Array(buffer[0..<bytesRead])
  427. }
  428. try writeBuffer(&buffer, toOutputStream: outputStream)
  429. } else if bytesRead < 0 {
  430. let failureReason = "Failed to read from input stream: \(inputStream)"
  431. throw Error.error(domain: NSURLErrorDomain, code: .InputStreamReadFailed, failureReason: failureReason)
  432. } else {
  433. break
  434. }
  435. }
  436. inputStream.close()
  437. }
  438. private func writeFinalBoundaryDataForBodyPart(
  439. bodyPart: BodyPart,
  440. toOutputStream outputStream: NSOutputStream)
  441. throws
  442. {
  443. if bodyPart.hasFinalBoundary {
  444. return try writeData(finalBoundaryData(), toOutputStream: outputStream)
  445. }
  446. }
  447. // MARK: - Private - Writing Buffered Data to Output Stream
  448. private func writeData(data: NSData, toOutputStream outputStream: NSOutputStream) throws {
  449. var buffer = [UInt8](count: data.length, repeatedValue: 0)
  450. data.getBytes(&buffer, length: data.length)
  451. return try writeBuffer(&buffer, toOutputStream: outputStream)
  452. }
  453. private func writeBuffer(inout buffer: [UInt8], toOutputStream outputStream: NSOutputStream) throws {
  454. var bytesToWrite = buffer.count
  455. while bytesToWrite > 0 {
  456. if outputStream.hasSpaceAvailable {
  457. let bytesWritten = outputStream.write(buffer, maxLength: bytesToWrite)
  458. if let streamError = outputStream.streamError {
  459. throw streamError
  460. }
  461. if bytesWritten < 0 {
  462. let failureReason = "Failed to write to output stream: \(outputStream)"
  463. throw Error.error(domain: NSURLErrorDomain, code: .OutputStreamWriteFailed, failureReason: failureReason)
  464. }
  465. bytesToWrite -= bytesWritten
  466. if bytesToWrite > 0 {
  467. buffer = Array(buffer[bytesWritten..<buffer.count])
  468. }
  469. } else if let streamError = outputStream.streamError {
  470. throw streamError
  471. }
  472. }
  473. }
  474. // MARK: - Private - Mime Type
  475. private func mimeTypeForPathExtension(pathExtension: String) -> String {
  476. if let
  477. id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil)?.takeRetainedValue(),
  478. contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue()
  479. {
  480. return contentType as String
  481. }
  482. return "application/octet-stream"
  483. }
  484. // MARK: - Private - Content Headers
  485. private func contentHeaders(name name: String) -> [String: String] {
  486. return ["Content-Disposition": "form-data; name=\"\(name)\""]
  487. }
  488. private func contentHeaders(name name: String, mimeType: String) -> [String: String] {
  489. return [
  490. "Content-Disposition": "form-data; name=\"\(name)\"",
  491. "Content-Type": "\(mimeType)"
  492. ]
  493. }
  494. private func contentHeaders(name name: String, fileName: String, mimeType: String) -> [String: String] {
  495. return [
  496. "Content-Disposition": "form-data; name=\"\(name)\"; filename=\"\(fileName)\"",
  497. "Content-Type": "\(mimeType)"
  498. ]
  499. }
  500. // MARK: - Private - Boundary Encoding
  501. private func initialBoundaryData() -> NSData {
  502. return BoundaryGenerator.boundaryData(boundaryType: .Initial, boundary: boundary)
  503. }
  504. private func encapsulatedBoundaryData() -> NSData {
  505. return BoundaryGenerator.boundaryData(boundaryType: .Encapsulated, boundary: boundary)
  506. }
  507. private func finalBoundaryData() -> NSData {
  508. return BoundaryGenerator.boundaryData(boundaryType: .Final, boundary: boundary)
  509. }
  510. // MARK: - Private - Errors
  511. private func setBodyPartError(code code: Int, failureReason: String) {
  512. guard bodyPartError == nil else { return }
  513. bodyPartError = Error.error(domain: NSURLErrorDomain, code: code, failureReason: failureReason)
  514. }
  515. }