module Network.S3.Types
( S3Method (..)
, S3Request (..)
, S3SignedRequest (..)
, S3Header
, getS3Header
, s3Header
, s3HeaderBuilder
) where
import Data.ByteString.UTF8 (ByteString)
import GHC.Generics (Generic)
import Data.Char (isSpace)
import Network.HTTP.Types (Query)
import Data.Time.Clock (UTCTime)
import Blaze.ByteString.Builder (Builder, fromByteString)
import Data.Monoid (mconcat)
import qualified Data.ByteString.Char8 as BS
import qualified Data.CaseInsensitive as CI
newtype S3Header = S3Header { getS3Header :: (ByteString, ByteString) }
deriving (Generic, Show)
data S3Method = S3GET
| S3PUT
| S3HEAD
| S3DELETE
deriving (Generic, Show)
data S3Request = S3Request {
s3method :: S3Method
, mimeType :: Maybe ByteString
, bucketName :: ByteString
, objectName :: ByteString
, regionName :: ByteString
, queryString :: Query
, requestTime :: UTCTime
, payloadHash :: Maybe ByteString
, s3headers :: [S3Header]
} deriving (Generic, Show)
data S3SignedRequest = S3SignedRequest {
sigHeaders :: [S3Header]
, sigDate :: ByteString
, sigCredential :: ByteString
, sigPolicy :: ByteString
, sigSignature :: ByteString
, sigAlgorithm :: ByteString
} deriving (Generic, Show)
trim :: ByteString -> ByteString
trim = BS.dropWhile isSpace . fst . BS.spanEnd isSpace
s3Header :: ByteString -> ByteString -> S3Header
s3Header header value = S3Header (lower, trimmed)
where
lower = CI.foldCase header
trimmed = trim value
s3HeaderBuilder :: S3Header -> Builder
s3HeaderBuilder (S3Header (header,value)) =
mconcat [fromByteString header, ":", fromByteString value, "\n"]