Skip to content

Commit 5dbb4ba

Browse files
committed
URL Session Refactoring
1 parent a33dfb5 commit 5dbb4ba

File tree

10 files changed

+931
-827
lines changed

10 files changed

+931
-827
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 29 additions & 19 deletions
Large diffs are not rendered by default.

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

Lines changed: 21 additions & 21 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
}
7272

73-
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk {
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
@@ -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
///
@@ -158,7 +158,7 @@ fileprivate extension _HTTPBodyFileSource {
158158
guard availableByteCount < desiredBufferLength else { return }
159159
guard !hasActiveReadHandler else { return } // We're already reading
160160
hasActiveReadHandler = true
161-
161+
162162
let lengthToRead = desiredBufferLength - availableByteCount
163163
channel.read(offset: 0, length: lengthToRead, queue: workQueue) { (done: Bool, data: DispatchData?, errno: Int32) in
164164
let wasEmpty = self.availableByteCount == 0
@@ -176,7 +176,7 @@ fileprivate extension _HTTPBodyFileSource {
176176
default:
177177
fatalError("Invalid arguments to read(3) callback.")
178178
}
179-
179+
180180
if wasEmpty && (0 < self.availableByteCount) {
181181
self.dataAvailableHandler()
182182
}
@@ -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()
@@ -222,7 +222,7 @@ extension _HTTPBodyFileSource : _HTTPBodySource {
222222

223223
availableChunk = tail.isEmpty ? .empty : .data(tail)
224224
readNextChunk()
225-
225+
226226
if head.isEmpty {
227227
return .retryLater
228228
} else {

0 commit comments

Comments
 (0)