1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Foundation/URLSession/BodySource.swift - URLSession & libcurl
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
// -----------------------------------------------------------------------------
///
/// These are libcurl helpers for the URLSession API code.
/// - SeeAlso: https://curl.haxx.se/libcurl/c/
/// - SeeAlso: URLSession.swift
///
// -----------------------------------------------------------------------------
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import SwiftFoundation
#else
import Foundation
#endif
@_implementationOnly import _CFURLSessionInterface
import Dispatch
/// Turn `Data` into `DispatchData`
internal func createDispatchData(_ data: Data) -> DispatchData {
//TODO: Avoid copying data
return data.withUnsafeBytes { DispatchData(bytes: $0) }
}
/// Copy data from `DispatchData` into memory pointed to by an `UnsafeMutableBufferPointer`.
internal func copyDispatchData<T>(_ data: DispatchData, infoBuffer buffer: UnsafeMutableBufferPointer<T>) {
precondition(data.count <= (buffer.count * MemoryLayout<T>.size))
_ = data.copyBytes(to: buffer)
}
/// Split `DispatchData` into `(head, tail)` pair.
internal func splitData(dispatchData data: DispatchData, atPosition position: Int) -> (DispatchData,DispatchData) {
return (data.subdata(in: 0..<position), data.subdata(in: position..<data.count))
}
/// A (non-blocking) source for body data.
internal protocol _BodySource: AnyObject {
/// Get the next chunck of data.
///
/// - Returns: `.data` until the source is exhausted, at which point it will
/// return `.done`. Since this is non-blocking, it will return `.retryLater`
/// if no data is available at this point, but will be available later.
func getNextChunk(withLength length: Int) -> _BodySourceDataChunk
}
internal enum _BodySourceDataChunk {
case data(DispatchData)
/// The source is depleted.
case done
/// Retry later to get more data.
case retryLater
case error
}
internal final class _BodyStreamSource {
let inputStream: InputStream
init(inputStream: InputStream) {
assert(inputStream.streamStatus == .notOpen)
inputStream.open()
self.inputStream = inputStream
}
}
extension _BodyStreamSource : _BodySource {
func getNextChunk(withLength length: Int) -> _BodySourceDataChunk {
guard inputStream.hasBytesAvailable else {
return .done
}
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: length, alignment: MemoryLayout<UInt8>.alignment)
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
buffer.deallocate()
return .error
}
let readBytes = self.inputStream.read(pointer, maxLength: length)
if readBytes > 0 {
let dispatchData = DispatchData(bytesNoCopy: UnsafeRawBufferPointer(buffer), deallocator: .custom(nil, { buffer.deallocate() }))
return .data(dispatchData.subdata(in: 0 ..< readBytes))
}
else if readBytes == 0 {
buffer.deallocate()
return .done
}
else {
buffer.deallocate()
return .error
}
}
}
/// A body data source backed by `DispatchData`.
internal final class _BodyDataSource {
var data: DispatchData!
init(data: DispatchData) {
self.data = data
}
}
extension _BodyDataSource : _BodySource {
enum _Error : Error {
case unableToRewindData
}
func getNextChunk(withLength length: Int) -> _BodySourceDataChunk {
let remaining = data.count
if remaining == 0 {
return .done
} else if remaining <= length {
let r: DispatchData! = data
data = DispatchData.empty
return .data(r)
} else {
let (chunk, remainder) = splitData(dispatchData: data, atPosition: length)
data = remainder
return .data(chunk)
}
}
}
/// A HTTP body data source backed by a file.
///
/// This allows non-blocking streaming of file data to the remote server.
///
/// The source reads data using a `DispatchIO` channel, and hence reading
/// file data is non-blocking. It has a local buffer that it fills as calls
/// to `getNextChunk(withLength:)` drain it.
///
/// - Note: Calls to `getNextChunk(withLength:)` and callbacks from libdispatch
/// should all happen on the same (serial) queue, and hence this code doesn't
/// have to be thread safe.
internal final class _BodyFileSource {
fileprivate let fileURL: URL
fileprivate let channel: DispatchIO
fileprivate let workQueue: DispatchQueue
fileprivate let dataAvailableHandler: () -> Void
fileprivate var hasActiveReadHandler = false
fileprivate var availableChunk: _Chunk = .empty
/// Create a new data source backed by a file.
///
/// - Parameter fileURL: the file to read from
/// - Parameter workQueue: the queue that it's safe to call
/// `getNextChunk(withLength:)` on, and that the `dataAvailableHandler`
/// will be called on.
/// - Parameter dataAvailableHandler: Will be called when data becomes
/// available. Reading data is done in a non-blocking way, such that
/// no data may be available even if there's more data in the file.
/// if `getNextChunk(withLength:)` returns `.retryLater`, this handler
/// will be called once data becomes available.
init(fileURL: URL, workQueue: DispatchQueue, dataAvailableHandler: @escaping () -> Void) {
guard fileURL.isFileURL else { fatalError("The body data URL must be a file URL.") }
self.fileURL = fileURL
self.workQueue = workQueue
self.dataAvailableHandler = dataAvailableHandler
guard let channel = fileURL.withUnsafeFileSystemRepresentation({
// DispatchIO (dispatch_io_create_with_path) makes a copy of the path
DispatchIO(type: .stream, path: $0!,
oflag: O_RDONLY, mode: 0, queue: workQueue,
cleanupHandler: {_ in })
}) else {
fatalError("Can't create DispatchIO channel")
}
self.channel = channel
self.channel.setLimit(highWater: CFURLSessionMaxWriteSize)
}
fileprivate enum _Chunk {
/// Nothing has been read, yet
case empty
/// An error has occurred while reading
case errorDetected(Int)
/// Data has been read
case data(DispatchData)
/// All data has been read from the file (EOF).
case done(DispatchData?)
}
}
extension _BodyFileSource {
fileprivate var desiredBufferLength: Int { return 3 * CFURLSessionMaxWriteSize }
/// Enqueue a dispatch I/O read to fill the buffer.
///
/// - Note: This is a no-op if the buffer is full, or if a read operation
/// is already enqueued.
fileprivate func readNextChunk() {
// libcurl likes to use a buffer of size CFURLSessionMaxWriteSize, we'll
// try to keep 3 x of that around in the `chunk` buffer.
guard availableByteCount < desiredBufferLength else { return }
guard !hasActiveReadHandler else { return } // We're already reading
hasActiveReadHandler = true
let lengthToRead = desiredBufferLength - availableByteCount
channel.read(offset: 0, length: lengthToRead, queue: workQueue) { (done: Bool, data: DispatchData?, errno: Int32) in
let wasEmpty = self.availableByteCount == 0
self.hasActiveReadHandler = !done
switch (done, data, errno) {
case (true, _, errno) where errno != 0:
self.availableChunk = .errorDetected(Int(errno))
case (true, let d?, 0) where d.isEmpty:
self.append(data: d, endOfFile: true)
case (true, let d?, 0):
self.append(data: d, endOfFile: false)
case (false, let d?, 0):
self.append(data: d, endOfFile: false)
default:
fatalError("Invalid arguments to read(3) callback.")
}
if wasEmpty && (0 < self.availableByteCount) {
self.dataAvailableHandler()
}
}
}
fileprivate func append(data: DispatchData, endOfFile: Bool) {
switch availableChunk {
case .empty:
availableChunk = endOfFile ? .done(data) : .data(data)
case .errorDetected:
break
case .data(var oldData):
oldData.append(data)
availableChunk = endOfFile ? .done(oldData) : .data(oldData)
case .done:
fatalError("Trying to append data, but end-of-file was already detected.")
}
}
fileprivate var availableByteCount: Int {
switch availableChunk {
case .empty: return 0
case .errorDetected: return 0
case .data(let d): return d.count
case .done(let d?): return d.count
case .done(nil): return 0
}
}
}
extension _BodyFileSource : _BodySource {
func getNextChunk(withLength length: Int) -> _BodySourceDataChunk {
switch availableChunk {
case .empty:
readNextChunk()
return .retryLater
case .errorDetected:
return .error
case .data(let data):
let l = min(length, data.count)
let (head, tail) = splitData(dispatchData: data, atPosition: l)
availableChunk = tail.isEmpty ? .empty : .data(tail)
readNextChunk()
if head.isEmpty {
return .retryLater
} else {
return .data(head)
}
case .done(let data?):
let l = min(length, data.count)
let (head, tail) = splitData(dispatchData: data, atPosition: l)
availableChunk = tail.isEmpty ? .done(nil) : .done(tail)
if head.isEmpty {
return .done
} else {
return .data(head)
}
case .done(nil):
return .done
}
}
}
|