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

add Storage to Request + Response, deprecate userInfo #2216

Merged
merged 1 commit into from Mar 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions Sources/Vapor/Application.swift
Expand Up @@ -7,7 +7,6 @@ public final class Application {
public let eventLoopGroupProvider: EventLoopGroupProvider
public let eventLoopGroup: EventLoopGroup
public var storage: Storage
public var userInfo: [AnyHashable: Any]
public private(set) var didShutdown: Bool
public var logger: Logger
private var isBooted: Bool
Expand Down Expand Up @@ -71,7 +70,6 @@ public final class Application {
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
}
self.locks = .init()
self.userInfo = [:]
self.didShutdown = false
self.logger = .init(label: "codes.vapor.application")
self.storage = .init(logger: self.logger)
Expand Down Expand Up @@ -140,7 +138,6 @@ public final class Application {
self.logger.trace("Clearing Application storage")
self.storage.shutdown()
self.storage.clear()
self.userInfo = [:]

switch self.eventLoopGroupProvider {
case .shared:
Expand Down
23 changes: 10 additions & 13 deletions Sources/Vapor/Authentication/AuthenticationCache.swift
Expand Up @@ -84,20 +84,17 @@ extension Request {
}
}

private struct AuthenticationCacheKey: StorageKey {
typealias Value = AuthenticationCache
}

internal var _authenticationCache: AuthenticationCache {
get {
if let existing = self.userInfo[_authenticationCacheKey] as? AuthenticationCache {
return existing
} else {
let new = AuthenticationCache()
self.userInfo[_authenticationCacheKey] = new
return new
}
}
set {
self.userInfo[_authenticationCacheKey] = newValue
if let existing = self.storage[AuthenticationCacheKey.self] {
return existing
} else {
let new = AuthenticationCache()
self.storage[AuthenticationCacheKey.self] = new
return new
}
}
}

private let _authenticationCacheKey = "authc"
32 changes: 31 additions & 1 deletion Sources/Vapor/Deprecated.swift
@@ -1 +1,31 @@
// nothing here yet...
extension Application {
private struct UserInfoKey: StorageKey {
typealias Value = [AnyHashable: Any]
}

@available(*, deprecated, message: "Use storage instead.")
public var userInfo: [AnyHashable: Any] {
get {
self.storage[UserInfoKey.self] ?? [:]
}
set {
self.storage[UserInfoKey.self] = newValue
}
}
}

extension Request {
private struct UserInfoKey: StorageKey {
typealias Value = [AnyHashable: Any]
}

@available(*, deprecated, message: "Use storage instead.")
public var userInfo: [AnyHashable: Any] {
get {
self.storage[UserInfoKey.self] ?? [:]
}
set {
self.storage[UserInfoKey.self] = newValue
}
}
}
6 changes: 3 additions & 3 deletions Sources/Vapor/Request/Request.swift
Expand Up @@ -128,8 +128,8 @@ public final class Request: CustomStringConvertible {
public let eventLoop: EventLoop

public var parameters: Parameters
public var userInfo: [AnyHashable: Any]

public var storage: Storage

public convenience init(
application: Application,
Expand Down Expand Up @@ -181,7 +181,7 @@ public final class Request: CustomStringConvertible {
self.remoteAddress = remoteAddress
self.eventLoop = eventLoop
self.parameters = .init()
self.userInfo = [:]
self.storage = .init()
self.isKeepAlive = true
self.logger = logger
self.logger[metadataKey: "request-id"] = .string(UUID().uuidString)
Expand Down
3 changes: 3 additions & 0 deletions Sources/Vapor/Response/Response.swift
Expand Up @@ -37,6 +37,8 @@ public final class Response: CustomStringConvertible {
}

internal var upgrader: Upgrader?

public var storage: Storage

/// Get and set `HTTPCookies` for this `HTTPResponse`
/// This accesses the `"Set-Cookie"` header.
Expand Down Expand Up @@ -131,6 +133,7 @@ public final class Response: CustomStringConvertible {
self.version = version
self.headers = headers
self.body = body
self.storage = .init()
}
}

Expand Down
23 changes: 10 additions & 13 deletions Sources/Vapor/Sessions/Request+Session.swift
Expand Up @@ -34,21 +34,18 @@ extension Request {
public func destroySession() {
self._sessionCache.session = nil
}

private struct SessionCacheKey: StorageKey {
typealias Value = SessionCache
}

internal var _sessionCache: SessionCache {
get {
if let existing = self.userInfo[_sessionCacheKey] as? SessionCache {
return existing
} else {
let new = SessionCache()
self.userInfo[_sessionCacheKey] = new
return new
}
}
set {
self.userInfo[_sessionCacheKey] = newValue
if let existing = self.storage[SessionCacheKey.self] {
return existing
} else {
let new = SessionCache()
self.storage[SessionCacheKey.self] = new
return new
}
}
}

private let _sessionCacheKey = "session"
16 changes: 7 additions & 9 deletions Sources/Vapor/Sessions/SessionsMiddleware.swift
Expand Up @@ -34,32 +34,30 @@ public final class SessionsMiddleware: Middleware {

/// See `Middleware.respond`
public func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
// Create a session cache
let cache = SessionCache()
request._sessionCache = cache
cache.middlewareFlag = true
// Signal middleware has been added.
request._sessionCache.middlewareFlag = true

// Check for an existing session
if let cookieValue = request.cookies[self.configuration.cookieName] {
// A cookie value exists, get the session for it.
let id = SessionID(string: cookieValue.string)
return self.session.readSession(id, for: request).flatMap { data in
cache.session = .init(id: id, data: data ?? .init())
request._sessionCache.session = .init(id: id, data: data ?? .init())
return next.respond(to: request).flatMap { res in
return self.addCookies(to: res, for: request, cache: cache)
return self.addCookies(to: res, for: request)
}
}
} else {
// No cookie value exists, simply respond.
return next.respond(to: request).flatMap { response in
return self.addCookies(to: response, for: request, cache: cache)
return self.addCookies(to: response, for: request)
}
}
}

/// Adds session cookie to response or clears if session was deleted.
private func addCookies(to response: Response, for request: Request, cache: SessionCache) -> EventLoopFuture<Response> {
if let session = cache.session {
private func addCookies(to response: Response, for request: Request) -> EventLoopFuture<Response> {
if let session = request._sessionCache.session {
// A session exists or has been created. we must
// set a cookie value on the response
let createOrUpdate: EventLoopFuture<SessionID>
Expand Down