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:
34:76aa4f4021c1
Parent:
33:77082c88748a
--- a/HTTPClient.cpp	Tue Jul 21 01:07:25 2015 +0000
+++ b/HTTPClient.cpp	Thu Apr 28 00:55:27 2016 +0000
@@ -17,6 +17,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include "settings.h"
 //Debug is disabled by default
 #if 0
 //Enable debug
@@ -58,28 +59,29 @@
 static char send_buf[SEND_BUF_SIZE] ;
 static char *send_buf_p ;
 
-static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *ctx)
+static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock)
 {
     int n ;
     int i ;
+    
 #define RECV_RETRY 3
-
     for(i=0; i<RECV_RETRY; i++) {
-        n = m_sock.receive(buf, sz) ;
+        n = ((TCPSocketConnection *)sock)->receive(buf, sz) ;
         if(n >= 0)return n  ;
-        WARN("Retrt Recv") ;
+        WARN("Retry Recv") ;
         wait(0.2) ;
     }
     ERR("SocketReceive:%d/%d\n", n, sz)  ;
     return n ;
+
 }
 
-static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
+static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *sock)
 {
     int n ;
 
     wait(0.1) ;
-    n = m_sock.send(buf, sz);
+    n = ((TCPSocketConnection *)sock)->send(buf, sz);
     if(n > 0) {
         wait(0.3) ;
         return n ;
@@ -195,13 +197,23 @@
 
 HTTPResult HTTPClient::setSSLversion(int minorV)
 {
-    if((minorV>=0) && (minorV<=3))
-        SSLver = minorV ;
-    else return HTTP_ERROR ;
+     switch(minorV) {
+     #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
+         case 0 : break ;
+     #endif
+     #if !defined(NO_OLD_TLS)
+         case 1 : break ;
+         case 2 : break ;
+     #endif
+         case 3 : break ;
+         default: 
+             ERR("Invalid SSL version");
+                 return HTTP_CONN;
+    }
+    SSLver = minorV ;
     return HTTP_OK ;
 }
 
-
 #define CHECK_CONN_ERR(ret) \
   do{ \
     if(ret) { \
@@ -297,18 +309,25 @@
         DBG("SSLver=%d", SSLver) ;
         if(ctx == NULL) {
             switch(SSLver) {
+                #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
                 case 0 :
                     SSLmethod = wolfSSLv3_client_method() ;
                     break ;
+                #endif
+                #if !defined(NO_OLD_TLS)
                 case 1 :
                     SSLmethod = wolfTLSv1_client_method() ;
                     break ;
                 case 2 :
                     SSLmethod = wolfTLSv1_1_client_method() ;
                     break ;
+                #endif
                 case 3 :
                     SSLmethod = wolfTLSv1_2_client_method() ;
                     break ;
+                default: 
+                    ERR("Invalid SSL version");
+                    return HTTP_CONN;
             }
             ctx = wolfSSL_CTX_new((WOLFSSL_METHOD *)SSLmethod);
             if (ctx == NULL) {
@@ -327,7 +346,8 @@
                 return HTTP_CONN;
             }
         }
-
+        wolfSSL_SetIOReadCtx (ssl, (void *)&m_sock) ;
+        wolfSSL_SetIOWriteCtx(ssl, (void *)&m_sock) ;
         DBG("ctx=%x, ssl=%x, ssl->ctx->CBIORecv, CBIOSend=%x, %x\n",
             ctx, ssl, SocketReceive, SocketSend ) ;
         if (wolfSSL_connect(ssl) != SSL_SUCCESS) {