{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE PatternSynonyms #-}

-- | Types and constants to describe the HTTP version.
--
-- There are no parsing functions, as the formats are fairly different
-- per version of HTTP. And seeing as there are only a handful of versions,
-- it is easier to manually parse the version ad hoc.
--
-- For example, when you are expecting HTTP v1, try to match @HTTP/1.1@ or
-- @HTTP/1.0@. If you're expecting HTTP v2 or v3, you'd be using ALPN tokens,
-- which would be @http/1.1@, @h2@ or @h3@.
module Network.HTTP.Types.Version (
#if __GLASGOW_HASKELL__ >= 800
    HttpVersion (
        ..,
        Http09,
        Http10,
        Http11,
        Http20,
        Http30
    ),
#else
    HttpVersion (..),
    pattern Http09,
    pattern Http10,
    pattern Http11,
    pattern Http20,
    pattern Http30,
#endif
    http09,
    http10,
    http11,
    http20,
    http30,
) where

import Data.Data (Data)
import GHC.Generics (Generic)

-- | HTTP Version.
--
-- Note that the 'Show' instance is intended merely for debugging.
data HttpVersion = HttpVersion
    { HttpVersion -> Int
httpMajor :: !Int
    , HttpVersion -> Int
httpMinor :: !Int
    }
    deriving
        ( HttpVersion -> HttpVersion -> Bool
(HttpVersion -> HttpVersion -> Bool)
-> (HttpVersion -> HttpVersion -> Bool) -> Eq HttpVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HttpVersion -> HttpVersion -> Bool
== :: HttpVersion -> HttpVersion -> Bool
$c/= :: HttpVersion -> HttpVersion -> Bool
/= :: HttpVersion -> HttpVersion -> Bool
Eq
        , Eq HttpVersion
Eq HttpVersion =>
(HttpVersion -> HttpVersion -> Ordering)
-> (HttpVersion -> HttpVersion -> Bool)
-> (HttpVersion -> HttpVersion -> Bool)
-> (HttpVersion -> HttpVersion -> Bool)
-> (HttpVersion -> HttpVersion -> Bool)
-> (HttpVersion -> HttpVersion -> HttpVersion)
-> (HttpVersion -> HttpVersion -> HttpVersion)
-> Ord HttpVersion
HttpVersion -> HttpVersion -> Bool
HttpVersion -> HttpVersion -> Ordering
HttpVersion -> HttpVersion -> HttpVersion
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: HttpVersion -> HttpVersion -> Ordering
compare :: HttpVersion -> HttpVersion -> Ordering
$c< :: HttpVersion -> HttpVersion -> Bool
< :: HttpVersion -> HttpVersion -> Bool
$c<= :: HttpVersion -> HttpVersion -> Bool
<= :: HttpVersion -> HttpVersion -> Bool
$c> :: HttpVersion -> HttpVersion -> Bool
> :: HttpVersion -> HttpVersion -> Bool
$c>= :: HttpVersion -> HttpVersion -> Bool
>= :: HttpVersion -> HttpVersion -> Bool
$cmax :: HttpVersion -> HttpVersion -> HttpVersion
max :: HttpVersion -> HttpVersion -> HttpVersion
$cmin :: HttpVersion -> HttpVersion -> HttpVersion
min :: HttpVersion -> HttpVersion -> HttpVersion
Ord
        , -- | @since 0.12.4
          Typeable HttpVersion
Typeable HttpVersion =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> HttpVersion -> c HttpVersion)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c HttpVersion)
-> (HttpVersion -> Constr)
-> (HttpVersion -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c HttpVersion))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c HttpVersion))
-> ((forall b. Data b => b -> b) -> HttpVersion -> HttpVersion)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> HttpVersion -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> HttpVersion -> r)
-> (forall u. (forall d. Data d => d -> u) -> HttpVersion -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> HttpVersion -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion)
-> Data HttpVersion
HttpVersion -> Constr
HttpVersion -> DataType
(forall b. Data b => b -> b) -> HttpVersion -> HttpVersion
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> HttpVersion -> u
forall u. (forall d. Data d => d -> u) -> HttpVersion -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c HttpVersion
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> HttpVersion -> c HttpVersion
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c HttpVersion)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c HttpVersion)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> HttpVersion -> c HttpVersion
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> HttpVersion -> c HttpVersion
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c HttpVersion
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c HttpVersion
$ctoConstr :: HttpVersion -> Constr
toConstr :: HttpVersion -> Constr
$cdataTypeOf :: HttpVersion -> DataType
dataTypeOf :: HttpVersion -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c HttpVersion)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c HttpVersion)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c HttpVersion)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c HttpVersion)
$cgmapT :: (forall b. Data b => b -> b) -> HttpVersion -> HttpVersion
gmapT :: (forall b. Data b => b -> b) -> HttpVersion -> HttpVersion
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HttpVersion -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> HttpVersion -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> HttpVersion -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> HttpVersion -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> HttpVersion -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> HttpVersion -> m HttpVersion
Data
        , -- | @since 0.12.4
          (forall x. HttpVersion -> Rep HttpVersion x)
