]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/HttpVersion.hs
Fixed breakage on GHC 6.10.1
[Lucu.git] / Network / HTTP / Lucu / HttpVersion.hs
index 9b955d37fd9644beb912feb1808a1f2982dd97b9..c988aab3dcd99776547f61cf8a09471c09918957 100644 (file)
@@ -1,16 +1,20 @@
+{-# OPTIONS_HADDOCK prune #-}
+
+-- |Manipulation of HTTP version string.
 module Network.HTTP.Lucu.HttpVersion
     ( HttpVersion(..)
-    , httpVersionP    -- Parser HttpVersion
-    , hPutHttpVersion -- Handle -> HttpVersion -> IO ()
+    , httpVersionP
+    , hPutHttpVersion
     )
     where
 
-import qualified Data.ByteString.Lazy.Char8 as B
-import           Data.ByteString.Lazy.Char8 (ByteString)
+import qualified Data.ByteString.Char8 as C8
 import           Network.HTTP.Lucu.Parser
+import           Prelude hiding (min)
 import           System.IO
 
-data HttpVersion = HttpVersion Int Int
+-- |@'HttpVersion' major minor@ represents \"HTTP\/major.minor\".
+data HttpVersion = HttpVersion !Int !Int
                    deriving (Eq)
 
 instance Show HttpVersion where
@@ -26,16 +30,30 @@ instance Ord HttpVersion where
 
 
 httpVersionP :: Parser HttpVersion
-httpVersionP = do string "HTTP/"
-                  major <- many1 digit
-                  char '.'
-                  minor <- many1 digit
-                  return $ HttpVersion (read major) (read minor)
+httpVersionP = string "HTTP/"
+               >>
+               -- 頻出するので高速化
+               choice [ do string "1.0"
+                           return $ HttpVersion 1 0
+                      , do string "1.1"
+                           return $ HttpVersion 1 1
+                        -- 一般の場合
+                      , do major <- many1 digit
+                           char '.'
+                           minor <- many1 digit
+                           return $ HttpVersion (read major) (read minor)
+                      ]
 
 
 hPutHttpVersion :: Handle -> HttpVersion -> IO ()
-hPutHttpVersion h (HttpVersion maj min)
-    = do hPutStr  h "HTTP/"
-         hPutStr  h (show maj)
-         hPutChar h '.'
-         hPutStr  h (show min)
\ No newline at end of file
+hPutHttpVersion !h !v
+    = case v of
+        -- 頻出するので高速化
+        HttpVersion 1 0 -> C8.hPut h (C8.pack "HTTP/1.0")
+        HttpVersion 1 1 -> C8.hPut h (C8.pack "HTTP/1.1")
+        -- 一般の場合
+        HttpVersion !maj !min
+            -> do C8.hPut  h (C8.pack "HTTP/")
+                  hPutStr  h (show maj)
+                  hPutChar h '.'
+                  hPutStr  h (show min)