PullRequest

Dependents:   CyaSSL-Twitter-OAuth4Tw TweetTest NetworkThermometer CyaSSL-Twitter-OAuth4Tw_arrange

Fork of HTTPClient by wolf SSL

Revision:
30:a9ecee69c6b5
Parent:
28:25b5d3720bd1
Child:
31:7fd621b83b60
--- a/HTTPClient.cpp	Wed Dec 03 05:26:32 2014 +0000
+++ b/HTTPClient.cpp	Fri Dec 05 07:03:47 2014 +0000
@@ -75,7 +75,7 @@
 static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx)
 {
     int n ;
-    
+
     wait(0.1) ;
     n = m_sock.send(buf, sz);
     if(n > 0) {
@@ -85,7 +85,8 @@
     return n ;
 }
 
-static void base64enc(char *out, const char *in) {
+static void base64enc(char *out, const char *in)
+{
     const char code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ;
     int i = 0, x = 0, l = 0;
 
@@ -110,10 +111,10 @@
 {
 
     /* CyaSSL_Debugging_ON() ; */
-    
+
     ctx = 0 ;
     ssl = 0 ;
-    SSLver = 3 ; 
+    SSLver = 3 ;
     m_basicAuthUser = NULL ;
     redirect_url = NULL ;
     redirect = 0 ;
@@ -127,7 +128,7 @@
 
 HTTPResult HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
 {
-    #define AUTHB_SIZE 128
+#define AUTHB_SIZE 128
     if((strlen(user) + strlen(password)) >= AUTHB_SIZE)
         return HTTP_ERROR ;
     m_basicAuthUser = user;
@@ -178,9 +179,9 @@
     redirect_url_size = size ;
 }
 
-HTTPResult HTTPClient::setSSLversion(int minorV) 
+HTTPResult HTTPClient::setSSLversion(int minorV)
 {
-    if((minorV>=0) && (minorV<=3)) 
+    if((minorV>=0) && (minorV<=3))
         SSLver = minorV ;
     else return HTTP_ERROR ;
     return HTTP_OK ;
@@ -216,7 +217,7 @@
         ctx = NULL ;
     }
     CyaSSL_Cleanup() ;
-} 
+}
 
 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
 {
@@ -224,7 +225,7 @@
     m_httpResponseCode = 0; //Invalidate code
     m_timeout = timeout;
     redirect = 0 ;
-    
+
     pDataIn->writeReset();
     if( pDataOut ) {
         pDataOut->readReset();
@@ -277,10 +278,18 @@
         DBG("SSLver=%d", SSLver) ;
         if(ctx == NULL) {
             switch(SSLver) {
-            case 0 : SSLmethod = CyaSSLv3_client_method() ; break ;
-            case 1 : SSLmethod = CyaTLSv1_client_method() ; break ;
-            case 2 : SSLmethod = CyaTLSv1_1_client_method() ; break ;           
-            case 3 : SSLmethod = CyaTLSv1_2_client_method() ; break ;      
+                case 0 :
+                    SSLmethod = CyaSSLv3_client_method() ;
+                    break ;
+                case 1 :
+                    SSLmethod = CyaTLSv1_client_method() ;
+                    break ;
+                case 2 :
+                    SSLmethod = CyaTLSv1_1_client_method() ;
+                    break ;
+                case 3 :
+                    SSLmethod = CyaTLSv1_2_client_method() ;
+                    break ;
             }
             ctx = CyaSSL_CTX_new((CYASSL_METHOD *)SSLmethod);
             if (ctx == NULL) {
@@ -324,13 +333,13 @@
     }
 
     wait(0.1) ;
-    
+
     //Send all headers
 
     //Send default headers
     DBG("Sending headers");
     if(m_basicAuthUser) {
-            bAuth() ; /* send out Basic Auth header */        
+        bAuth() ; /* send out Basic Auth header */
     }
     if( pDataOut != NULL ) {
         if( pDataOut->getIsChunked() ) {
@@ -459,13 +468,29 @@
                 DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
                 CHECK_CONN_ERR(ret);
                 continue;
-            } else {
-                PRTCL_ERR();
+            } else {  // Too large header. Skip to the next.
+                WARN("Header too large [%20s]. Skip to the next.\n", buf) ;
+                while(true) {
+                    ret = recv(buf, 1, CHUNK_SIZE-1, &trfLen);
+                    buf[trfLen] = '\0' ;
+                    crlfPtr = strstr(buf, "\r\n");
+                    if(crlfPtr != NULL) {
+                        crlfPos = crlfPtr - buf;
+                        memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
+                        trfLen -= (crlfPos + 2);
+                        DBG("Got next header(%d)[%s]", trfLen, buf) ;
+                        break ;
+                    } else {
+                        DBG("Skipped[%s]\n", buf) ;
+                        continue ;
+                    }
+                }
+                continue ; // to fill out rest of buff
             }
         }
 
         crlfPos = crlfPtr - buf;
-
+        DBG("crlfPos=%d", crlfPos) ;
         if(crlfPos == 0) { //End of headers
             DBG("Headers read");
             memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
@@ -482,26 +507,30 @@
         value[31] = '\0';
 
         int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
+        DBG("Read header(%d) : %s: %s\n", n, key, value);
         if ( n == 2 ) {
-            DBG("Read header : %s: %s\n", key, value);
-            if( !strcmp(key, "Content-Length") ) {
+            //DBG("Read header : %s: %s\n", key, value);
+            char *k, *v ;
+            for(k=key ;   *k != '\0'; k++)*k = toupper(*k) ;
+            for(v=value ; *v != '\0'; v++)*v = toupper(*v) ;
+            if( !strcmp(key, "CONTENT-LENGTH") ) {
                 sscanf(value, "%d", &recvContentLength);
                 pDataIn->setDataLen(recvContentLength);
-            } else if( !strcmp(key, "Transfer-Encoding") ) {
-                if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) {
+            } else if( !strcmp(key, "TRANSFER-ENCODING") ) {
+                if( !strcmp(value, "CHUNKED") ) {
                     recvChunked = true;
                     pDataIn->setIsChunked(true);
                 }
-            } else if( !strcmp(key, "Content-Type") ) {
+            } else if( !strcmp(key, "CONTENT-TYPE") ) {
                 pDataIn->setDataType(value);
-            } else if( !strcmp(key, "location") && redirect_url) {
+            } else if( !strcmp(key, "LOCATION") && redirect_url) {
                 sscanf(buf, "%31[^:]: %128[^\r\n]", key, redirect_url);
                 DBG("Redirect %s: %s", key, redirect_url) ;
                 redirect = 1 ;
             }
             memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
             trfLen -= (crlfPos + 2);
-
+            DBG("next header(trfLen:%d)[%s]", trfLen, buf) ;
         } else {
             ERR("Could not parse header");
             PRTCL_ERR();
@@ -689,7 +718,7 @@
         len -= cp_len ;
 
         if(send_buf_p == send_buf + SEND_BUF_SIZE) {
-            if(port == HTTPS_PORT){
+            if(port == HTTPS_PORT) {
                 ERR("HTTPClient::send buffer overflow");
                 return HTTP_ERROR ;
             }
@@ -808,7 +837,7 @@
     HTTPResult ret ;
     char b_auth[(int)((AUTHB_SIZE+3)*4/3+1)] ;
     char base64buff[AUTHB_SIZE+3] ;
- 
+
     ret = send("Authorization: Basic ") ;
     CHECK_CONN_ERR(ret);
     sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
@@ -818,6 +847,6 @@
     b_auth[strlen(b_auth)] = '\n' ;
     DBG("b_auth:%s", b_auth) ;
     ret = send(b_auth) ;
-    CHECK_CONN_ERR(ret); 
+    CHECK_CONN_ERR(ret);
     return HTTP_OK ;
 }