notAllowedContainsAllowHeader

This commit is contained in:
Julian K. Arni 2016-04-23 22:02:49 +02:00
parent 3189902c4b
commit 0bb6346cfc
2 changed files with 21 additions and 7 deletions

View File

@ -41,6 +41,7 @@ library
, warp >= 3.2.4 && < 3.3 , warp >= 3.2.4 && < 3.3
, process == 1.2.* , process == 1.2.*
, temporary == 1.2.* , temporary == 1.2.*
, split == 0.2.*
, case-insensitive , case-insensitive
, hspec , hspec
, text == 1.* , text == 1.*

View File

@ -5,13 +5,16 @@ import Data.Aeson (Object, decode)
import Data.Bifunctor (Bifunctor (..)) import Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString.Lazy as LBS import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString as SBS import qualified Data.ByteString as SBS
import qualified Data.ByteString.Char8 as SBSC
import Data.CaseInsensitive (mk) import Data.CaseInsensitive (mk)
import Data.Either (isRight)
import Data.List.Split (wordsBy)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Data.Text (Text) import Data.Text (Text)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Network.HTTP.Client (Manager, Request, Response, httpLbs, import Network.HTTP.Client (Manager, Request, Response, httpLbs,
responseBody, responseStatus, responseHeaders) responseBody, responseStatus, responseHeaders)
import Network.HTTP.Types (status500) import Network.HTTP.Types (status500, status405, parseMethod)
-- | @500 Internal Server Error@ should be avoided - it may represent some -- | @500 Internal Server Error@ should be avoided - it may represent some
-- issue with the application code, and it moreover gives the client little -- issue with the application code, and it moreover gives the client little
@ -52,20 +55,28 @@ createContainsValidLocation :: ResponsePredicate Text Bool
createContainsValidLocation createContainsValidLocation
= ResponsePredicate "createContainsValidLocation" (\resp -> = ResponsePredicate "createContainsValidLocation" (\resp ->
getsHaveLastModifiedHeader :: Response b -> IO Bool getsHaveLastModifiedHeader :: ResponsePredicate Text Bool
getsHaveLastModifiedHeader getsHaveLastModifiedHeader
= ResponsePredicate "getsHaveLastModifiedHeader" _ = ResponsePredicate "getsHaveLastModifiedHeader" (\resp ->
-}
-- | When an HTTP request has a method that is not allowed, a 405 response -- | When an HTTP request has a method that is not allowed, a 405 response
-- should be returned. Additionally, it is good practice to return an @Allow@ -- should be returned. Additionally, it is good practice to return an @Allow@
-- header with the list of allowed methods. -- header with the list of allowed methods.
-- --
-- This function checks that every @405 Method Not Allowed@ response contains -- This function checks that every @405 Method Not Allowed@ response contains
-- an @Allow@ header with a list of standard HTTP methods. -- an @Allow@ header with a list of standard HTTP methods.
notAllowedContainsAllowHeader :: Response b -> IO Bool notAllowedContainsAllowHeader :: ResponsePredicate Text Bool
notAllowedContainsAllowHeader notAllowedContainsAllowHeader
= ResponsePredicate "notAllowedContainsAllowHeader" _ = ResponsePredicate "notAllowedContainsAllowHeader" (\resp ->
if responseStatus resp == status405
then hasValidHeader "Allow" go resp
else True)
where
go x = all (\y -> isRight $ parseMethod $ SBSC.pack y)
$ wordsBy (`elem` (", " :: [Char])) (SBSC.unpack x)
{-
-- | When a request contains an @Accept@ header, the server must either return -- | When a request contains an @Accept@ header, the server must either return
-- content in one of the requested representations, or respond with @406 Not -- content in one of the requested representations, or respond with @406 Not
-- Acceptable@. -- Acceptable@.
@ -230,5 +241,7 @@ finishPredicates p req mgr = do
-- * helpers -- * helpers
hasHeader :: SBS.ByteString -> Response b -> Bool hasValidHeader :: SBS.ByteString -> (SBS.ByteString -> Bool) -> Response b -> Bool
hasHeader hdr r = mk hdr `elem` (fst <$> responseHeaders r) hasValidHeader hdr p r = case lookup (mk hdr) (responseHeaders r) of
Nothing -> False
Just v -> p v