-> (forall x. Rep HttpVersion x -> HttpVersion)
-> Generic HttpVersion
forall x. Rep HttpVersion x -> HttpVersion
forall x. HttpVersion -> Rep HttpVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HttpVersion -> Rep HttpVersion x
from :: forall x. HttpVersion -> Rep HttpVersion x
$cto :: forall x. Rep HttpVersion x -> HttpVersion
to :: forall x. Rep HttpVersion x -> HttpVersion
Generic
        )

-- | >>> show http11
-- "HTTP/1.1"
-- >>> show http20
-- "HTTP/2.0"
--
-- This should not be used to render the HTTP version, as different versions
-- have different ways of rendering (i.e. HTTP v2 uses @"h2"@ instead of @"HTTP/2.0"@)
instance Show HttpVersion where
    show :: HttpVersion -> String
show (HttpVersion Int
major Int
minor) = String
"HTTP/" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
major String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"." String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
minor

-- | HTTP 0.9
http09 :: HttpVersion
http09 :: HttpVersion
http09 = Int -> Int -> HttpVersion
HttpVersion Int
0 Int
9

-- | HTTP 1.0
http10 :: HttpVersion
http10 :: HttpVersion
http10 = Int -> Int -> HttpVersion
HttpVersion Int
1 Int
0

-- | HTTP 1.1
http11 :: HttpVersion
http11 :: HttpVersion
http11 = Int -> Int -> HttpVersion
HttpVersion Int
1 Int
1

-- | HTTP 2.0
--
-- @since 0.10
http20 :: HttpVersion
http20 :: HttpVersion
http20 = Int -> Int -> HttpVersion
HttpVersion Int
2 Int
0

-- | HTTP 3.0
--
-- @since 0.12.5
http30 :: HttpVersion
http30 :: HttpVersion
http30 = Int -> Int -> HttpVersion
HttpVersion Int
3 Int
0

----------------------
-- Pattern Synonyms --
----------------------

-- DO NOT put these on one line with commas, as GHC 7.10.3 doesn't parse that
pattern Http09 :: HttpVersion
pattern Http10 :: HttpVersion
pattern Http11 :: HttpVersion
pattern Http20 :: HttpVersion
pattern Http30 :: HttpVersion

-- | @since 0.13.0
pattern $mHttp09 :: forall {r}. HttpVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bHttp09 :: HttpVersion
Http09 = HttpVersion 0 9

-- | @since 0.13.0
pattern $mHttp10 :: forall {r}. HttpVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bHttp10 :: HttpVersion
Http10 = HttpVersion 1 0

-- | @since 0.13.0
pattern $mHttp11 :: forall {r}. HttpVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bHttp11 :: HttpVersion
Http11 = HttpVersion 1 1

-- | @since 0.13.0
pattern $mHttp20 :: forall {r}. HttpVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bHttp20 :: HttpVersion
Http20 = HttpVersion 2 0

-- | @since 0.13.0
pattern $mHttp30 :: forall {r}. HttpVersion -> ((# #) -> r) -> ((# #) -> r) -> r
$bHttp30 :: HttpVersion
Http30 = HttpVersion 3 0