From 9c2b3964e591089660869143a717e5b570b089e4 Mon Sep 17 00:00:00 2001 From: Takayoshi Koizumi Date: Sat, 7 Nov 2026 22:39:04 +0900 Subject: [PATCH] HttpClient: Fix hanging up post request with large request body When post() is called with a large request body, startRequest() does not check the return value of write(). Since write() delegates to the underlying socket client's write() method, it may write fewer bytes than requested. If a partial write occurs, the HTTP connection may hang because the server continues waiting for the remaining data. --- src/HttpClient.cpp | 12 +++++++++++- src/HttpClient.h | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/HttpClient.cpp b/src/HttpClient.cpp index 31909d9..8749f4b 100644 --- a/src/HttpClient.cpp +++ b/src/HttpClient.cpp @@ -137,7 +137,17 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod, if (hasBody) { - write(aBody, aContentLength); + int sent, written; + sent = 0; + while (sent < aContentLength) + { + written = write(&aBody[sent], aContentLength - sent); + if (written < 0) + { + return HTTP_ERROR_SOCKET_CLIENT; + } + sent += written; + } } } diff --git a/src/HttpClient.h b/src/HttpClient.h index 3d404af..634535b 100644 --- a/src/HttpClient.h +++ b/src/HttpClient.h @@ -22,6 +22,8 @@ static const int HTTP_ERROR_TIMED_OUT =-3; // The response from the server is invalid, is it definitely an HTTP // server? static const int HTTP_ERROR_INVALID_RESPONSE =-4; +// Socket client returned an error +static const int HTTP_ERROR_SOCKET_CLIENT =-5; // Define some of the common methods and headers here // That lets other code reuse them without having to declare another copy