Skip to content

Commit 8f983cc

Browse files
rlubosdanieldegrasse
authored andcommitted
net: lib: http_client: Fix end of message detection
HTTP 1.1 server has two ways of indicating the message body length - either by providing the Content Length header, or by closing the connection when the entire body has been transmitted. The second method didn't work with Zephyr's HTTP client implementation, as EOF on a socket was treated as an error condition. Therefore, if no Content Length was provided by the server, such transfers would always end up with ECONNRESET error. In order to fix this, we need to notify the parser about the EOF on a socket when connection is closed. It is the parser role to determine whether the EOF was expected in current state (by marking end of message flag) or not (by setting an error). Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
1 parent b3ccee4 commit 8f983cc

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

subsys/net/lib/http/http_client.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ static int http_wait_data(int sock, struct http_request *req, const k_timepoint_
520520
} else if (fds[0].revents & ZSOCK_POLLIN) {
521521
received = zsock_recv(sock, req->internal.response.recv_buf + offset,
522522
req->internal.response.recv_buf_len - offset, 0);
523-
if (received == 0) {
524-
/* Connection closed */
523+
if (received == 0 && total_received == 0) {
524+
/* Connection closed, no data received */
525525
goto closed;
526526
} else if (received < 0) {
527527
ret = -errno;
@@ -534,9 +534,12 @@ static int http_wait_data(int sock, struct http_request *req, const k_timepoint_
534534
/* Initialize the data length with the received data length. */
535535
req->internal.response.data_len = offset;
536536

537+
/* In case of EOF on a socket, indicate this by passing
538+
* 0 length to the parser.
539+
*/
537540
processed = http_parser_execute(
538541
&req->internal.parser, &req->internal.parser_settings,
539-
req->internal.response.recv_buf, offset);
542+
req->internal.response.recv_buf, received > 0 ? offset : 0);
540543

541544
if (processed > offset) {
542545
LOG_ERR("HTTP parser error, too much data consumed");

0 commit comments

Comments
 (0)