Skip to content

Commit 1ad6f9d

Browse files
committed
fixed potential issue with multipart form
1 parent ea1eb28 commit 1ad6f9d

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

Sources/SimpleHTTP/Request/Request.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public struct Request<Output> {
2222
public let method: Method
2323
public let body: Body?
2424
public let query: [String: QueryParam]
25-
/// content type to use for encoding the request body. If nil, the session default is used
26-
public var contentType: HTTPContentType?
2725
public var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
2826
public var headers: HTTPHeaderFields = [:]
2927

Sources/SimpleHTTP/Session/Session.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,19 @@ public class Session {
7171
extension Session {
7272
private func dataPublisher<Output>(for request: Request<Output>) async throws -> Response<Output> {
7373
let modifiedRequest = try await config.interceptor.adaptRequest(request)
74-
let requestContentType = modifiedRequest.contentType ?? config.data.defaultType
74+
let requestContentType = modifiedRequest.headers.contentType ?? config.data.defaultType
75+
let encoder: ContentDataEncoder?
76+
77+
// FIXME: we also check body inside toURLRequest
78+
switch modifiedRequest.body {
79+
case .encodable:
80+
encoder = config.data.encoder[requestContentType]
81+
case .multipart, .none:
82+
// this one is supposed to never be nil
83+
encoder = config.data.encoder[config.data.defaultType]
84+
}
7585

76-
guard let encoder = config.data.encoder[requestContentType] else {
86+
guard let encoder else {
7787
throw SessionConfigurationError.missingEncoder(requestContentType)
7888
}
7989

Sources/SimpleHTTPFoundation/HTTP/HTTPHeader.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ extension HTTPHeader {
3131
public static let proxyAuthorization: Self = "Proxy-Authorization"
3232
public static let wwwAuthenticate: Self = "WWW-Authenticate"
3333
}
34+
35+
public extension HTTPHeaderFields {
36+
/// - Returns the content type if HTTPHeader.contentType was set
37+
var contentType: HTTPContentType? {
38+
self[.contentType].map(HTTPContentType.init(value:))
39+
}
40+
41+
func contentType(_ value: HTTPContentType) -> Self {
42+
var copy = self
43+
44+
copy[.contentType] = value.value
45+
46+
return copy
47+
}
48+
}

0 commit comments

Comments
 (0)