@@ -21,7 +21,7 @@ import CoreFoundation
21
21
22
22
23
23
24
- extension _HTTPURLProtocol {
24
+ extension _NativeProtocol {
25
25
/// State related to an ongoing transfer.
26
26
///
27
27
/// This contains headers received so far, body data received so far, etc.
@@ -31,13 +31,13 @@ extension _HTTPURLProtocol {
31
31
///
32
32
/// - TODO: Might move the `EasyHandle` into this `struct` ?
33
33
/// - SeeAlso: `URLSessionTask.EasyHandle`
34
- internal struct _HTTPTransferState {
34
+ internal struct _TransferState {
35
35
/// The URL that's being requested
36
36
let url : URL
37
37
/// Raw headers received.
38
38
let parsedResponseHeader : _ParsedResponseHeader
39
39
/// Once the headers is complete, this will contain the response
40
- var response : HTTPURLResponse ?
40
+ var response : URLResponse ?
41
41
/// The body data to be sent in the request
42
42
let requestBodySource : _BodySource ?
43
43
/// Body data received
@@ -46,7 +46,7 @@ extension _HTTPURLProtocol {
46
46
}
47
47
}
48
48
49
- extension _HTTPURLProtocol {
49
+ extension _NativeProtocol {
50
50
enum _DataDrain {
51
51
/// Concatenate in-memory
52
52
case inMemory( NSMutableData ? )
@@ -57,37 +57,33 @@ extension _HTTPURLProtocol {
57
57
}
58
58
}
59
59
60
- extension _HTTPURLProtocol . _HTTPTransferState {
60
+ extension _NativeProtocol . _TransferState {
61
61
/// 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 ) {
63
63
self . url = url
64
- self . parsedResponseHeader = _HTTPURLProtocol . _ParsedResponseHeader ( )
64
+ self . parsedResponseHeader = _NativeProtocol . _ParsedResponseHeader ( )
65
65
self . response = nil
66
66
self . requestBodySource = nil
67
67
self . bodyDataDrain = bodyDataDrain
68
68
}
69
69
/// 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 ) {
71
71
self . url = url
72
- self . parsedResponseHeader = _HTTPURLProtocol . _ParsedResponseHeader ( )
72
+ self . parsedResponseHeader = _NativeProtocol . _ParsedResponseHeader ( )
73
73
self . response = nil
74
74
self . requestBodySource = bodySource
75
75
self . bodyDataDrain = bodyDataDrain
76
76
}
77
77
}
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 {
84
80
/// Appends a header line
85
81
///
86
82
/// Will set the complete response once the header is complete, i.e. the
87
83
/// return value's `isHeaderComplete` will then by `true`.
88
84
///
89
85
/// - Throws: When a parsing error occurs
90
- func byAppending( headerLine data: Data ) throws -> _HTTPURLProtocol . _HTTPTransferState {
86
+ func byAppending( headerLine data: Data ) throws -> _NativeProtocol . _TransferState {
91
87
guard let h = parsedResponseHeader. byAppending ( headerLine: data) else {
92
88
throw _Error. parseSingleLineError
93
89
}
@@ -97,11 +93,20 @@ extension _HTTPURLProtocol._HTTPTransferState {
97
93
guard response != nil else {
98
94
throw _Error. parseCompleteHeaderError
99
95
}
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)
101
97
} 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)
103
99
}
104
100
}
101
+ }
102
+
103
+ extension _NativeProtocol . _TransferState {
104
+
105
+ enum _Error : Error {
106
+ case parseSingleLineError
107
+ case parseCompleteHeaderError
108
+ }
109
+
105
110
var isHeaderComplete : Bool {
106
111
return response != nil
107
112
}
@@ -110,13 +115,13 @@ extension _HTTPURLProtocol._HTTPTransferState {
110
115
/// - Important: This will mutate the existing `NSMutableData` that the
111
116
/// struct may already have in place -- copying the data is too
112
117
/// expensive. This behaviour
113
- func byAppending( bodyData buffer: Data ) -> _HTTPURLProtocol . _HTTPTransferState {
118
+ func byAppending( bodyData buffer: Data ) -> _NativeProtocol . _TransferState {
114
119
switch bodyDataDrain {
115
120
case . inMemory( let bodyData) :
116
121
let data : NSMutableData = bodyData ?? NSMutableData ( )
117
122
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)
120
125
case . toFile( _, let fileHandle) :
121
126
//TODO: Create / open the file for writing
122
127
// Append to the file
@@ -131,7 +136,7 @@ extension _HTTPURLProtocol._HTTPTransferState {
131
136
///
132
137
/// This can be used to either set the initial body source, or to reset it
133
138
/// 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)
136
141
}
137
142
}
0 commit comments