1
- // Foundation/URLSession/Message.swift - URLSession & libcurl
1
+ // Foundation/URLSession/Message.swift - Message parsing for native protocols
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
10
10
//
11
11
// -----------------------------------------------------------------------------
12
12
///
13
- /// Common code for Header parsing
14
- ///
13
+ /// These are libcurl helpers for the URLSession API code.
15
14
/// - SeeAlso: https://curl.haxx.se/libcurl/c/
16
15
/// - SeeAlso: URLSession.swift
17
16
///
18
17
// -----------------------------------------------------------------------------
19
18
20
- import CoreFoundation
21
-
22
- extension _HTTPURLProtocol {
23
- /// An HTTP header being parsed.
19
+ extension _NativeProtocol {
20
+ /// A native protocol like FTP or HTTP header being parsed.
24
21
///
25
22
/// It can either be complete (i.e. the final CR LF CR LF has been
26
23
/// received), or partial.
@@ -45,19 +42,18 @@ extension _HTTPURLProtocol {
45
42
}
46
43
}
47
44
48
- extension _HTTPURLProtocol . _ParsedResponseHeader {
45
+ extension _NativeProtocol . _ParsedResponseHeader {
49
46
/// Parse a header line passed by libcurl.
50
47
///
51
48
/// These contain the <CRLF> ending and the final line contains nothing but
52
49
/// that ending.
53
50
/// - Returns: Returning nil indicates failure. Otherwise returns a new
54
51
/// `ParsedResponseHeader` with the given line added.
55
- func byAppending( headerLine data: Data ) -> _HTTPURLProtocol . _ParsedResponseHeader ? {
52
+ func byAppending( headerLine data: Data ) -> _NativeProtocol . _ParsedResponseHeader ? {
56
53
// The buffer must end in CRLF
57
- guard
58
- 2 <= data. count &&
59
- data [ data. endIndex - 2 ] == _HTTPCharacters. CR &&
60
- data [ data. endIndex - 1 ] == _HTTPCharacters. LF
54
+ guard 2 <= data. count &&
55
+ data [ data. endIndex - 2 ] == _Delimiters. CR &&
56
+ data [ data. endIndex - 1 ] == _Delimiters. LF
61
57
else { return nil }
62
58
let lineBuffer = data. subdata ( in: Range ( data. startIndex..< data. endIndex- 2 ) )
63
59
guard let line = String ( data: lineBuffer, encoding: String . Encoding. utf8) else { return nil }
@@ -69,36 +65,38 @@ extension _HTTPURLProtocol._ParsedResponseHeader {
69
65
/// is a complete header. Otherwise it's a partial header.
70
66
/// - Note: Appending a line to a complete header results in a partial
71
67
/// header with just that line.
72
- private func byAppending( headerLine line: String ) -> _HTTPURLProtocol . _ParsedResponseHeader {
68
+ private func byAppending( headerLine line: String ) -> _NativeProtocol . _ParsedResponseHeader {
73
69
if line. isEmpty {
74
70
switch self {
75
71
case . partial( let header) : return . complete( header)
76
- case . complete: return . partial( _HTTPURLProtocol . _ResponseHeaderLines ( ) )
72
+ case . complete: return . partial( _NativeProtocol . _ResponseHeaderLines ( ) )
77
73
}
78
74
} else {
79
75
let header = partialResponseHeader
80
76
return . partial( header. byAppending ( headerLine: line) )
81
77
}
82
78
}
83
- private var partialResponseHeader : _HTTPURLProtocol . _ResponseHeaderLines {
79
+
80
+ private var partialResponseHeader : _NativeProtocol . _ResponseHeaderLines {
84
81
switch self {
85
82
case . partial( let header) : return header
86
- case . complete: return _HTTPURLProtocol . _ResponseHeaderLines ( )
83
+ case . complete: return _NativeProtocol . _ResponseHeaderLines ( )
87
84
}
88
85
}
89
86
}
90
87
91
- private extension _HTTPURLProtocol . _ResponseHeaderLines {
88
+ private extension _NativeProtocol . _ResponseHeaderLines {
92
89
/// Returns a copy of the lines with the new line appended to it.
93
- func byAppending( headerLine line: String ) -> _HTTPURLProtocol . _ResponseHeaderLines {
90
+ func byAppending( headerLine line: String ) -> _NativeProtocol . _ResponseHeaderLines {
94
91
var l = self . lines
95
92
l. append ( line)
96
- return _HTTPURLProtocol . _ResponseHeaderLines ( headerLines: l)
93
+ return _NativeProtocol . _ResponseHeaderLines ( headerLines: l)
97
94
}
98
95
}
99
96
100
- // Characters that we need for HTTP parsing:
101
- struct _HTTPCharacters {
97
+ // Characters that we need for Header parsing:
98
+
99
+ struct _Delimiters {
102
100
/// *Carriage Return* symbol
103
101
static let CR : UInt8 = 0x0d
104
102
/// *Line Feed* symbol
0 commit comments