@@ -67,6 +67,32 @@ public class EventSource {
6767 /// The maximum amount of time between receiving any data before considering the connection to have timed out.
6868 public var idleTimeout : TimeInterval = 300.0
6969
70+ private var _urlSessionConfiguration : URLSessionConfiguration = URLSessionConfiguration . default
71+ /**
72+ The `URLSessionConfiguration` used to create the `URLSession`.
73+
74+ - Important:
75+ Note that this copies the given `URLSessionConfiguration` when set, and returns copies (updated with any
76+ overrides specified by other configuration options) when the value is retrieved. This prevents updating the
77+ `URLSessionConfiguration` after initializing `EventSource` with the `Config`, and prevents the `EventSource`
78+ from updating any properties of the given `URLSessionConfiguration`.
79+
80+ - Since: 1.3.0
81+ */
82+ public var urlSessionConfiguration : URLSessionConfiguration {
83+ get {
84+ // swiftlint:disable:next force_cast
85+ let sessionConfig = _urlSessionConfiguration. copy ( ) as! URLSessionConfiguration
86+ sessionConfig. httpAdditionalHeaders = [ " Accept " : " text/event-stream " , " Cache-Control " : " no-cache " ]
87+ sessionConfig. timeoutIntervalForRequest = idleTimeout
88+ return sessionConfig
89+ }
90+ set {
91+ // swiftlint:disable:next force_cast
92+ _urlSessionConfiguration = newValue. copy ( ) as! URLSessionConfiguration
93+ }
94+ }
95+
7096 /**
7197 An error handler that is called when an error occurs and can shut down the client in response.
7298
@@ -136,10 +162,7 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
136162 func getLastEventId( ) -> String ? { lastEventId }
137163
138164 func createSession( ) -> URLSession {
139- let sessionConfig = URLSessionConfiguration . default
140- sessionConfig. httpAdditionalHeaders = [ " Accept " : " text/event-stream " , " Cache-Control " : " no-cache " ]
141- sessionConfig. timeoutIntervalForRequest = self . config. idleTimeout
142- return URLSession ( configuration: sessionConfig, delegate: self , delegateQueue: nil )
165+ URLSession ( configuration: config. urlSessionConfiguration, delegate: self , delegateQueue: nil )
143166 }
144167
145168 func createRequest( ) -> URLRequest {
@@ -148,7 +171,7 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
148171 timeoutInterval: self . config. idleTimeout)
149172 urlRequest. httpMethod = self . config. method
150173 urlRequest. httpBody = self . config. body
151- urlRequest. setValue ( self . lastEventId, forHTTPHeaderField: " Last-Event-ID " )
174+ urlRequest. setValue ( self . lastEventId, forHTTPHeaderField: " Last-Event-Id " )
152175 urlRequest. allHTTPHeaderFields = self . config. headerTransform (
153176 urlRequest. allHTTPHeaderFields? . merging ( self . config. headers) { $1 } ?? self . config. headers
154177 )
0 commit comments