]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/HttpVersion.hs
Better name-rewriting engine
[Lucu.git] / Network / HTTP / Lucu / HttpVersion.hs
index 4531c837782ef9b6eda9edd4849e3771f2b0b0a1..4466f1ecda8959aa102e76e6137cd57c8b53e420 100644 (file)
@@ -1,23 +1,23 @@
 {-# LANGUAGE
-    BangPatterns
-  , OverloadedStrings
+    OverloadedStrings
   , UnicodeSyntax
   #-}
-{-# OPTIONS_HADDOCK prune #-}
-
--- |Manipulation of HTTP version string.
+-- |An internal module for HTTP version numbers.
 module Network.HTTP.Lucu.HttpVersion
     ( HttpVersion(..)
-    , httpVersionP
-    , hPutHttpVersion
+    , printHttpVersion
+    , httpVersion
     )
     where
-import Control.Monad.Unicode
+import Control.Applicative
+import Control.Applicative.Unicode
+import Data.Ascii (AsciiBuilder)
+import qualified Data.Ascii as A
 import Data.Attoparsec.Char8
-import Network.HTTP.Lucu.HandleLike
+import Data.Monoid.Unicode
 import Prelude hiding (min)
 
--- |@'HttpVersion' major minor@ represents \"HTTP\/major.minor\".
+-- |An HTTP version consists of major and minor versions.
 data HttpVersion
     = HttpVersion !Int !Int
       deriving (Eq, Show)
@@ -30,26 +30,22 @@ 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
-                      ]
-
-hPutHttpVersion ∷ HandleLike h ⇒ h → HttpVersion → IO ()
-hPutHttpVersion !h !v
+-- |Convert an 'HttpVersion' to 'AsciiBuilder'.
+printHttpVersion ∷ HttpVersion → AsciiBuilder
+printHttpVersion 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)
+        -- Optimisation for special cases.
+        HttpVersion 1 0 → A.toAsciiBuilder "HTTP/1.0"
+        HttpVersion 1 1 → A.toAsciiBuilder "HTTP/1.1"
+        -- General (but almost never stumbling) cases.
+        HttpVersion maj min
+            → A.toAsciiBuilder "HTTP/" ⊕
+              A.toAsciiBuilder (A.unsafeFromString $ show maj) ⊕
+              A.toAsciiBuilder "." ⊕
+              A.toAsciiBuilder (A.unsafeFromString $ show min)
+
+-- |'Parser' for an 'HttpVersion'.
+httpVersion ∷ Parser HttpVersion
+httpVersion = string "HTTP/"
+              *>
+              (HttpVersion <$> decimal ⊛ (char '.' *> decimal))