NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Revision:
5:dd63a1e02b1b
Parent:
3:95e0bc00a1bb
Child:
9:c79fa4034f5b
--- a/services/http/client/HTTPClient.cpp	Fri Jul 09 14:46:47 2010 +0000
+++ b/services/http/client/HTTPClient.cpp	Tue Jul 27 15:59:42 2010 +0000
@@ -41,7 +41,7 @@
 {
   setTimeout(HTTP_REQUEST_TIMEOUT);
   m_buf = new char[CHUNK_SIZE];
-  DBG("\r\nNew HTTPClient %p\r\n",this);
+  DBG("New HTTPClient %p\n",this);
 }
 
 HTTPClient::~HTTPClient()
@@ -62,7 +62,7 @@
   decStr += ":";
   decStr += password;
   auth.append( Base64::encode(decStr) );
-  DBG("\r\nAuth str is %s\r\n", auth.c_str());
+  DBG("Auth str is %s\n", auth.c_str());
   m_reqHeaders["Authorization"] = auth; 
 }
 
@@ -164,7 +164,7 @@
     if( m_state == HTTP_DONE ) 
     {
       //All data has been read, close w/ success :)
-      DBG("\r\nDone :)!\r\n");
+      DBG("Done :)!\n");
       onResult(HTTP_OK);
       close();
     }
@@ -267,7 +267,7 @@
     m_server.setPort( HTTP_PORT );
   }
   
-  DBG("\r\nURL parsed,\r\nHost: %s\r\nPort: %d\r\nPath: %s\r\n", url.getHost().c_str(), url.getPort(), url.getPath().c_str());
+  DBG("URL parsed,\r\nHost: %s\r\nPort: %d\r\nPath: %s\n", url.getHost().c_str(), url.getPort(), url.getPath().c_str());
   
   IpAddr ip;
   if( url.getHostIp(&ip) )
@@ -277,11 +277,11 @@
   }
   else
   {
-    DBG("\r\nDNS Query...\r\n");
+    DBG("DNS Query...\n");
     m_pDnsReq = new DNSRequest();
     m_pDnsReq->setOnReply(this, &HTTPClient::onDNSReply);
     m_pDnsReq->resolve(&m_server);
-    DBG("\r\nHTTPClient : DNSRequest %p\r\n", m_pDnsReq);
+    DBG("HTTPClient : DNSRequest %p\n", m_pDnsReq);
   }
   
 }
@@ -289,7 +289,7 @@
 void HTTPClient::connect() //Start Connection
 {
   resetTimeout();
-  DBG("\r\nConnecting...\r\n");
+  DBG("Connecting...\n");
   m_pTCPSocket->connect(m_server);
 }
 
@@ -352,7 +352,7 @@
     m_state = HTTP_DONE;
     return;
   }
