Skip to content

Double quoted cookie values are supported now. #460

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

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 14 additions & 1 deletion Sources/AsyncHTTPClient/HTTPClient+HTTPCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension HTTPClient {
}

self.name = nameAndValue[0]
self.value = nameAndValue[1]
self.value = nameAndValue[1].omittingQuotes()

guard !self.name.isEmpty else {
return nil
Expand Down Expand Up @@ -153,6 +153,19 @@ extension HTTPClient {
}
}

private extension String {
func omittingQuotes() -> String {
let dquote = "\""
if !hasPrefix(dquote) || !hasSuffix(dquote) {
return self
}

let begin = index(after: startIndex)
let end = index(before: endIndex)
return String(self[begin..<end])
}
}

extension HTTPClient.Response {
/// List of HTTP cookies returned by the server.
public var cookies: [HTTPClient.Cookie] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extension HTTPClientCookieTests {
("testCookieInit", testCookieInit),
("testMalformedCookies", testMalformedCookies),
("testCookieExpiresDateParsing", testCookieExpiresDateParsing),
("testQuotedCookies", testQuotedCookies),
]
}
}
12 changes: 12 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientCookieTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ class HTTPClientCookieTests: XCTestCase {
c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sun Nov 6 08:49:37 1994;", defaultDomain: "example.org")!
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c.expires)
}

func testQuotedCookies() {
var c = HTTPClient.Cookie(header: "key=\"value\"", defaultDomain: "example.org")!
XCTAssertEqual("value", c.value)

c = HTTPClient.Cookie(header: "key=\"value\"; Path=/path", defaultDomain: "example.org")!
XCTAssertEqual("value", c.value)
XCTAssertEqual("/path", c.path)

c = HTTPClient.Cookie(header: "key=\"\"", defaultDomain: "example.org")!
XCTAssertEqual("", c.value)
}
}