Skip to content

Commit

Permalink
still return cookies if some fail to parse values, and accept % in di…
Browse files Browse the repository at this point in the history
…rective name
  • Loading branch information
gregoryyoung2 committed Oct 18, 2020
1 parent 45d19c3 commit 2ae7fa7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 6 additions & 8 deletions Sources/Vapor/HTTP/Headers/HTTPCookies.swift
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2ae7fa7

Please sign in to comment.