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:
19:1e2f05809eb1
Parent:
18:d89df40b4cf3
Child:
20:bec882d85856
--- a/HTTPClient.cpp	Mon Apr 07 23:41:06 2014 +0000
+++ b/HTTPClient.cpp	Tue Apr 08 09:09:54 2014 +0000
@@ -51,16 +51,7 @@
 #include "HTTPClient.h"
 #include "TCPSocketConnection.h"
 
-class TCPSocketConnection_fd: public TCPSocketConnection
-{
-public:
-    int get_fd() {
-        return _sock_fd ;
-    }
-} ;
-
-static  TCPSocketConnection_fd m_sock;
-
+static  TCPSocketConnection m_sock;
 #define CHUNK_SIZE    256
 #define SEND_BUF_SIZE 512
 static char send_buf[SEND_BUF_SIZE] ;
@@ -169,10 +160,14 @@
 
 void HTTPClient::cyassl_free(void)
 {
-    if(ssl)
+    if(ssl) {
         CyaSSL_free(ssl) ;
-    if(ctx)
+        ssl = NULL ;
+    }
+    if(ctx) {
         CyaSSL_CTX_free(ctx) ;
+        ctx = NULL ;
+    }
 }
 
 
@@ -213,7 +208,6 @@
 
     //Connect
     DBG("Connecting socket to server");
-    sockfd = m_sock.get_fd() ;
 
 #define MAX_RETRY 5
     int retry ;
@@ -230,27 +224,28 @@
 
     if(port == HTTPS_PORT) {
         /* Start SSL connect */
-        ctx = CyaSSL_CTX_new(
-                  CyaTLSv1_2_client_method
-                  //CyaSSLv3_client_method
-                  ());
-        if (ctx == NULL) {
-            ERR("unable to get ctx");
-            return HTTP_CONN;
+        if(ctx == NULL) {
+            ctx = CyaSSL_CTX_new(
+                      CyaTLSv1_2_client_method
+                      //CyaSSLv3_client_method
+                      ());
+            if (ctx == NULL) {
+                ERR("unable to get ctx");
+                return HTTP_CONN;
+            }
+            CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
+            CyaSSL_SetIORecv(ctx, SocketReceive) ;
+            CyaSSL_SetIOSend(ctx, SocketSend) ;
         }
-        CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
-
-        ssl = CyaSSL_new(ctx);
         if (ssl == NULL) {
-            ERR("unable to get SSL object");
-            cyassl_free() ;
-            return HTTP_CONN;
+            ssl = CyaSSL_new(ctx);
+            if (ssl == NULL) {
+                ERR("unable to get SSL object");
+                cyassl_free() ;
+                return HTTP_CONN;
+            }
         }
 
-        CyaSSL_SetVersion(ssl, CYASSL_TLSV1_2) ;
-        CyaSSL_set_fd(ssl, sockfd);
-        CyaSSL_SetIORecv(ctx, SocketReceive) ;
-        CyaSSL_SetIOSend(ctx, SocketSend) ;
         DBG("ctx=%x, ssl=%x, ssl->ctx->CBIORecv, CBIOSend=%x, %x\n",
             ctx, ssl, SocketReceive, SocketSend ) ;
         if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
@@ -532,15 +527,15 @@
         }
 
     }
-
-    cyassl_free() ;
+    CyaSSL_free(ssl) ;
+    ssl = NULL ;
     m_sock.close();
     DBG("Completed HTTP transaction");
 
     return HTTP_OK;
 }
 
-HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
+HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen)   //0 on success, err code on failure
 {
     DBG("Trying to read between %d and %d bytes", minLen, maxLen);
     size_t readLen = 0;
@@ -603,7 +598,7 @@
     return HTTP_OK;
 }
 
-HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
+HTTPResult HTTPClient::send(char* buf, size_t len)   //0 on success, err code on failure
 {
     HTTPResult ret ;
     int cp_len ;
@@ -630,7 +625,7 @@
     return HTTP_OK ;
 }
 
-HTTPResult HTTPClient::flush() //0 on success, err code on failure
+HTTPResult HTTPClient::flush()   //0 on success, err code on failure
 {
     int len ;
     char * buf ;
@@ -672,7 +667,7 @@
     return HTTP_OK;
 }
 
-HTTPResult HTTPClient::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen) //Parse URL
+HTTPResult HTTPClient::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen)   //Parse URL
 {
     char* schemePtr = (char*) url;
     char* hostPtr = (char*) strstr(url, "://");