Customized HTTPClient

Fork of HTTPClient by ban4jp -

Revision:
21:725b3b105601
Parent:
20:f020c92bd1a2
--- a/HTTPClient.cpp	Sat Nov 22 08:54:27 2014 +0000
+++ b/HTTPClient.cpp	Tue Feb 10 12:26:21 2015 +0000
@@ -69,7 +69,7 @@
 
 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
 {
-  return connect(url, HTTP_GET, NULL, pDataIn, timeout);
+  return connect(url, HTTP_GET, NULL, pDataIn, HTTP_CLIENT_DEFAULT_TIMEOUT);
 }
 
 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
@@ -103,6 +103,7 @@
   do{ \
     if(ret) { \
       m_sock.close(); \
+      printf("Connection error (%d)", ret); \
       ERR("Connection error (%d)", ret); \
       return HTTP_CONN; \
     } \
@@ -111,6 +112,7 @@
 #define PRTCL_ERR() \
   do{ \
     m_sock.close(); \
+    printf("Protocol error"); \
     ERR("Protocol error"); \
     return HTTP_PRTCL; \
   } while(0)
@@ -134,6 +136,7 @@
   HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
   if(res != HTTP_OK)
   {
+    printf("not HTTP_OK");
     ERR("parseURL returned %d", res);
     return res;
   }
@@ -144,29 +147,37 @@
   }
 
   DBG("Scheme: %s", scheme);
-  DBG("Host: %s", host);
-  DBG("Port: %d", port);
+  printf("Host: %s\n", host);
+  printf("Port: %d\n", port);
   DBG("Path: %s", path);
 
   //Connect
+  printf("Connecting socket to server ");
   DBG("Connecting socket to server");
   int ret = m_sock.connect(host, port);
   if (ret < 0)
   {
     m_sock.close();
+    printf("not Socket to server");
     ERR("Could not connect");
     return HTTP_CONN;
   }
 
   //Send request
+  printf("Sending request ");
   DBG("Sending request");
   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
+  
+  //snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
+  snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s:%d\r\n", meth, path, host, port); //Write request
+  
+  
   ret = send(buf);
   if(ret)
   {
     m_sock.close();
+    printf("Could not write request ");
     ERR("Could not write request");
     return HTTP_CONN;
   }
@@ -174,6 +185,7 @@
   //Send all headers
 
   //Send default headers
+  printf("Sending headers");
   DBG("Sending headers");
   if( pDataOut != NULL )
   {
@@ -268,8 +280,10 @@
       }
     }
   }
+  printf("/* HTTP send1 : %s */", buf);
   
   //Receive response
+  printf(" Receiving response ");
   DBG("Receiving response");
   ret = recv(buf, 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
   CHECK_CONN_ERR(ret);
@@ -301,6 +315,8 @@
     break;
   }
 
+  printf("/HTTP answer1 : %s/", buf);
+
   int crlfPos = crlfPtr - buf;
   buf[crlfPos] = '\0';
 
@@ -310,9 +326,11 @@
   {
     buf[13] = '\0';
   }
+  printf("/HTTP answer2 : %s/", buf);
   if( sscanf(buf, "HTTP/%*d.%*d %d", &m_httpResponseCode) != 1 ) //Kludge for newlib nano
   {
     //Cannot match string, error
+    printf("Not a correct HTTP answer : %s", buf);
     ERR("Not a correct HTTP answer : %s", buf);
     PRTCL_ERR();
   }
@@ -320,12 +338,13 @@
   if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) )
   {
     //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers 
+    printf("Response code %d", m_httpResponseCode);
     WARN("Response code %d", m_httpResponseCode);
     PRTCL_ERR();
   }
 
   DBG("Reading headers");
-
+  printf(" Reading headers ");
   memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
   trfLen -= (crlfPos + 2);
 
@@ -380,6 +399,10 @@
     int n = 0;
     
     char* keyEnd = strchr(buf, ':');
+    
+    printf("keyEnd: ");
+    printf(buf);
+    printf(" ");
     if(keyEnd != NULL)
     {
       *keyEnd = '\0';
@@ -427,6 +450,7 @@
     }
     else
     {
+      printf("Could not parse header");
       ERR("Could not parse header");
       PRTCL_ERR();
     }
@@ -434,6 +458,7 @@
   }
 
   //Receive data
+  printf("Receiving data");
   DBG("Receiving data");
   while(true)
   {