Fork of the working HTTPClient adaptation using CyaSSL. This version adds a derivation of HTTPText called HTTPJson to emit JSON text properly. Additionally, the URL parser has defines that permit longer URLs to be utilized.

Dependencies:   mbedTLSLibrary

Dependents:   SalesforceInterface df-2014-heroku-thermostat-k64f SalesforceInterface

Fork of HTTPClient by wolf SSL

This is a fork of the working HTTPS/SSL library that contains two extensions:

- HTTPJson - a derivation of HTTPText for emitting JSON strings specifically. No JSON parsing/checking is accomplished - HTTPJson simply sets the right Content-Type for HTTP(S).

- Expanded internal buffers for longer URLs. This is set in HTTPClient.cpp and is tunable.

Files at this revision

API Documentation at this revision

Comitter:
wolfSSL
Date:
Mon Jul 21 11:29:00 2014 +0000
Parent:
25:531f03a263a9
Child:
28:6fbaf49cc109
Commit message:
Minor fix

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	Sat Jul 12 12:09:38 2014 +0000
+++ b/HTTPClient.cpp	Mon Jul 21 11:29:00 2014 +0000
@@ -114,6 +114,10 @@
     ctx = 0 ;
     ssl = 0 ;
     SSLver = 3 ; 
+    m_basicAuthUser = NULL ;
+    redirect_url = NULL ;
+    redirect = 0 ;
+    header = NULL ;
 }
 
 HTTPClient::~HTTPClient()
@@ -163,11 +167,17 @@
     return m_httpResponseCode;
 }
 
-void HTTPClient::setHeader(char * h)
+void HTTPClient::setHeader(const char * h)
 {
     header = h ;
 }
 
+void HTTPClient::setLocationBuf(char * url, int size)
+{
+    redirect_url = url ;
+    redirect_url_size = size ;
+}
+
 HTTPResult HTTPClient::setSSLversion(int minorV) 
 {
     if((minorV>=0) && (minorV<=3)) 
@@ -213,7 +223,8 @@
     CYASSL_METHOD * SSLmethod ;
     m_httpResponseCode = 0; //Invalidate code
     m_timeout = timeout;
-
+    redirect = 0 ;
+    
     pDataIn->writeReset();
     if( pDataOut ) {
         pDataOut->readReset();
@@ -263,7 +274,7 @@
     if(port == HTTPS_PORT) {
 
         /* Start SSL connect */
-        DBG("SSLmethod=%d", SSLmethod) ;
+        DBG("SSLver=%d", SSLver) ;
         if(ctx == NULL) {
             switch(SSLver) {
             case 0 : SSLmethod = CyaSSLv3_client_method() ; break ;
@@ -316,6 +327,9 @@
 
     //Send default headers
     DBG("Sending headers");
+    if(m_basicAuthUser) {
+            bAuth() ; /* send out Basic Auth header */        
+    }
     if( pDataOut != NULL ) {
         if( pDataOut->getIsChunked() ) {
             ret = send("Transfer-Encoding: chunked\r\n");
@@ -332,14 +346,11 @@
             ret = send(buf);
             CHECK_CONN_ERR(ret);
         }
-        if(m_basicAuthUser) {
-            bAuth() ; /* send out Basic Auth header */        
-        }
     }
 
     //Add user headers
     if(header) {
-        ret = send(header);
+        ret = send((char *)header);
         CHECK_CONN_ERR(ret);
     }
 
@@ -421,7 +432,7 @@
         PRTCL_ERR();
     }
 
-    if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) {
+    if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) {
         //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers
         WARN("Response code %d", m_httpResponseCode);
         PRTCL_ERR();
@@ -481,8 +492,11 @@
                 }
             } else if( !strcmp(key, "Content-Type") ) {
                 pDataIn->setDataType(value);
+            } else if( !strcmp(key, "location") && redirect_url) {
+                sscanf(buf, "%31[^:]: %128[^\r\n]", key, redirect_url);
+                DBG("Redirect %s: %s", key, redirect_url) ;
+                redirect = 1 ;
             }
-
             memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
             trfLen -= (crlfPos + 2);
 
@@ -584,8 +598,8 @@
     cyassl_free() ;
     m_sock.close();
     DBG("Completed HTTP transaction");
-
-    return HTTP_OK;
+    if(redirect)return HTTP_REDIRECT ;
+    else        return HTTP_OK;
 }
 
 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen)   //0 on success, err code on failure
@@ -796,6 +810,7 @@
     ret = send("Authorization: Basic ") ;
     CHECK_CONN_ERR(ret);
     sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
+    DBG("bAuth: %s", base64buff) ;
     base64enc(b_auth, base64buff) ;
     b_auth[strlen(b_auth)+1] = '\0' ;
     b_auth[strlen(b_auth)] = '\n' ;
--- a/HTTPClient.h	Sat Jul 12 12:09:38 2014 +0000
+++ b/HTTPClient.h	Mon Jul 21 11:29:00 2014 +0000
@@ -43,6 +43,7 @@
     HTTP_TIMEOUT, ///<Connection timeout
     HTTP_CONN, ///<Connection error
     HTTP_CLOSED, ///<Connection was closed by remote host
+    HTTP_REDIRECT, ///<HTTP 300 - 303
     HTTP_OK = 0, ///<Success
 };
 
@@ -121,8 +122,9 @@
     */
     int getHTTPResponseCode();
     
-    void setHeader(char *header) ;   /* set http headers */
+    void setHeader(const char *header) ;   /* set http 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 */
 
 private:
     enum HTTP_METH {
@@ -140,6 +142,7 @@
     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 cyassl_free(void) ;
     HTTPResult bAuth(void) ;
+    HTTPResult readHeader(void) ;
     
     //Parameters
 
@@ -149,8 +152,11 @@
     const char* m_basicAuthPassword;
     int m_httpResponseCode;
 
-    char * header ;
-
+    const char * header ;
+    char * redirect_url ;
+    int    redirect_url_size ;
+    int    redirect ;
+    
     /* for CyaSSL */
     int    SSLver ;
     uint16_t port;