Response L.ByteString
これは怠惰とは関係ありませんが、Simple モジュールでResponse BodyReader
得られる と TLS モジュールで得られるの違いによるものです。
BodyReader
aが であることに気付きましたIO ByteString
。しかし、特に、次のバイトのチャンクで毎回繰り返すことができるアクションです。ファイルの最後にある場合を除いて、null バイト文字列を送信しないというプロトコルに従います。(BodyReader
と呼ばれていたかもしれませんChunkGetter
)。bip
以下はあなたが書いたようなものです: から / を抽出した後、BodyReader
それを実行して最初のチャンクを取得し、それを出力します。しかし、より多くを得るためにアクションを繰り返さないので、この場合、創世記の最初のいくつかの章を見るだけです. 必要なのは、以下のようにチャンクを使い果たすためのループです。これにより、欽定訳聖書全体がコンソールに流出します。IO ByteString
Response
bop
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Client
import Network.HTTP.Client.TLS
import qualified Data.ByteString.Char8 as B
main = bip
-- main = bop
bip = do
manager <- newManager tlsManagerSettings
request <- parseRequest "https://raw.githubusercontent.com/michaelt/kjv/master/kjv.txt"
withResponse request manager $ \response -> do
putStrLn "The status code was: "
print (responseStatus response)
chunk <- responseBody response
B.putStrLn chunk
bop = do
manager <- newManager tlsManagerSettings
request <- parseRequest "https://raw.githubusercontent.com/michaelt/kjv/master/kjv.txt"
withResponse request manager $ \response -> do
putStrLn "The status code was: "
print (responseStatus response)
let loop = do
chunk <- responseBody response
if B.null chunk
then return ()
else B.putStr chunk >> loop
loop
ループは、eof を表す空の文字列を取得するまで、より多くのチャンクを取得するために戻り続けるため、ターミナルでは黙示録の最後まで出力されます。
これは単純な動作ですが、少し技術的です。BodyReader
手書きの再帰のみを使用できます。しかし、http-client
ライブラリの目的は、このようなことをhttp-conduit
可能にすることです。の結果はwithResponse
typeResponse (ConduitM i ByteString m ())
です。ConduitM i ByteString m ()
バイトストリームのコンジットタイプです。このバイト ストリームにはファイル全体が含まれます。
http-client
/http-conduit
マテリアルの元の形では、Response
このようなコンジットが含まれていました。のようなさまざまなストリーミング ライブラリで使用できるように、このBodyReader
部分は後で分解されました。http-client
pipes
簡単な例を挙げると、streaming
およびstreaming-bytestring
ライブラリの対応する http 資料ではwithHTTP
、タイプ の応答が得られますResponse (ByteString IO ())
。 ByteString IO ()
その名前が示すように、IO で発生するバイト ストリームのタイプです。ByteString Identity ()
遅延バイト文字列 (事実上、チャンクの純粋なリスト) と同等です。ByteString IO ()
この場合の will は、アポカリプスまでのバイトストリーム全体を表します。だからインポートで
import qualified Data.ByteString.Streaming.HTTP as Bytes -- streaming-utils
import qualified Data.ByteString.Streaming.Char8 as Bytes -- streaming-bytestring
プログラムは遅延バイト文字列プログラムと同じです:
bap = do
manager <- newManager tlsManagerSettings
request <- parseRequest "https://raw.githubusercontent.com/michaelt/kjv/master/kjv.txt"
Bytes.withHTTP request manager $ \response -> do
putStrLn "The status code was: "
print (responseStatus response)
Bytes.putStrLn $ responseBody response
実際、「IO からバイトを抽出する」がないため、少し単純です。
lazy_bytes <- responseStatus response
Lazy.putStrLn lazy_bytes
しかし、書くだけ
Bytes.putStrLn $ responseBody response
それらを直接「印刷」するだけです。KJV の途中から少しだけ表示したい場合は、代わりに遅延バイト文字列を使用して、次のように終了することができます。
Bytes.putStrLn $ Bytes.take 1000 $ Bytes.drop 50000 $ responseBody response
次に、アブラハムについて何かがわかります。
withHTTP
forは、マテリアルを直接streaming-bytestring
使用するために必要な再帰ループを隠しているだけです。これは、バイト文字列チャンクのストリームを として表す にあると同じであり、 と同じです。これらすべてのケースで、バイト ストリームを取得したら、手書きの再帰を使用せずに、ストリーミング IO フレームワークの典型的な方法で処理します。それらはすべてfromを使用してこれを行い、これがライブラリの主な目的でした。BodyReader
http-client
withHTTP
pipes-http
Producer ByteString IO ()
http-conduit
BodyReader
http-client