]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/HttpVersion.hs
Use base64-bytestring instead of dataenc
[Lucu.git] / Network / HTTP / Lucu / HttpVersion.hs
index 38d0e5b81ad49767621098c06bb499f347bae175..d48f6ec8c58f3d5009c3038ed500eb4e863e5003 100644 (file)
@@ -1,4 +1,8 @@
--- #prune
+{-# LANGUAGE
+    BangPatterns
+  , UnicodeSyntax
+  #-}
+{-# OPTIONS_HADDOCK prune #-}
 
 -- |Manipulation of HTTP version string.
 module Network.HTTP.Lucu.HttpVersion
@@ -8,13 +12,13 @@ module Network.HTTP.Lucu.HttpVersion
     )
     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.HandleLike
 import           Network.HTTP.Lucu.Parser
-import           System.IO
+import           Prelude hiding (min)
 
 -- |@'HttpVersion' major minor@ represents \"HTTP\/major.minor\".
-data HttpVersion = HttpVersion Int Int
+data HttpVersion = HttpVersion !Int !Int
                    deriving (Eq)
 
 instance Show HttpVersion where
@@ -30,16 +34,28 @@ instance Ord HttpVersion where
 
 
 httpVersionP :: Parser HttpVersion
-httpVersionP = do string "HTTP/"
-                  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
+httpVersionP = string "HTTP/"
+               >>
+               -- 頻出するので高速化
+               choice [ string "1.0" >> return (HttpVersion 1 0)
+                      , string "1.1" >> return (HttpVersion 1 1)
+                        -- 一般の場合
+                      , do 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 (C8.pack "HTTP/1.0")
+        HttpVersion 1 1 -> hPutBS h (C8.pack "HTTP/1.1")
+        -- 一般の場合
+        HttpVersion !maj !min
+            -> do hPutBS   h (C8.pack "HTTP/")
+                  hPutStr  h (show maj)
+                  hPutChar h '.'
+                  hPutStr  h (show min)