Skip to content

Commit 1361ecc

Browse files
authored
Double quoted cookie values are supported now. (#460)
Motivation: In according to RFC 6265 a cookie value may be placed between double quotes. Modifications: HTTPClient.Cookie ignores now the double quotes at the beginning and the ending of a cookie value. New unit test is added to check it. Result: Quoted cookie values are parsed properly now.
1 parent c1a60d8 commit 1361ecc

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPClient+HTTPCookie.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension HTTPClient {
5959
}
6060

6161
self.name = nameAndValue[0]
62-
self.value = nameAndValue[1]
62+
self.value = nameAndValue[1].omittingQuotes()
6363

6464
guard !self.name.isEmpty else {
6565
return nil
@@ -153,6 +153,19 @@ extension HTTPClient {
153153
}
154154
}
155155

156+
private extension String {
157+
func omittingQuotes() -> String {
158+
let dquote = "\""
159+
if !hasPrefix(dquote) || !hasSuffix(dquote) {
160+
return self
161+
}
162+
163+
let begin = index(after: startIndex)
164+
let end = index(before: endIndex)
165+
return String(self[begin..<end])
166+
}
167+
}
168+
156169
extension HTTPClient.Response {
157170
/// List of HTTP cookies returned by the server.
158171
public var cookies: [HTTPClient.Cookie] {

Diff for: Tests/AsyncHTTPClientTests/HTTPClientCookieTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extension HTTPClientCookieTests {
3131
("testCookieInit", testCookieInit),
3232
("testMalformedCookies", testMalformedCookies),
3333
("testCookieExpiresDateParsing", testCookieExpiresDateParsing),
34+
("testQuotedCookies", testQuotedCookies),
3435
]
3536
}
3637
}

Diff for: Tests/AsyncHTTPClientTests/HTTPClientCookieTests.swift

+12
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ class HTTPClientCookieTests: XCTestCase {
9393
c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sun Nov 6 08:49:37 1994;", defaultDomain: "example.org")!
9494
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c.expires)
9595
}
96+
97+
func testQuotedCookies() {
98+
var c = HTTPClient.Cookie(header: "key=\"value\"", defaultDomain: "example.org")!
99+
XCTAssertEqual("value", c.value)
100+
101+
c = HTTPClient.Cookie(header: "key=\"value\"; Path=/path", defaultDomain: "example.org")!
102+
XCTAssertEqual("value", c.value)
103+
XCTAssertEqual("/path", c.path)
104+
105+
c = HTTPClient.Cookie(header: "key=\"\"", defaultDomain: "example.org")!
106+
XCTAssertEqual("", c.value)
107+
}
96108
}

0 commit comments

Comments
 (0)