----------------------------------------------------------------

sendRsp :: Connection
        -> H.HttpVersion
        -> H.Status
        -> H.ResponseHeaders
        -> Rsp
        -> IO ()
sendRsp conn ver s0 hs0 (RspFile path mPart mRange isHead hook) = do
    ex <- fileRange s0 hs path mPart mRange
    case ex of
        Left _ex ->
#ifdef WARP_DEBUG
          print _ex >>
#endif
          sendRsp conn ver s2 hs2 (RspBuilder body True)
        Right (s, hs1, beg, len)
          | len >= 0 ->
            if isHead then
                sendRsp conn ver s hs1 (RspBuilder mempty False)
              else do
                lheader <- composeHeader ver s hs1
                connSendFile conn path beg len hook [lheader]
          | otherwise -> do
            sendRsp conn ver H.status416
                (filter (\(k, _) -> k /= "content-length") hs1)
                (RspBuilder mempty True)
  where
    hs = addAcceptRanges hs0
    s2 = H.status404
    hs2 =  replaceHeader H.hContentType "text/plain; charset=utf-8" hs0
    body = fromByteString "File not found"

----------------------------------------------------------------
sendRsp :: Connection
        -> H.HttpVersion
        -> H.Status
        -> H.ResponseHeaders
        -> Rsp
        -> IO ()
sendRsp conn ver s0 hs0 (RspFile path mPart mRange isHead hook) = do
    ex <- fileRange s0 hs path mPart mRange
    case ex of
        Left _ex ->
#ifdef WARP_DEBUG
          print _ex >>
#endif
          sendRsp conn ver s2 hs2 (RspBuilder body True)
        Right (s, hs1, beg, len)
          | len >= 0 ->
            if isHead then
                sendRsp conn ver s hs1 (RspBuilder mempty False)
              else do
                lheader <- composeHeader ver s hs1
                connSendFile conn path beg len hook [lheader]
          | otherwise -> do
            sendRsp conn ver H.status416
                (filter (\(k, _) -> k /= "content-length") hs1)
                (RspBuilder mempty True)
  where
    hs = addAcceptRanges hs0
    s2 = H.status404
    hs2 =  replaceHeader H.hContentType "text/plain; charset=utf-8" hs0
    body = fromByteString "File not found"

----------------------------------------------------------------
sendRsp conn ver s0 hs0 (RspFile path mPart mRange isHead hook) = do
    ex <- fileRange s0 hs path mPart mRange
    case ex of
        Left _ex ->
#ifdef WARP_DEBUG
          print _ex >>
#endif
          sendRsp conn ver s2 hs2 (RspBuilder body True)
        Right (s, hs1, beg, len)
          | len >= 0 ->
            if isHead then
                sendRsp conn ver s hs1 (RspBuilder mempty False)
              else do
                lheader <- composeHeader ver s hs1
                connSendFile conn path beg len hook [lheader]
          | otherwise -> do
            sendRsp conn ver H.status416
                (filter (\(k, _) -> k /= "content-length") hs1)
                (RspBuilder mempty True)
  where
    hs = addAcceptRanges hs0
    s2 = H.status404
    hs2 =  replaceHeader H.hContentType "text/plain; charset=utf-8" hs0
    body = fromByteString "File not found"

----------------------------------------------------------------