Skip to content

Commit

Permalink
fix: duplicate headers in response
Browse files Browse the repository at this point in the history
  • Loading branch information
taimoorzaeem authored and steve-chavez committed Aug 25, 2023
1 parent 57fa271 commit 7dc6e2b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- #2899, Fix `application/vnd.pgrst.array` not accepted as a valid mediatype - @taimoorzaeem
- #2524, Fix schema cache and configuration reloading with `NOTIFY` not working on Windows - @diogob, @laurenceisla
- #2915, Fix duplicate headers in response - @taimoorzaeem

## [11.2.0] - 2023-08-10

Expand Down
31 changes: 28 additions & 3 deletions src/PostgREST/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,36 @@ optionalRollback AppConfig{..} ApiRequest{iPreferences=Preferences{..}} resp = d
| otherwise =
identity

-- | Add headers not already included to allow the user to override them instead of duplicating them
-- | Add headers not already included to allow the user to override them instead of duplicating them. The exception here is the Preference-Applied header, which will be duplicated here, but later get combined into a single header
--
-- >>> :{
-- addHeadersIfNotIncluded
-- [("Content-Type","application/json"),
-- ("Preference-Applied","tx=commit"),
-- ("Content-Range","*/*")]
-- [("Content-Type","custom/type"),
-- ("Preference-Applied","return=representation")]
-- :}
-- [("Preference-Applied","tx=commit"),("Content-Range","*/*"),("Content-Type","custom/type"),("Preference-Applied","return=representation")]
--
-- | Hmm, below seems like a problem, however this won't happen practically
-- because preferRepresentation is only added once in the request processing
-- pipeline. Thus, no need to add an extra filter. In case this becomes a
-- problem in the future, we can always change this
--
-- >>> :{
-- addHeadersIfNotIncluded
-- [("Preference-Applied","return=minimal")]
-- [("Preference-Applied","return=representation")]
-- :}
-- [("Preference-Applied","return=minimal"),("Preference-Applied","return=representation")]

addHeadersIfNotIncluded :: [HTTP.Header] -> [HTTP.Header] -> [HTTP.Header]
addHeadersIfNotIncluded newHeaders initialHeaders =
filter (\(nk, nv) -> isNothing $ find (\(ik, iv) -> ik == nk && nv == iv) initialHeaders) newHeaders ++
initialHeaders
filter (keyNotSameOrPrefApp . fst) newHeaders ++ initialHeaders
where
keyNotSameOrPrefApp k = isNothing (find ((== k) . fst) initialHeaders) ||
(k == HTTP.hPreferenceApplied)

-- | Filters out multiple Preference-Applied Headers from the list and concatenate them into a single Preference-Applied header:
--
Expand Down

0 comments on commit 7dc6e2b

Please sign in to comment.