Skip to content

Adding an option to ignore httpCookieStorage Cookies #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Source/SocketIO/Client/SocketIOClientOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public enum SocketIOClientOption : ClientOption {

/// An array of cookies that will be sent during the initial connection.
case cookies([HTTPCookie])

/// if `false` cookies from cookieStorage will be ignored.
case useCookieStorageCookies(Bool)

/// Any extra HTTP headers that should be sent during the initial connection.
case extraHeaders([String: String])
Expand Down Expand Up @@ -127,6 +130,8 @@ public enum SocketIOClientOption : ClientOption {
description = "connectParams"
case .cookies:
description = "cookies"
case .useCookieStorageCookies:
description = "useCookieStorageCookies"
case .extraHeaders:
description = "extraHeaders"
case .forceNew:
Expand Down Expand Up @@ -182,6 +187,8 @@ public enum SocketIOClientOption : ClientOption {
value = params
case let .cookies(cookies):
value = cookies
case let .useCookieStorageCookies(useCookieStorageCookies):
value = useCookieStorageCookies
case let .extraHeaders(headers):
value = headers
case let .forceNew(force):
Expand Down
10 changes: 9 additions & 1 deletion Source/SocketIO/Engine/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,

/// An array of HTTPCookies that are sent during the connection.
public private(set) var cookies: [HTTPCookie]?

/// it `true` the engine will add all cookies from cookieStorage for the connectionURL
/// if you need to manual manage all cookies set this to `false`
public private(set) var useCookieStorageCookies: Bool = true

/// When `true`, the engine is in the process of switching to WebSockets.
///
Expand Down Expand Up @@ -307,9 +311,11 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,
private func createWebSocketAndConnect() {
var req = URLRequest(url: urlWebSocketWithSid)

var cookies:[HTTPCookie] = useCookieStorageCookies ? session?.configuration.httpCookieStorage?.cookies(for: urlPollingWithSid) ?? [] : []

addHeaders(
to: &req,
includingCookies: session?.configuration.httpCookieStorage?.cookies(for: urlPollingWithSid)
includingCookies: cookies
)

ws = WebSocket(request: req, certPinner: certPinner, compressionHandler: compress ? WSCompression() : nil, useCustomEngine: useCustomEngine)
Expand Down Expand Up @@ -605,6 +611,8 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,
connectParams = params
case let .cookies(cookies):
self.cookies = cookies
case let .useCookieStorageCookies(useCookieStorageCookies):
self.useCookieStorageCookies = useCookieStorageCookies
case let .extraHeaders(headers):
extraHeaders = headers
case let .sessionDelegate(delegate):
Expand Down