Making serial requests using the same SSL context

I just want to make many requests using the same TLS/SSL context, in other words, I’d like to use the HTTP 1.1 keep-alive/persistent connections to make several requests using the same TLS connection, instead of creating a TLS connection for every request.

I can use the ssl_client1.c sample normally with HTTP 1.0 to make one or multiple request, but the problem happens when I attempt to make two requests under the same TLS connection (using HTTP 1.1), basically by doing the following:

for (int i = 0; i < 3; i++)
{
    /*
    * 3. Write the GET request
    */
    for (int i = 0; i < 3; i++)
    {
        mbedtls_printf("  > Write to server:");
        fflush(stdout);

        len = sprintf((char *) buf, GET_REQUEST);

        while ((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0) {
            if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
                mbedtls_printf(" failed\n  ! mbedtls_ssl_write returned %d\n\n", ret);
                goto exit;
            }
        }

        len = ret;
        mbedtls_printf(" %d bytes written\n\n%s", len, (char *) buf);

        /*
        * 7. Read the HTTP response
        */
        mbedtls_printf("  < Read from server:");
        fflush(stdout);

        do {
            len = sizeof(buf) - 1;
            memset(buf, 0, sizeof(buf));
            ret = mbedtls_ssl_read(&ssl, buf, len);

            if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
                continue;
            }

            if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
                break;
            }

            if (ret < 0) {
                mbedtls_printf("failed\n  ! mbedtls_ssl_read returned %d\n\n", ret);
                break;
            }

            if (ret == 0) {
                mbedtls_printf("\n\nEOF\n\n");
                break;
            }

            len = ret;
            mbedtls_printf(" %d bytes read\n\n%s", len, (char *) buf);

            // Check if the response is complete
            if (strstr((char *)buf, "\r\n\r\n")) {
                break; // Break if we reached the end of the response
            }
        } while (1);
    }
}

What happens is: I can make the first request perfectly, but after that, I stay “blocked” in a read loop. Why? Because, the client cannot know that the server have already finished to send the response of the first request. Then, I only start the second request after receiving a close notify message from the server, which takes ~60s in the case of the postman-echo.com test server.

To summarize, I just would like to have an example of how to make many requests using the same TLS connection.

I didn’t understand how the mbedTLS ssl_client1.c sample works with many requests over the same TLS connection - how the client will now that the server finished it’s response, given that the server doesn’t send EOF, besides waiting for the close notify? I mean, how can I work around the fact that I’m facing now, which is the client is blocked waiting for the server close notify, and only after that I start the next request.