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 */

Files at this revision

API Documentation at this revision

Comitter:
wolfSSL
Date:
Tue Jul 21 01:07:25 2015 +0000
Parent:
32:9aadb8a34e80
Child:
34:76aa4f4021c1
Commit message:
Added dumpReqHeader, dumpResHeader

Changed in this revision

HTTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPClient.h Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPClient.cpp	Mon Jul 20 09:21:07 2015 +0000
+++ b/HTTPClient.cpp	Tue Jul 21 01:07:25 2015 +0000
@@ -111,9 +111,7 @@
 HTTPClient::HTTPClient() :
     m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
 {
-
     WOLF_DEBUG_ON ;
-
     ctx = 0 ;
     ssl = 0 ;
     SSLver = 3 ;
@@ -121,6 +119,8 @@
     redirect_url = NULL ;
     redirect = 0 ;
     header = NULL ;
+    dumpReqH = false ;
+    dumpResH = false ;
 }
 
 HTTPClient::~HTTPClient()
@@ -177,6 +177,16 @@
     header = h ;
 }
 
+void HTTPClient::dumpReqHeader(bool sw)
+{
+    dumpReqH = sw ;
+}
+
+void HTTPClient::dumpResHeader(bool sw)
+{
+    dumpResH = sw ;
+}
+
 void HTTPClient::setLocationBuf(char * url, int size)
 {
     redirect_url = url ;
@@ -210,6 +220,11 @@
     return HTTP_PRTCL; \
   } while(0)
 
+#define DUMP_REQ_HEADER(buff) \
+    if(dumpReqH)printf("%s", buff) ;
+#define DUMP_RES_HEADER(buff) \
+    if(dumpResH)printf("%s\n", buff) ;
+
 void HTTPClient::wolfssl_free(void)
 {
     if(ssl) {
@@ -259,7 +274,7 @@
     DBG("Host: %s", host);
     DBG("Port: %d", port);
     DBG("Path: %s", path);
-
+    if(dumpReqH)printf("\nHTTP Request: %s://%s:%d\n", scheme, host, port) ;
     //Connect
     DBG("Connecting socket to server");
 
@@ -329,6 +344,7 @@
 
     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
+    DUMP_REQ_HEADER(buf) ;
     ret = send(buf);
     if(ret) {
         m_sock.close();
@@ -348,9 +364,11 @@
     if( pDataOut != NULL ) {
         if( pDataOut->getIsChunked() ) {
             ret = send("Transfer-Encoding: chunked\r\n");
+            DUMP_REQ_HEADER("Transfer-Encoding: chunked\r\n") ;
             CHECK_CONN_ERR(ret);
         } else {
             snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
+            DUMP_REQ_HEADER(buf) ;           
             DBG("Content buf:%s", buf) ;
             ret = send(buf);
             CHECK_CONN_ERR(ret);
@@ -358,6 +376,7 @@
         char type[48];
         if( pDataOut->getDataType(type, 48) == HTTP_OK ) {
             snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
+            DUMP_REQ_HEADER(buf) ;
             ret = send(buf);
             CHECK_CONN_ERR(ret);
         }
@@ -366,6 +385,7 @@
     //Add user headers
     if(header) {
         ret = send((char *)header);
+        DUMP_REQ_HEADER(header) ;
         CHECK_CONN_ERR(ret);
     }
 
@@ -377,6 +397,7 @@
     size_t trfLen;
 
     //Send data (if available)
+    DUMP_REQ_HEADER("\n") ;
     if( pDataOut != NULL ) {
         DBG("Sending data");
         while(true) {
@@ -397,6 +418,7 @@
             DBG("trfLen 1=%d", trfLen) ;
             if( trfLen != 0 ) {
                 DBG("Sending 1") ;
+                DUMP_REQ_HEADER(buf) ;
                 ret = send(buf, trfLen);
                 DBG("Sent 1") ;
                 CHECK_CONN_ERR(ret);
@@ -421,11 +443,12 @@
         }
 
     }
+    DUMP_REQ_HEADER("\n") ;
     ret = flush() ; // flush the send buffer ;
     CHECK_CONN_ERR(ret);
 
     //Receive response
-    DBG("Receiving response");
+    DBG("Receiving response:");
 
     ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
     CHECK_CONN_ERR(ret);
@@ -439,6 +462,8 @@
 
     int crlfPos = crlfPtr - buf;
     buf[crlfPos] = '\0';
+    DUMP_RES_HEADER("\nHTTP Response:") ;
+    DUMP_RES_HEADER(buf) ;
 
     //Parse HTTP response
     if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) {
@@ -503,7 +528,8 @@
         }
 
         buf[crlfPos] = '\0';
-
+        DUMP_RES_HEADER(buf) ;
+        
         char key[32];
         char value[32];
 
@@ -853,8 +879,10 @@
     char base64buff[AUTHB_SIZE+3] ;
 
     ret = send("Authorization: Basic ") ;
+    DUMP_REQ_HEADER("Authorization: Basic ") ;
     CHECK_CONN_ERR(ret);
     sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
+    DUMP_REQ_HEADER(base64buff) ;
     DBG("bAuth: %s", base64buff) ;
     base64enc(b_auth, base64buff) ;
     b_auth[strlen(b_auth)+1] = '\0' ;
--- a/HTTPClient.h	Mon Jul 20 09:21:07 2015 +0000
+++ b/HTTPClient.h	Tue Jul 21 01:07:25 2015 +0000
@@ -121,8 +121,10 @@
     @return The HTTP response code of the last request
     */
     int getHTTPResponseCode();
-    
+
     void setHeader(const char *header) ;   /* set http headers */
+    void dumpReqHeader(bool sw) ;          /* switch on dumpling request headers */
+    void dumpResHeader(bool sw) ;          /* switch on dumpling response headers */
     HTTPResult setSSLversion(int minorV) ; /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */
     void setLocationBuf(char *url, int size) ; /* set URL buffer for redirection */
 
@@ -142,7 +144,6 @@
     HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
     void wolfssl_free(void) ;
     HTTPResult bAuth(void) ;
-    HTTPResult readHeader(void) ;
     
     //Parameters
 
@@ -156,8 +157,10 @@
     char * redirect_url ;
     int    redirect_url_size ;
     int    redirect ;
+    bool   dumpReqH ;
+    bool   dumpResH ;
     
-    /* for CyaSSL */
+    /* for wolfSSL */
     int    SSLver ;
     uint16_t port;
     struct WOLFSSL_CTX* ctx ;