[gjs: 1/2] examples: support no content-length in http-client.js




commit a878815969063bef256295edd5dfb65cc22e51f3
Author: Andy Holmes <andrew g r holmes gmail com>
Date:   Mon Aug 8 13:47:11 2022 -0700

    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

 examples/http-client.js | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
---
diff --git a/examples/http-client.js b/examples/http-client.js
index 5f1d7d941..dd0a8878c 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();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]