Skip to content
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

Better Cookie Parsing (#2504) #2511

Merged
merged 5 commits into from Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions Sources/Vapor/HTTP/Headers/HTTPCookies.swift
Expand Up @@ -3,7 +3,7 @@ extension HTTPHeaders {
/// This accesses the `"Cookie"` header.
public var cookie: HTTPCookies? {
get {
self.parseDirectives(name: .cookie).first.flatMap {
self.parseDirectives(name: .cookie).first.map {
HTTPCookies(directives: $0)
}
}
Expand Down Expand Up @@ -238,14 +238,12 @@ public struct HTTPCookies: ExpressibleByDictionaryLiteral {
self.cookies = [:]
}

init?(directives: [HTTPHeaders.Directive]) {
self.cookies = [:]
for directive in directives {
guard let value = directive.parameter else {
return nil
init(directives: [HTTPHeaders.Directive]) {
self.cookies = directives.reduce(into: [:], { (cookies, directive) in
if let value = directive.parameter {
cookies[.init(directive.value)] = .init(string: .init(value))
}
self.cookies[.init(directive.value)] = .init(string: .init(value))
}
})
}

/// See `ExpressibleByDictionaryLiteral`.
Expand Down
5 changes: 4 additions & 1 deletion Sources/Vapor/HTTP/Headers/HTTPHeaders+Directive.swift
Expand Up @@ -245,9 +245,12 @@ private extension Character {
static var space: Self {
.init(" ")
}
static var percent: Self {
.init("%")
}

var isDirectiveKey: Bool {
self.isLetter || self.isNumber || self == .dash || self == .underscore || self == .period
self.isLetter || self.isNumber || self == .dash || self == .underscore || self == .period || self == .percent
}
}

Expand Down