Skip to content

Commit a64972a

Browse files
committed
Moving common implementation to Nativeprotocol
1 parent 972bd9d commit a64972a

File tree

6 files changed

+58
-28
lines changed

6 files changed

+58
-28
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
5BF9B8021FABD5DA00EE1A7C /* CFBundle_Tables.c in Sources */ = {isa = PBXBuildFile; fileRef = 5BF9B7F71FABD5D400EE1A7C /* CFBundle_Tables.c */; };
311311
5FE52C951D147D1C00F7D270 /* TestNSTextCheckingResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE52C941D147D1C00F7D270 /* TestNSTextCheckingResult.swift */; };
312312
6105D30F1FEBC5FC0022865A /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6105D30E1FEBC5FC0022865A /* Message.swift */; };
313+
61D2F9AF1FECFB3E0033306A /* NativeProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61D2F9AE1FECFB3E0033306A /* NativeProtocol.swift */; };
313314
61E0117D1C1B5590000037DD /* RunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B761BD15DFF00C49C64 /* RunLoop.swift */; };
314315
61E0117E1C1B55B9000037DD /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F481BCC5DCB00ED97BB /* Timer.swift */; };
315316
61E0117F1C1B5990000037DD /* CFRunLoop.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D81BBC9AD800234F36 /* CFRunLoop.c */; };
@@ -781,6 +782,7 @@
781782
5FE52C941D147D1C00F7D270 /* TestNSTextCheckingResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSTextCheckingResult.swift; sourceTree = "<group>"; };
782783
6105D30E1FEBC5FC0022865A /* Message.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
783784
61A395F91C2484490029B337 /* TestNSLocale.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSLocale.swift; sourceTree = "<group>"; };
785+
61D2F9AE1FECFB3E0033306A /* NativeProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeProtocol.swift; sourceTree = "<group>"; };
784786
61D6C9EE1C1DFE9500DEF583 /* TestTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestTimer.swift; sourceTree = "<group>"; };
785787
61E0117B1C1B554D000037DD /* TestRunLoop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestRunLoop.swift; sourceTree = "<group>"; };
786788
61F8AE7C1C180FC600FB62F0 /* TestNotificationCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNotificationCenter.swift; sourceTree = "<group>"; };
@@ -1020,6 +1022,7 @@
10201022
5B1FD9D11D6D16580080E83C /* URLSessionTask.swift */,
10211023
5B1FD9D21D6D16580080E83C /* TaskRegistry.swift */,
10221024
6105D30E1FEBC5FC0022865A /* Message.swift */,
1025+
61D2F9AE1FECFB3E0033306A /* NativeProtocol.swift */,
10231026
);
10241027
name = Session;
10251028
path = URLSession;
@@ -2294,6 +2297,7 @@
22942297
EADE0BA61BD15E0000C49C64 /* MassFormatter.swift in Sources */,
22952298
5BECBA3A1D1CAE9A00B39B1F /* NSMeasurement.swift in Sources */,
22962299
5BF7AEB21BCD51F9008F214A /* NSNumber.swift in Sources */,
2300+
61D2F9AF1FECFB3E0033306A /* NativeProtocol.swift in Sources */,
22972301
B9974B991EDF4A22007F15B8 /* HTTPURLProtocol.swift in Sources */,
22982302
5BCD03821D3EE35C00E3FF9B /* TimeZone.swift in Sources */,
22992303
EADE0BBC1BD15E0000C49C64 /* URLCache.swift in Sources */,

Foundation/URLSession/Message.swift

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
//
@@ -10,17 +10,14 @@
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
import CoreFoundation
12+
import Dispatch
13+
14+
internal class _NativeProtocol: URLProtocol {
15+
16+
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
17+
return request
18+
}
19+
20+
override func startLoading() {
21+
NSRequiresConcreteImplementation()
22+
}
23+
24+
override func stopLoading() {
25+
NSRequiresConcreteImplementation()
26+
}
27+
}

Foundation/URLSession/http/HTTPMessage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private extension _HTTPURLProtocol._HTTPMessage._Header {
194194
guard let (head, tail) = lines.decompose else { return nil }
195195
let headView = head.unicodeScalars[...]
196196
guard let nameRange = headView.rangeOfTokenPrefix else { return nil }
197-
guard headView.index(after: nameRange.upperBound) <= headView.endIndex && headView[nameRange.upperBound] == _HTTPCharacters.Colon else { return nil }
197+
guard headView.index(after: nameRange.upperBound) <= headView.endIndex && headView[nameRange.upperBound] == _Delimiters.Colon else { return nil }
198198
let name = String(headView[nameRange])
199199
var value: String?
200200
let line = headView[headView.index(after: nameRange.upperBound)..<headView.endIndex]
@@ -241,21 +241,21 @@ private extension String.UnicodeScalarView.SubSequence {
241241
/// The range of space (U+0020) characters.
242242
var rangeOfSpace: Range<Index>? {
243243
guard !isEmpty else { return startIndex..<startIndex }
244-
guard let idx = index(of: _HTTPCharacters.Space!) else { return nil }
244+
guard let idx = index(of: _Delimiters.Space!) else { return nil }
245245
return idx..<self.index(after: idx)
246246
}
247247
// Has a space (SP) or horizontal tab (HT) prefix
248248
var hasSPHTPrefix: Bool {
249249
guard !isEmpty else { return false }
250-
return self[startIndex] == _HTTPCharacters.Space || self[startIndex] == _HTTPCharacters.HorizontalTab
250+
return self[startIndex] == _Delimiters.Space || self[startIndex] == _Delimiters.HorizontalTab
251251
}
252252
/// Unicode scalars after removing the leading spaces (SP) and horizontal tabs (HT).
253253
/// Returns `nil` if the unicode scalars do not start with a SP or HT.
254254
var trimSPHTPrefix: SubSequence? {
255255
guard !isEmpty else { return nil }
256256
var idx = startIndex
257257
while idx < endIndex {
258-
if self[idx] == _HTTPCharacters.Space || self[idx] == _HTTPCharacters.HorizontalTab {
258+
if self[idx] == _Delimiters.Space || self[idx] == _Delimiters.HorizontalTab {
259259
idx = self.index(after: idx)
260260
} else {
261261
guard startIndex < idx else { return nil }
@@ -271,6 +271,6 @@ private extension UnicodeScalar {
271271
/// - SeeAlso: https://tools.ietf.org/html/rfc2616#section-2
272272
var isValidMessageToken: Bool {
273273
guard UnicodeScalar(32) <= self && self <= UnicodeScalar(126) else { return false }
274-
return !_HTTPCharacters.Separators.characterIsMember(UInt16(self.value))
274+
return !_Delimiters.Separators.characterIsMember(UInt16(self.value))
275275
}
276276
}

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import CoreFoundation
1111
import Dispatch
1212

13-
internal class _HTTPURLProtocol: URLProtocol {
13+
internal class _HTTPURLProtocol: _NativeProtocol {
1414

1515
fileprivate var easyHandle: _EasyHandle!
1616
fileprivate lazy var tempFileURL: URL = {

build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@
439439
'Foundation/URLSession/URLSessionDelegate.swift',
440440
'Foundation/URLSession/URLSessionTask.swift',
441441
'Foundation/URLSession/TaskRegistry.swift',
442+
'Foundation/URLSession/NativeProtocol.swift',
442443
'Foundation/URLSession/TransferState.swift',
443444
'Foundation/URLSession/libcurl/libcurlHelpers.swift',
444445
'Foundation/URLSession/http/HTTPURLProtocol.swift',

0 commit comments

Comments
 (0)