Adding a check for existing vary header before overwriting with CORS … #2023
Conversation
@@ -54,19 +55,28 @@ object CORS { | |||
else | |||
config.exposedHeaders.map(headerFromStrings("Access-Control-Expose-Headers", _)) | |||
|
|||
def checkVaryHeader(response: Response[G]): Option[Header] = { |
jmcardon
Aug 21, 2018
Member
I think this function can be Response[G] => Response[G]
, saving you a fold down the line
If you look down below:
checkVaryHeader(withMethodBasedHeader)
.fold(withMethodBasedHeader)(h => withMethodBasedHeader.putHeaders(h))
you could simplify this into:
def varyHeader(response: Response[G]): Response[G] = {
// Match avoids a fold higher order call, but you can just do this with fold if you don't want
response.headers.get(CaseInsensitiveString("Vary")) match {
case None =>
response.putHeaders(Header("Vary", "Origin,Access-Control-Request-Method"))
case _ =>
response
}
}
and then, down the line:
varyHeader(withMethodBasedHeader)
.putHeaders(..
I think this function can be Response[G] => Response[G]
, saving you a fold down the line
If you look down below:
checkVaryHeader(withMethodBasedHeader)
.fold(withMethodBasedHeader)(h => withMethodBasedHeader.putHeaders(h))
you could simplify this into:
def varyHeader(response: Response[G]): Response[G] = {
// Match avoids a fold higher order call, but you can just do this with fold if you don't want
response.headers.get(CaseInsensitiveString("Vary")) match {
case None =>
response.putHeaders(Header("Vary", "Origin,Access-Control-Request-Method"))
case _ =>
response
}
}
and then, down the line:
varyHeader(withMethodBasedHeader)
.putHeaders(..
This looks good to me. I'll leave it open for a short while in case @yasuba wants to polish it a bit more, but I think the logic is correct. |
def checkVaryHeader(response: Response[G]): Option[Header] = { | ||
val maybeHeader = response.headers.get(CaseInsensitiveString("Vary")) | ||
if (maybeHeader.isEmpty) Option(Header("Vary", "Origin,Access-Control-Request-Method")) | ||
else maybeHeader |
yasuba
Aug 21, 2018
Author
Contributor
I'd like to polish the PR a bit - thanks for the comments
I'd like to polish the PR a bit - thanks for the comments
@@ -54,19 +55,28 @@ object CORS { | |||
else | |||
config.exposedHeaders.map(headerFromStrings("Access-Control-Expose-Headers", _)) | |||
|
|||
def checkVaryHeader(response: Response[G]): Option[Header] = { | |||
val maybeHeader = response.headers.get(CaseInsensitiveString("Vary")) | |||
if (maybeHeader.isEmpty) Option(Header("Vary", "Origin,Access-Control-Request-Method")) |
rossabaker
Aug 21, 2018
Member
Microoptimization: this default header could be stored in a val and shared by all requests.
Microoptimization: this default header could be stored in a val and shared by all requests.
jmcardon
Aug 21, 2018
Member
A microoptimization suggested by Ross himself?!
Are my repeated pedantic comments on microoptimizations finally making a dent?
A microoptimization suggested by Ross himself?!
Are my repeated pedantic comments on microoptimizations finally making a dent?
|
Thanks for the contribution @yasuba ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
…vary header
fixes #2003