Skip to content

Commit ee50cd5

Browse files
committed
fix multiple cookie: Enhance HTTP header parsing in ZitiUrlProtocol to handle multiple Set-Cookie headers. Collect all Set-Cookie values into a single entry in the resulting dictionary.
1 parent aeadb6a commit ee50cd5

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

deps/vcpkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 2ec65725864eb30841b0b58573f8324a5d545a86

lib/ZitiUrlProtocol.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,37 @@ import CZitiPrivate
295295
return
296296
}
297297

298-
var hdrMap:[String:String] = [:]
299-
var curr = resp.pointee.headers.lh_first
300-
while curr != nil {
301-
hdrMap[String(cString: curr!.pointee.name)] = String(cString: curr!.pointee.value)
302-
curr = curr!.pointee._next.le_next
298+
/// Parses HTTP response headers from the C structure and constructs a Swift dictionary.
299+
/// Special handling is done for "Set-Cookie" headers, which may appear multiple times.
300+
/// All "Set-Cookie" values are collected into an array and then joined into a single
301+
/// comma-separated string, as expected by HTTPURLResponse.
302+
///
303+
/// - Returns: A dictionary mapping header names to their values, with all "Set-Cookie"
304+
/// headers combined into a single entry if present.
305+
var hdrMap: [String: String] = [:]
306+
var setCookieValues: [String] = []
307+
308+
var currentHeader = resp.pointee.headers.lh_first
309+
310+
while let header = currentHeader {
311+
let headerName = String(cString: header.pointee.name)
312+
let headerValue = String(cString: header.pointee.value)
313+
314+
// Collect all "Set-Cookie" headers into an array
315+
if headerName.lowercased() == "set-cookie" {
316+
setCookieValues.append(headerValue)
317+
} else {
318+
hdrMap[headerName] = headerValue
319+
}
320+
321+
currentHeader = header.pointee._next.le_next
303322
}
304-
323+
324+
// Combine all "Set-Cookie" values into a single comma-separated string
325+
if !setCookieValues.isEmpty {
326+
hdrMap["Set-Cookie"] = setCookieValues.joined(separator: ", ")
327+
}
328+
305329
// On TLS handshake error getting a negative response code (-53), notifyDidReceive
306330
// nothing, so we end up waiting for timeout. So notifyDidFailWithError instead...
307331
let code = Int(resp.pointee.code)

0 commit comments

Comments
 (0)