]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/HttpVersion.hs
The attoparsec branch. It doesn't even compile for now.
[Lucu.git] / Network / HTTP / Lucu / HttpVersion.hs
index 88dc24e5b61673726da5f31e70d184e42b9cf48f..4531c837782ef9b6eda9edd4849e3771f2b0b0a1 100644 (file)
@@ -1,15 +1,26 @@
+{-# LANGUAGE
+    BangPatterns
+  , OverloadedStrings
+  , UnicodeSyntax
+  #-}
+{-# OPTIONS_HADDOCK prune #-}
+
+-- |Manipulation of HTTP version string.
 module Network.HTTP.Lucu.HttpVersion
     ( HttpVersion(..)
-    , httpVersionP -- Parser HttpVersion
+    , httpVersionP
+    , hPutHttpVersion
     )
     where
+import Control.Monad.Unicode
+import Data.Attoparsec.Char8
+import Network.HTTP.Lucu.HandleLike
+import Prelude hiding (min)
 
-import qualified Data.ByteString.Lazy.Char8 as B
-import           Data.ByteString.Lazy.Char8 (ByteString)
-import           Network.HTTP.Lucu.Parser
-
-data HttpVersion = HttpVersion Int Int
-                   deriving (Show, Eq)
+-- |@'HttpVersion' major minor@ represents \"HTTP\/major.minor\".
+data HttpVersion
+    = HttpVersion !Int !Int
+      deriving (Eq, Show)
 
 instance Ord HttpVersion where
     (HttpVersion majA minA) `compare` (HttpVersion majB minB)
@@ -19,11 +30,26 @@ instance Ord HttpVersion where
         | minA < minB = LT
         | otherwise   = EQ
 
+httpVersionP ∷ Parser HttpVersion
+httpVersionP = string "HTTP/"
+               ≫
+               choice [ string "1.1" ≫ return (HttpVersion 1 1)
+                      , string "1.0" ≫ return (HttpVersion 1 0)
+                      , do major ← decimal
+                           _     ← char '.'
+                           minor ← decimal
+                           return $ HttpVersion major minor
+                      ]
 
-httpVersionP :: Parser HttpVersion
-httpVersionP = do string "HTTP/"
-                  major <- many1 digit
-                  char '.'
-                  minor <- many1 digit
-                  return $ HttpVersion (read major) (read minor)
-
+hPutHttpVersion ∷ HandleLike h ⇒ h → HttpVersion → IO ()
+hPutHttpVersion !h !v
+    = case v of
+        -- 頻出するので高速化
+        HttpVersion 1 0 → hPutBS h "HTTP/1.0"
+        HttpVersion 1 1 → hPutBS h "HTTP/1.1"
+        -- 一般の場合
+        HttpVersion !maj !min
+            → do hPutBS   h "HTTP/"
+                 hPutStr  h (show maj)
+                 hPutChar h '.'
+                 hPutStr  h (show min)