@@ -106,6 +106,8 @@ static struct http_url *http_find_url(struct httpd *httpd, const char *path) {
106106 return NULL ;
107107}
108108
109+ static void http_close_client (struct http_client * hc );
110+
109111void http_send_response_headers (struct http_client * hc , int status_code , const char * status_text , const char * content_type , size_t content_length )
110112{
111113 char txt_response [HTTP_TX_BUF_LEN ];
@@ -133,10 +135,7 @@ void http_send_response_headers(struct http_client *hc, int status_code, const c
133135 }
134136 if (rc <= 0 ) {
135137 /* Error – close connection */
136- wolfSSL_free (hc -> ssl );
137- hc -> ssl = NULL ;
138- wolfIP_sock_close (hc -> httpd -> ipstack , hc -> client_sd );
139- hc -> client_sd = 0 ;
138+ http_close_client (hc );
140139 }
141140}
142141
@@ -150,10 +149,7 @@ void http_send_response_body(struct http_client *hc, const void *body, size_t le
150149 rc = wolfIP_sock_send (hc -> httpd -> ipstack , hc -> client_sd , body , len , 0 );
151150
152151 if (rc <= 0 ) {
153- wolfSSL_free (hc -> ssl );
154- hc -> ssl = NULL ;
155- wolfIP_sock_close (hc -> httpd -> ipstack , hc -> client_sd );
156- hc -> client_sd = 0 ;
152+ http_close_client (hc );
157153 }
158154}
159155
@@ -169,6 +165,21 @@ static int http_write_response(struct http_client *hc, const void *buf, size_t l
169165 return wolfIP_sock_send (s , hc -> client_sd , buf , len , 0 );
170166}
171167
168+ static void http_close_client (struct http_client * hc )
169+ {
170+ if (!hc )
171+ return ;
172+
173+ if (hc -> ssl ) {
174+ wolfSSL_shutdown (hc -> ssl );
175+ wolfSSL_CleanupIO_wolfIP (hc -> ssl );
176+ wolfSSL_free (hc -> ssl );
177+ hc -> ssl = NULL ;
178+ }
179+ wolfIP_sock_close (hc -> httpd -> ipstack , hc -> client_sd );
180+ hc -> client_sd = 0 ;
181+ }
182+
172183void http_send_response_chunk (struct http_client * hc , const void * chunk , size_t len ) {
173184 char txt_chunk [8 ];
174185 memset (txt_chunk , 0 , sizeof (txt_chunk ));
@@ -178,10 +189,7 @@ void http_send_response_chunk(struct http_client *hc, const void *chunk, size_t
178189 if ((http_write_response (hc , txt_chunk , strlen (txt_chunk )) <= 0 ) ||
179190 (http_write_response (hc , chunk , len ) <= 0 ) ||
180191 (http_write_response (hc , "\r\n" , 2 ) <= 0 )) {
181- wolfSSL_free (hc -> ssl );
182- hc -> ssl = NULL ;
183- wolfIP_sock_close (hc -> httpd -> ipstack , hc -> client_sd );
184- hc -> client_sd = 0 ;
192+ http_close_client (hc );
185193 }
186194}
187195
@@ -194,10 +202,7 @@ void http_send_response_chunk_end(struct http_client *hc) {
194202 else
195203 rc = wolfIP_sock_send (hc -> httpd -> ipstack , hc -> client_sd , "0\r\n\r\n" , 5 , 0 );
196204 if (rc <= 0 ) {
197- wolfSSL_free (hc -> ssl );
198- hc -> ssl = NULL ;
199- wolfIP_sock_close (hc -> httpd -> ipstack , hc -> client_sd );
200- hc -> client_sd = 0 ;
205+ http_close_client (hc );
201206 }
202207}
203208
@@ -483,19 +488,14 @@ static void http_recv_cb(int sd, uint16_t event, void *arg) {
483488 return ;
484489
485490fail_close :
486- if (hc -> ssl ) {
487- wolfSSL_free (hc -> ssl );
488- hc -> ssl = NULL ;
489- }
490- wolfIP_sock_close (hc -> httpd -> ipstack , sd );
491+ http_close_client (hc );
491492 /* wolfIP_sock_close on an ESTABLISHED socket only starts the active close
492493 * (FIN_WAIT_1) and returns -EAGAIN; the socket lingers, still carrying this
493494 * callback and its arg. Once we zero client_sd the slot is reused by the
494495 * next accept, so the lingering socket's callback_arg would dangle onto a
495496 * different live connection's state. Deregister it here so a late segment
496497 * on the half-closed socket can no longer fire http_recv_cb. */
497498 wolfIP_register_callback (hc -> httpd -> ipstack , sd , NULL , NULL );
498- hc -> client_sd = 0 ;
499499}
500500
501501static void http_accept_cb (int sd , uint16_t event , void * arg ) {
0 commit comments