HTTP/HTTPS Client Library for the X-NUCLEO-IDW01M1v2 wifi board.

Dependents:   HTTPClient_HelloWorld_IDW01M1 wifigianluigi HTTPClient_HelloWorld_IDW01M1_Fabio_Ricezione

Fork of HTTPClient by ST Expansion SW Team

Revision:
19:17578cfdb57a
Parent:
18:277279a1891e
Child:
20:bbbfaf4cc055
--- a/HTTPClient.cpp	Wed May 07 16:48:10 2014 +0000
+++ b/HTTPClient.cpp	Mon Nov 07 17:08:02 2016 +0000
@@ -40,16 +40,16 @@
 #define MIN(x,y) (((x)<(y))?(x):(y))
 #define MAX(x,y) (((x)>(y))?(x):(y))
 
-#define CHUNK_SIZE 256
+
+#define MAX_KEY     64
+#define MAX_VALUE   64
 
 #include <cstring>
 
 #include "HTTPClient.h"
 
-HTTPClient::HTTPClient() :
-m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
+HTTPClient::HTTPClient(HTTPWiFi & _m_sock) : m_sock(_m_sock) 
 {
-
 }
 
 HTTPClient::~HTTPClient()
@@ -65,18 +65,18 @@
 }
 #endif
 
-HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
+HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT) //Blocking
 {
   return connect(url, HTTP_GET, NULL, pDataIn, timeout);
 }
 
-HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
+HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT) //Blocking
 {
   HTTPText str(result, maxResultLen);
   return get(url, &str, timeout);
 }
 
-HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
+HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT) //Blocking
 {
   return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
 }
@@ -113,6 +113,7 @@
     return HTTP_PRTCL; \
   } while(0)
 
+
 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
 { 
   m_httpResponseCode = 0; //Invalidate code
@@ -145,6 +146,8 @@
   DBG("Host: %s", host);
   DBG("Port: %d", port);
   DBG("Path: %s", path);
+// Open
+  m_sock.open(&m_sock.getWiFi());
 
   //Connect
   DBG("Connecting socket to server");
@@ -152,18 +155,19 @@
   if (ret < 0)
   {
     m_sock.close();
-    ERR("Could not connect");
+    ERR("TCP Could not connect");
     return HTTP_CONN;
   }
-
+  DBG ("TCP connected\n\r");
   //Send request
   DBG("Sending request");
-  char buf[CHUNK_SIZE];
+//  char buf[CHUNK_SIZE];
   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\r\n", meth, path, host); //Write request
   ret = send(buf);
   if(ret)
   {
+    //m_sock.close();
     m_sock.close();
     ERR("Could not write request");
     return HTTP_CONN;
@@ -298,7 +302,7 @@
     }
     break;
   }
-
+printf (" ---->> buf: %s\n\r",buf);
   int crlfPos = crlfPtr - buf;
   buf[crlfPos] = '\0';
 
@@ -322,7 +326,7 @@
     PRTCL_ERR();
   }
 
-  DBG("Reading headers");
+  DBG("=======>>>>  Reading headers");
 
   memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
   trfLen -= (crlfPos + 2);
@@ -364,14 +368,14 @@
 
     buf[crlfPos] = '\0';
 
-    char key[32];
-    char value[32];
+    char key[MAX_KEY];
+    char value[MAX_VALUE];
 
     //key[31] = '\0';
     //value[31] = '\0';
 
-    memset(key, 0, 32);
-    memset(value, 0, 32);
+    memset(key, 0, MAX_KEY);
+    memset(value, 0, MAX_VALUE);
 
     //int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
     
@@ -381,14 +385,14 @@
     if(keyEnd != NULL)
     {
       *keyEnd = '\0';
-      if(strlen(buf) < 32)
+      if(strlen(buf) < MAX_KEY)
       {
         strcpy(key, buf);
         n++;
         char* valueStart = keyEnd + 2;
         if( (valueStart - buf) < crlfPos )
         {
-          if(strlen(valueStart) < 32)
+          if(strlen(valueStart) < MAX_VALUE)
           { 
             strcpy(value, valueStart);
             n++;
@@ -574,27 +578,40 @@
 {
   DBG("Trying to read between %d and %d bytes", minLen, maxLen);
   size_t readLen = 0;
-      
+
+#ifdef LICIO
+
+#else      
   if(!m_sock.is_connected())
   {
     WARN("Connection was closed by server");
     return HTTP_CLOSED; //Connection was closed by server 
   }
-    
+#endif    
   int ret;
   while(readLen < maxLen)
   {
     if(readLen < minLen)
     {
       DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
+#ifdef LICIO
+      m_sock.set_blocking(false);
+      ret = m_sock.recv(buf + readLen, minLen - readLen);
+#else      
       m_sock.set_blocking(false, m_timeout);
       ret = m_sock.receive_all(buf + readLen, minLen - readLen);
+#endif
     }
     else
     {
       DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
+#ifdef LICIO
+      m_sock.set_blocking(false);
+      ret = m_sock.recv(buf + readLen, maxLen - readLen);
+#else      
       m_sock.set_blocking(false, 0);
       ret = m_sock.receive(buf + readLen, maxLen - readLen);
+#endif      
     }
     
     if( ret > 0)
@@ -607,6 +624,9 @@
     }
     else
     {
+#ifdef LICIO      
+//    if (ret == NSAPI_ERROR_NO_CONNECTION) { *pReadLen = readLen; return HTTP_CONN; }
+#else 
       if(!m_sock.is_connected())
       {
         ERR("Connection error (recv returned %d)", ret);
@@ -617,12 +637,16 @@
       {
         break;      
       }
+#endif
     }
-    
+#ifdef LICIO
+//    if (ret == NSAPI_ERROR_NO_CONNECTION) { *pReadLen = readLen; return HTTP_CONN; }
+#else
     if(!m_sock.is_connected())
     {
       break;
     }
+#endif
   }
   DBG("Read %d bytes", readLen);
   *pReadLen = readLen;
@@ -637,15 +661,23 @@
   }
   DBG("Trying to write %d bytes", len);
   size_t writtenLen = 0;
-    
+
+#ifdef LICIO
+#else
   if(!m_sock.is_connected())
   {
     WARN("Connection was closed by server");
     return HTTP_CLOSED; //Connection was closed by server 
   }
-  
+#endif
+#ifdef LICIO
+  m_sock.set_blocking(false);
+  int ret = m_sock.send(buf, len);
+
+#else
   m_sock.set_blocking(false, m_timeout);
   int ret = m_sock.send_all(buf, len);
+#endif
   if(ret > 0)
   {
     writtenLen += ret;
@@ -736,4 +768,4 @@
   path[pathLen] = '\0';
 
   return HTTP_OK;
-}
+}
\ No newline at end of file