Skip to content

Commit 8b5ad15

Browse files
committed
URL Session Refactoring
1 parent 3618f00 commit 8b5ad15

File tree

10 files changed

+893
-826
lines changed

10 files changed

+893
-826
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 28 additions & 18 deletions
Large diffs are not rendered by default.

Foundation/URLSession/http/HTTPBodySource.swift renamed to Foundation/URLSession/BodySource.swift

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
@@ -127,13 +127,14 @@ internal final class _HTTPBodyFileSource {
127127
}
128128
guard let channel = DispatchIO(type: .stream, path: fileSystemRepresentation,
129129
oflag: O_RDONLY, mode: 0, queue: workQueue,
130-
cleanupHandler: {_ in }) else {
131-
fatalError("Cant create DispatchIO channel")
130+
cleanupHandler: {_ in })
131+
else {
132+
fatalError("Cant create DispatchIO channel")
132133
}
133134
self.channel = channel
134135
self.channel.setLimit(highWater: CFURLSessionMaxWriteSize)
135136
}
136-
137+
137138
fileprivate enum _Chunk {
138139
/// Nothing has been read, yet
139140
case empty
@@ -146,7 +147,7 @@ internal final class _HTTPBodyFileSource {
146147
}
147148
}
148149

149-
fileprivate extension _HTTPBodyFileSource {
150+
fileprivate extension _BodyFileSource {
150151
fileprivate var desiredBufferLength: Int { return 3 * CFURLSessionMaxWriteSize }
151152
/// Enqueue a dispatch I/O read to fill the buffer.
152153
///
@@ -182,7 +183,7 @@ fileprivate extension _HTTPBodyFileSource {
182183
}
183184
}
184185
}
185-
186+
186187
fileprivate func append(data: DispatchData, endOfFile: Bool) {
187188
switch availableChunk {
188189
case .empty:
@@ -196,7 +197,7 @@ fileprivate extension _HTTPBodyFileSource {
196197
fatalError("Trying to append data, but end-of-file was already detected.")
197198
}
198199
}
199-
200+
200201
fileprivate var availableByteCount: Int {
201202
switch availableChunk {
202203
case .empty: return 0
@@ -208,8 +209,8 @@ fileprivate extension _HTTPBodyFileSource {
208209
}
209210
}
210211

211-
extension _HTTPBodyFileSource : _HTTPBodySource {
212-
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk {
212+
extension _BodyFileSource : _BodySource {
213+
func getNextChunk(withLength length: Int) -> _BodySourceDataChunk {
213214
switch availableChunk {
214215
case .empty:
215216
readNextChunk()

0 commit comments

Comments
 (0)