]> granicus.if.org Git - esp-idf/commitdiff
https example: Use correct pattern around mbedtls_ssl_write()
authorAngus Gratton <angus@espressif.com>
Fri, 18 Aug 2017 04:27:36 +0000 (14:27 +1000)
committerAngus Gratton <gus@projectgus.com>
Fri, 18 Aug 2017 07:15:11 +0000 (17:15 +1000)
mbedtls_ssl_write() will always write the request here in one go,
but it's good to have correct patterns in examples.

examples/protocols/https_request/main/https_request_example_main.c

index c12f7c18d484977f9d2050e9f04f4b42230cb9f2..a6cac56ca9eb37edd92d7e5ec433a2a801409b74 100644 (file)
@@ -258,17 +258,20 @@ static void https_get_task(void *pvParameters)
 
         ESP_LOGI(TAG, "Writing HTTP request...");
 
-        while((ret = mbedtls_ssl_write(&ssl, (const unsigned char *)REQUEST, strlen(REQUEST))) <= 0)
-        {
-            if(ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
-            {
+        size_t written_bytes = 0;
+        do {
+            ret = mbedtls_ssl_write(&ssl,
+                                    (const unsigned char *)REQUEST + written_bytes,
+                                    strlen(REQUEST) - written_bytes);
+            if (ret >= 0) {
+                ESP_LOGI(TAG, "%d bytes written", ret);
+                written_bytes += ret;
+            } else if (ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret != MBEDTLS_ERR_SSL_WANT_READ) {
                 ESP_LOGE(TAG, "mbedtls_ssl_write returned -0x%x", -ret);
                 goto exit;
             }
-        }
+        } while(written_bytes < strlen(REQUEST));
 
-        len = ret;
-        ESP_LOGI(TAG, "%d bytes written", len);
         ESP_LOGI(TAG, "Reading HTTP response...");
 
         do