this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Revision:
96:f5ed273881af
Parent:
95:290859010c8c
Child:
98:65c2333a38b6
--- a/source/WiFiManager.cpp	Wed Apr 17 18:11:45 2019 +0000
+++ b/source/WiFiManager.cpp	Fri Apr 19 08:51:58 2019 +0000
@@ -183,8 +183,10 @@
         setNextCommand(cmd->wifi_cmd);
 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
         _aT2WiFimPool->free(cmd);
+        cmd = NULL;
 #else
         free(cmd);
+        cmd = NULL;
 #endif
     }
     return true;
@@ -450,6 +452,7 @@
 {
     // free memory after processing
     _aT2WiFiDatamPool->free(data_msg);
+    data_msg = NULL;
 }
 
 
@@ -518,7 +521,7 @@
     }
     printf("[WIFI-MAN] Connecting to network ssid = %s passwd = %s  security = %s \r\n", 
                                                     wifi_config.ssid, 
-                                                    wifi_config.pass, 
+                                                    "****************", 
                                                     sec2str(wifi_config.security));
     error = network->connect(wifi_config.ssid,
                      wifi_config.pass,
@@ -537,7 +540,11 @@
 void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
 {    
 
+    printf("before call to new at_data_msg_t \n");
+    print_memory_info();
     at_data_resp = new at_data_msg_t;
+    printf("after call to new at_data_msg_t \n");
+    print_memory_info();
     at_data_resp->at_resp = at_cmd;
     size_t bufSize = sizeof(at_data_resp->buffer);
     int pos = 0;
@@ -583,11 +590,40 @@
     // do status line
     numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(), 
                                                      http_response->get_status_message().c_str());
+    printf("after getting HTTP status line \n");
+    print_memory_info();
     msgPtr += numChars;
-    for (size_t ix = 0; ix < http_response->get_headers_length(); ix++) {
-        numChars = sprintf(msgPtr, "%s: %s\r\n", 
-                           http_response->get_headers_fields()[ix]->c_str(), 
-                           http_response->get_headers_values()[ix]->c_str());
+    //vector<string*> hdrFields(http_response->get_headers_fields());
+    //hdrFields.reserve(http_response->get_headers_length());
+    //printf("after defining HTTP header fields vector \r\n");
+    //hdrFields = http_response->get_headers_fields();
+    //printf("after getting HTTP header fields \n");
+    //print_memory_info();
+    //vector<string*> hdrValues(http_response->get_headers_values());
+    //printf("after getting HTTP header values \n");
+    //print_memory_info();
+    int hdrsLen = http_response->get_headers_length();
+    printf("after getting HTTP headers length = %d\n", hdrsLen);
+    print_memory_info();
+    for (size_t ix = 0; ix < hdrsLen; ix++) {
+        int sLen = http_response->get_headers_fields()[ix]->size()+1;
+        printf("before creating allocation HTTP headers field [size = %d] \n", sLen);
+        char * hdrField = (char *) malloc(sLen);
+        printf("after creating allocation HTTP headers field \n");
+        print_memory_info();
+        std::strcpy (hdrField, http_response->get_headers_fields()[ix]->c_str());
+        printf("after getting HTTP headers field copy \n");
+        print_memory_info();
+        char * hdrValue = new char [http_response->get_headers_values()[ix]->size()+1];
+        std::strcpy (hdrValue, http_response->get_headers_values()[ix]->c_str());
+        numChars = sprintf(msgPtr, "%s: %s\r\n", hdrField, hdrValue);
+        //numChars = sprintf(msgPtr, "%s: %s\r\n", 
+        //                   http_response->get_headers_fields()[ix]->c_str(), 
+        //                   http_response->get_headers_values()[ix]->c_str());
+        printf("after call #%d to get http header field:values \r\n", ix);
+        print_memory_info();
+        free(hdrField);
+        delete hdrField;
         msgPtr += numChars;
     }
     numChars = sprintf(msgPtr, "\r\n");
@@ -693,11 +729,16 @@
     return true;
 }
 #define TESTING_HTTPS
+//#define DONT_USE_TLS_SOCKET
 void WiFiManager::createHttpsRequest()
 {
     // reset chunk #;
     chunkNum = 0;
+#ifdef MIX_HDR_AND_BODY
     http_response_hdr_sent = false;
+#else
+    http_response_hdr_sent = true;
+#endif    
     printf("\n[WIFI MAN] Http Request received:\n");
     http_req_cfg = (http_request_t *) data_msg->buffer;
     printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
@@ -767,10 +808,10 @@
                  free_DataMsg();
                  return;
             }
-            printf("[create https] TLS connection successful for https site :  %s\n", host);
+            //printf("[create https] TLS connection successful for https site :  %s\n", host);
+            printf("after call to createTLSconnection \n");
+            print_memory_info();
         }        
-        printf("after call to createTLSconnection \n");
-        print_memory_info();
         // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
         //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
         //_wmutex.lock();
@@ -796,17 +837,21 @@
             http_response = https_request->send(NULL, 0);
         }
         else{
+            printf("about to setup https headers\r\n");
             setHttpsHeader("Host", http_req_cfg->hostName);
             setHttpsHeader("Accept", http_req_cfg->AcceptVal);
             setHttpsHeader("Content-Type", http_req_cfg->contentType);
             setHttpsHeader("Content-Length", http_req_cfg->contentLen);
+            printf("https headers setup - about to send request\r\n");
+            //_wmutex.lock();
             http_response = https_request->send(http_req_cfg->body, bodyLen);
+            //_wmutex.unlock();
         }
 #else
         setHttpsHeader("Host", http_req_cfg->hostName);
         setHttpsHeader("Accept", http_req_cfg->AcceptVal);
-        setHttpHeader("Content-Type", http_req_cfg->contentType);
-        setHttpHeader("Content-Length", http_req_cfg->contentLen);
+        setHttpsHeader("Content-Type", http_req_cfg->contentType);
+        setHttpsHeader("Content-Length", http_req_cfg->contentLen);
         http_response = https_request->send(http_req_cfg->body, bodyLen);
 #endif        
         
@@ -817,20 +862,29 @@
             mbedtls_strerror(https_request->get_error(), buf, 100);
             printf("HttpsRequest failed (error code %s)\n", buf);
             delete https_request; // free the memory
+            https_request = NULL;
             https_connection_active = false; // reset true whenever connection fails
             delete socket;
+            socket = NULL;
             free_DataMsg();
             return;
         }
         https_connection_active = true; // set true whenever connection succeeds
         printf("\n----- HTTPS POST response -----\r\n");
-   }
-   if(http_response != NULL){
-    //return_response(http_response);
-    delete http_response; // free the response memory
     }
-     free_DataMsg();
-    //delete https_request; // free the request memory
+    if(http_response != NULL){
+
+#ifndef MIX_HDR_AND_BODY
+        return_response(http_response);
+#endif    
+        //delete http_response; // free the response memory
+        //http_response = NULL;
+        //printf("deleted http_response\r\n");
+    }
+    free_DataMsg();
+    delete https_request; // free the request & response memory
+    printf("deleted https_request\r\n");
+    https_request = NULL;
 }
 
 void WiFiManager::createHttpRequest(http_method method,