-  DBG("\r\nReading response...\r\n");
+  DBG("Reading response...\n");
   int len = 0;
   do
   {
@@ -360,7 +360,7 @@
     {
       if(m_dataLen==0)
       {
-        DBG("\r\nReading chunk length...\r\n");
+        DBG("Reading chunk length...\n");
         //New block
         static char chunkHeader[16];
         //We use m_dataPos to retain the read position in chunkHeader, it has been set to 0 before the first call of readData()
@@ -370,19 +370,19 @@
           if( chunkHeader[strlen(chunkHeader)-1] == 0x0d )
           {
             sscanf(chunkHeader, "%x%*[^\r\n]", &m_dataLen);
-            DBG("\r\nChunk length is %d\r\n", m_dataLen);
+            DBG("Chunk length is %d\n", m_dataLen);
             m_dataPos = 0;
           }
           else
           {
             //Wait for end of line
-            DBG("\r\nWait for CRLF\r\n");
+            DBG("Wait for CRLF\n");
             return;
           }
         }
         else
         {
-          DBG("\r\nWait for data\r\n");
+          DBG("Wait for data\n");
           //Wait for data
           return;
         }
@@ -408,17 +408,17 @@
     {  
       if(m_dataPos >= m_dataLen)
       {
-        DBG("\r\nChunk read, wait for CRLF\r\n");
+        DBG("Chunk read, wait for CRLF\n");
         char chunkTail[3];
         m_dataPos += readLine(chunkTail, 3);
       }
       
       if(m_dataPos >= m_dataLen + 1) //1 == strlen("\n"),
       {
-        DBG("\r\nEnd of chunk\r\n");
+        DBG("End of chunk\n");
         if(m_dataLen==0)
         {
-          DBG("\r\nEnd of file\r\n");
+          DBG("End of file\n");
           //End of file
           m_state = HTTP_DONE; //Done
         }
@@ -432,7 +432,7 @@
   
   if(!m_dataChunked && (m_dataPos >= m_dataLen)) //All Data has been received
   {
-    DBG("\r\nEnd of file\r\n");
+    DBG("End of file\n");
     m_state = HTTP_DONE; //Done
   }
 }
@@ -450,7 +450,7 @@
   {
     //Write chunk header
     char chunkHeader[16];
-    sprintf(chunkHeader, "%d\r\n", len);
+    sprintf(chunkHeader, "%d\n", len);
     int ret = m_pTCPSocket->send(chunkHeader, strlen(chunkHeader));
     if(ret < 0)//Error
     {
@@ -462,7 +462,7 @@
   m_dataPos+=len;
   if( m_dataChunked )
   {
-    m_pTCPSocket->send("\r\n", 2); //Chunk terminating CRLF
+    m_pTCPSocket->send("", 2); //Chunk terminating CRLF
   }
   if( ( !m_dataChunked && (m_dataPos >= m_dataLen) )
     || ( m_dataChunked && !len ) ) //All Data has been sent
@@ -474,11 +474,11 @@
   
 void HTTPClient::onTCPSocketEvent(TCPSocketEvent e)
 {
-  DBG("\r\nEvent %d in HTTPClient::onTCPSocketEvent()\r\n", e);
+  DBG("Event %d in HTTPClient::onTCPSocketEvent()\n", e);
 
   if(m_closed)
   {
-    DBG("\r\nWARN: Discarded\r\n");
+    DBG("WARN: Discarded\n");
     return;
   }
 
@@ -501,18 +501,18 @@
           m_dataChunked = true;
           m_dataPos = 0;
           m_dataLen = 0; 
-          DBG("\r\nEncoding is chunked, Content-Type is %s\r\n", m_respHeaders["Content-Type"].c_str() );
+          DBG("Encoding is chunked, Content-Type is %s\n", m_respHeaders["Content-Type"].c_str() );
         }
         else
         {    
           m_dataChunked = false;
           int len = 0;
-          //DBG("\r\nPreparing read... len = %s\r\n", m_respHeaders["Content-Length"].c_str());
+          //DBG("Preparing read... len = %s\n", m_respHeaders["Content-Length"].c_str());
           sscanf(m_respHeaders["Content-Length"].c_str(), "%d", &len);
           m_pDataIn->setDataLen( len );
           m_dataPos = 0;
           m_dataLen = len; 
-          DBG("\r\nContent-Length is %d, Content-Type is %s\r\n", len, m_respHeaders["Content-Type"].c_str() );
+          DBG("Content-Length is %d, Content-Type is %s\n", len, m_respHeaders["Content-Type"].c_str() );
         }
         m_pDataIn->setDataType( m_respHeaders["Content-Type"] );
       }
@@ -528,7 +528,7 @@
  //All data has been read, close w/ success :)
     if( m_state == HTTP_DONE ) 
     {
-      DBG("\r\nDone :)!\r\n");
+      DBG("Done :)!\n");
       onResult(HTTP_OK);
     }
     break;
@@ -584,7 +584,7 @@
   case TCPSOCKET_CONRST:
   case TCPSOCKET_CONABRT:
   case TCPSOCKET_ERROR:
-    DBG("\r\nConnection error.\r\n");
+    DBG("Connection error.\n");
     onResult(HTTP_CONN);
   case TCPSOCKET_DISCONNECTED:
     //There might still be some data available for reading
@@ -593,7 +593,7 @@
     {
       onResult(HTTP_CONN);
     }
-    DBG("\r\nConnection closed by remote host.\r\n");
+    DBG("Connection closed by remote host.\n");
     break;
   }
 }
@@ -602,18 +602,18 @@
 {
   if(m_closed)
   {
-    DBG("\r\nWARN: Discarded\r\n");
+    DBG("WARN: Discarded\n");
     return;
   }
   
   if( r != DNS_FOUND )
   {
-    DBG("\r\nCould not resolve hostname.\r\n");
+    DBG("Could not resolve hostname.\n");
     onResult(HTTP_DNS);
     return;
   }
   
-  DBG("\r\nDNS Resolved to %d.%d.%d.%d.\r\n",m_server.getIp()[0],m_server.getIp()[1],m_server.getIp()[2],m_server.getIp()[3]);
+  DBG("DNS Resolved to %d.%d.%d.%d.\n",m_server.getIp()[0],m_server.getIp()[1],m_server.getIp()[2],m_server.getIp()[3]);
   //If no error, m_server has been updated by m_pDnsReq so we're set to go !
   m_pDnsReq->close();
   delete m_pDnsReq;
@@ -633,7 +633,7 @@
 
 void HTTPClient::onTimeout() //Connection has timed out
 {
-  DBG("\r\nTimed out.\n");
+  DBG("Timed out.\n");
   onResult(HTTP_TIMEOUT);
   close();
 }
@@ -671,7 +671,7 @@
       if( sscanf(line, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 )
       {
         //Cannot match string, error
-        DBG("\r\nNot a correct HTTP answer : %s\r\n", line);
+        DBG("Not a correct HTTP answer : %s\n", line);
         onResult(HTTP_PRTCL);
         close();
         return false;
@@ -679,7 +679,7 @@
       
       if(m_httpResponseCode != 200)
       {
-        DBG("\r\nResponse: error code %d\r\n", m_httpResponseCode);
+        DBG("Response: error code %d\n", m_httpResponseCode);
         HTTPResult res = HTTP_ERROR;
         switch(m_httpResponseCode)
         {
@@ -696,12 +696,12 @@
         close();
         return false;
       }
-      DBG("\r\nResponse OK\r\n");
+      DBG("Response OK\n");
     }
     else
     {
       //Empty packet, weird!
-      DBG("\r\nEmpty packet!\r\n");
+      DBG("Empty packet!\n");
       onResult(HTTP_PRTCL);
       close();
       return false;
@@ -714,7 +714,7 @@
     m_dataLen = 0;
     if( readLen <= 2 ) //if == 1 or 2, it is an empty line = end of headers
     {
-      DBG("\r\nAll headers read.\r\n");
+      DBG("All headers read.\n");
       m_state = HTTP_READ_DATA;
       break;
     }
@@ -723,11 +723,11 @@
       m_dataLen = readLen;//Sets data length available in buffer
       return false;
     }
-    //DBG("\r\nHeader : %s\r\n", line);
+    //DBG("Header : %s\n", line);
     int n = sscanf(line, "%[^:] : %[^\r\n]", key, value);
     if ( n == 2 )
     {
-      DBG("\r\nRead header : %s: %s\r\n", key, value);
+      DBG("Read header : %s: %s\n", key, value);
       m_respHeaders[key] = value;
     }
     //TODO: Impl n==1 case (part 2 of previous header)
@@ -742,19 +742,19 @@
   const char* HTTP_METH_STR[] = {"GET", "POST", "HEAD"};
   
   //Req
-  sprintf(line, "%s %s HTTP/1.1\r\nHost: %s\r\n", HTTP_METH_STR[m_meth], m_path.c_str(), m_server.getName()); //Write request
+  sprintf(line, "%s %s HTTP/1.1\r\nHost: %s\n", HTTP_METH_STR[m_meth], m_path.c_str(), m_server.getName()); //Write request
   m_pTCPSocket->send(line, strlen(line));
-  DBG("\r\nRequest: %s\r\n", line);
+  DBG("Request: %s\n", line);
   
-  DBG("\r\nWriting headers:\r\n");
+  DBG("Writing headers:\n");
   map<string,string>::iterator it;
   for( it = m_reqHeaders.begin(); it != m_reqHeaders.end(); it++ )
   {
-    sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
-    DBG("\r\n%s", line);
+    sprintf(line, "%s: %s\n", (*it).first.c_str(), (*it).second.c_str() );
+    DBG("%s", line);
     m_pTCPSocket->send(line, strlen(line));
   }
-  m_pTCPSocket->send("\r\n",2); //End of head
+  m_pTCPSocket->send("",2); //End of head
   m_state = HTTP_WRITE_DATA;
   return true;
 }