Return HTTP 415 on non root path for openapi req
This commit is contained in:
@@ -6,6 +6,7 @@ module PostgREST.Middleware where
|
|||||||
import Data.Aeson (Value (..))
|
import Data.Aeson (Value (..))
|
||||||
import qualified Data.HashMap.Strict as M
|
import qualified Data.HashMap.Strict as M
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
|
import Data.Maybe (fromMaybe, listToMaybe)
|
||||||
import Data.Text
|
import Data.Text
|
||||||
import qualified Hasql.Transaction as H
|
import qualified Hasql.Transaction as H
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ import Network.Wai.Middleware.Cors (cors)
|
|||||||
import Network.Wai.Middleware.Gzip (def, gzip)
|
import Network.Wai.Middleware.Gzip (def, gzip)
|
||||||
import Network.Wai.Middleware.Static (only, staticPolicy)
|
import Network.Wai.Middleware.Static (only, staticPolicy)
|
||||||
|
|
||||||
import PostgREST.ApiRequest (ApiRequest(..), pickContentType)
|
import PostgREST.ApiRequest (ApiRequest(..), ContentType(..), pickContentType)
|
||||||
import PostgREST.Auth (claimsToSQL)
|
import PostgREST.Auth (claimsToSQL)
|
||||||
import PostgREST.Config (AppConfig (..), corsPolicy)
|
import PostgREST.Config (AppConfig (..), corsPolicy)
|
||||||
import PostgREST.Error (errResponse)
|
import PostgREST.Error (errResponse)
|
||||||
@@ -40,10 +41,14 @@ runWithClaims conf eClaims app req =
|
|||||||
|
|
||||||
unsupportedAccept :: Application -> Application
|
unsupportedAccept :: Application -> Application
|
||||||
unsupportedAccept app req respond =
|
unsupportedAccept app req respond =
|
||||||
case accept of
|
case (isTargetRoot, accept) of
|
||||||
Left _ -> respond $ errResponse status415 "Unsupported Accept header, try: application/json"
|
(_, Left _) -> unsupportedAcceptRespond
|
||||||
Right _ -> app req respond
|
(False, Right OpenAPI) -> unsupportedAcceptRespond
|
||||||
|
(_, Right _) -> app req respond
|
||||||
where accept = pickContentType $ lookup hAccept $ requestHeaders req
|
where accept = pickContentType $ lookup hAccept $ requestHeaders req
|
||||||
|
path = pathInfo req
|
||||||
|
isTargetRoot = fromMaybe True $ (== "") <$> listToMaybe path
|
||||||
|
unsupportedAcceptRespond = respond $ errResponse status415 "Unsupported Accept header, try: application/json"
|
||||||
|
|
||||||
defaultMiddle :: Application -> Application
|
defaultMiddle :: Application -> Application
|
||||||
defaultMiddle =
|
defaultMiddle =
|
||||||
|
|||||||
Reference in New Issue
Block a user