-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NetworkingSpecific.swift
96 lines (82 loc) · 3.19 KB
/
NetworkingSpecific.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import SwiftFoundation
#else
import Foundation
#endif
internal func NSUnimplemented(_ fn: String = #function, file: StaticString = #file, line: UInt = #line) -> Never {
#if os(Android)
NSLog("\(fn) is not yet implemented. \(file):\(line)")
#endif
fatalError("\(fn) is not yet implemented", file: file, line: line)
}
internal func NSUnsupported(_ fn: String = #function, file: StaticString = #file, line: UInt = #line) -> Never {
#if os(Android)
NSLog("\(fn) is not supported. \(file):\(line)")
#endif
fatalError("\(fn) is not supported", file: file, line: line)
}
internal func NSRequiresConcreteImplementation(_ fn: String = #function, file: StaticString = #file, line: UInt = #line) -> Never {
#if os(Android)
NSLog("\(fn) must be overridden. \(file):\(line)")
#endif
fatalError("\(fn) must be overridden", file: file, line: line)
}
@usableFromInline
class _NSNonfileURLContentLoader: _NSNonfileURLContentLoading {
@usableFromInline
required init() {}
@usableFromInline
func contentsOf(url: URL) throws -> (result: NSData, textEncodingNameIfAvailable: String?) {
func cocoaError(with error: Error? = nil) -> Error {
var userInfo: [String: Any] = [:]
if let error = error {
userInfo[NSUnderlyingErrorKey] = error
}
return CocoaError.error(.fileReadUnknown, userInfo: userInfo, url: url)
}
var urlResponse: URLResponse?
let session = URLSession(configuration: URLSessionConfiguration.default)
let cond = NSCondition()
cond.lock()
var resError: Error?
var resData: Data?
var taskFinished = false
let task = session.dataTask(with: url, completionHandler: { data, response, error in
cond.lock()
resData = data
urlResponse = response
resError = error
taskFinished = true
cond.signal()
cond.unlock()
})
task.resume()
while taskFinished == false {
cond.wait()
}
cond.unlock()
guard resError == nil else {
throw cocoaError(with: resError)
}
guard let data = resData else {
throw cocoaError()
}
if let statusCode = (urlResponse as? HTTPURLResponse)?.statusCode {
switch statusCode {
// These are the only valid response codes that data will be returned for, all other codes will be treated as error.
case 101, 200...399, 401, 407:
return (NSData(bytes: UnsafeMutableRawPointer(mutating: (data as NSData).bytes), length: data.count), urlResponse?.textEncodingName)
default:
break
}
}
throw cocoaError()
}
}