File: File.hs

package info (click to toggle)
haskell-http-semantics 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 144 kB
  • sloc: haskell: 1,071; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Network.HTTP.Semantics.File (
    -- * Position read
    PositionRead,
    PositionReadMaker,
    Sentinel (..),
    defaultPositionReadMaker,
) where

import System.IO

import Network.ByteOrder
import Network.HTTP.Semantics

-- | Position read for files.
type PositionRead = FileOffset -> ByteCount -> Buffer -> IO ByteCount

-- | Making a position read and its closer.
type PositionReadMaker = FilePath -> IO (PositionRead, Sentinel)

--- | Manipulating a file resource.
data Sentinel
    = -- | Closing a file resource. Its refresher is automatiaclly generated by
      --   the internal timer.
      Closer (IO ())
    | -- | Refreshing a file resource while reading.
      --   Closing the file must be done by its own timer or something.
      Refresher (IO ())

-- | Position read based on 'Handle'.
defaultPositionReadMaker :: PositionReadMaker
defaultPositionReadMaker file = do
    hdl <- openBinaryFile file ReadMode
    return (pread hdl, Closer $ hClose hdl)
  where
    pread :: Handle -> PositionRead
    pread hdl off bytes buf = do
        hSeek hdl AbsoluteSeek $ fromIntegral off
        fromIntegral <$> hGetBufSome hdl buf (fromIntegral bytes)