1
- // Foundation/URLSession/HTTPBodySource .swift - URLSession & libcurl
1
+ // Foundation/URLSession/BodySource .swift - URLSession & libcurl
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
@@ -39,16 +39,16 @@ internal func splitData(dispatchData data: DispatchData, atPosition position: In
39
39
return ( data. subdata ( in: 0 ..< position) , data. subdata ( in: position..< data. count) )
40
40
}
41
41
42
- /// A (non-blocking) source for HTTP body data.
43
- internal protocol _HTTPBodySource : class {
42
+ /// A (non-blocking) source for body data.
43
+ internal protocol _BodySource : class {
44
44
/// Get the next chunck of data.
45
45
///
46
46
/// - Returns: `.data` until the source is exhausted, at which point it will
47
47
/// return `.done`. Since this is non-blocking, it will return `.retryLater`
48
48
/// if no data is available at this point, but will be available later.
49
- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk
49
+ func getNextChunk( withLength length: Int ) -> _BodySourceDataChunk
50
50
}
51
- internal enum _HTTPBodySourceDataChunk {
51
+ internal enum _BodySourceDataChunk {
52
52
case data( DispatchData )
53
53
/// The source is depleted.
54
54
case done
@@ -57,26 +57,26 @@ internal enum _HTTPBodySourceDataChunk {
57
57
case error
58
58
}
59
59
60
- /// A HTTP body data source backed by `dispatch_data_t`.
61
- internal final class _HTTPBodyDataSource {
62
- var data : DispatchData !
60
+ /// A body data source backed by `dispatch_data_t`.
61
+ internal final class _BodyDataSource {
62
+ var data : DispatchData !
63
63
init ( data: DispatchData ) {
64
64
self . data = data
65
65
}
66
66
}
67
67
68
- extension _HTTPBodyDataSource : _HTTPBodySource {
68
+ extension _BodyDataSource : _BodySource {
69
69
enum _Error : Error {
70
70
case unableToRewindData
71
71
}
72
72
73
- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
73
+ func getNextChunk( withLength length: Int ) -> _BodySourceDataChunk {
74
74
let remaining = data. count
75
75
if remaining == 0 {
76
76
return . done
77
77
} else if remaining <= length {
78
78
let r : DispatchData ! = data
79
- data = DispatchData . empty
79
+ data = DispatchData . empty
80
80
return . data( r)
81
81
} else {
82
82
let ( chunk, remainder) = splitData ( dispatchData: data, atPosition: length)
@@ -87,7 +87,7 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
87
87
}
88
88
89
89
90
- /// A HTTP body data source backed by a file.
90
+ /// A body data source backed by a file.
91
91
///
92
92
/// This allows non-blocking streaming of file data to the remote server.
93
93
///
@@ -98,10 +98,10 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
98
98
/// - Note: Calls to `getNextChunk(withLength:)` and callbacks from libdispatch
99
99
/// should all happen on the same (serial) queue, and hence this code doesn't
100
100
/// have to be thread safe.
101
- internal final class _HTTPBodyFileSource {
101
+ internal final class _BodyFileSource {
102
102
fileprivate let fileURL : URL
103
- fileprivate let channel : DispatchIO
104
- fileprivate let workQueue : DispatchQueue
103
+ fileprivate let channel : DispatchIO
104
+ fileprivate let workQueue : DispatchQueue
105
105
fileprivate let dataAvailableHandler : ( ) -> Void
106
106
fileprivate var hasActiveReadHandler = false
107
107
fileprivate var availableChunk : _Chunk = . empty
@@ -146,7 +146,7 @@ internal final class _HTTPBodyFileSource {
146
146
}
147
147
}
148
148
149
- fileprivate extension _HTTPBodyFileSource {
149
+ fileprivate extension _BodyFileSource {
150
150
fileprivate var desiredBufferLength : Int { return 3 * CFURLSessionMaxWriteSize }
151
151
/// Enqueue a dispatch I/O read to fill the buffer.
152
152
///
@@ -158,7 +158,7 @@ fileprivate extension _HTTPBodyFileSource {
158
158
guard availableByteCount < desiredBufferLength else { return }
159
159
guard !hasActiveReadHandler else { return } // We're already reading
160
160
hasActiveReadHandler = true
161
-
161
+
162
162
let lengthToRead = desiredBufferLength - availableByteCount
163
163
channel. read ( offset: 0 , length: lengthToRead, queue: workQueue) { ( done: Bool , data: DispatchData ? , errno: Int32 ) in
164
164
let wasEmpty = self . availableByteCount == 0
@@ -176,7 +176,7 @@ fileprivate extension _HTTPBodyFileSource {
176
176
default :
177
177
fatalError ( " Invalid arguments to read(3) callback. " )
178
178
}
179
-
179
+
180
180
if wasEmpty && ( 0 < self . availableByteCount) {
181
181
self . dataAvailableHandler ( )
182
182
}
@@ -208,8 +208,8 @@ fileprivate extension _HTTPBodyFileSource {
208
208
}
209
209
}
210
210
211
- extension _HTTPBodyFileSource : _HTTPBodySource {
212
- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
211
+ extension _BodyFileSource : _BodySource {
212
+ func getNextChunk( withLength length: Int ) -> _BodySourceDataChunk {
213
213
switch availableChunk {
214
214
case . empty:
215
215
readNextChunk ( )
@@ -222,7 +222,7 @@ extension _HTTPBodyFileSource : _HTTPBodySource {
222
222
223
223
availableChunk = tail. isEmpty ? . empty : . data( tail)
224
224
readNextChunk ( )
225
-
225
+
226
226
if head. isEmpty {
227
227
return . retryLater
228
228
} else {
0 commit comments