HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

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, "://");