implemented readMax

This commit is contained in:
2021-11-19 20:58:41 -05:00
parent 4a99e5cb0b
commit 64444bbc81
4 changed files with 58 additions and 19 deletions

View File

@@ -40,10 +40,15 @@ module Network.Gemini.Capsule.Internal (
stripCRLF
) where
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Maybe (MaybeT (..))
import qualified Data.ByteString as BS
import Data.Connection (Connection)
import Data.ByteString.Builder (Builder, byteString, toLazyByteString)
import qualified Data.ByteString.Lazy as BSL
import Data.Connection (Connection, source)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8')
import System.IO.Streams as S
import Network.Gemini.Capsule.Encoding
import Network.Gemini.Capsule.Types
@@ -99,11 +104,24 @@ readMax
-> Connection a
-- ^ the 'Connection' to read from
-> IO (Maybe BS.ByteString)
readMax = undefined
readMax maxLen conn = do
let src = source conn
runMaybeT $
BS.pack . BSL.unpack . toLazyByteString
<$> readLoop maxLen src
-- | Strips the CR/LF characters from the end of a string, retuning
-- Nothing if they are not present
stripCRLF :: String -> Maybe String
stripCRLF = undefined
readLoop :: Int -> S.InputStream BS.ByteString -> MaybeT IO Builder
readLoop maxLen src =
lift (S.read src) >>= \case
Nothing -> return mempty
Just bs -> let len = BS.length bs in
if len > maxLen
then MaybeT $ return Nothing
else (byteString bs <>) <$> readLoop (maxLen - len) src
--jl