Skip to content

Commit 0c4ea69

Browse files
committed
Fix cookie passthrough for ws connection upgrade
1 parent 5fc397d commit 0c4ea69

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import AsyncHTTPClient
22
import Foundation
3+
import NIO
4+
import NIOHTTP1
35

46
extension HTTPClient.Request {
57
public mutating func addDomainCookies() {
6-
let cookieJson = UserDefaults.standard.string(forKey: "\(url.host!)-cookies")
8+
headers.addDomainCookies(for: url.host!)
9+
}
10+
}
11+
12+
extension HTTPHeaders {
13+
public mutating func addDomainCookies(for domain: String) {
14+
let cookieJson = UserDefaults.standard.string(forKey: "\(domain)-cookies")
715
let cookies: [HTTPClient.Cookie?]? = try? cookieJson?.fromJson(to: [HTTPClient.Cookie].self)
816
?? [(try? cookieJson?.fromJson(to: HTTPClient.Cookie.self))]
917

1018
if let authCookie = cookies?.first(where: { $0?.name.starts(with: "a_session_") == true } ) {
11-
headers.add(name: "cookie", value: "\(authCookie!.name)=\(authCookie!.value)")
19+
add(name: "cookie", value: "\(authCookie!.name)=\(authCookie!.value)")
1220
}
1321
}
1422
}

templates/swift/Sources/WebSockets/HTTPHandler.swift.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extension HTTPHandler : ChannelInboundHandler, RemovableChannelHandler {
5151
headers.add(name: "Content-Type", value: "text/plain")
5252
headers.add(name: "Content-Length", value: "\(1)")
5353
headers.add(contentsOf: self.headers)
54+
headers.addDomainCookies(for: client.host)
5455
let requestHead = HTTPRequestHead(
5556
version: .http1_1,
5657
method: .GET,

0 commit comments

Comments
 (0)