module Network.HTTP.Types.Header
(
Header
, HeaderName
, RequestHeaders
, ResponseHeaders
, hAccept
, hAcceptLanguage
, hAuthorization
, hCacheControl
, hCookie
, hConnection
, hContentEncoding
, hContentLength
, hContentMD5
, hContentType
, hDate
, hIfModifiedSince
, hIfRange
, hLastModified
, hLocation
, hRange
, hReferer
, hServer
, hUserAgent
, ByteRange(..)
, renderByteRangeBuilder
, renderByteRange
, ByteRanges
, renderByteRangesBuilder
, renderByteRanges
, headerAccept
, headerAuthorization
, headerCacheControl
, headerConnection
, headerContentLength
, headerContentType
, headerContentMD5
, headerDate
)
where
import Data.List
import Data.Monoid
import qualified Blaze.ByteString.Builder as Blaze
import qualified Blaze.ByteString.Builder.Char8 as Blaze
import qualified Data.ByteString as B
import qualified Data.CaseInsensitive as CI
import Data.ByteString.Char8 ()
type Header = (HeaderName, B.ByteString)
type HeaderName = CI.CI B.ByteString
type RequestHeaders = [Header]
type ResponseHeaders = [Header]
hAccept, hAcceptLanguage, hAuthorization, hCacheControl, hConnection, hContentEncoding, hContentLength, hContentMD5, hContentType, hCookie, hDate, hIfModifiedSince, hIfRange, hLastModified, hLocation, hRange, hReferer, hServer, hUserAgent :: HeaderName
hAccept = "Accept"
hAcceptLanguage = "Accept-Language"
hAuthorization = "Authorization"
hCacheControl = "Cache-Control"
hConnection = "Connection"
hContentEncoding = "Content-Encoding"
hContentLength = "Content-Length"
hContentMD5 = "Content-MD5"
hContentType = "Content-Type"
hCookie = "Cookie"
hDate = "Date"
hIfModifiedSince = "If-Modified-Since"
hIfRange = "If-Range"
hLastModified = "Last-Modified"
hLocation = "Location"
hRange = "Range"
hReferer = "Referer"
hServer = "Server"
hUserAgent = "User-Agent"
headerAccept, headerAuthorization, headerCacheControl, headerConnection, headerContentLength, headerContentType, headerContentMD5, headerDate :: B.ByteString -> Header
headerAccept = (,) hAccept
headerAuthorization = (,) hAuthorization
headerCacheControl = (,) hCacheControl
headerConnection = (,) hConnection
headerContentLength = (,) hContentLength
headerContentMD5 = (,) hContentMD5
headerContentType = (,) hContentType
headerDate = (,) hDate
data ByteRange
= ByteRangeFrom !Integer
| ByteRangeFromTo !Integer !Integer
| ByteRangeSuffix !Integer
renderByteRangeBuilder :: ByteRange -> Blaze.Builder
renderByteRangeBuilder (ByteRangeFrom from) = Blaze.fromShow from `mappend` Blaze.fromChar '-'
renderByteRangeBuilder (ByteRangeFromTo from to) = Blaze.fromShow from `mappend` Blaze.fromChar '-' `mappend` Blaze.fromShow to
renderByteRangeBuilder (ByteRangeSuffix suffix) = Blaze.fromChar '-' `mappend` Blaze.fromShow suffix
renderByteRange :: ByteRange -> B.ByteString
renderByteRange = Blaze.toByteString . renderByteRangeBuilder
type ByteRanges = [ByteRange]
renderByteRangesBuilder :: ByteRanges -> Blaze.Builder
renderByteRangesBuilder xs = Blaze.copyByteString "bytes=" `mappend`
mconcat (intersperse (Blaze.fromChar ',') (map renderByteRangeBuilder xs))
renderByteRanges :: ByteRanges -> B.ByteString
renderByteRanges = Blaze.toByteString . renderByteRangesBuilder