Skip to content

Commit

Permalink
Merge pull request #48 from UbiqueInnovation/bugfix/TOPO-2278-fix-hea…
Browse files Browse the repository at this point in the history
…d-request

TOPO-2278: Allow head request to proceed
  • Loading branch information
zenokoller authored Mar 9, 2023
2 parents ed6d9a0 + 34b6e99 commit 1430188
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Sources/UBFoundation/Networking/UBURLDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,7 @@ extension UBURLDataTask {
// Create the block that gets called when decoding is ready
executionBlock = { data, response, info, callbackQueue, caller in
guard let data = data else {
callbackQueue.addOperation {
completion(.failure(UBNetworkingError.internal(.responseBodyIsEmpty)), response, info, caller)
}
Self.handleMissingData(from: response, info: info, callbackQueue: callbackQueue, caller: caller, completion: completion)
return
}
do {
Expand All @@ -791,15 +789,31 @@ extension UBURLDataTask {
}
}

private static func handleMissingData<T>(from response: HTTPURLResponse?, info: UBNetworkingTaskInfo?, callbackQueue: OperationQueue, caller: UBURLDataTask, completion: @escaping CompletionHandlingBlock<T>) {
guard caller.request.httpMethod == .head else {
callbackQueue.addOperation {
completion(.failure(UBNetworkingError.internal(.responseBodyIsEmpty)), response, info, caller)
}
return
}

guard let data = Data() as? T else {
assertionFailure("HEAD request should always be used with passthrough decoder")
return
}

callbackQueue.addOperation {
completion(.success(data), response, info, caller)
}
}

/// :nodoc:
init<T, E: UBURLDataTaskErrorBody>(decoder: UBURLDataTaskDecoder<T>, errorDecoder: UBURLDataTaskDecoder<E>, completion: @escaping CompletionHandlingBlock<T>, callbackQueue: OperationQueue?) {
self.callbackQueue = callbackQueue
// Create the block that gets called when decoding is ready
executionBlock = { data, response, info, callbackQueue, caller in
guard let data = data else {
callbackQueue.addOperation {
completion(.failure(UBNetworkingError.internal(.responseBodyIsEmpty)), response, info, caller)
}
Self.handleMissingData(from: response, info: info, callbackQueue: callbackQueue, caller: caller, completion: completion)
return
}
do {
Expand Down

0 comments on commit 1430188

Please sign in to comment.