| 1 |
{-# LANGUAGE OverloadedStrings #-} |
| 2 |
|
| 3 |
module Network.HTTP.Date.Formatter (formatHTTPDate) where |
| 4 |
|
| 5 |
import Data.ByteString.Char8 () |
| 6 |
import Data.ByteString.Internal |
| 7 |
import Data.Word |
| 8 |
import Foreign.ForeignPtr |
| 9 |
import Foreign.Ptr |
| 10 |
import Foreign.Storable |
| 11 |
import Network.HTTP.Date.Types |
| 12 |
|
| 13 |
---------------------------------------------------------------- |
| 14 |
|
| 15 |
-- | Generating HTTP Date in RFC1123 style. |
| 16 |
-- |
| 17 |
-- >>> formatHTTPDate defaultHTTPDate {hdYear = 1994, hdMonth = 11, hdDay = 15, hdHour = 8, hdMinute = 12, hdSecond = 31, hdWkday = 2} |
| 18 |
-- "Tue, 15 Nov 1994 08:12:31 GMT" |
| 19 |
|
| 20 |
formatHTTPDate :: HTTPDate -> ByteString |
| 21 |
formatHTTPDate hd = |
| 22 |
unsafeCreate 29 $ \ptr -> do |
| 23 |
cpy3 ptr weekDays (3 * w) |
| 24 |
poke (ptr `plusPtr` 3) comma |
| 25 |
poke (ptr `plusPtr` 4) spc |
| 26 |
int2 (ptr `plusPtr` 5) d |
| 27 |
poke (ptr `plusPtr` 7) spc |
| 28 |
cpy3 (ptr `plusPtr` 8) months (3 * m) |
| 29 |
poke (ptr `plusPtr` 11) spc |
| 30 |
int4 (ptr `plusPtr` 12) y |
| 31 |
poke (ptr `plusPtr` 16) spc |
| 32 |
int2 (ptr `plusPtr` 17) h |
| 33 |
poke (ptr `plusPtr` 19) colon |
| 34 |
int2 (ptr `plusPtr` 20) n |
| 35 |
poke (ptr `plusPtr` 22) colon |
| 36 |
int2 (ptr `plusPtr` 23) s |
| 37 |
poke (ptr `plusPtr` 25) spc |
| 38 |
poke (ptr `plusPtr` 26) (71 :: Word8) |
| 39 |
poke (ptr `plusPtr` 27) (77 :: Word8) |
| 40 |
poke (ptr `plusPtr` 28) (84 :: Word8) |
| 41 |
where |
| 42 |
y = hdYear hd |
| 43 |
m = hdMonth hd |
| 44 |
d = hdDay hd |
| 45 |
h = hdHour hd |
| 46 |
n = hdMinute hd |
| 47 |
s = hdSecond hd |
| 48 |
w = hdWkday hd |
| 49 |
cpy3 :: Ptr Word8 -> ForeignPtr Word8 -> Int -> IO () |
| 50 |
cpy3 ptr p o = withForeignPtr p $ \fp -> |
| 51 |
memcpy ptr (fp `plusPtr` o) 3 |
| 52 |
|
| 53 |
---------------------------------------------------------------- |
| 54 |
|
| 55 |
int2 :: Ptr Word8 -> Int -> IO () |
| 56 |
int2 ptr n |
| 57 |
| n < 10 = do |
| 58 |
poke ptr zero |
| 59 |
poke (ptr `plusPtr` 1) (i2w8 n) |
| 60 |
| otherwise = do |
| 61 |
poke ptr (i2w8 (n `quot` 10)) |
| 62 |
poke (ptr `plusPtr` 1) (i2w8 (n `rem` 10)) |
| 63 |
|
| 64 |
int4 :: Ptr Word8 -> Int -> IO () |
| 65 |
int4 ptr n0 = do |
| 66 |
let (n1,x1) = n0 `quotRem` 10 |
| 67 |
(n2,x2) = n1 `quotRem` 10 |
| 68 |
(x4,x3) = n2 `quotRem` 10 |
| 69 |
poke ptr (i2w8 x4) |
| 70 |
poke (ptr `plusPtr` 1) (i2w8 x3) |
| 71 |
poke (ptr `plusPtr` 2) (i2w8 x2) |
| 72 |
poke (ptr `plusPtr` 3) (i2w8 x1) |
| 73 |
|
| 74 |
i2w8 :: Int -> Word8 |
| 75 |
i2w8 n = fromIntegral n + zero |
| 76 |
|
| 77 |
---------------------------------------------------------------- |
| 78 |
|
| 79 |
months :: ForeignPtr Word8 |
| 80 |
months = let (PS p _ _) = "___JanFebMarAprMayJunJulAugSepOctNovDec" in p |
| 81 |
|
| 82 |
weekDays :: ForeignPtr Word8 |
| 83 |
weekDays = let (PS p _ _) = "___MonTueWedThuFriSatSun" in p |
| 84 |
|
| 85 |
---------------------------------------------------------------- |
| 86 |
|
| 87 |
spc :: Word8 |
| 88 |
spc = 32 |
| 89 |
|
| 90 |
comma :: Word8 |
| 91 |
comma = 44 |
| 92 |
|
| 93 |
colon :: Word8 |
| 94 |
colon = 58 |
| 95 |
|
| 96 |
zero :: Word8 |
| 97 |
zero = 48 |