implemented readMax
This commit is contained in:
parent
4a99e5cb0b
commit
64444bbc81
|
@ -40,6 +40,7 @@ library
|
||||||
, tcp-streams >=1.0.1.1 && <1.1
|
, tcp-streams >=1.0.1.1 && <1.1
|
||||||
, text >=1.2.4.1 && <1.3
|
, text >=1.2.4.1 && <1.3
|
||||||
, tls
|
, tls
|
||||||
|
, transformers
|
||||||
, x509
|
, x509
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
autogen-modules: Paths_gemcap
|
autogen-modules: Paths_gemcap
|
||||||
|
@ -65,6 +66,7 @@ test-suite gemcap-test
|
||||||
, tcp-streams >=1.0.1.1 && <1.1
|
, tcp-streams >=1.0.1.1 && <1.1
|
||||||
, text >=1.2.4.1 && <1.3
|
, text >=1.2.4.1 && <1.3
|
||||||
, tls
|
, tls
|
||||||
|
, transformers
|
||||||
, x509
|
, x509
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
autogen-modules: Paths_gemcap
|
autogen-modules: Paths_gemcap
|
||||||
|
|
|
@ -31,6 +31,7 @@ dependencies:
|
||||||
- io-streams
|
- io-streams
|
||||||
- network
|
- network
|
||||||
- tls
|
- tls
|
||||||
|
- transformers
|
||||||
- x509
|
- x509
|
||||||
|
|
||||||
library:
|
library:
|
||||||
|
|
|
@ -40,10 +40,15 @@ module Network.Gemini.Capsule.Internal (
|
||||||
stripCRLF
|
stripCRLF
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Control.Monad.Trans.Class (lift)
|
||||||
|
import Control.Monad.Trans.Maybe (MaybeT (..))
|
||||||
import qualified Data.ByteString as BS
|
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 qualified Data.Text as T
|
||||||
import Data.Text.Encoding (decodeUtf8')
|
import Data.Text.Encoding (decodeUtf8')
|
||||||
|
import System.IO.Streams as S
|
||||||
|
|
||||||
import Network.Gemini.Capsule.Encoding
|
import Network.Gemini.Capsule.Encoding
|
||||||
import Network.Gemini.Capsule.Types
|
import Network.Gemini.Capsule.Types
|
||||||
|
@ -99,11 +104,24 @@ readMax
|
||||||
-> Connection a
|
-> Connection a
|
||||||
-- ^ the 'Connection' to read from
|
-- ^ the 'Connection' to read from
|
||||||
-> IO (Maybe BS.ByteString)
|
-> 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
|
-- | Strips the CR/LF characters from the end of a string, retuning
|
||||||
-- Nothing if they are not present
|
-- Nothing if they are not present
|
||||||
stripCRLF :: String -> Maybe String
|
stripCRLF :: String -> Maybe String
|
||||||
stripCRLF = undefined
|
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
|
--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
|
module Network.Gemini.Capsule.InternalSpec (spec) where
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ import qualified Data.ByteString as BS
|
||||||
import Data.Char (ord)
|
import Data.Char (ord)
|
||||||
import Data.Connection (Connection (..))
|
import Data.Connection (Connection (..))
|
||||||
import System.IO.Streams (nullInput, unRead)
|
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.Types
|
||||||
import Network.Gemini.Capsule.Internal
|
import Network.Gemini.Capsule.Internal
|
||||||
|
@ -89,24 +89,42 @@ strFromConnSpec = describe "strFromConn" $ mapM_
|
||||||
, ( "non-ASCII", 100, mkInConn ["\xc3\xa9\r\n"], Just "\xe9" )
|
, ( "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 :: 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 :: Spec
|
||||||
stripCRLFSpec = describe "stripCRLF" $ return ()
|
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
|
--jl
|
||||||
|
|
Loading…
Reference in New Issue
Block a user