Skip to content

Commit

Permalink
[wip] testing chunked transfer encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 24, 2024
1 parent 0ff06cf commit 43a27d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Network/HTTP/Scheduler.idr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Data.List1
import Data.Compress.Interface
import Data.Compress.GZip
import Data.Compress.ZLib
import Data.String
import System.Concurrency

public export
record ScheduleResponse (e : Type) (m : Type -> Type) where
Expand All @@ -34,16 +36,23 @@ interface Scheduler (e : Type) (m : Type -> Type) (0 a : Type) where
channel_to_stream : (HasIO m, Decompressor c) => c -> Channel (Either (HttpError e) (Maybe (List Bits8))) ->
Stream (Of Bits8) m (Either (HttpError e) ())
channel_to_stream decomp channel = do
Right (Just content) <- liftIO $ channelGet channel
| Right Nothing =>
putStrLn "Asking for data"
result <- liftIO $ channelGet channel
putStrLn "Asked for data"
case result of
Right (Just content) => do
putStrLn $ "got " ++ show content
let Right (decompressed, newDecomp) = feed decomp content
| Left err => pure (Left $ DecompressionError err)
fromList_ decompressed *> channel_to_stream newDecomp channel
Right Nothing => do
putStrLn "Got nothing"
case done decomp of
Right [] => pure (Right ())
Right xs => pure (Left $ DecompressionError "\{show $ length xs} leftover bytes in decompression buffer")
Left err => pure (Left $ DecompressionError err)
| Left err => pure (Left err)
let Right (content, decomp) = feed decomp content
| Left err => pure (Left $ DecompressionError err)
fromList_ content *> channel_to_stream decomp channel
Left err => pure (Left err)


decompressor : List ContentEncodingScheme -> DPair Type Decompressor
decompressor [ GZip ] = MkDPair GZipState %search
Expand Down
27 changes: 27 additions & 0 deletions src/Test.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Test

import Data.Nat
import Network.HTTP
import Control.Monad.Error.Either
import Control.Monad.Error.Interface

ResultMonad : Type -> Type
ResultMonad = EitherT (HttpError ()) IO

getClient: IO (HttpClient ())
getClient = new_client certificate_ignore_check 1 1 False False

performRequest : HttpClient () -> ResultMonad (HttpResponse, Stream (Of Bits8) ResultMonad ())
performRequest client =
request client GET (url' "https://anglesharp.azurewebsites.net/Chunked") [("Authorization", "Bearer foo")] ()

silly_me = do
client <- getClient
Right (result, stream) <- runEitherT $ performRequest client
| Left e => printLn $ "Error from calling url " ++ show e
putStrLn $ show result
Right (content, _) <- runEitherT $ toList stream
| Left e => printLn $ "Error from reading response stream " ++ show e
putStrLn $ show content
putStrLn "Done"
close client

0 comments on commit 43a27d2

Please sign in to comment.