1- // Foundation/URLSession/Message.swift - URLSession & libcurl
1+ // Foundation/URLSession/Message.swift - Message parsing for native protocols
22//
33// This source file is part of the Swift.org open source project
44//
1010//
1111// -----------------------------------------------------------------------------
1212///
13- /// Common code for Header parsing
14- ///
13+ /// These are libcurl helpers for the URLSession API code.
1514/// - SeeAlso: https://curl.haxx.se/libcurl/c/
1615/// - SeeAlso: URLSession.swift
1716///
1817// -----------------------------------------------------------------------------
1918
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.
2421 ///
2522 /// It can either be complete (i.e. the final CR LF CR LF has been
2623 /// received), or partial.
@@ -45,19 +42,18 @@ extension _HTTPURLProtocol {
4542 }
4643}
4744
48- extension _HTTPURLProtocol . _ParsedResponseHeader {
45+ extension _NativeProtocol . _ParsedResponseHeader {
4946 /// Parse a header line passed by libcurl.
5047 ///
5148 /// These contain the <CRLF> ending and the final line contains nothing but
5249 /// that ending.
5350 /// - Returns: Returning nil indicates failure. Otherwise returns a new
5451 /// `ParsedResponseHeader` with the given line added.
55- func byAppending( headerLine data: Data ) -> _HTTPURLProtocol . _ParsedResponseHeader ? {
52+ func byAppending( headerLine data: Data ) -> _NativeProtocol . _ParsedResponseHeader ? {
5653 // 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
6157 else { return nil }
6258 let lineBuffer = data. subdata ( in: Range ( data. startIndex..< data. endIndex- 2 ) )
6359 guard let line = String ( data: lineBuffer, encoding: String . Encoding. utf8) else { return nil }
@@ -69,36 +65,38 @@ extension _HTTPURLProtocol._ParsedResponseHeader {
6965 /// is a complete header. Otherwise it's a partial header.
7066 /// - Note: Appending a line to a complete header results in a partial
7167 /// header with just that line.
72- private func byAppending( headerLine line: String ) -> _HTTPURLProtocol . _ParsedResponseHeader {
68+ private func byAppending( headerLine line: String ) -> _NativeProtocol . _ParsedResponseHeader {
7369 if line. isEmpty {
7470 switch self {
7571 case . partial( let header) : return . complete( header)
76- case . complete: return . partial( _HTTPURLProtocol . _ResponseHeaderLines ( ) )
72+ case . complete: return . partial( _NativeProtocol . _ResponseHeaderLines ( ) )
7773 }
7874 } else {
7975 let header = partialResponseHeader
8076 return . partial( header. byAppending ( headerLine: line) )
8177 }
8278 }
83- private var partialResponseHeader : _HTTPURLProtocol . _ResponseHeaderLines {
79+
80+ private var partialResponseHeader : _NativeProtocol . _ResponseHeaderLines {
8481 switch self {
8582 case . partial( let header) : return header
86- case . complete: return _HTTPURLProtocol . _ResponseHeaderLines ( )
83+ case . complete: return _NativeProtocol . _ResponseHeaderLines ( )
8784 }
8885 }
8986}
9087
91- private extension _HTTPURLProtocol . _ResponseHeaderLines {
88+ private extension _NativeProtocol . _ResponseHeaderLines {
9289 /// 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 {
9491 var l = self . lines
9592 l. append ( line)
96- return _HTTPURLProtocol . _ResponseHeaderLines ( headerLines: l)
93+ return _NativeProtocol . _ResponseHeaderLines ( headerLines: l)
9794 }
9895}
9996
100- // Characters that we need for HTTP parsing:
101- struct _HTTPCharacters {
97+ // Characters that we need for Header parsing:
98+
99+ struct _Delimiters {
102100 /// *Carriage Return* symbol
103101 static let CR : UInt8 = 0x0d
104102 /// *Line Feed* symbol
0 commit comments