Skip to content

Commit

Permalink
Fix CORSMiddleware configuration initializer parameter (vapor#2314)
Browse files Browse the repository at this point in the history
* Makes sure CORSMiddleware always adds before ErrorMiddleware

* Deprecates the CORSMiddleware initializer which takes an [String]

* Added the doc comment

* Tanner's requested changes

* Switched to a 'convenience' initializer
  • Loading branch information
grosch authored and pull[bot] committed Apr 17, 2020
1 parent 20d26ca commit 6937cef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
@@ -0,0 +1,27 @@
extension CORSMiddleware.Configuration {
/// Instantiate a CORSConfiguration struct that can be used to create a `CORSConfiguration`
/// middleware for adding support for CORS in your responses.
///
/// - parameters:
/// - allowedOrigin: Setting that controls which origin values are allowed.
/// - allowedMethods: Methods that are allowed for a CORS request response.
/// - allowedHeaders: Headers that are allowed in a response for CORS request.
/// - allowCredentials: If cookies and other credentials will be sent in the response.
/// - cacheExpiration: Optionally sets expiration of the cached pre-flight request in seconds.
/// - exposedHeaders: Headers exposed in the response of pre-flight request.
@available(*, deprecated, message: "exposedHeaders parameter now accepts [HTTPHeaders.Name]")
public init(
allowedOrigin: CORSMiddleware.AllowOriginSetting,
allowedMethods: [HTTPMethod],
allowedHeaders: [HTTPHeaders.Name],
allowCredentials: Bool = false,
cacheExpiration: Int? = 600,
exposedHeaders: [String]
) {
let exposed = exposedHeaders.map { HTTPHeaders.Name($0) }
self.init(
allowedOrigin: allowedOrigin, allowedMethods: allowedMethods, allowedHeaders: allowedHeaders,
allowCredentials: allowCredentials, cacheExpiration: cacheExpiration, exposedHeaders: exposed
)
}
}
6 changes: 3 additions & 3 deletions Sources/Vapor/Middleware/CORSMiddleware.swift
Expand Up @@ -97,14 +97,14 @@ public final class CORSMiddleware: Middleware {
allowedHeaders: [HTTPHeaders.Name],
allowCredentials: Bool = false,
cacheExpiration: Int? = 600,
exposedHeaders: [String]? = nil
exposedHeaders: [HTTPHeaders.Name]? = nil
) {
self.allowedOrigin = allowedOrigin
self.allowedMethods = allowedMethods.map({ "\($0)" }).joined(separator: ", ")
self.allowedHeaders = allowedHeaders.map({ $0.description }).joined(separator: ", ")
self.allowedHeaders = allowedHeaders.map({ String(describing: $0) }).joined(separator: ", ")
self.allowCredentials = allowCredentials
self.cacheExpiration = cacheExpiration
self.exposedHeaders = exposedHeaders?.joined(separator: ", ")
self.exposedHeaders = exposedHeaders?.map({ String(describing: $0) }).joined(separator: ", ")
}
}

Expand Down

0 comments on commit 6937cef

Please sign in to comment.