From 1e32b5e123aa1689505472bdbfcbd897eac41977 Mon Sep 17 00:00:00 2001
From: Andrzej Surdej <Andrzej_Surdej@comcast.com>
Date: Thu, 6 Feb 2025 12:40:25 +0100
Subject: [PATCH] [http1][chunked] Report an error in case of unexpected stream
 end

Report an error when chunked encoding transfer is incomplete
instead of silently inoring it so app is aware of broken connection.
With previous implementation there was no way for app to tell
if chunked transfer was completed or silently broken after reading
full data chunk

Fixes #153
Fixes #381

Conflict:Context adapt
Reference:https://gitlab.gnome.org/GNOME/libsoup/-/commit/1e32b5e123aa1689505472bdbfcbd897eac41977
---
 libsoup/soup-body-input-stream.c | 35 ++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/libsoup/soup-body-input-stream.c b/libsoup/soup-body-input-stream.c
index 06323ae..fb02bdb 100644
--- a/libsoup/soup-body-input-stream.c
+++ b/libsoup/soup-body-input-stream.c
@@ -163,13 +163,15 @@ again:
 			fstream, metabuf, sizeof (metabuf),
 			"\r\n", 2, blocking, TRUE,
 			&got_line, cancellable, error);
-		if (nread <= 0)
+		if (nread < 0)
 			return nread;
 
-		if (!got_line) {
-			g_set_error_literal (error, G_IO_ERROR,
-					     G_IO_ERROR_PARTIAL_INPUT,
-					     _("Connection terminated unexpectedly"));
+		if (nread == 0 || !got_line) {
+			if (error && *error == NULL) {
+				g_set_error_literal (error, G_IO_ERROR,
+						     G_IO_ERROR_PARTIAL_INPUT,
+						     _("Connection terminated unexpectedly"));
+			}
 			return -1;
 		}
 
@@ -198,12 +200,14 @@ again:
 			metabuf, sizeof (metabuf),
 			"\r\n", 2, blocking, TRUE,
 			&got_line, cancellable, error);
-		if (nread <= 0)
+		if (nread < 0)
 			return nread;
-		if (!got_line) {
-			g_set_error_literal (error, G_IO_ERROR,
-					     G_IO_ERROR_PARTIAL_INPUT,
-					     _("Connection terminated unexpectedly"));
+		if (nread == 0 || !got_line) {
+			if (error && *error == NULL) {
+				g_set_error_literal (error, G_IO_ERROR,
+						     G_IO_ERROR_PARTIAL_INPUT,
+						     _("Connection terminated unexpectedly"));
+			}
 			return -1;
 		}
 
@@ -215,9 +219,18 @@ again:
 			fstream, metabuf, sizeof (metabuf),
 			"\r\n", 2, blocking, TRUE,
 			&got_line, cancellable, error);
-		if (nread <= 0)
+		if (nread < 0)
 			return nread;
 
+		if (nread == 0) {
+			if (error && *error == NULL) {
+				g_set_error_literal (error, G_IO_ERROR,
+						     G_IO_ERROR_PARTIAL_INPUT,
+						     _("Connection terminated unexpectedly"));
+			}
+			return -1;
+		}
+
 		if (nread == 2 && strncmp (metabuf, "\r\n", nread) == 0) {
 			bistream->priv->chunked_state = SOUP_BODY_INPUT_STREAM_STATE_DONE;
 			bistream->priv->eof = TRUE;
-- 
2.43.0