Fixed custom headers and Basic authorization, added support for redirection, functional file download interface can be used for SW updates and more.

Dependents:   Sample_HTTPClient Sample_HTTPClient LWM2M_NanoService_Ethernet LWM2M_NanoService_Ethernet ... more

Fork of HTTPClient by Vincent Wochnik

More recent changes - added iCal processing.

Derivative of a derivative, however this one works when it comes to supplying Basic authorization to access a protected resource. Some additional changes to the debug interface to clean it up for consistency with many other components I have.

Revision:
36:a5c13e512b78
Parent:
35:d9e2d1c96b75
Child:
37:74c1c4527f70
--- a/HTTPClient.cpp	Thu Aug 06 11:11:31 2015 +0000
+++ b/HTTPClient.cpp	Tue Jan 26 11:44:35 2016 +0000
@@ -17,6 +17,12 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+//#include "Utility.h"            // private memory manager
+#ifndef UTILITY_H
+#define swMalloc malloc         // use the standard
+#define swFree free
+#endif
+
 //Debug is disabled by default
 //#define DEBUG "HTCL"
 #include <cstdio>
@@ -56,19 +62,21 @@
 
 HTTPClient::~HTTPClient()
 {
-
+    if (m_location) // if any redirection was involved, clean up after it.
+        swFree(m_location);
+    m_location = NULL;      // this step isn't necessary...
 }
 
 void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
 {
 #if 1
     if (m_basicAuthUser)
-        free(m_basicAuthUser);
-    m_basicAuthUser = (char *)malloc(strlen(user)+1);
+        swFree(m_basicAuthUser);
+    m_basicAuthUser = (char *)swMalloc(strlen(user)+1);
     strcpy(m_basicAuthUser, user);
     if (m_basicAuthPassword)
-        free(m_basicAuthPassword);
-    m_basicAuthPassword = (char *)malloc(strlen(password)+1);
+        swFree(m_basicAuthPassword);
+    m_basicAuthPassword = (char *)swMalloc(strlen(password)+1);
     strcpy(m_basicAuthPassword, password);
 #else
     m_basicAuthUser = user;
@@ -193,10 +201,10 @@
             return HTTP_CONN;
         }
 
-        //Send request
+        // Send request
         DBG("Sending request");
         const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":"";
-        snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s:%d\r\nConnection: keep-alive\r\n", meth, path, host, port); //Write request
+        snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s:%d\r\n", meth, path, host, port); //Write request
         INFO(" buf{%s}", buf);
         ret = send(buf);
         if (ret) {
@@ -221,11 +229,10 @@
             }
         }
 
-        //Send all headers
+        // Send all headers
         INFO("Send custom header(s) %d (if any)", m_nCustomHeaders);
         for (size_t nh = 0; nh < m_nCustomHeaders * 2; nh+=2) {
-            INFO("hdr[%d] %s:", nh, m_customHeaders[nh]);
-            INFO("        %s", m_customHeaders[nh+1]);
+            INFO("hdr[%2d] %s: %s", nh, m_customHeaders[nh], m_customHeaders[nh+1]);
             snprintf(buf, sizeof(buf), "%s: %s\r\n", m_customHeaders[nh], m_customHeaders[nh+1]);
             ret = send(buf);
             if (ret) {
@@ -235,7 +242,7 @@
                 ERR("Could not write request");
                 return HTTP_CONN;
             }
-            INFO(" hdr %d", ret);
+            INFO("   send() returned %d", ret);
         }
 
         //Send default headers
@@ -383,9 +390,10 @@
                 } else if( !strcmp(key, "Content-Type") ) {
                     pDataIn->setDataType(value);
                 } else if ( !strcmp(key, "Location") ) {
-                    if (m_location)
-                        free(m_location);
-                    m_location = (char *)malloc(strlen(value)+1);
+                    if (m_location) {
+                        swFree(m_location);
+                    }
+                    m_location = (char *)swMalloc(strlen(value)+1);
                     if (m_location) {
                         strcpy(m_location,value);
                         url = m_location;
@@ -393,6 +401,8 @@
                         m_sock.close();
                         takeRedirect = true;
                         break;   // exit the while(true) header to follow the redirect
+                    } else {
+                        ERR("Could not allocate memory for %s", key);
                     }
                 }