From a4afbef893f27a2e5ede3defd948b4ab08e6b2c2 Mon Sep 17 00:00:00 2001 From: Juha Ylinen Date: Tue, 12 May 2026 13:57:32 +0300 Subject: [PATCH] app: http: Detect chunked transfer terminator in manual receive mode Add detection of the chunked transfer terminator (0\r\n\r\n) to signal end of response. Signed-off-by: Juha Ylinen --- app/src/sm_at_httpc.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/src/sm_at_httpc.c b/app/src/sm_at_httpc.c index b7b22930..478e8477 100644 --- a/app/src/sm_at_httpc.c +++ b/app/src/sm_at_httpc.c @@ -1237,6 +1237,7 @@ static int pull_data(int socket_fd, int pull_len) { struct http_request *req; int ret; + int last_sent = 0; /* Bytes forwarded to host in this call. Used for chunked EOF check */ k_mutex_lock(&http_mutex, K_FOREVER); req = find_request(socket_fd); @@ -1277,6 +1278,7 @@ static int pull_data(int socket_fd, int pull_len) if (req->recv_buf_len > 0) { memmove(req->recv_buf, req->recv_buf + send_len, req->recv_buf_len); } + last_sent = send_len; goto check_complete; } @@ -1308,6 +1310,7 @@ static int pull_data(int socket_fd, int pull_len) rsp_send("\r\n#XHTTPCDATA: %d,%d,%d\r\n", req->fd, req->bytes_sent, ret); req->bytes_sent += ret; data_send(req->pipe, req->recv_buf, ret); + last_sent = ret; check_complete: if (req->content_length > 0 && req->bytes_sent >= req->content_length) { @@ -1316,6 +1319,19 @@ static int pull_data(int socket_fd, int pull_len) return 0; } + /* For chunked transfer (no Content-Length), detect the terminal "0\r\n\r\n" + * marker in the last forwarded block. Only check when recv_buf is fully + * drained (recv_buf_len == 0): if a partial drain left bytes in the buffer, + * the terminator may still be in the remaining data, not at the end of what + * was just sent. + */ + if (req->content_length < 0 && req->recv_buf_len == 0 && + last_sent > 0 && chunked_eof(req->recv_buf, last_sent)) { + http_finish_request(req); + k_mutex_unlock(&http_mutex); + return 0; + } + k_mutex_unlock(&http_mutex); return 0; }