on master help sign in
implementing packStatus.
commit204eeb885f5cc1058f8688f099f84d6f87043f2f authorKazu Yamamoto <kazu@iij.ad.jp> dateThu 7 Apr 2016, 13:32 (11 years ago) parent6e3347f
2 files changed+27−2
warp/Network/Wai/Handler/Warp/HTTP2/HPACK.hs+1−1 warp/Network/Wai/Handler/Warp/PackInt.hs+26−1
warp/Network/Wai/Handler/Warp/HTTP2/HPACK.hs+1−1
@@ -37,7 +37,7 @@ hpackEncodeHeader Context{..} buf siz ii settings s (ths0,tbl) = do
3737 !ths1 = addHeader tokenServer defServer tbl ths0
3838 date <- getDate ii
3939 let !ths2 = addHeader tokenDate date tbl ths1
40− !status = packIntegral $ H.statusCode s
40+ !status = packStatus s
4141 !ths3 = (tokenStatus, status) : ths2
4242 encodeTokenHeader buf siz strategy True encodeDynamicTable ths3
4343
warp/Network/Wai/Handler/Warp/PackInt.hs+26−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE OverloadedStrings, BangPatterns #-}
22
33module Network.Wai.Handler.Warp.PackInt where
44
@@ -7,6 +7,11 @@ import Data.ByteString.Internal (ByteString(..), unsafeCreate)
77import Data.Word8 (Word8)
88import Foreign.Ptr (Ptr, plusPtr)
99import Foreign.Storable (poke)
10+import qualified Network.HTTP.Types as H
11+
12+-- $setup
13+-- >>> import Data.ByteString.Char8 as B
14+-- >>> import Test.QuickCheck (Large(..))
1015
1116-- |
1217--
@@ -29,3 +34,23 @@ packIntegral n = unsafeCreate len go0
2934
3035{-# SPECIALIZE packIntegral :: Int -> ByteString #-}
3136{-# SPECIALIZE packIntegral :: Integer -> ByteString #-}
37+
38+-- |
39+--
40+-- >>> packStatus H.status200
41+-- "200"
42+-- >>> packStatus H.preconditionFailed412
43+-- "412"
44+
45+packStatus :: H.Status -> ByteString
46+packStatus status = unsafeCreate 3 $ \p -> do
47+ poke p (toW8 r2)
48+ poke (p `plusPtr` 1) (toW8 r1)
49+ poke (p `plusPtr` 2) (toW8 r0)
50+ where
51+ toW8 :: Int -> Word8
52+ toW8 n = 48 + fromIntegral n
53+ !s = fromIntegral $ H.statusCode status
54+ (!q0,!r0) = s `divMod` 10
55+ (!q1,!r1) = q0 `divMod` 10
56+ !r2 = q1 `mod` 10