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.
Dependents: SalesforceInterface df-2014-heroku-thermostat-k64f SalesforceInterface
Fork of HTTPClient by
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.
Diff: HTTPClient.cpp
- Revision:
- 36:debaeb6006a7
- Parent:
- 35:16f0a44cc53e
- Child:
- 37:29941a3bae90
--- a/HTTPClient.cpp Fri Sep 12 20:36:21 2014 +0000 +++ b/HTTPClient.cpp Wed Sep 17 21:36:14 2014 +0000 @@ -26,7 +26,7 @@ #include "rtos.h" //Debug is disabled by default -#if 0 +#ifdef DEBUG_HTTP //Enable debug #include <cstdio> #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); @@ -114,7 +114,7 @@ } HTTPClient::HTTPClient() : - m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0) + m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0), m_oauthToken(NULL) { /* CyaSSL_Debugging_ON() ; */ @@ -123,6 +123,9 @@ ssl = 0 ; SSLver = 3 ; m_basicAuthUser = NULL ; + m_basicAuthPassword = NULL; + m_httpResponseCode = 0; + m_oauthToken = NULL; redirect_url = NULL ; redirect = 0 ; header = NULL ; @@ -133,6 +136,22 @@ } +HTTPResult HTTPClient::oauthToken(const char *token) { // OAUTH2 Authentication + // reset if called + if (m_oauthToken != NULL) free((void *)m_oauthToken); + m_oauthToken = NULL; + + // fill in if able... + if (token != NULL && strlen(token) > 0) { + m_oauthToken = (char *)malloc(strlen(token)+1); + memset((void *)m_oauthToken,0,strlen(token)+1); + strcpy((char *)m_oauthToken,token); + this->basicAuth(NULL,NULL); + } + + return HTTP_OK ; +} + HTTPResult HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification { #define AUTHB_SIZE 128 @@ -143,12 +162,14 @@ if (user != NULL) { m_basicAuthUser = (char *)malloc(strlen(user)+1); strcpy((char *)m_basicAuthUser, user); + this->oauthToken(NULL); } if (m_basicAuthPassword) free((void *)m_basicAuthPassword); if (password != NULL) { m_basicAuthPassword = (char *)malloc(strlen(password)+1); strcpy((char *)m_basicAuthPassword, password); + this->oauthToken(NULL); } return HTTP_OK ; @@ -353,7 +374,12 @@ //Send default headers DBG("Sending headers"); if(m_basicAuthUser && m_basicAuthPassword) { - bAuth() ; /* send out Basic Auth header */ + DBG("Sending basic auth header"); + bAuth() ; /* send out Basic Auth header */ + } + else if (m_oauthToken) { + DBG("Sending OAUTH header"); + tokenAuth(); /* send out OAUTH header */ } if( pDataOut != NULL ) { if( pDataOut->getIsChunked() ) { @@ -826,6 +852,17 @@ return HTTP_OK; } +HTTPResult HTTPClient::tokenAuth(void) +{ + HTTPResult ret ; + ret = send("Authorization: Bearer ") ; + CHECK_CONN_ERR(ret); + DBG("oauthToken: %s", m_oauthToken) ; + ret = send((char *)m_oauthToken) ; + CHECK_CONN_ERR(ret); + return HTTP_OK ; +} + HTTPResult HTTPClient::bAuth(void) { HTTPResult ret ;