A HTTP/HTTPS Client for the mbed networking/CyaSSL ssl library

Dependents:   Anpi dropbox_access php_access_auth TwitterReader ... more

Fork of HTTPClient by Donatien Garnier

HTTP and HTTPS Client Class with wolfSSL, embedded SSL library.

/media/uploads/wolfSSL/wolfssl_logo.png

The class was forked from http://mbed.org/users/donatien/code/HTTPClient/

It, now, accepts url both with "http://" and "https://".

Allocate caller thread with 16kbytes or larger stack for "https" requests.

Rest of the API stays compatible with HTTPClient.

For more about the library, see http://www.wolfssl.com. http://wolfssl.com/yaSSL/Docs.html.

Extended methods:

  • HTTPResult basicAuth(const char* user, const char* password); /* set id/passwd for basic Authentication */
  • void setHeader(char *header) ; /* set http headers */
  • HTTPResult setSSLversion(int minorV) ; /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */
Revision:
32:9aadb8a34e80
Parent:
31:7fd621b83b60
Child:
33:77082c88748a
--- a/HTTPClient.cpp	Fri Jun 26 00:39:47 2015 +0000
+++ b/HTTPClient.cpp	Mon Jul 20 09:21:07 2015 +0000
@@ -22,17 +22,16 @@
 //Enable debug
 #include <cstdio>
 #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
-#define WOLF_DEBUG_ON    wolfSSL_Debugging_ON() ;
+
+#define WOLF_DEBUG_ON   // wolfSSL_Debugging_ON() ;
 #else
 //Disable debug
 #define DBG(x, ...)
-#define WARN(x, ...)
-#define ERR(x, ...)
 #define WOLF_DEBUG_ON 
+#endif
 
-#endif
+#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
 
 #define HTTP_PORT 80
 #define HTTPS_PORT 443
@@ -51,9 +50,10 @@
 
 #include "HTTPClient.h"
 #include "TCPSocketConnection.h"
+#include <string.h>
 
 static  TCPSocketConnection m_sock;
-#define CHUNK_SIZE    256
+#define CHUNK_SIZE    (256*4*8)
 #define SEND_BUF_SIZE 512
 static char send_buf[SEND_BUF_SIZE] ;
 static char *send_buf_p ;
@@ -67,6 +67,7 @@
     for(i=0; i<RECV_RETRY; i++) {
         n = m_sock.receive(buf, sz) ;
         if(n >= 0)return n  ;
+        WARN("Retrt Recv") ;
         wait(0.2) ;
     }
     ERR("SocketReceive:%d/%d\n", n, sz)  ;
@@ -139,12 +140,14 @@
 
 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
 {
-    return connect(url, HTTP_GET, NULL, pDataIn, timeout);
+    HTTPResult ret ;
+    ret = connect(url, HTTP_GET, NULL, pDataIn, timeout);
+    return ret;
 }
 
 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
 {
-    HTTPText str(result, maxResultLen);
+    HTTPText str(result, maxResultLen);   
     return get(url, &str, timeout);
 }
 
@@ -510,7 +513,6 @@
         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);
             char *k, *v ;
             for(k=key ;   *k != '\0'; k++)*k = toupper(*k) ;
             for(v=value ; *v != '\0'; v++)*v = toupper(*v) ;
@@ -553,12 +555,18 @@
                 crlfPos=0;
                 buf[trfLen]=0;
                 if(trfLen >= 2) {
-                    for(; crlfPos < trfLen - 2; crlfPos++) {
+                    crlfPtr = strstr(buf, "\r\n") ;
+                    if(crlfPtr != NULL){ 
+                        foundCrlf = true;
+                        crlfPos = crlfPtr - buf;
+                        break ; 
+                    }
+                    /*for(; crlfPos < trfLen - 2; crlfPos++) {
                         if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) {
                             foundCrlf = true;
                             break;
                         }
-                    }
+                    }*/
                 }
                 if(!foundCrlf) { //Try to read more
                     if( trfLen < CHUNK_SIZE ) {
@@ -573,12 +581,17 @@
                 }
             } while(!foundCrlf);
             buf[crlfPos] = '\0';
-            int n = sscanf(buf, "%x", &readLen);
-            if(n!=1) {
-                ERR("Could not read chunk length");
-                PRTCL_ERR();
+            if(((buf[crlfPos-2] == 0x0a) && (buf[crlfPos-1] == 0x0a))){
+                WARN("null chunck\n") ;
+                readLen = 0 ;
+            } else {
+                int n = sscanf(buf, "%x", &readLen);
+                if(n!=1) {
+                    ERR("Could not read chunk length:%02x,%02x,%02x,%02x,\"%s\"", 
+                    buf[crlfPos-4],buf[crlfPos-3],buf[crlfPos-2],buf[crlfPos-1],buf);
+                    PRTCL_ERR();
+                }
             }
-
             memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
             trfLen -= (crlfPos + 2);
 
@@ -616,9 +629,9 @@
                 CHECK_CONN_ERR(ret);
                 trfLen += newTrfLen;
             }
-            if( (buf[0] != '\r') || (buf[1] != '\n') ) {
-                ERR("Format error");
-                PRTCL_ERR();
+            if(strcmp(buf, "\r\n") == 0) {
+                WARN("Null Chunck 2\n") ;
+                break ;
             }
             memmove(buf, &buf[2], trfLen - 2);
             trfLen -= 2;