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
-
73
- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
72
+
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
@@ -128,12 +128,12 @@ internal final class _HTTPBodyFileSource {
128
128
guard let channel = DispatchIO ( type: . stream, path: fileSystemRepresentation,
129
129
oflag: O_RDONLY, mode: 0 , queue: workQueue,
130
130
cleanupHandler: { _ in } ) else {
131
- fatalError ( " Cant create DispatchIO channel " )
131
+ fatalError ( " Cant create DispatchIO channel " )
132
132
}
133
133
self . channel = channel
134
134
self . channel. setLimit ( highWater: CFURLSessionMaxWriteSize)
135
135
}
136
-
136
+
137
137
fileprivate enum _Chunk {
138
138
/// Nothing has been read, yet
139
139
case 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
///
@@ -182,7 +182,7 @@ fileprivate extension _HTTPBodyFileSource {
182
182
}
183
183
}
184
184
}
185
-
185
+
186
186
fileprivate func append( data: DispatchData , endOfFile: Bool ) {
187
187
switch availableChunk {
188
188
case . empty:
@@ -196,7 +196,7 @@ fileprivate extension _HTTPBodyFileSource {
196
196
fatalError ( " Trying to append data, but end-of-file was already detected. " )
197
197
}
198
198
}
199
-
199
+
200
200
fileprivate var availableByteCount : Int {
201
201
switch availableChunk {
202
202
case . empty: return 0
@@ -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 ( )
0 commit comments