2017-07-14 06:28:57 +00:00
|
|
|
module HTTPure.Headers
|
|
|
|
( Headers
|
2017-09-26 06:08:07 +00:00
|
|
|
, headers
|
|
|
|
, read
|
2017-07-17 23:42:13 +00:00
|
|
|
, write
|
2017-07-14 06:28:57 +00:00
|
|
|
) where
|
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2017-07-17 23:42:13 +00:00
|
|
|
|
2017-07-18 05:25:14 +00:00
|
|
|
import Control.Monad.Eff as Eff
|
2017-07-17 23:42:13 +00:00
|
|
|
import Data.Maybe as Maybe
|
2017-07-18 01:51:43 +00:00
|
|
|
import Data.String as StringUtil
|
2017-07-14 06:28:57 +00:00
|
|
|
import Data.StrMap as StrMap
|
2017-09-26 06:08:07 +00:00
|
|
|
import Data.TraversableWithIndex as TraversableWithIndex
|
|
|
|
import Data.Tuple as Tuple
|
2017-07-17 23:42:13 +00:00
|
|
|
import Node.HTTP as HTTP
|
|
|
|
|
2017-09-26 06:08:07 +00:00
|
|
|
import HTTPure.Lookup as Lookup
|
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
-- | The Headers type is just sugar for a StrMap of Strings that represents the
|
|
|
|
-- | set of headers sent or received in an HTTP request or response.
|
2017-09-26 06:08:07 +00:00
|
|
|
newtype Headers = Headers (StrMap.StrMap String)
|
|
|
|
|
|
|
|
-- | Given a string, return the matching headers. This search is
|
|
|
|
-- | case-insensitive.
|
|
|
|
instance lookupHeaders :: Lookup.Lookup Headers String String where
|
|
|
|
lookup (Headers headers') =
|
|
|
|
Maybe.fromMaybe "" <<< flip StrMap.lookup headers' <<< StringUtil.toLower
|
|
|
|
|
|
|
|
-- | Allow a headers set to be represented as a string.
|
|
|
|
instance showHeaders :: Show Headers where
|
|
|
|
show (Headers headers') =
|
|
|
|
StrMap.foldMap showField headers' <> "\n"
|
|
|
|
where
|
|
|
|
showField key value = key <> ": " <> value <> "\n"
|
2017-07-17 23:42:13 +00:00
|
|
|
|
2017-09-26 06:08:07 +00:00
|
|
|
-- | Compare two Headers objects by comparing the underlying StrMaps.
|
|
|
|
instance eqHeaders :: Eq Headers where
|
|
|
|
eq (Headers a) (Headers b) = eq a b
|
2017-07-18 01:51:43 +00:00
|
|
|
|
2017-09-26 06:08:07 +00:00
|
|
|
-- | Get the headers out of a HTTP Request object.
|
|
|
|
read :: HTTP.Request -> Headers
|
|
|
|
read = HTTP.requestHeaders >>> Headers
|
|
|
|
|
|
|
|
-- | Given an HTTP Response and a Headers object, return an effect that will
|
|
|
|
-- | write the Headers to the Response.
|
2017-07-18 05:25:14 +00:00
|
|
|
write :: forall e.
|
|
|
|
HTTP.Response ->
|
|
|
|
Headers ->
|
|
|
|
Eff.Eff (http :: HTTP.HTTP | e) Unit
|
2017-09-26 06:08:07 +00:00
|
|
|
write response (Headers headers') = void $
|
|
|
|
TraversableWithIndex.traverseWithIndex (HTTP.setHeader response) headers'
|
|
|
|
|
|
|
|
-- | Convert an Array of Tuples of 2 Strings to a Headers object.
|
|
|
|
headers :: Array (Tuple.Tuple String String) -> Headers
|
|
|
|
headers = StrMap.fromFoldable >>> Headers
|