net: http: use shutdown(..., SHUT_RD) in receive timeout handler

So far close() was called on underlying socket when timeout has expired.
This is wrong in several ways. First of all POSIX specification of
close() does not specify what should happen on blocking recv() call and
different systems have different behaviors (e.g. Linux does not wake up
recv() caller if there was no new incoming data, while other systems
might wakeup recv() caller immediately). Another (and much more severe)
problem is that HTTP client user does not know whether underlying socket
was already closed or not after HTTP request has finished. As a result
it was not clear whether close() should be called by HTTP client user.

Use shutdown(..., SHUT_RD) in internal HTTP client implementation, so
that recv() is woken up immediately with 0 as result (which means EOF).
This will allow to gracefully handle timeouts and make it clear that it
is application responsibility to always call close() after HTTP
request (successful or not).

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
This commit is contained in:
Marcin Niestroj 2021-08-24 14:28:01 +02:00 committed by Marti Bolivar
commit ab2f616641

View file

@ -471,7 +471,7 @@ static void http_timeout(struct k_work *work)
struct http_client_internal_data *data =
CONTAINER_OF(dwork, struct http_client_internal_data, work);
(void)zsock_close(data->sock);
(void)zsock_shutdown(data->sock, ZSOCK_SHUT_RD);
}
int http_client_req(int sock, struct http_request *req,