socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

SocketIOClientOption.cookies ignored in Starscream with HTTPCookieStorage

Open jamamp opened this issue 2 years ago • 1 comments

The SocketIOClientOption.cookies configuration option is sometimes ignored entirely, if any cookie exists in the HTTPCookieStorage.shared.cookies(for: url) for the installed Swift application.

See https://github.com/daltoniam/Starscream/blob/4.0.4/Sources/Framer/HTTPHandler.swift#L70

        if let cookies = HTTPCookieStorage.shared.cookies(for: url), !cookies.isEmpty {
            let headers = HTTPCookie.requestHeaderFields(with: cookies)
            for (key, val) in headers {
                req.setValue(val, forHTTPHeaderField: key)
            }
        }

The code will overwrite whatever cookies are attempted to be set by this library. This caused a lot of confusion because when debugging an issue, it looked like down to the Engine.IO layer that the configured cookies were being set properly. But it was in the Starscream layer that they were ignored.

It doesn't look like there's a solution other than to document this behavior more clearly to avoid confusion and bugs in the future. Starscream doesn't look like it allows custom cookies if any HTTPCookieStorage cookies exist.

jamamp avatar Aug 14 '23 00:08 jamamp

This happened to occur with something like the following SocketManager configuration:

socketManager = SocketManager(
	socketURL: URL(string: "wss://example.com")!,
	config: [
		.log(true),
		.compress,
		.forceWebsockets(true),
		.extraHeaders([
			"Host": "example.com",
			"Origin": "https://example.com",
		]),
		.cookies([
			HTTPCookie(properties: [
				.domain: ".example.com",
				.path: "/",
				.name: "sid",
				.value: sidValue,
				.secure: "Secure",
				.expires: NSDate(timeIntervalSinceNow: 1_000_000_000),
			])!,
		]),
		.reconnects(false),
		.secure(true),
		.path("/"),
		.connectParams([
			"__sails_io_sdk_version": "0.13.8",
			"__sails_io_sdk_platform": "tvos",
			"__sails_io_sdk_language": "swift",
		]),
		.version(.two),
	])

jamamp avatar Aug 14 '23 00:08 jamamp