Skip to content

Commit b6e9873

Browse files
committed
Update RSWeb with changes from Evergreen.
1 parent 73b23fb commit b6e9873

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+702
-339
lines changed

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

RSWeb.xcodeproj/project.pbxproj

100644100755
+84-222
Large diffs are not rendered by default.

RSWeb.xcodeproj/project.xcworkspace/contents.xcworkspacedata

100644100755
File mode changed.

RSWeb/Credentials.swift

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Credentials.swift
3+
// RSWeb
4+
//
5+
// Created by Brent Simmons on 12/9/17.
6+
// Copyright © 2017 Ranchero Software. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public protocol Credentials {
12+
13+
var username: String? { get set }
14+
var password: String? { get set }
15+
}
16+

RSWeb/Dictionary+RSWeb.swift

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Dictionary+RSWeb.swift
3+
// RSWeb
4+
//
5+
// Created by Brent Simmons on 1/13/18.
6+
// Copyright © 2018 Ranchero Software. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Dictionary {
12+
13+
public func urlQueryString() -> String? {
14+
15+
// Turn a dictionary into string like foo=bar&param2=some%20thing
16+
// Return nil if empty dictionary.
17+
18+
if isEmpty {
19+
return nil
20+
}
21+
22+
var s = ""
23+
var numberAdded = 0
24+
for (key, value) in self {
25+
26+
guard let key = key as? String, let value = value as? String else {
27+
continue
28+
}
29+
guard let encodedKey = key.encodedForURLQuery(), let encodedValue = value.encodedForURLQuery() else {
30+
continue
31+
}
32+
33+
if numberAdded > 0 {
34+
s += "&"
35+
}
36+
s += "\(encodedKey)=\(encodedValue)"
37+
numberAdded += 1
38+
}
39+
40+
if numberAdded < 1 {
41+
return nil
42+
}
43+
44+
return s
45+
}
46+
}

RSWeb/DownloadObject.swift

100644100755
+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RSWeb
44
//
55
// Created by Brent Simmons on 8/3/16.
6-
// Copyright © 2016 Ranchero Software. All rights reserved.
6+
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
77
//
88

