@@ -21,7 +21,7 @@ import CoreFoundation
2121
2222
2323
24- extension _HTTPURLProtocol {
24+ extension _NativeProtocol {
2525 /// State related to an ongoing transfer.
2626 ///
2727 /// This contains headers received so far, body data received so far, etc.
@@ -31,13 +31,13 @@ extension _HTTPURLProtocol {
3131 ///
3232 /// - TODO: Might move the `EasyHandle` into this `struct` ?
3333 /// - SeeAlso: `URLSessionTask.EasyHandle`
34- internal struct _HTTPTransferState {
34+ internal struct _TransferState {
3535 /// The URL that's being requested
3636 let url : URL
3737 /// Raw headers received.
3838 let parsedResponseHeader : _ParsedResponseHeader
3939 /// Once the headers is complete, this will contain the response
40- var response : HTTPURLResponse ?
40+ var response : URLResponse ?
4141 /// The body data to be sent in the request
4242 let requestBodySource : _BodySource ?
4343 /// Body data received
@@ -46,7 +46,7 @@ extension _HTTPURLProtocol {
4646 }
4747}
4848
49- extension _HTTPURLProtocol {
49+ extension _NativeProtocol {
5050 enum _DataDrain {
5151 /// Concatenate in-memory
5252 case inMemory( NSMutableData ? )
@@ -57,37 +57,33 @@ extension _HTTPURLProtocol {
5757 }
5858}
5959
60- extension _HTTPURLProtocol . _HTTPTransferState {
60+ extension _NativeProtocol . _TransferState {
6161 /// Transfer state that can receive body data, but will not send body data.
62- init ( url: URL , bodyDataDrain: _HTTPURLProtocol . _DataDrain ) {
62+ init ( url: URL , bodyDataDrain: _NativeProtocol . _DataDrain ) {
6363 self . url = url
64- self . parsedResponseHeader = _HTTPURLProtocol . _ParsedResponseHeader ( )
64+ self . parsedResponseHeader = _NativeProtocol . _ParsedResponseHeader ( )
6565 self . response = nil
6666 self . requestBodySource = nil
6767 self . bodyDataDrain = bodyDataDrain
6868 }
6969 /// Transfer state that sends body data and can receive body data.
70- init ( url: URL , bodyDataDrain: _HTTPURLProtocol . _DataDrain , bodySource: _BodySource ) {
70+ init ( url: URL , bodyDataDrain: _NativeProtocol . _DataDrain , bodySource: _BodySource ) {
7171 self . url = url
72- self . parsedResponseHeader = _HTTPURLProtocol . _ParsedResponseHeader ( )
72+ self . parsedResponseHeader = _NativeProtocol . _ParsedResponseHeader ( )
7373 self . response = nil
7474 self . requestBodySource = bodySource
7575 self . bodyDataDrain = bodyDataDrain
7676 }
7777}
78-
79- extension _HTTPURLProtocol . _HTTPTransferState {
80- enum _Error : Error {
81- case parseSingleLineError
82- case parseCompleteHeaderError
83- }
78+ // specific to HTTP protocol
79+ extension _HTTPURLProtocol . _TransferState {
8480 /// Appends a header line
8581 ///
8682 /// Will set the complete response once the header is complete, i.e. the
8783 /// return value's `isHeaderComplete` will then by `true`.
8884 ///
8985 /// - Throws: When a parsing error occurs
90- func byAppending( headerLine data: Data ) throws -> _HTTPURLProtocol . _HTTPTransferState {
86+ func byAppending( headerLine data: Data ) throws -> _NativeProtocol . _TransferState {
9187 guard let h = parsedResponseHeader. byAppending ( headerLine: data) else {
9288 throw _Error. parseSingleLineError
9389 }
@@ -97,11 +93,20 @@ extension _HTTPURLProtocol._HTTPTransferState {
9793 guard response != nil else {
9894 throw _Error. parseCompleteHeaderError
9995 }
100- return _HTTPURLProtocol . _HTTPTransferState ( url: url, parsedResponseHeader: _HTTPURLProtocol . _ParsedResponseHeader ( ) , response: response, requestBodySource: requestBodySource, bodyDataDrain: bodyDataDrain)
96+ return _NativeProtocol . _TransferState ( url: url, parsedResponseHeader: _NativeProtocol . _ParsedResponseHeader ( ) , response: response, requestBodySource: requestBodySource, bodyDataDrain: bodyDataDrain)
10197 } else {
102- return _HTTPURLProtocol . _HTTPTransferState ( url: url, parsedResponseHeader: h, response: nil , requestBodySource: requestBodySource, bodyDataDrain: bodyDataDrain)
98+ return _NativeProtocol . _TransferState ( url: url, parsedResponseHeader: h, response: nil , requestBodySource: requestBodySource, bodyDataDrain: bodyDataDrain)
10399 }
104100 }
101+ }
102+
103+ extension _NativeProtocol . _TransferState {
104+
105+ enum _Error : Error {
106+ case parseSingleLineError
107+ case parseCompleteHeaderError
108+ }
109+
105110 var isHeaderComplete : Bool {
106111 return response != nil
107112 }
@@ -110,13 +115,13 @@ extension _HTTPURLProtocol._HTTPTransferState {
110115 /// - Important: This will mutate the existing `NSMutableData` that the
111116 /// struct may already have in place -- copying the data is too
112117 /// expensive. This behaviour
113- func byAppending( bodyData buffer: Data ) -> _HTTPURLProtocol . _HTTPTransferState {
118+ func byAppending( bodyData buffer: Data ) -> _NativeProtocol . _TransferState {
114119 switch bodyDataDrain {
115120 case . inMemory( let bodyData) :
116121 let data : NSMutableData = bodyData ?? NSMutableData ( )
117122 data. append ( buffer)
118- let drain = _HTTPURLProtocol . _DataDrain. inMemory ( data)
119- return _HTTPURLProtocol . _HTTPTransferState ( url: url, parsedResponseHeader: parsedResponseHeader, response: response, requestBodySource: requestBodySource, bodyDataDrain: drain)
123+ let drain = _NativeProtocol . _DataDrain. inMemory ( data)
124+ return _NativeProtocol . _TransferState ( url: url, parsedResponseHeader: parsedResponseHeader, response: response, requestBodySource: requestBodySource, bodyDataDrain: drain)
120125 case . toFile( _, let fileHandle) :
121126 //TODO: Create / open the file for writing
122127 // Append to the file
@@ -131,7 +136,7 @@ extension _HTTPURLProtocol._HTTPTransferState {
131136 ///
132137 /// This can be used to either set the initial body source, or to reset it
133138 /// e.g. when restarting a transfer.
134- func bySetting( bodySource newSource: _BodySource ) -> _HTTPURLProtocol . _HTTPTransferState {
135- return _HTTPURLProtocol . _HTTPTransferState ( url: url, parsedResponseHeader: parsedResponseHeader, response: response, requestBodySource: newSource, bodyDataDrain: bodyDataDrain)
139+ func bySetting( bodySource newSource: _BodySource ) -> _NativeProtocol . _TransferState {
140+ return _NativeProtocol . _TransferState ( url: url, parsedResponseHeader: parsedResponseHeader, response: response, requestBodySource: newSource, bodyDataDrain: bodyDataDrain)
136141 }
137142}
0 commit comments