implemented readMax
This commit is contained in:
parent
4a99e5cb0b
commit
64444bbc81
|
@ -40,6 +40,7 @@ library
|
|||
, tcp-streams >=1.0.1.1 && <1.1
|
||||
, text >=1.2.4.1 && <1.3
|
||||
, tls
|
||||
, transformers
|
||||
, x509
|
||||
default-language: Haskell2010
|
||||
autogen-modules: Paths_gemcap
|
||||
|
@ -65,6 +66,7 @@ test-suite gemcap-test
|
|||
, tcp-streams >=1.0.1.1 && <1.1
|
||||
, text >=1.2.4.1 && <1.3
|
||||
, tls
|
||||
, transformers
|
||||
, x509
|
||||
default-language: Haskell2010
|
||||
autogen-modules: Paths_gemcap
|
||||
|
|
|
@ -31,6 +31,7 @@ dependencies:
|
|||
- io-streams
|
||||
- network
|
||||
- tls
|
||||
- transformers
|
||||
- x509
|
||||
|
||||
library:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -20,7 +20,7 @@ License along with this program. If not, see
|
|||
|
||||
-}
|
||||
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
|
||||
|
||||
module Network.Gemini.Capsule.InternalSpec (spec) where
|
||||
|
||||
|
@ -28,7 +28,7 @@ import qualified Data.ByteString as BS
|
|||
import Data.Char (ord)
|
||||
import Data.Connection (Connection (..))
|
||||
import System.IO.Streams (nullInput, unRead)
|
||||
import Test.Hspec (Spec, context, describe, shouldReturn, xit)
|
||||
import Test.Hspec (Spec, context, describe, it, shouldReturn, xit)
|
||||
|
||||
import Network.Gemini.Capsule.Types
|
||||
import Network.Gemini.Capsule.Internal
|
||||
|
@ -89,24 +89,42 @@ strFromConnSpec = describe "strFromConn" $ mapM_
|
|||
, ( "non-ASCII", 100, mkInConn ["\xc3\xa9\r\n"], Just "\xe9" )
|
||||
]
|
||||
|
||||
mkInConn :: [BS.ByteString] -> IO (Connection a)
|
||||
mkInConn bss = do
|
||||
s <- nullInput
|
||||
mapM_ (`unRead` s) (reverse bss)
|
||||
return sampleConnection { source = s }
|
||||
|
||||
sampleConnection :: Connection a
|
||||
sampleConnection = Connection
|
||||
{ source = undefined
|
||||
, send = const $ return ()
|
||||
, close = return ()
|
||||
, connExtraInfo = undefined
|
||||
}
|
||||
|
||||
readMaxSpec :: Spec
|
||||
readMaxSpec = describe "readMax" $ return ()
|
||||
readMaxSpec = describe "readMax" $ mapM_
|
||||
( \(desc, maxLen, ioConn, expect) -> context desc $
|
||||
it ("should return " ++ show expect) $ do
|
||||
conn <- ioConn
|
||||
readMax maxLen conn `shouldReturn` expect
|
||||
)
|
||||
|
||||
-- description, max length, connection, expected
|
||||
[ ( "single input", 1024, singleConn, Just singleBS )
|
||||
, ( "multi input", 1024, multiConn, Just multiBS )
|
||||
, ( "long input", longLen, longConn, Just longBS )
|
||||
, ( "too long", pred longLen, longConn, Nothing )
|
||||
, ( "empty input", 1024, mkInConn [], Just "" )
|
||||
]
|
||||
|
||||
where
|
||||
singleConn = mkInConn ["foo"]
|
||||
multiConn = mkInConn ["foo", "bar", "baz"]
|
||||
longConn = mkInConn [longBS]
|
||||
longLen = BS.length longBS
|
||||
singleBS = "foo"
|
||||
multiBS = "foobarbaz"
|
||||
longBS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
stripCRLFSpec :: Spec
|
||||
stripCRLFSpec = describe "stripCRLF" $ return ()
|
||||
|
||||
mkInConn :: [BS.ByteString] -> IO (Connection ())
|
||||
mkInConn bss = do
|
||||
source <- nullInput
|
||||
mapM_ (`unRead` source) (reverse bss)
|
||||
let
|
||||
send = const $ return ()
|
||||
close = return ()
|
||||
connExtraInfo = ()
|
||||
return Connection {..}
|
||||
|
||||
--jl
|
||||
|
|
Loading…
Reference in New Issue
Block a user