Skip to content

Commit d3beb2b

Browse files
committed
app: http: Cancel active request on AT#XCLOSE
sm_at_httpc_socket_closed() finds any active request for the fd, sends a cancel status URC (#XHTTPCSTAT: fd,-1,...) to the host, and frees the request. Without this, the stale http_request remained alive after the close. Jira: SM-342 Signed-off-by: Juha Ylinen <juha.ylinen@nordicsemi.no>
1 parent 9c1e98d commit d3beb2b

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

app/src/sm_at_httpc.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,20 @@ bool sm_at_httpc_poll_event(int fd, uint8_t events)
956956
return (req && req->need_rearm_pollin);
957957
}
958958

959+
void sm_at_httpc_socket_closed(int fd)
960+
{
961+
k_mutex_lock(&http_mutex, K_FOREVER);
962+
struct http_request *req = find_request(fd);
963+
964+
if (req) {
965+
LOG_WRN("HTTP %d: socket closed with active request (state=%d); cleaning up",
966+
fd, req->state);
967+
http_send_cancel_status(req);
968+
http_close_request(req);
969+
}
970+
k_mutex_unlock(&http_mutex);
971+
}
972+
959973
/* Build HTTP request headers and send them synchronously for streaming POST/PUT */
960974
static int http_send_request_headers(struct http_request *req)
961975
{

app/src/sm_at_httpc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
*/
2626
bool sm_at_httpc_poll_event(int fd, uint8_t events);
2727

28+
/**
29+
* @brief Notify HTTP client that a socket has been closed (e.g. via AT#XCLOSE).
30+
*
31+
* If a request is active on @p fd, it is cancelled: a cancel status URC
32+
* (#XHTTPCSTAT with -1 status) is sent to the host and the request is freed.
33+
* No-op if no request is active on @p fd.
34+
*
35+
* @param fd Socket file descriptor that was closed.
36+
*/
37+
void sm_at_httpc_socket_closed(int fd);
38+
2839
/** @} */
2940

3041
#endif /* SM_AT_HTTPC_H_ */

app/src/sm_at_socket.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ static int do_socket_close(struct sm_socket *sock)
715715

716716
rsp_send("\r\n#XCLOSE: %d,%d\r\n", sock->fd, ret);
717717

718+
#if defined(CONFIG_SM_HTTPC)
719+
sm_at_httpc_socket_closed(sock->fd);
720+
#endif
718721
init_socket(sock);
719722

720723
return ret;

doc/app/at_httpc.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ In hex mode (``<format>=1``), the data is ``2×<length>`` lower-case ASCII hex c
159159
* The ``<total_bytes>`` parameter is an integer.
160160
On successful completion, failure, or timeout, it contains the total number of response body bytes received by the HTTP client.
161161
For chunked transfer encoding this includes the raw framing bytes (chunk-size lines, ``\r\n`` separators, and the final ``0\r\n\r\n`` terminator).
162-
On cancel (``status_code=-1`` from ``AT#XHTTPCCANCEL``), it contains the number of response body bytes already delivered to the host.
162+
On cancel (``status_code=-1`` from ``AT#XHTTPCCANCEL`` or ``AT#XCLOSE``), it contains the number of response body bytes already delivered to the host.
163163
* The ``<connection_close>`` parameter is an integer.
164164
It is ``1`` when the server includes a ``Connection: close`` header in its response, indicating that the TCP connection will be closed after this response.
165165
It is ``0`` otherwise (keep-alive connection).
@@ -483,6 +483,10 @@ Syntax
483483

484484
An unsolicited ``#XHTTPCSTAT: <handle>,-1,<total_bytes>,<connection_close>`` notification is emitted after cancellation, where ``<total_bytes>`` is the number of response body bytes already delivered to the host.
485485

486+
.. note::
487+
488+
Closing the socket with ``AT#XCLOSE`` while a request is active also cancels the request and emits ``#XHTTPCSTAT: <handle>,-1,<total_bytes>,<connection_close>`` before the socket is freed.
489+
486490
Example
487491
~~~~~~~
488492

0 commit comments

Comments
 (0)