implemented high-level logic for strFromConn

This commit is contained in:
2021-11-19 19:30:54 -05:00
parent 489d0fdb78
commit 4a99e5cb0b
2 changed files with 63 additions and 12 deletions

View File

@@ -34,10 +34,16 @@ time.
module Network.Gemini.Capsule.Internal (
readURL,
sendResponse
sendResponse,
strFromConn,
readMax,
stripCRLF
) where
import qualified Data.ByteString as BS
import Data.Connection (Connection)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8')
import Network.Gemini.Capsule.Encoding
import Network.Gemini.Capsule.Types
@@ -76,6 +82,28 @@ strFromConn
-> Connection a
-- ^ The connection to read from
-> IO (Maybe String)
strFromConn = undefined
strFromConn maxLen conn = do
mbs <- readMax maxLen conn
return $ do
bs <- mbs
txt <- case decodeUtf8' bs of
Left _ -> Nothing
Right s -> Just s
stripCRLF $ T.unpack txt
-- | Reads from a connection up to a maximum number of bytes,
-- returning 'Nothing' if the limit is exceeded
readMax
:: Int
-- ^ the maximum number of bytes
-> Connection a
-- ^ the 'Connection' to read from
-> IO (Maybe BS.ByteString)
readMax = undefined
-- | Strips the CR/LF characters from the end of a string, retuning
-- Nothing if they are not present
stripCRLF :: String -> Maybe String
stripCRLF = undefined
--jl