diff options
author | Andy Holmes <andrew.g.r.holmes@gmail.com> | 2022-08-08 13:47:11 -0700 |
---|---|---|
committer | Andy Holmes <andrew.g.r.holmes@gmail.com> | 2022-08-08 13:47:11 -0700 |
commit | a878815969063bef256295edd5dfb65cc22e51f3 (patch) | |
tree | f3c4d066f909aca49ff7c4ee6ecbabd194068e8d | |
parent | 580dec7522a11b8f14d2af554be249fe0b2b069b (diff) | |
download | gjs-a878815969063bef256295edd5dfb65cc22e51f3.tar.gz |
examples: support no content-length in http-client.js
Adjust the code path where we read the HTTP content, demonstrating
`Gio.OutputStream.splice()` which can handle the case where there is no
`content-length` header in an HTTP response.
fixes #498
-rw-r--r-- | examples/http-client.js | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/examples/http-client.js b/examples/http-client.js index 5f1d7d94..dd0a8878 100644 --- a/examples/http-client.js +++ b/examples/http-client.js @@ -6,6 +6,7 @@ import Soup from 'gi://Soup?version=3.0'; import GLib from 'gi://GLib'; +import Gio from 'gi://Gio'; const loop = GLib.MainLoop.new(null, false); @@ -18,11 +19,12 @@ const decoder = new TextDecoder(); session.send_async(message, null, null, send_async_callback); -function read_bytes_async_callback(inputStream, res) { +function splice_callback(outputStream, result) { let data; try { - data = inputStream.read_bytes_finish(res); + outputStream.splice_finish(outputStream, result); + data = outputStream.steal_as_bytes(); } catch (err) { logError(err); loop.quit(); @@ -51,8 +53,12 @@ function send_async_callback(self, res) { response_headers.foreach((name, value) => { console.log(name, ':', value); }); + const contentType_ = response_headers.get_one('content-type'); - inputStream.read_bytes_async(response_headers.get_one('content-length'), null, null, read_bytes_async_callback); + const outputStream = Gio.MemoryOutputStream.new_resizable(); + outputStream.splice_async(inputStream, + Gio.OutputStreamSpliceFlags.CLOSE_TARGET, + GLib.PRIORITY_DEFAULT, null, splice_callback); } loop.run(); |