-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
743 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Sources/SimpleHTTP/Encoder/MultipartFormDataEncoder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Foundation | ||
|
||
struct MultipartFormDataEncoder { | ||
|
||
let boundary: String | ||
private var bodyParts: [BodyPart] | ||
|
||
// | ||
// The optimal read/write buffer size in bytes for input and output streams is 1024 (1KB). For more | ||
// information, please refer to the following article: | ||
// - https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/ReadingInputStreams.html | ||
// | ||
private let streamBufferSize = 1024 | ||
|
||
public init(body: MultipartFormData) { | ||
self.boundary = body.boundary | ||
self.bodyParts = body.bodyParts | ||
} | ||
|
||
mutating func encode() throws -> Data { | ||
var encoded = Data() | ||
|
||
if var first = bodyParts.first { | ||
first.hasInitialBoundary = true | ||
bodyParts[0] = first | ||
} | ||
|
||
if var last = bodyParts.last { | ||
last.hasFinalBoundary = true | ||
bodyParts[bodyParts.count - 1] = last | ||
} | ||
|
||
for bodyPart in bodyParts { | ||
encoded.append(try encodeBodyPart(bodyPart)) | ||
} | ||
|
||
return encoded | ||
} | ||
|
||
private func encodeBodyPart(_ bodyPart: BodyPart) throws -> Data { | ||
var encoded = Data() | ||
|
||
if bodyPart.hasInitialBoundary { | ||
encoded.append(Boundary.data(for: .initial, boundary: boundary)) | ||
} else { | ||
encoded.append(Boundary.data(for: .encapsulated, boundary: boundary)) | ||
} | ||
|
||
encoded.append(try encodeBodyPart(headers: bodyPart.headers)) | ||
encoded.append(try encodeBodyPart(stream: bodyPart.stream, length: bodyPart.length)) | ||
|
||
if bodyPart.hasFinalBoundary { | ||
encoded.append(Boundary.data(for: .final, boundary: boundary)) | ||
} | ||
|
||
return encoded | ||
} | ||
|
||
private func encodeBodyPart(headers: [Header]) throws -> Data { | ||
let headerText = headers.map { "\($0.name.key): \($0.value)\(EncodingCharacters.crlf)" } | ||
.joined() | ||
+ EncodingCharacters.crlf | ||
|
||
return Data(headerText.utf8) | ||
} | ||
|
||
private func encodeBodyPart(stream: InputStream, length: Int) throws -> Data { | ||
var encoded = Data() | ||
|
||
stream.open() | ||
defer { stream.close() } | ||
|
||
while stream.hasBytesAvailable { | ||
var buffer = [UInt8](repeating: 0, count: streamBufferSize) | ||
let bytesRead = stream.read(&buffer, maxLength: streamBufferSize) | ||
|
||
if let error = stream.streamError { | ||
throw BodyPart.Error.inputStreamReadFailed(error.localizedDescription) | ||
} | ||
|
||
if bytesRead > 0 { | ||
encoded.append(buffer, count: bytesRead) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
guard encoded.count == length else { | ||
throw BodyPart.Error.unexpectedInputStreamLength(expected: length, bytesRead: encoded.count) | ||
} | ||
|
||
return encoded | ||
} | ||
|
||
} | ||
|
||
extension BodyPart { | ||
|
||
enum Error: Swift.Error { | ||
case inputStreamReadFailed(String) | ||
case unexpectedInputStreamLength(expected: Int, bytesRead: Int) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
Sources/SimpleHTTP/Foundation/URLRequest/URLRequest+Multipart.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
extension URLRequest { | ||
public mutating func multipartBody(_ body: MultipartFormData) throws { | ||
var multipartEncode = MultipartFormDataEncoder(body: body) | ||
httpBody = try multipartEncode.encode() | ||
setHeaders([.contentType: HTTPContentType.multipart(boundary: body.boundary).value]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.