1- // Foundation/URLSession/HTTPBodySource .swift - URLSession & libcurl
1+ // Foundation/URLSession/BodySource .swift - URLSession & libcurl
22//
33// This source file is part of the Swift.org open source project
44//
@@ -39,16 +39,16 @@ internal func splitData(dispatchData data: DispatchData, atPosition position: In
3939 return ( data. subdata ( in: 0 ..< position) , data. subdata ( in: position..< data. count) )
4040}
4141
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 {
4444 /// Get the next chunck of data.
4545 ///
4646 /// - Returns: `.data` until the source is exhausted, at which point it will
4747 /// return `.done`. Since this is non-blocking, it will return `.retryLater`
4848 /// 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
5050}
51- internal enum _HTTPBodySourceDataChunk {
51+ internal enum _BodySourceDataChunk {
5252 case data( DispatchData )
5353 /// The source is depleted.
5454 case done
@@ -57,26 +57,26 @@ internal enum _HTTPBodySourceDataChunk {
5757 case error
5858}
5959
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 !
6363 init ( data: DispatchData ) {
6464 self . data = data
6565 }
6666}
6767
68- extension _HTTPBodyDataSource : _HTTPBodySource {
68+ extension _BodyDataSource : _BodySource {
6969 enum _Error : Error {
7070 case unableToRewindData
7171 }
72-
73- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
72+
73+ func getNextChunk( withLength length: Int ) -> _BodySourceDataChunk {
7474 let remaining = data. count
7575 if remaining == 0 {
7676 return . done
7777 } else if remaining <= length {
7878 let r : DispatchData ! = data
79- data = DispatchData . empty
79+ data = DispatchData . empty
8080 return . data( r)
8181 } else {
8282 let ( chunk, remainder) = splitData ( dispatchData: data, atPosition: length)
@@ -87,7 +87,7 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
8787}
8888
8989
90- /// A HTTP body data source backed by a file.
90+ /// A body data source backed by a file.
9191///
9292/// This allows non-blocking streaming of file data to the remote server.
9393///
@@ -98,10 +98,10 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
9898/// - Note: Calls to `getNextChunk(withLength:)` and callbacks from libdispatch
9999/// should all happen on the same (serial) queue, and hence this code doesn't
100100/// have to be thread safe.
101- internal final class _HTTPBodyFileSource {
101+ internal final class _BodyFileSource {
102102 fileprivate let fileURL : URL
103- fileprivate let channel : DispatchIO
104- fileprivate let workQueue : DispatchQueue
103+ fileprivate let channel : DispatchIO
104+ fileprivate let workQueue : DispatchQueue
105105 fileprivate let dataAvailableHandler : ( ) -> Void
106106 fileprivate var hasActiveReadHandler = false
107107 fileprivate var availableChunk : _Chunk = . empty
@@ -128,12 +128,12 @@ internal final class _HTTPBodyFileSource {
128128 guard let channel = DispatchIO ( type: . stream, path: fileSystemRepresentation,
129129 oflag: O_RDONLY, mode: 0 , queue: workQueue,
130130 cleanupHandler: { _ in } ) else {
131- fatalError ( " Cant create DispatchIO channel " )
131+ fatalError ( " Cant create DispatchIO channel " )
132132 }
133133 self . channel = channel
134134 self . channel. setLimit ( highWater: CFURLSessionMaxWriteSize)
135135 }
136-
136+
137137 fileprivate enum _Chunk {
138138 /// Nothing has been read, yet
139139 case empty
@@ -146,7 +146,7 @@ internal final class _HTTPBodyFileSource {
146146 }
147147}
148148
149- fileprivate extension _HTTPBodyFileSource {
149+ fileprivate extension _BodyFileSource {
150150 fileprivate var desiredBufferLength : Int { return 3 * CFURLSessionMaxWriteSize }
151151 /// Enqueue a dispatch I/O read to fill the buffer.
152152 ///
@@ -182,7 +182,7 @@ fileprivate extension _HTTPBodyFileSource {
182182 }
183183 }
184184 }
185-
185+
186186 fileprivate func append( data: DispatchData , endOfFile: Bool ) {
187187 switch availableChunk {
188188 case . empty:
@@ -196,7 +196,7 @@ fileprivate extension _HTTPBodyFileSource {
196196 fatalError ( " Trying to append data, but end-of-file was already detected. " )
197197 }
198198 }
199-
199+
200200 fileprivate var availableByteCount : Int {
201201 switch availableChunk {
202202 case . empty: return 0
@@ -208,8 +208,8 @@ fileprivate extension _HTTPBodyFileSource {
208208 }
209209}
210210
211- extension _HTTPBodyFileSource : _HTTPBodySource {
212- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
211+ extension _BodyFileSource : _BodySource {
212+ func getNextChunk( withLength length: Int ) -> _BodySourceDataChunk {
213213 switch availableChunk {
214214 case . empty:
215215 readNextChunk ( )
0 commit comments