99
import Foundation
@@ -14,9 +14,7 @@ public final class DownloadObject: Hashable {
1414
public var data = Data()
1515

1616
public var hashValue: Int {
17-
get {
18-
return url.hashValue
19-
}
17+
return url.hashValue
2018
}
2119

2220
public init(url: URL) {

RSWeb/DownloadProgress.swift

100644100755
+18-16
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,43 @@ import Foundation
1010

1111
public extension Notification.Name {
1212

13-
public static let DownloadProgressDidChange = Notification.Name(rawValue: "DownloadProgressDidChangeNotification")
13+
public static let DownloadProgressDidChange = Notification.Name(rawValue: "DownloadProgressDidChange")
1414
}
1515

16-
public class DownloadProgress {
16+
public final class DownloadProgress {
1717

1818
public var numberOfTasks = 0 {
1919
didSet {
2020
if numberOfTasks == 0 {
2121
numberRemaining = 0
2222
}
23-
postDidChangeNotification()
23+
if numberOfTasks != oldValue {
24+
postDidChangeNotification()
25+
}
2426
}
2527
}
2628

2729
public var numberRemaining = 0 {
2830
didSet {
29-
postDidChangeNotification()
31+
if numberRemaining != oldValue {
32+
postDidChangeNotification()
33+
}
3034
}
3135
}
3236

3337
public var numberCompleted: Int {
34-
get {
35-
var n = numberOfTasks - numberRemaining
36-
if n < 0 {
37-
n = 0
38-
}
39-
if n > numberOfTasks {
40-
n = numberOfTasks
41-
}
42-
return n
38+
var n = numberOfTasks - numberRemaining
39+
if n < 0 {
40+
n = 0
4341
}
42+
if n > numberOfTasks {
43+
n = numberOfTasks
44+
}
45+
return n
4446
}
4547

4648
public var isComplete: Bool {
47-
get {
48-
return numberRemaining < 1
49-
}
49+
return numberRemaining < 1
5050
}
5151

5252
public init(numberOfTasks: Int) {
@@ -65,6 +65,8 @@ public class DownloadProgress {
6565
}
6666
}
6767

68+
// MARK: - Private
69+
6870
private extension DownloadProgress {
6971

7072
func postDidChangeNotification() {

RSWeb/DownloadSession.swift

100644100755
+52-45
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RSWeb
44
//
55
// Created by Brent Simmons on 3/12/16.
6-
// Copyright © 2016 Ranchero Software. All rights reserved.
6+
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
77
//
88

99
import Foundation
@@ -25,17 +25,17 @@ public protocol DownloadSessionDelegate {
2525
}
2626

2727

28-
@objc public final class DownloadSession: NSObject, URLSessionDataDelegate {
28+
@objc public final class DownloadSession: NSObject {
2929

3030
public var progress = DownloadProgress(numberOfTasks: 0)
3131

32-
fileprivate var urlSession: URLSession!
33-
fileprivate var tasksInProgress = Set<URLSessionTask>()
34-
fileprivate var tasksPending = Set<URLSessionTask>()
35-
fileprivate var taskIdentifierToInfoDictionary = [Int: DownloadInfo]()
36-
fileprivate let representedObjects = NSMutableSet()
37-
fileprivate let delegate: DownloadSessionDelegate
38-
fileprivate var redirectCache = [String: String]()
32+
private var urlSession: URLSession!
33+
private var tasksInProgress = Set<URLSessionTask>()
34+
private var tasksPending = Set<URLSessionTask>()
35+
private var taskIdentifierToInfoDictionary = [Int: DownloadInfo]()
36+
private let representedObjects = NSMutableSet()
37+
private let delegate: DownloadSessionDelegate
38+
private var redirectCache = [String: String]()
3939

4040
public init(delegate: DownloadSessionDelegate) {
4141

@@ -52,10 +52,8 @@ public protocol DownloadSessionDelegate {
5252
sessionConfiguration.httpCookieStorage = nil
5353
sessionConfiguration.urlCache = nil
5454

55-
if let userAgent = UserAgent.fromInfoPlist() {
56-
var headers = [AnyHashable : Any]()
57-
headers[HTTPRequestHeader.userAgent] = userAgent
58-
sessionConfiguration.httpAdditionalHeaders = headers
55+
if let userAgentHeaders = UserAgent.headers() {
56+
sessionConfiguration.httpAdditionalHeaders = userAgentHeaders
5957
}
6058

6159
urlSession = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
@@ -64,9 +62,36 @@ public protocol DownloadSessionDelegate {
6462
deinit {
6563
urlSession.invalidateAndCancel()
6664
}
67-
68-
// MARK: URLSessionTaskDelegate
69-
65+
66+
// MARK: - API
67+
68+
public func cancel() {
69+
70+
// TODO
71+
}
72+
73+
public func downloadObjects(_ objects: NSSet) {
74+
75+
var numberOfTasksAdded = 0
76+
77+
for oneObject in objects {
78+
79+
if !representedObjects.contains(oneObject) {
80+
representedObjects.add(oneObject)
81+
addDataTask(oneObject as AnyObject)
82+
numberOfTasksAdded += 1
83+
}
84+
}
85+
86+
progress.addToNumberOfTasks(numberOfTasksAdded)
87+
updateProgress()
88+
}
89+
}
90+
91+
// MARK: - URLSessionTaskDelegate
92+
93+
extension DownloadSession: URLSessionTaskDelegate {
94+
7095
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
7196

7297
tasksInProgress.remove(task)
@@ -92,9 +117,12 @@ public protocol DownloadSessionDelegate {
92117

93118
completionHandler(request)
94119
}
95-
96-
// MARK: URLSessionDataDelegate
97-
120+
}
121+
122+
// MARK: - URLSessionDataDelegate
123+
124+
extension DownloadSession: URLSessionDataDelegate {
125+
98126
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
99127

100128
tasksInProgress.insert(dataTask)
@@ -146,31 +174,10 @@ public protocol DownloadSessionDelegate {
146174
}
147175
}
148176

149-
// MARK: API
150-
151-
public func cancel() {
152-
153-
// TODO
154-
}
155-
156-
public func downloadObjects(_ objects: NSSet) {
157-
158-
var numberOfTasksAdded = 0
159-
160-
for oneObject in objects {
161-
162-
if !representedObjects.contains(oneObject) {
163-
representedObjects.add(oneObject)
164-
addDataTask(oneObject as AnyObject)
165-
numberOfTasksAdded += 1
166-
}
167-
}
168-
169-
progress.addToNumberOfTasks(numberOfTasksAdded)
170-
updateProgress()
171-
}
172177
}
173178

179+
// MARK: - Private
180+
174181
private extension DownloadSession {
175182

176183
func updateProgress() {
@@ -275,6 +282,8 @@ private extension DownloadSession {
275282
}
276283
}
277284

285+
// MARK: - DownloadInfo
286+
278287
private final class DownloadInfo {
279288

280289
let representedObject: AnyObject
@@ -285,9 +294,7 @@ private final class DownloadInfo {
285294
var canceled = false
286295

287296
var statusCode: Int {
288-
get {
289-
return urlResponse?.forcedStatusCode ?? 0
290-
}
297+
return urlResponse?.forcedStatusCode ?? 0
291298
}
292299

293300
init(_ representedObject: AnyObject, urlRequest: URLRequest) {

RSWeb/HTTPConditionalGetInfo.swift

100644100755
+21-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RSWeb
44
//
55
// Created by Brent Simmons on 4/11/16.
6-
// Copyright © 2016 Ranchero Software. All rights reserved.
6+
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
77
//
88

99
import Foundation
@@ -12,26 +12,40 @@ public struct HTTPConditionalGetInfo {
1212

1313
public let lastModified: String?
1414
public let etag: String?
15-
public var isEmpty: Bool {
16-
get {
17-
return lastModified == nil && etag == nil
15+
16+
public var dictionary: [String: String] {
17+
var d = [String: String]()
18+
if let lastModified = lastModified {
19+
d[HTTPResponseHeader.lastModified] = lastModified
1820
}
21+
if let etag = etag {
22+
d[HTTPResponseHeader.etag] = etag
23+
}
24+
return d
1925
}
2026

21-
public init(lastModified: String?, etag: String?) {
22-
27+
public init?(lastModified: String?, etag: String?) {
28+
29+
if lastModified == nil && etag == nil {
30+
return nil
31+
}
2332
self.lastModified = lastModified
2433
self.etag = etag
2534
}
2635

27-
public init(urlResponse: HTTPURLResponse) {
36+
public init?(urlResponse: HTTPURLResponse) {
2837

2938
let lastModified = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.lastModified)
3039
let etag = urlResponse.valueForHTTPHeaderField(HTTPResponseHeader.etag)
3140

3241
self.init(lastModified: lastModified, etag: etag)
3342
}
3443

44+
public init?(dictionary: [String: String]) {
45+
46+
self.init(lastModified: dictionary[HTTPResponseHeader.lastModified], etag: dictionary[HTTPResponseHeader.etag])
47+
}
48+
3549
public func addRequestHeadersToURLRequest(_ urlRequest: NSMutableURLRequest) {
3650

3751
if let lastModified = lastModified {

RSWeb/HTTPMethod.swift

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RSWeb
44
//
55
// Created by Brent Simmons on 12/26/16.
6-
// Copyright © 2016 Ranchero Software. All rights reserved.
6+
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
77
//
88

99
import Foundation

RSWeb/HTTPRequestHeader.swift

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RSWeb
44
//
55
// Created by Brent Simmons on 12/26/16.
6-
// Copyright © 2016 Ranchero Software. All rights reserved.
6+
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
77
//
88

99
import Foundation

0 commit comments

Comments
 (0)