Skip to content

make cookie parsing case insensitive #207

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 1 commit into from
May 13, 2020
Merged
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
39 changes: 13 additions & 26 deletions Sources/AsyncHTTPClient/HTTPClient+HTTPCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,24 @@ extension HTTPClient {
self.secure = false

for component in components[1...] {
if component.starts(with: "Path"), let value = parseComponentValue(component) {
switch self.parseComponent(component) {
case ("path", .some(let value)):
self.path = value
continue
}

if component.starts(with: "Domain"), let value = parseComponentValue(component) {
case ("domain", .some(let value)):
self.domain = value
continue
}

if component.starts(with: "Expires") {
case ("expires", let value):
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.timeZone = TimeZone(identifier: "GMT")
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
self.expires = self.parseComponentValue(component).flatMap { formatter.date(from: $0) }
continue
}

if component.starts(with: "Max-Age"), let value = parseComponentValue(component).flatMap(Int.init) {
self.maxAge = value
continue
}

if component == "Secure" {
self.expires = value.flatMap { formatter.date(from: $0) }
case ("max-age", let value):
self.maxAge = value.flatMap(Int.init)
case ("secure", nil):
self.secure = true
continue
}

if component == "HttpOnly" {
case ("httponly", nil):
self.httpOnly = true
default:
continue
}
}
Expand Down Expand Up @@ -127,14 +114,14 @@ extension HTTPClient {
self.secure = secure
}

func parseComponentValue(_ component: String) -> String? {
func parseComponent(_ component: String) -> (String, String?) {
let nameAndValue = component.split(separator: "=", maxSplits: 1).map {
$0.trimmingCharacters(in: .whitespaces)
}
if nameAndValue.count == 2 {
return nameAndValue[1]
return (nameAndValue[0].lowercased(), nameAndValue[1])
}
return nil
return (nameAndValue[0].lowercased(), nil)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/AsyncHTTPClientTests/HTTPClientCookieTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest

class HTTPClientCookieTests: XCTestCase {
func testCookie() {
let v = "key=value; Path=/path; Domain=example.com; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Max-Age=42; Secure; HttpOnly"
let v = "key=value; PaTh=/path; DoMaIn=example.com; eXpIRes=Wed, 21 Oct 2015 07:28:00 GMT; max-AGE=42; seCURE; HTTPOnly"
let c = HTTPClient.Cookie(header: v, defaultDomain: "exampe.org")!
XCTAssertEqual("key", c.name)
XCTAssertEqual("value", c.value)
Expand Down