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.

Committer:
ansond
Date:
Wed Oct 28 18:11:12 2015 +0000
Revision:
53:fa91a40cecb5
Parent:
52:cea1021a822d
updates to augment header information

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:2ccb9960a044 1 /* HTTPClient.cpp */
donatien 10:e1351de84c16 2 /* Copyright (C) 2012 mbed.org, MIT License
donatien 10:e1351de84c16 3 *
donatien 10:e1351de84c16 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien 10:e1351de84c16 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
donatien 10:e1351de84c16 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
donatien 10:e1351de84c16 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
donatien 10:e1351de84c16 8 * furnished to do so, subject to the following conditions:
donatien 10:e1351de84c16 9 *
donatien 10:e1351de84c16 10 * The above copyright notice and this permission notice shall be included in all copies or
donatien 10:e1351de84c16 11 * substantial portions of the Software.
donatien 10:e1351de84c16 12 *
donatien 10:e1351de84c16 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien 10:e1351de84c16 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien 10:e1351de84c16 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien 10:e1351de84c16 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 10:e1351de84c16 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
donatien 10:e1351de84c16 18 */
ansond 29:2d96cc752d19 19
ansond 46:7cd69cc809b8 20 // RTOS for Thread definition
ansond 35:16f0a44cc53e 21 #include "rtos.h"
ansond 35:16f0a44cc53e 22
donatien 7:4e39864f7b15 23 //Debug is disabled by default
ansond 38:38720bd5dd16 24 #if 0
donatien 12:89d09a6db00a 25 //Enable debug
donatien 11:390362de8c3f 26 #include <cstdio>
ansond 37:29941a3bae90 27 #define DBG(x, ...) std::printf("[HTTPClient : DBG] "x"\r\n", ##__VA_ARGS__);
ansond 37:29941a3bae90 28 #define WARN(x, ...) std::printf("[HTTPClient : WARN] "x"\r\n", ##__VA_ARGS__);
ansond 37:29941a3bae90 29 #define ERR(x, ...) std::printf("[HTTPClient : ERR] "x"\r\n", ##__VA_ARGS__);
donatien 12:89d09a6db00a 30
donatien 12:89d09a6db00a 31 #else
donatien 12:89d09a6db00a 32 //Disable debug
wolfSSL 18:d89df40b4cf3 33 #define DBG(x, ...)
donatien 12:89d09a6db00a 34 #define WARN(x, ...)
wolfSSL 18:d89df40b4cf3 35 #define ERR(x, ...)
donatien 12:89d09a6db00a 36
donatien 7:4e39864f7b15 37 #endif
donatien 0:2ccb9960a044 38
donatien 0:2ccb9960a044 39 #define HTTP_PORT 80
wolfSSL 17:c73d8e61d391 40 #define HTTPS_PORT 443
donatien 0:2ccb9960a044 41
donatien 11:390362de8c3f 42 #define OK 0
donatien 11:390362de8c3f 43
donatien 11:390362de8c3f 44 #define MIN(x,y) (((x)<(y))?(x):(y))
donatien 11:390362de8c3f 45 #define MAX(x,y) (((x)>(y))?(x):(y))
donatien 11:390362de8c3f 46
wolfSSL 17:c73d8e61d391 47 #include <cstring>
donatien 0:2ccb9960a044 48
donatien 11:390362de8c3f 49 #include "HTTPClient.h"
wolfSSL 17:c73d8e61d391 50
sarahmarshy 49:a564fc323921 51 // ************ should be a better way to adjust for platform limitations
ansond 40:c0a52a6bb50f 52
ansond 40:c0a52a6bb50f 53 #if defined (TARGET_K64F)
ansond 46:7cd69cc809b8 54 // can use larger buffers since we have (lots) more memory
ansond 46:7cd69cc809b8 55 #define BIG_MEMORY_CHUNK 4096
ansond 46:7cd69cc809b8 56 #define CHUNK_SIZE 768
ansond 46:7cd69cc809b8 57 #define SEND_BUF_SIZE BIG_MEMORY_CHUNK
ansond 46:7cd69cc809b8 58 #define MAX_URL_HOSTNAME_LENGTH 256
ansond 46:7cd69cc809b8 59 #define MAX_URL_PATH_LENGTH BIG_MEMORY_CHUNK
ansond 46:7cd69cc809b8 60 #define MAX_HEADER_VALUE_LENGTH BIG_MEMORY_CHUNK
ansond 46:7cd69cc809b8 61 #define MAX_HEADER_KEY_LENGTH 40
ansond 46:7cd69cc809b8 62 #define HEADER_SCANF_FORMAT "%40[^:]: %4096[^\r\n]" /* must align with BIG_MEMORY_CHUNK */
ansond 46:7cd69cc809b8 63 #define REDIRECT_SCANF_FORMAT "%40[^:]: %4096[^\r\n]" /* must align with BIG_MEMORY_CHUNK */
ansond 51:3bdf57f7fd60 64 #define RECV_RETRY 3
ansond 40:c0a52a6bb50f 65 #else
ansond 40:c0a52a6bb50f 66 // default smaller buffers
ansond 46:7cd69cc809b8 67 #define CHUNK_SIZE 256
ansond 46:7cd69cc809b8 68 #define SEND_BUF_SIZE 1100
ansond 46:7cd69cc809b8 69 #define MAX_URL_HOSTNAME_LENGTH 128
ansond 46:7cd69cc809b8 70 #define MAX_URL_PATH_LENGTH 128
ansond 46:7cd69cc809b8 71 #define MAX_HEADER_VALUE_LENGTH 41
ansond 46:7cd69cc809b8 72 #define MAX_HEADER_KEY_LENGTH 40
ansond 46:7cd69cc809b8 73 #define HEADER_SCANF_FORMAT "%40[^:]: %40[^\r\n]" /* must align with MAX_HEADER_VALUE_LENGTH */
ansond 46:7cd69cc809b8 74 #define REDIRECT_SCANF_FORMAT "%40[^:]: %128[^\r\n]" /* must align with MAX_URL_PATH_LENGTH */
ansond 51:3bdf57f7fd60 75 #define RECV_RETRY 3
ansond 40:c0a52a6bb50f 76 #endif
ansond 40:c0a52a6bb50f 77
ansond 40:c0a52a6bb50f 78 // ************ should be a better way to adjust for platform limitations
ansond 40:c0a52a6bb50f 79
wolfSSL 17:c73d8e61d391 80 static char send_buf[SEND_BUF_SIZE] ;
ansond 32:d9db238bb8a3 81 static char *send_buf_p = NULL;
sarahmarshy 50:a18a06b000f3 82 static TCPSocketConnection* m_sock_p = NULL;
donatien 11:390362de8c3f 83
wolfSSL 22:4b9a4151cc73 84 static void base64enc(char *out, const char *in) {
wolfSSL 22:4b9a4151cc73 85 const char code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ;
wolfSSL 22:4b9a4151cc73 86 int i = 0, x = 0, l = 0;
wolfSSL 22:4b9a4151cc73 87
wolfSSL 22:4b9a4151cc73 88 for (; *in; in++) {
wolfSSL 22:4b9a4151cc73 89 x = x << 8 | *in;
wolfSSL 22:4b9a4151cc73 90 for (l += 8; l >= 6; l -= 6) {
wolfSSL 22:4b9a4151cc73 91 out[i++] = code[(x >> (l - 6)) & 0x3f];
wolfSSL 22:4b9a4151cc73 92 }
wolfSSL 22:4b9a4151cc73 93 }
wolfSSL 22:4b9a4151cc73 94 if (l > 0) {
wolfSSL 22:4b9a4151cc73 95 x <<= 6 - l;
wolfSSL 22:4b9a4151cc73 96 out[i++] = code[x & 0x3f];
wolfSSL 22:4b9a4151cc73 97 }
wolfSSL 22:4b9a4151cc73 98 for (; i % 4;) {
wolfSSL 22:4b9a4151cc73 99 out[i++] = '=';
wolfSSL 22:4b9a4151cc73 100 }
wolfSSL 22:4b9a4151cc73 101 out[i] = '\0' ;
wolfSSL 22:4b9a4151cc73 102 }
ansond 51:3bdf57f7fd60 103
ansond 51:3bdf57f7fd60 104 static int SocketReceive(void* ssl, unsigned char *buf, size_t sz)
sarahmarshy 50:a18a06b000f3 105 {
ansond 51:3bdf57f7fd60 106 int n = 0;
ansond 51:3bdf57f7fd60 107 int i = 0;
sarahmarshy 50:a18a06b000f3 108
sarahmarshy 50:a18a06b000f3 109 for(i=0; i<RECV_RETRY; i++) {
ansond 51:3bdf57f7fd60 110 n = m_sock_p->receive((char *)buf,(int)sz) ;
ansond 51:3bdf57f7fd60 111 if (n >= 0) return n;
sarahmarshy 50:a18a06b000f3 112 }
ansond 51:3bdf57f7fd60 113 ERR("SocketReceive: receive failed. received: %d bytes. tried to receive: %d bytes\n", n, sz) ;
ansond 51:3bdf57f7fd60 114 return n;
sarahmarshy 50:a18a06b000f3 115 }
ansond 51:3bdf57f7fd60 116
ansond 51:3bdf57f7fd60 117 static int SocketSend(void* ssl, const unsigned char *buf, size_t sz)
sarahmarshy 50:a18a06b000f3 118 {
ansond 51:3bdf57f7fd60 119 int n = 0;
ansond 51:3bdf57f7fd60 120 n = m_sock_p->send((char *)buf,(int)sz);
ansond 51:3bdf57f7fd60 121 if(n > 0) return n;
ansond 51:3bdf57f7fd60 122 ERR("SocketSend: send failed. sent: %d bytes. tried to send: %d bytes\n", n, sz);
ansond 51:3bdf57f7fd60 123 return n;
ansond 51:3bdf57f7fd60 124 }
ansond 51:3bdf57f7fd60 125
ansond 51:3bdf57f7fd60 126 short ssl_socket_read(ssl_context *ssl, char *receiveBuffer, unsigned int receiveBufferSize, unsigned int timeoutMilliseconds) {
ansond 51:3bdf57f7fd60 127 int n = ssl_read(ssl,(unsigned char *)receiveBuffer, (size_t)receiveBufferSize);
ansond 51:3bdf57f7fd60 128 if (n <= 0) {
ansond 51:3bdf57f7fd60 129 ERR("ssl_socket_read failed\r\n");
ansond 51:3bdf57f7fd60 130 return -1;
ansond 51:3bdf57f7fd60 131 }
ansond 51:3bdf57f7fd60 132 DBG("ssl_socket_read: read %d bytes...",n);
ansond 51:3bdf57f7fd60 133 return n;
ansond 51:3bdf57f7fd60 134 }
ansond 51:3bdf57f7fd60 135
ansond 51:3bdf57f7fd60 136 short ssl_socket_write(ssl_context *ssl, const char *sendBuffer, unsigned int sendBufferSize) {
ansond 51:3bdf57f7fd60 137 sendBufferSize = strlen(sendBuffer);
ansond 51:3bdf57f7fd60 138 int n = ssl_write(ssl,(const unsigned char *)sendBuffer,(size_t)sendBufferSize);
ansond 51:3bdf57f7fd60 139 if (n != sendBufferSize) {
ansond 51:3bdf57f7fd60 140 ERR("ssl_socket_write failed written=%d\r\n",n);
ansond 51:3bdf57f7fd60 141 return -1;
ansond 51:3bdf57f7fd60 142 }
ansond 51:3bdf57f7fd60 143 DBG("ssl_socket_write: sent %d bytes...",sendBufferSize);
ansond 51:3bdf57f7fd60 144 return sendBufferSize;
sarahmarshy 50:a18a06b000f3 145 }
wolfSSL 22:4b9a4151cc73 146
ansond 51:3bdf57f7fd60 147 void HTTPClient::set_ssl_version() {
ansond 51:3bdf57f7fd60 148 // set the SSL version to be used
ansond 51:3bdf57f7fd60 149 ssl_set_max_version(&this->ssl,SSL_MAX_MAJOR_VERSION,SSL_MAX_MINOR_VERSION);
ansond 51:3bdf57f7fd60 150 if (this->SSLver == 0) {
ansond 51:3bdf57f7fd60 151 // use v3
ansond 51:3bdf57f7fd60 152 ssl_set_min_version(&this->ssl,SSL_MIN_MAJOR_VERSION,SSL_MIN_MINOR_VERSION);
ansond 51:3bdf57f7fd60 153 }
ansond 51:3bdf57f7fd60 154 if (this->SSLver == 1) {
ansond 51:3bdf57f7fd60 155 // use v1
ansond 51:3bdf57f7fd60 156 ssl_set_min_version(&this->ssl,SSL_MIN_MAJOR_VERSION,SSL_MINOR_VERSION_1);
ansond 51:3bdf57f7fd60 157 }
ansond 51:3bdf57f7fd60 158 if (this->SSLver == 2) {
ansond 51:3bdf57f7fd60 159 // use v1.1
ansond 51:3bdf57f7fd60 160 ssl_set_min_version(&this->ssl,SSL_MIN_MAJOR_VERSION,SSL_MINOR_VERSION_2);
ansond 51:3bdf57f7fd60 161 }
ansond 51:3bdf57f7fd60 162 if (this->SSLver == 3) {
ansond 51:3bdf57f7fd60 163 // use v1.2
ansond 51:3bdf57f7fd60 164 ssl_set_min_version(&this->ssl,SSL_MIN_MAJOR_VERSION,SSL_MINOR_VERSION_3);
ansond 51:3bdf57f7fd60 165 }
ansond 51:3bdf57f7fd60 166 }
ansond 51:3bdf57f7fd60 167
ansond 51:3bdf57f7fd60 168 void HTTPClient::ssl_setup(const unsigned char *tag) {
ansond 51:3bdf57f7fd60 169 entropy_init(&this->entropy);
ansond 51:3bdf57f7fd60 170 ctr_drbg_init(&this->ctr_drbg,entropy_func,&this->entropy,tag,strlen((const char *)tag));
ansond 51:3bdf57f7fd60 171 memset(&this->ssl,0,sizeof(ssl_context));
ansond 51:3bdf57f7fd60 172 this->ssl_connected = false;
ansond 51:3bdf57f7fd60 173 }
ansond 51:3bdf57f7fd60 174
ansond 51:3bdf57f7fd60 175 short HTTPClient::ssl_connect() {
ansond 51:3bdf57f7fd60 176 short status = 0;
ansond 51:3bdf57f7fd60 177 if (this->ssl_connected == false) {
ansond 51:3bdf57f7fd60 178 this->ssl_setup("mbed_https_lib"); // debug tag could be taken from constructor parameter... fix me...
ansond 51:3bdf57f7fd60 179 short status = ssl_init(&this->ssl);
ansond 51:3bdf57f7fd60 180 if (status == 0) {
ansond 51:3bdf57f7fd60 181 ssl_set_endpoint(&this->ssl,SSL_IS_CLIENT);
ansond 51:3bdf57f7fd60 182 this->set_ssl_version();
ansond 51:3bdf57f7fd60 183 ssl_set_rng(&this->ssl, ctr_drbg_random, &this->ctr_drbg);
ansond 51:3bdf57f7fd60 184 ssl_set_authmode(&this->ssl,SSL_VERIFY_NONE); // FIX ME:
ansond 51:3bdf57f7fd60 185 ssl_set_bio(&this->ssl,SocketReceive,(void *)NULL,SocketSend,(void *)NULL);
ansond 51:3bdf57f7fd60 186 this->ssl_connected = true;
ansond 51:3bdf57f7fd60 187 }
ansond 51:3bdf57f7fd60 188 DBG("ssl_connect: status=%d\r\n",status);
ansond 51:3bdf57f7fd60 189 }
ansond 51:3bdf57f7fd60 190 else {
ansond 51:3bdf57f7fd60 191 DBG("ssl_connect: already connected (OK) status=%d\r\n",status);
ansond 51:3bdf57f7fd60 192 }
ansond 51:3bdf57f7fd60 193 return status;
ansond 51:3bdf57f7fd60 194 }
ansond 51:3bdf57f7fd60 195
ansond 51:3bdf57f7fd60 196 HTTPClient::HTTPClient() : m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0), m_oauthToken(NULL)
donatien 0:2ccb9960a044 197 {
wolfSSL 22:4b9a4151cc73 198 SSLver = 3 ;
wolfSSL 27:5d4739eae63e 199 m_basicAuthUser = NULL ;
ansond 36:debaeb6006a7 200 m_basicAuthPassword = NULL;
ansond 36:debaeb6006a7 201 m_httpResponseCode = 0;
ansond 36:debaeb6006a7 202 m_oauthToken = NULL;
wolfSSL 27:5d4739eae63e 203 redirect_url = NULL ;
wolfSSL 27:5d4739eae63e 204 redirect = 0 ;
wolfSSL 27:5d4739eae63e 205 header = NULL ;
sarahmarshy 50:a18a06b000f3 206 m_sock_p = &m_sock;
donatien 0:2ccb9960a044 207 }
donatien 0:2ccb9960a044 208
donatien 0:2ccb9960a044 209 HTTPClient::~HTTPClient()
donatien 0:2ccb9960a044 210 {
donatien 0:2ccb9960a044 211
donatien 0:2ccb9960a044 212 }
donatien 0:2ccb9960a044 213
sarahmarshy 50:a18a06b000f3 214
ansond 36:debaeb6006a7 215 HTTPResult HTTPClient::oauthToken(const char *token) { // OAUTH2 Authentication
ansond 36:debaeb6006a7 216 // reset if called
ansond 36:debaeb6006a7 217 if (m_oauthToken != NULL) free((void *)m_oauthToken);
ansond 36:debaeb6006a7 218 m_oauthToken = NULL;
ansond 36:debaeb6006a7 219
ansond 36:debaeb6006a7 220 // fill in if able...
ansond 36:debaeb6006a7 221 if (token != NULL && strlen(token) > 0) {
ansond 36:debaeb6006a7 222 m_oauthToken = (char *)malloc(strlen(token)+1);
ansond 36:debaeb6006a7 223 memset((void *)m_oauthToken,0,strlen(token)+1);
ansond 36:debaeb6006a7 224 strcpy((char *)m_oauthToken,token);
ansond 36:debaeb6006a7 225 this->basicAuth(NULL,NULL);
ansond 36:debaeb6006a7 226 }
ansond 36:debaeb6006a7 227
ansond 36:debaeb6006a7 228 return HTTP_OK ;
ansond 36:debaeb6006a7 229 }
ansond 36:debaeb6006a7 230
wolfSSL 22:4b9a4151cc73 231 HTTPResult HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
donatien 0:2ccb9960a044 232 {
wolfSSL 22:4b9a4151cc73 233 #define AUTHB_SIZE 128
wolfSSL 22:4b9a4151cc73 234 if((strlen(user) + strlen(password)) >= AUTHB_SIZE)
wolfSSL 22:4b9a4151cc73 235 return HTTP_ERROR ;
ansond 30:6fef375c94e6 236
ansond 30:6fef375c94e6 237 if (m_basicAuthUser) free((void *)m_basicAuthUser);
ansond 30:6fef375c94e6 238 if (user != NULL) {
ansond 30:6fef375c94e6 239 m_basicAuthUser = (char *)malloc(strlen(user)+1);
ansond 30:6fef375c94e6 240 strcpy((char *)m_basicAuthUser, user);
ansond 36:debaeb6006a7 241 this->oauthToken(NULL);
ansond 30:6fef375c94e6 242 }
ansond 30:6fef375c94e6 243
ansond 30:6fef375c94e6 244 if (m_basicAuthPassword) free((void *)m_basicAuthPassword);
ansond 30:6fef375c94e6 245 if (password != NULL) {
ansond 30:6fef375c94e6 246 m_basicAuthPassword = (char *)malloc(strlen(password)+1);
ansond 30:6fef375c94e6 247 strcpy((char *)m_basicAuthPassword, password);
ansond 36:debaeb6006a7 248 this->oauthToken(NULL);
ansond 30:6fef375c94e6 249 }
ansond 30:6fef375c94e6 250
wolfSSL 22:4b9a4151cc73 251 return HTTP_OK ;
donatien 0:2ccb9960a044 252 }
donatien 0:2ccb9960a044 253
donatien 12:89d09a6db00a 254 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 255 {
wolfSSL 18:d89df40b4cf3 256 return connect(url, HTTP_GET, NULL, pDataIn, timeout);
donatien 0:2ccb9960a044 257 }
donatien 0:2ccb9960a044 258
donatien 12:89d09a6db00a 259 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 260 {
wolfSSL 18:d89df40b4cf3 261 HTTPText str(result, maxResultLen);
wolfSSL 18:d89df40b4cf3 262 return get(url, &str, timeout);
donatien 0:2ccb9960a044 263 }
donatien 0:2ccb9960a044 264
donatien 12:89d09a6db00a 265 HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 266 {
wolfSSL 18:d89df40b4cf3 267 return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
donatien 0:2ccb9960a044 268 }
donatien 0:2ccb9960a044 269
donatien 16:1f743885e7de 270 HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 16:1f743885e7de 271 {
wolfSSL 18:d89df40b4cf3 272 return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
donatien 16:1f743885e7de 273 }
donatien 16:1f743885e7de 274
donatien 16:1f743885e7de 275 HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 16:1f743885e7de 276 {
wolfSSL 18:d89df40b4cf3 277 return connect(url, HTTP_DELETE, NULL, pDataIn, timeout);
donatien 16:1f743885e7de 278 }
donatien 16:1f743885e7de 279
donatien 16:1f743885e7de 280
donatien 0:2ccb9960a044 281 int HTTPClient::getHTTPResponseCode()
donatien 0:2ccb9960a044 282 {
wolfSSL 18:d89df40b4cf3 283 return m_httpResponseCode;
donatien 0:2ccb9960a044 284 }
donatien 0:2ccb9960a044 285
wolfSSL 27:5d4739eae63e 286 void HTTPClient::setHeader(const char * h)
wolfSSL 17:c73d8e61d391 287 {
wolfSSL 17:c73d8e61d391 288 header = h ;
wolfSSL 17:c73d8e61d391 289 }
wolfSSL 17:c73d8e61d391 290
wolfSSL 27:5d4739eae63e 291 void HTTPClient::setLocationBuf(char * url, int size)
wolfSSL 27:5d4739eae63e 292 {
wolfSSL 27:5d4739eae63e 293 redirect_url = url ;
wolfSSL 27:5d4739eae63e 294 redirect_url_size = size ;
wolfSSL 27:5d4739eae63e 295 }
wolfSSL 27:5d4739eae63e 296
wolfSSL 22:4b9a4151cc73 297 HTTPResult HTTPClient::setSSLversion(int minorV)
wolfSSL 22:4b9a4151cc73 298 {
wolfSSL 22:4b9a4151cc73 299 if((minorV>=0) && (minorV<=3))
wolfSSL 22:4b9a4151cc73 300 SSLver = minorV ;
wolfSSL 22:4b9a4151cc73 301 else return HTTP_ERROR ;
wolfSSL 22:4b9a4151cc73 302 return HTTP_OK ;
wolfSSL 22:4b9a4151cc73 303 }
wolfSSL 22:4b9a4151cc73 304
wolfSSL 17:c73d8e61d391 305
donatien 5:791fc3dcb6c4 306 #define CHECK_CONN_ERR(ret) \
donatien 5:791fc3dcb6c4 307 do{ \
donatien 7:4e39864f7b15 308 if(ret) { \
ansond 51:3bdf57f7fd60 309 ssl_cleanup() ;\
donatien 7:4e39864f7b15 310 m_sock.close(); \
donatien 5:791fc3dcb6c4 311 ERR("Connection error (%d)", ret); \
donatien 11:390362de8c3f 312 return HTTP_CONN; \
donatien 5:791fc3dcb6c4 313 } \
donatien 5:791fc3dcb6c4 314 } while(0)
donatien 5:791fc3dcb6c4 315
donatien 5:791fc3dcb6c4 316 #define PRTCL_ERR() \
donatien 5:791fc3dcb6c4 317 do{ \
ansond 51:3bdf57f7fd60 318 ssl_cleanup() ;\
donatien 7:4e39864f7b15 319 m_sock.close(); \
donatien 5:791fc3dcb6c4 320 ERR("Protocol error"); \
donatien 11:390362de8c3f 321 return HTTP_PRTCL; \
donatien 5:791fc3dcb6c4 322 } while(0)
donatien 0:2ccb9960a044 323
ansond 51:3bdf57f7fd60 324 void HTTPClient::ssl_cleanup(void)
wolfSSL 17:c73d8e61d391 325 {
ansond 51:3bdf57f7fd60 326 ssl_free( &this->ssl );
ansond 51:3bdf57f7fd60 327 ctr_drbg_free( &this->ctr_drbg );
ansond 51:3bdf57f7fd60 328 entropy_free( &this->entropy );
ansond 51:3bdf57f7fd60 329 this->ssl_connected = false;
wolfSSL 22:4b9a4151cc73 330 }
wolfSSL 17:c73d8e61d391 331
donatien 12:89d09a6db00a 332 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
wolfSSL 18:d89df40b4cf3 333 {
wolfSSL 18:d89df40b4cf3 334 m_httpResponseCode = 0; //Invalidate code
wolfSSL 18:d89df40b4cf3 335 m_timeout = timeout;
wolfSSL 27:5d4739eae63e 336 redirect = 0 ;
wolfSSL 27:5d4739eae63e 337
ansond 46:7cd69cc809b8 338 // reset the send buffer
ansond 46:7cd69cc809b8 339 memset(send_buf,0,SEND_BUF_SIZE);
ansond 46:7cd69cc809b8 340
wolfSSL 18:d89df40b4cf3 341 pDataIn->writeReset();
wolfSSL 18:d89df40b4cf3 342 if( pDataOut ) {
wolfSSL 18:d89df40b4cf3 343 pDataOut->readReset();
wolfSSL 18:d89df40b4cf3 344 }
wolfSSL 17:c73d8e61d391 345
wolfSSL 18:d89df40b4cf3 346 char scheme[8];
ansond 29:2d96cc752d19 347 char host[MAX_URL_HOSTNAME_LENGTH];
ansond 29:2d96cc752d19 348 char path[MAX_URL_PATH_LENGTH];
wolfSSL 18:d89df40b4cf3 349
wolfSSL 18:d89df40b4cf3 350 int ret ;
donatien 0:2ccb9960a044 351
wolfSSL 18:d89df40b4cf3 352 //First we need to parse the url (http[s]://host[:port][/[path]])
wolfSSL 18:d89df40b4cf3 353 HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
wolfSSL 18:d89df40b4cf3 354 if(res != HTTP_OK) {
wolfSSL 18:d89df40b4cf3 355 ERR("parseURL returned %d", res);
wolfSSL 18:d89df40b4cf3 356 return res;
wolfSSL 18:d89df40b4cf3 357 }
donatien 0:2ccb9960a044 358
wolfSSL 22:4b9a4151cc73 359 if(port == 0) {
wolfSSL 18:d89df40b4cf3 360 if(strcmp(scheme, "http") == 0)
wolfSSL 18:d89df40b4cf3 361 port = HTTP_PORT ;
wolfSSL 18:d89df40b4cf3 362 else if(strcmp(scheme, "https") == 0)
wolfSSL 18:d89df40b4cf3 363 port = HTTPS_PORT ;
wolfSSL 18:d89df40b4cf3 364 }
donatien 0:2ccb9960a044 365
wolfSSL 18:d89df40b4cf3 366 DBG("Scheme: %s", scheme);
wolfSSL 18:d89df40b4cf3 367 DBG("Host: %s", host);
wolfSSL 18:d89df40b4cf3 368 DBG("Port: %d", port);
wolfSSL 18:d89df40b4cf3 369 DBG("Path: %s", path);
wolfSSL 17:c73d8e61d391 370
wolfSSL 18:d89df40b4cf3 371 //Connect
wolfSSL 18:d89df40b4cf3 372 DBG("Connecting socket to server");
wolfSSL 18:d89df40b4cf3 373
wolfSSL 18:d89df40b4cf3 374 #define MAX_RETRY 5
wolfSSL 18:d89df40b4cf3 375 int retry ;
donatien 0:2ccb9960a044 376
wolfSSL 18:d89df40b4cf3 377 for(retry=0; retry<MAX_RETRY; retry++) {
wolfSSL 18:d89df40b4cf3 378 int ret = m_sock.connect(host, port);
wolfSSL 18:d89df40b4cf3 379 if(ret == 0)break ;
wolfSSL 17:c73d8e61d391 380 }
wolfSSL 18:d89df40b4cf3 381 if(retry == MAX_RETRY) {
wolfSSL 18:d89df40b4cf3 382 m_sock.close();
wolfSSL 18:d89df40b4cf3 383 ERR("Could not connect");
wolfSSL 18:d89df40b4cf3 384 return HTTP_CONN;
wolfSSL 17:c73d8e61d391 385 }
wolfSSL 17:c73d8e61d391 386
wolfSSL 18:d89df40b4cf3 387 if(port == HTTPS_PORT) {
wolfSSL 18:d89df40b4cf3 388 /* Start SSL connect */
ansond 51:3bdf57f7fd60 389 DBG("SSLver=%d", SSLver);
ansond 51:3bdf57f7fd60 390 short ssl_connect_status = this->ssl_connect();
ansond 51:3bdf57f7fd60 391 if (ssl_connect_status != 0) {
ansond 51:3bdf57f7fd60 392 ERR("SSL connection failure: %d\r\n",ssl_connect_status);
ansond 51:3bdf57f7fd60 393 ssl_cleanup();
wolfSSL 18:d89df40b4cf3 394 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 395 }
ansond 51:3bdf57f7fd60 396 DBG("SSL connection succeeded...\r\n");
wolfSSL 18:d89df40b4cf3 397 } /* SSL connect complete */
donatien 0:2ccb9960a044 398
wolfSSL 18:d89df40b4cf3 399 //Send request
wolfSSL 18:d89df40b4cf3 400 DBG("Sending request");
wolfSSL 18:d89df40b4cf3 401 char buf[CHUNK_SIZE];
ansond 30:6fef375c94e6 402 memset(buf,0,CHUNK_SIZE);
wolfSSL 18:d89df40b4cf3 403 send_buf_p = send_buf ; // Reset send buffer ;
wolfSSL 18:d89df40b4cf3 404
wolfSSL 18:d89df40b4cf3 405 const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":"";
ansond 53:fa91a40cecb5 406 snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\nAccept: */*\r\nAccept-Language: en-us\r\nUser-Agent: curl/7.43.0\r\n", meth, path, host); //Write request
wolfSSL 18:d89df40b4cf3 407 ret = send(buf);
wolfSSL 18:d89df40b4cf3 408 if(ret) {
wolfSSL 18:d89df40b4cf3 409 m_sock.close();
wolfSSL 18:d89df40b4cf3 410 ERR("Could not write request");
wolfSSL 18:d89df40b4cf3 411 return HTTP_CONN;
donatien 0:2ccb9960a044 412 }
wolfSSL 17:c73d8e61d391 413
wolfSSL 18:d89df40b4cf3 414 //Send all headers
donatien 0:2ccb9960a044 415
wolfSSL 18:d89df40b4cf3 416 //Send default headers
wolfSSL 18:d89df40b4cf3 417 DBG("Sending headers");
ansond 31:0675a342e45c 418 if(m_basicAuthUser && m_basicAuthPassword) {
ansond 36:debaeb6006a7 419 DBG("Sending basic auth header");
ansond 36:debaeb6006a7 420 bAuth() ; /* send out Basic Auth header */
ansond 36:debaeb6006a7 421 }
ansond 36:debaeb6006a7 422 else if (m_oauthToken) {
ansond 36:debaeb6006a7 423 DBG("Sending OAUTH header");
ansond 36:debaeb6006a7 424 tokenAuth(); /* send out OAUTH header */
wolfSSL 27:5d4739eae63e 425 }
wolfSSL 18:d89df40b4cf3 426 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 427 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 428 ret = send("Transfer-Encoding: chunked\r\n");
wolfSSL 18:d89df40b4cf3 429 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 430 } else {
wolfSSL 18:d89df40b4cf3 431 snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
wolfSSL 22:4b9a4151cc73 432 DBG("Content buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 433 ret = send(buf);
wolfSSL 18:d89df40b4cf3 434 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 435 }
wolfSSL 18:d89df40b4cf3 436 char type[48];
wolfSSL 18:d89df40b4cf3 437 if( pDataOut->getDataType(type, 48) == HTTP_OK ) {
wolfSSL 18:d89df40b4cf3 438 snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
wolfSSL 18:d89df40b4cf3 439 ret = send(buf);
wolfSSL 18:d89df40b4cf3 440 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 441 }
wolfSSL 18:d89df40b4cf3 442 }
wolfSSL 18:d89df40b4cf3 443
wolfSSL 18:d89df40b4cf3 444 //Add user headers
wolfSSL 18:d89df40b4cf3 445 if(header) {
wolfSSL 27:5d4739eae63e 446 ret = send((char *)header);
donatien 5:791fc3dcb6c4 447 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 448 }
donatien 0:2ccb9960a044 449
wolfSSL 18:d89df40b4cf3 450 //Close headers
wolfSSL 18:d89df40b4cf3 451 DBG("Headers sent");
wolfSSL 18:d89df40b4cf3 452 ret = send("\r\n");
wolfSSL 18:d89df40b4cf3 453 CHECK_CONN_ERR(ret);
wolfSSL 17:c73d8e61d391 454
wolfSSL 18:d89df40b4cf3 455 size_t trfLen;
donatien 0:2ccb9960a044 456
wolfSSL 18:d89df40b4cf3 457 //Send data (if available)
wolfSSL 18:d89df40b4cf3 458 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 459 DBG("Sending data");
wolfSSL 18:d89df40b4cf3 460 while(true) {
wolfSSL 18:d89df40b4cf3 461 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 462 pDataOut->read(buf, CHUNK_SIZE, &trfLen);
wolfSSL 18:d89df40b4cf3 463 buf[trfLen] = 0x0 ;
wolfSSL 18:d89df40b4cf3 464 DBG("buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 465 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 466 //Write chunk header
wolfSSL 22:4b9a4151cc73 467 char chunkHeader[64];
wolfSSL 18:d89df40b4cf3 468 snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
wolfSSL 18:d89df40b4cf3 469 ret = send(chunkHeader);
wolfSSL 18:d89df40b4cf3 470 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 471 } else if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 472 DBG("trfLen==0") ;
wolfSSL 18:d89df40b4cf3 473 break;
wolfSSL 18:d89df40b4cf3 474 }
wolfSSL 22:4b9a4151cc73 475 DBG("trfLen 1=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 476 if( trfLen != 0 ) {
wolfSSL 22:4b9a4151cc73 477 DBG("Sending 1") ;
wolfSSL 18:d89df40b4cf3 478 ret = send(buf, trfLen);
wolfSSL 22:4b9a4151cc73 479 DBG("Sent 1") ;
wolfSSL 18:d89df40b4cf3 480 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 481 }
donatien 0:2ccb9960a044 482
wolfSSL 18:d89df40b4cf3 483 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 484 ret = send("\r\n"); //Chunk-terminating CRLF
wolfSSL 18:d89df40b4cf3 485 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 486 } else {
wolfSSL 18:d89df40b4cf3 487 writtenLen += trfLen;
wolfSSL 18:d89df40b4cf3 488 if( writtenLen >= pDataOut->getDataLen() ) {
wolfSSL 22:4b9a4151cc73 489 DBG("writtenLen=%d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 490 break;
wolfSSL 18:d89df40b4cf3 491 }
wolfSSL 22:4b9a4151cc73 492 DBG("writtenLen+=trfLen = %d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 493 }
wolfSSL 22:4b9a4151cc73 494 DBG("trfLen 2=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 495 if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 496 DBG("trfLen == 0") ;
wolfSSL 18:d89df40b4cf3 497 break;
wolfSSL 18:d89df40b4cf3 498 }
wolfSSL 18:d89df40b4cf3 499 }
donatien 0:2ccb9960a044 500
wolfSSL 18:d89df40b4cf3 501 }
wolfSSL 18:d89df40b4cf3 502 ret = flush() ; // flush the send buffer ;
wolfSSL 18:d89df40b4cf3 503 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 504
wolfSSL 18:d89df40b4cf3 505 //Receive response
wolfSSL 18:d89df40b4cf3 506 DBG("Receiving response");
wolfSSL 18:d89df40b4cf3 507
wolfSSL 18:d89df40b4cf3 508 ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
wolfSSL 18:d89df40b4cf3 509 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 510
wolfSSL 18:d89df40b4cf3 511 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 512
wolfSSL 18:d89df40b4cf3 513 char* crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 514 if(crlfPtr == NULL) {
donatien 5:791fc3dcb6c4 515 PRTCL_ERR();
donatien 0:2ccb9960a044 516 }
donatien 0:2ccb9960a044 517
wolfSSL 18:d89df40b4cf3 518 int crlfPos = crlfPtr - buf;
donatien 0:2ccb9960a044 519 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 520
wolfSSL 18:d89df40b4cf3 521 //Parse HTTP response
wolfSSL 18:d89df40b4cf3 522 if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) {
wolfSSL 18:d89df40b4cf3 523 //Cannot match string, error
wolfSSL 18:d89df40b4cf3 524 ERR("Not a correct HTTP answer : %s\n", buf);
wolfSSL 18:d89df40b4cf3 525 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 526 }
donatien 4:c071b05ac026 527
wolfSSL 27:5d4739eae63e 528 if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) {
wolfSSL 18:d89df40b4cf3 529 //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers
wolfSSL 18:d89df40b4cf3 530 WARN("Response code %d", m_httpResponseCode);
wolfSSL 18:d89df40b4cf3 531 PRTCL_ERR();
donatien 0:2ccb9960a044 532 }
donatien 0:2ccb9960a044 533
wolfSSL 18:d89df40b4cf3 534 DBG("Reading headers");
donatien 0:2ccb9960a044 535
wolfSSL 18:d89df40b4cf3 536 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 537 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 538
wolfSSL 18:d89df40b4cf3 539 size_t recvContentLength = 0;
wolfSSL 18:d89df40b4cf3 540 bool recvChunked = false;
wolfSSL 18:d89df40b4cf3 541 //Now get headers
wolfSSL 18:d89df40b4cf3 542 while( true ) {
wolfSSL 18:d89df40b4cf3 543 crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 544 if(crlfPtr == NULL) {
wolfSSL 18:d89df40b4cf3 545 if( trfLen < CHUNK_SIZE - 1 ) {
ansond 30:6fef375c94e6 546 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 547 ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 548 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 549 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 550 DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
wolfSSL 18:d89df40b4cf3 551 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 552 continue;
wolfSSL 18:d89df40b4cf3 553 } else {
wolfSSL 18:d89df40b4cf3 554 PRTCL_ERR();
donatien 14:2744e0c0e527 555 }
wolfSSL 18:d89df40b4cf3 556 }
wolfSSL 18:d89df40b4cf3 557
wolfSSL 18:d89df40b4cf3 558 crlfPos = crlfPtr - buf;
wolfSSL 18:d89df40b4cf3 559
wolfSSL 18:d89df40b4cf3 560 if(crlfPos == 0) { //End of headers
wolfSSL 18:d89df40b4cf3 561 DBG("Headers read");
wolfSSL 18:d89df40b4cf3 562 memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 563 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 564 break;
donatien 0:2ccb9960a044 565 }
wolfSSL 18:d89df40b4cf3 566
wolfSSL 18:d89df40b4cf3 567 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 568
ansond 46:7cd69cc809b8 569 char key[MAX_HEADER_KEY_LENGTH+1];
ansond 46:7cd69cc809b8 570 memset(key,0,MAX_HEADER_KEY_LENGTH+1);
ansond 46:7cd69cc809b8 571
ansond 46:7cd69cc809b8 572 char value[MAX_HEADER_VALUE_LENGTH+1];
ansond 46:7cd69cc809b8 573 memset(value,0,MAX_HEADER_VALUE_LENGTH+1);
ansond 46:7cd69cc809b8 574
ansond 46:7cd69cc809b8 575 int n = sscanf(buf, HEADER_SCANF_FORMAT, key, value);
wolfSSL 18:d89df40b4cf3 576 if ( n == 2 ) {
wolfSSL 18:d89df40b4cf3 577 DBG("Read header : %s: %s\n", key, value);
ansond 52:cea1021a822d 578 if( !strcmp(key, "Content-Length") || !strcmp(key, "content-length")) {
wolfSSL 18:d89df40b4cf3 579 sscanf(value, "%d", &recvContentLength);
wolfSSL 18:d89df40b4cf3 580 pDataIn->setDataLen(recvContentLength);
ansond 52:cea1021a822d 581 } else if( !strcmp(key, "Transfer-Encoding") || !strcmp(key, "transfer-encoding")) {
wolfSSL 18:d89df40b4cf3 582 if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) {
wolfSSL 18:d89df40b4cf3 583 recvChunked = true;
wolfSSL 18:d89df40b4cf3 584 pDataIn->setIsChunked(true);
wolfSSL 18:d89df40b4cf3 585 }
ansond 52:cea1021a822d 586 } else if( !strcmp(key, "Content-Type") || !strcmp(key, "content-type") ) {
wolfSSL 18:d89df40b4cf3 587 pDataIn->setDataType(value);
ansond 52:cea1021a822d 588 } else if( (!strcmp(key, "Location") || !strcmp(key, "location") )&& redirect_url) {
ansond 46:7cd69cc809b8 589 sscanf(buf, REDIRECT_SCANF_FORMAT, key, redirect_url);
wolfSSL 27:5d4739eae63e 590 DBG("Redirect %s: %s", key, redirect_url) ;
wolfSSL 27:5d4739eae63e 591 redirect = 1 ;
wolfSSL 18:d89df40b4cf3 592 }
wolfSSL 18:d89df40b4cf3 593 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 594 trfLen -= (crlfPos + 2);
wolfSSL 18:d89df40b4cf3 595
wolfSSL 18:d89df40b4cf3 596 } else {
wolfSSL 18:d89df40b4cf3 597 ERR("Could not parse header");
donatien 14:2744e0c0e527 598 PRTCL_ERR();
donatien 0:2ccb9960a044 599 }
donatien 0:2ccb9960a044 600
donatien 0:2ccb9960a044 601 }
donatien 0:2ccb9960a044 602
wolfSSL 18:d89df40b4cf3 603 //Receive data
wolfSSL 18:d89df40b4cf3 604 DBG("Receiving data");
wolfSSL 18:d89df40b4cf3 605
wolfSSL 18:d89df40b4cf3 606 while(true) {
wolfSSL 18:d89df40b4cf3 607 size_t readLen = 0;
donatien 0:2ccb9960a044 608
wolfSSL 18:d89df40b4cf3 609 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 610 //Read chunk header
wolfSSL 18:d89df40b4cf3 611 bool foundCrlf;
wolfSSL 18:d89df40b4cf3 612 do {
wolfSSL 18:d89df40b4cf3 613 foundCrlf = false;
wolfSSL 18:d89df40b4cf3 614 crlfPos=0;
wolfSSL 18:d89df40b4cf3 615 buf[trfLen]=0;
wolfSSL 18:d89df40b4cf3 616 if(trfLen >= 2) {
wolfSSL 18:d89df40b4cf3 617 for(; crlfPos < trfLen - 2; crlfPos++) {
wolfSSL 18:d89df40b4cf3 618 if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) {
wolfSSL 18:d89df40b4cf3 619 foundCrlf = true;
wolfSSL 18:d89df40b4cf3 620 break;
wolfSSL 18:d89df40b4cf3 621 }
wolfSSL 18:d89df40b4cf3 622 }
wolfSSL 18:d89df40b4cf3 623 }
wolfSSL 18:d89df40b4cf3 624 if(!foundCrlf) { //Try to read more
wolfSSL 18:d89df40b4cf3 625 if( trfLen < CHUNK_SIZE ) {
ansond 30:6fef375c94e6 626 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 627 ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 628 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 629 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 630 continue;
wolfSSL 18:d89df40b4cf3 631 } else {
wolfSSL 18:d89df40b4cf3 632 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 633 }
wolfSSL 18:d89df40b4cf3 634 }
wolfSSL 18:d89df40b4cf3 635 } while(!foundCrlf);
wolfSSL 18:d89df40b4cf3 636 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 637 int n = sscanf(buf, "%x", &readLen);
wolfSSL 18:d89df40b4cf3 638 if(n!=1) {
wolfSSL 18:d89df40b4cf3 639 ERR("Could not read chunk length");
wolfSSL 18:d89df40b4cf3 640 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 641 }
wolfSSL 18:d89df40b4cf3 642
wolfSSL 18:d89df40b4cf3 643 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
wolfSSL 18:d89df40b4cf3 644 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 645
wolfSSL 18:d89df40b4cf3 646 if( readLen == 0 ) {
wolfSSL 18:d89df40b4cf3 647 //Last chunk
wolfSSL 18:d89df40b4cf3 648 break;
wolfSSL 18:d89df40b4cf3 649 }
wolfSSL 18:d89df40b4cf3 650 } else {
wolfSSL 18:d89df40b4cf3 651 readLen = recvContentLength;
wolfSSL 18:d89df40b4cf3 652 }
wolfSSL 18:d89df40b4cf3 653
wolfSSL 18:d89df40b4cf3 654 DBG("Retrieving %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 655
wolfSSL 18:d89df40b4cf3 656 do {
wolfSSL 18:d89df40b4cf3 657 pDataIn->write(buf, MIN(trfLen, readLen));
wolfSSL 18:d89df40b4cf3 658 if( trfLen > readLen ) {
wolfSSL 18:d89df40b4cf3 659 memmove(buf, &buf[readLen], trfLen - readLen);
wolfSSL 18:d89df40b4cf3 660 trfLen -= readLen;
wolfSSL 18:d89df40b4cf3 661 readLen = 0;
wolfSSL 18:d89df40b4cf3 662 } else {
wolfSSL 18:d89df40b4cf3 663 readLen -= trfLen;
wolfSSL 18:d89df40b4cf3 664 }
donatien 0:2ccb9960a044 665
wolfSSL 18:d89df40b4cf3 666 if(readLen) {
wolfSSL 18:d89df40b4cf3 667 ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
wolfSSL 18:d89df40b4cf3 668 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 669 }
wolfSSL 18:d89df40b4cf3 670 } while(readLen);
wolfSSL 18:d89df40b4cf3 671
wolfSSL 18:d89df40b4cf3 672 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 673 if(trfLen < 2) {
ansond 30:6fef375c94e6 674 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 675 //Read missing chars to find end of chunk
wolfSSL 18:d89df40b4cf3 676 ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 677 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 678 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 679 }
wolfSSL 18:d89df40b4cf3 680 if( (buf[0] != '\r') || (buf[1] != '\n') ) {
wolfSSL 18:d89df40b4cf3 681 ERR("Format error");
wolfSSL 18:d89df40b4cf3 682 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 683 }
wolfSSL 18:d89df40b4cf3 684 memmove(buf, &buf[2], trfLen - 2);
wolfSSL 18:d89df40b4cf3 685 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 686 } else {
wolfSSL 18:d89df40b4cf3 687 break;
wolfSSL 18:d89df40b4cf3 688 }
wolfSSL 18:d89df40b4cf3 689
donatien 0:2ccb9960a044 690 }
ansond 51:3bdf57f7fd60 691 ssl_cleanup() ;
wolfSSL 18:d89df40b4cf3 692 m_sock.close();
wolfSSL 18:d89df40b4cf3 693 DBG("Completed HTTP transaction");
wolfSSL 27:5d4739eae63e 694 if(redirect)return HTTP_REDIRECT ;
wolfSSL 27:5d4739eae63e 695 else return HTTP_OK;
donatien 0:2ccb9960a044 696 }
donatien 0:2ccb9960a044 697
wolfSSL 19:1e2f05809eb1 698 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
donatien 0:2ccb9960a044 699 {
wolfSSL 18:d89df40b4cf3 700 DBG("Trying to read between %d and %d bytes", minLen, maxLen);
wolfSSL 18:d89df40b4cf3 701 size_t readLen = 0;
wolfSSL 18:d89df40b4cf3 702
wolfSSL 18:d89df40b4cf3 703 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 704 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 705 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 706 }
wolfSSL 18:d89df40b4cf3 707
wolfSSL 18:d89df40b4cf3 708 int ret;
wolfSSL 18:d89df40b4cf3 709
wolfSSL 18:d89df40b4cf3 710 if(port == HTTPS_PORT) {
ansond 51:3bdf57f7fd60 711 DBG("Enter ssl_socket_read") ;
wolfSSL 18:d89df40b4cf3 712
wolfSSL 18:d89df40b4cf3 713 m_sock.set_blocking(false, m_timeout);
ansond 51:3bdf57f7fd60 714 readLen = ssl_socket_read(&this->ssl, buf, maxLen, m_timeout);
wolfSSL 18:d89df40b4cf3 715 if (readLen > 0) {
wolfSSL 18:d89df40b4cf3 716 buf[readLen] = 0;
ansond 51:3bdf57f7fd60 717 DBG("ssl_socket_read:%s\n", buf);
wolfSSL 18:d89df40b4cf3 718 } else {
ansond 51:3bdf57f7fd60 719 ERR("ssl_socket_read, ret = %d", readLen) ;
wolfSSL 18:d89df40b4cf3 720 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 721 }
wolfSSL 18:d89df40b4cf3 722 DBG("Read %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 723 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 724 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 725 }
wolfSSL 18:d89df40b4cf3 726
wolfSSL 18:d89df40b4cf3 727 while(readLen < maxLen) {
wolfSSL 18:d89df40b4cf3 728 if(readLen < minLen) {
wolfSSL 18:d89df40b4cf3 729 DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
wolfSSL 18:d89df40b4cf3 730 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 731 ret = m_sock.receive_all(buf + readLen, minLen - readLen);
wolfSSL 18:d89df40b4cf3 732 } else {
wolfSSL 18:d89df40b4cf3 733 DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
wolfSSL 18:d89df40b4cf3 734 m_sock.set_blocking(false, 0);
wolfSSL 18:d89df40b4cf3 735 ret = m_sock.receive(buf + readLen, maxLen - readLen);
wolfSSL 18:d89df40b4cf3 736 }
wolfSSL 18:d89df40b4cf3 737
wolfSSL 18:d89df40b4cf3 738 if( ret > 0) {
wolfSSL 18:d89df40b4cf3 739 readLen += ret;
wolfSSL 18:d89df40b4cf3 740 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 741 break;
wolfSSL 18:d89df40b4cf3 742 } else {
wolfSSL 18:d89df40b4cf3 743 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 744 ERR("Connection error (recv returned %d)", ret);
wolfSSL 18:d89df40b4cf3 745 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 746 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 747 } else {
wolfSSL 18:d89df40b4cf3 748 break;
wolfSSL 18:d89df40b4cf3 749 }
wolfSSL 18:d89df40b4cf3 750 }
wolfSSL 18:d89df40b4cf3 751
wolfSSL 18:d89df40b4cf3 752 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 753 break;
wolfSSL 18:d89df40b4cf3 754 }
wolfSSL 17:c73d8e61d391 755 }
wolfSSL 17:c73d8e61d391 756 DBG("Read %d bytes", readLen);
wolfSSL 17:c73d8e61d391 757 *pReadLen = readLen;
wolfSSL 17:c73d8e61d391 758 return HTTP_OK;
donatien 7:4e39864f7b15 759 }
donatien 7:4e39864f7b15 760
wolfSSL 19:1e2f05809eb1 761 HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
donatien 7:4e39864f7b15 762 {
wolfSSL 18:d89df40b4cf3 763 HTTPResult ret ;
wolfSSL 18:d89df40b4cf3 764 int cp_len ;
wolfSSL 18:d89df40b4cf3 765
wolfSSL 18:d89df40b4cf3 766 if(len == 0) {
wolfSSL 18:d89df40b4cf3 767 len = strlen(buf);
wolfSSL 17:c73d8e61d391 768 }
wolfSSL 17:c73d8e61d391 769
wolfSSL 18:d89df40b4cf3 770 do {
wolfSSL 22:4b9a4151cc73 771
wolfSSL 18:d89df40b4cf3 772 if((SEND_BUF_SIZE - (send_buf_p - send_buf)) >= len) {
wolfSSL 18:d89df40b4cf3 773 cp_len = len ;
wolfSSL 18:d89df40b4cf3 774 } else {
wolfSSL 22:4b9a4151cc73 775 cp_len = SEND_BUF_SIZE - (send_buf_p - send_buf) ;
wolfSSL 18:d89df40b4cf3 776 }
wolfSSL 22:4b9a4151cc73 777 DBG("send_buf_p:%x. send_buf+SIZE:%x, len=%d, cp_len=%d", send_buf_p, send_buf+SEND_BUF_SIZE, len, cp_len) ;
wolfSSL 18:d89df40b4cf3 778 memcpy(send_buf_p, buf, cp_len) ;
wolfSSL 18:d89df40b4cf3 779 send_buf_p += cp_len ;
wolfSSL 18:d89df40b4cf3 780 len -= cp_len ;
wolfSSL 18:d89df40b4cf3 781
wolfSSL 18:d89df40b4cf3 782 if(send_buf_p == send_buf + SEND_BUF_SIZE) {
wolfSSL 22:4b9a4151cc73 783 if(port == HTTPS_PORT){
wolfSSL 22:4b9a4151cc73 784 ERR("HTTPClient::send buffer overflow");
wolfSSL 22:4b9a4151cc73 785 return HTTP_ERROR ;
wolfSSL 22:4b9a4151cc73 786 }
wolfSSL 18:d89df40b4cf3 787 ret = flush() ;
wolfSSL 18:d89df40b4cf3 788 if(ret)return(ret) ;
wolfSSL 18:d89df40b4cf3 789 }
wolfSSL 18:d89df40b4cf3 790 } while(len) ;
wolfSSL 18:d89df40b4cf3 791 return HTTP_OK ;
wolfSSL 17:c73d8e61d391 792 }
wolfSSL 17:c73d8e61d391 793
wolfSSL 19:1e2f05809eb1 794 HTTPResult HTTPClient::flush() //0 on success, err code on failure
wolfSSL 17:c73d8e61d391 795 {
wolfSSL 18:d89df40b4cf3 796 int len ;
wolfSSL 18:d89df40b4cf3 797 char * buf ;
wolfSSL 18:d89df40b4cf3 798
wolfSSL 18:d89df40b4cf3 799 buf = send_buf ;
wolfSSL 18:d89df40b4cf3 800 len = send_buf_p - send_buf ;
wolfSSL 18:d89df40b4cf3 801 send_buf_p = send_buf ; // reset send buffer
wolfSSL 18:d89df40b4cf3 802
wolfSSL 18:d89df40b4cf3 803 DBG("Trying to write %d bytes:%s\n", len, buf);
wolfSSL 18:d89df40b4cf3 804 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 805
wolfSSL 18:d89df40b4cf3 806 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 807 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 808 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 17:c73d8e61d391 809 }
wolfSSL 18:d89df40b4cf3 810
wolfSSL 18:d89df40b4cf3 811 if(port == HTTPS_PORT) {
ansond 51:3bdf57f7fd60 812 DBG("Enter ssl_socket_write");
ansond 51:3bdf57f7fd60 813 int write_len = ssl_socket_write(&this->ssl, buf, len);
ansond 51:3bdf57f7fd60 814 if (write_len != len) {
ansond 51:3bdf57f7fd60 815 ERR("ssl_socket_write failed: wrote: %d bytes, expected to write %d bytes\r\n",write_len,len);
wolfSSL 18:d89df40b4cf3 816 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 817 }
ansond 51:3bdf57f7fd60 818 DBG("ssl_socket_write: sent %d bytes", writtenLen);
wolfSSL 18:d89df40b4cf3 819 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 820 }
wolfSSL 18:d89df40b4cf3 821 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 822 int ret = m_sock.send_all(buf, len);
wolfSSL 18:d89df40b4cf3 823 if(ret > 0) {
wolfSSL 18:d89df40b4cf3 824 writtenLen += ret;
wolfSSL 18:d89df40b4cf3 825 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 826 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 827 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 828 } else {
wolfSSL 18:d89df40b4cf3 829 ERR("Connection error (send returned %d)", ret);
wolfSSL 18:d89df40b4cf3 830 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 831 }
wolfSSL 18:d89df40b4cf3 832
wolfSSL 17:c73d8e61d391 833 DBG("Written %d bytes", writtenLen);
wolfSSL 17:c73d8e61d391 834 return HTTP_OK;
donatien 0:2ccb9960a044 835 }
donatien 0:2ccb9960a044 836
wolfSSL 19:1e2f05809eb1 837 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
donatien 0:2ccb9960a044 838 {
ansond 46:7cd69cc809b8 839 DBG("Parsing URL: %s",url);
wolfSSL 18:d89df40b4cf3 840 char* schemePtr = (char*) url;
wolfSSL 18:d89df40b4cf3 841 char* hostPtr = (char*) strstr(url, "://");
wolfSSL 18:d89df40b4cf3 842 if(hostPtr == NULL) {
wolfSSL 18:d89df40b4cf3 843 WARN("Could not find host");
wolfSSL 18:d89df40b4cf3 844 return HTTP_PARSE; //URL is invalid
wolfSSL 18:d89df40b4cf3 845 }
wolfSSL 18:d89df40b4cf3 846
wolfSSL 18:d89df40b4cf3 847 if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 848 WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
wolfSSL 18:d89df40b4cf3 849 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 850 }
wolfSSL 18:d89df40b4cf3 851 memcpy(scheme, schemePtr, hostPtr - schemePtr);
wolfSSL 18:d89df40b4cf3 852 scheme[hostPtr - schemePtr] = '\0';
donatien 0:2ccb9960a044 853
wolfSSL 18:d89df40b4cf3 854 hostPtr+=3;
donatien 0:2ccb9960a044 855
wolfSSL 18:d89df40b4cf3 856 size_t hostLen = 0;
donatien 0:2ccb9960a044 857
wolfSSL 18:d89df40b4cf3 858 char* portPtr = strchr(hostPtr, ':');
wolfSSL 18:d89df40b4cf3 859 if( portPtr != NULL ) {
wolfSSL 18:d89df40b4cf3 860 hostLen = portPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 861 portPtr++;
wolfSSL 18:d89df40b4cf3 862 if( sscanf(portPtr, "%hu", port) != 1) {
wolfSSL 18:d89df40b4cf3 863 WARN("Could not find port");
wolfSSL 18:d89df40b4cf3 864 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 865 }
wolfSSL 18:d89df40b4cf3 866 } else {
wolfSSL 18:d89df40b4cf3 867 *port=0;
donatien 0:2ccb9960a044 868 }
wolfSSL 18:d89df40b4cf3 869 char* pathPtr = strchr(hostPtr, '/');
wolfSSL 18:d89df40b4cf3 870 if( hostLen == 0 ) {
wolfSSL 18:d89df40b4cf3 871 hostLen = pathPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 872 }
donatien 0:2ccb9960a044 873
wolfSSL 18:d89df40b4cf3 874 if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 875 WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
wolfSSL 18:d89df40b4cf3 876 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 877 }
wolfSSL 18:d89df40b4cf3 878 memcpy(host, hostPtr, hostLen);
wolfSSL 18:d89df40b4cf3 879 host[hostLen] = '\0';
donatien 0:2ccb9960a044 880
wolfSSL 18:d89df40b4cf3 881 size_t pathLen;
wolfSSL 18:d89df40b4cf3 882 char* fragmentPtr = strchr(hostPtr, '#');
wolfSSL 18:d89df40b4cf3 883 if(fragmentPtr != NULL) {
wolfSSL 18:d89df40b4cf3 884 pathLen = fragmentPtr - pathPtr;
wolfSSL 18:d89df40b4cf3 885 } else {
wolfSSL 18:d89df40b4cf3 886 pathLen = strlen(pathPtr);
wolfSSL 18:d89df40b4cf3 887 }
donatien 0:2ccb9960a044 888
wolfSSL 18:d89df40b4cf3 889 if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 890 WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
wolfSSL 18:d89df40b4cf3 891 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 892 }
wolfSSL 18:d89df40b4cf3 893 memcpy(path, pathPtr, pathLen);
wolfSSL 18:d89df40b4cf3 894 path[pathLen] = '\0';
donatien 0:2ccb9960a044 895
wolfSSL 18:d89df40b4cf3 896 return HTTP_OK;
donatien 0:2ccb9960a044 897 }
wolfSSL 22:4b9a4151cc73 898
ansond 36:debaeb6006a7 899 HTTPResult HTTPClient::tokenAuth(void)
ansond 36:debaeb6006a7 900 {
ansond 36:debaeb6006a7 901 HTTPResult ret ;
ansond 36:debaeb6006a7 902 ret = send("Authorization: Bearer ") ;
ansond 36:debaeb6006a7 903 CHECK_CONN_ERR(ret);
ansond 46:7cd69cc809b8 904 strcat((char *)m_oauthToken,"\n");
ansond 36:debaeb6006a7 905 DBG("oauthToken: %s", m_oauthToken) ;
ansond 46:7cd69cc809b8 906 ret = send((char *)m_oauthToken);
ansond 36:debaeb6006a7 907 CHECK_CONN_ERR(ret);
ansond 36:debaeb6006a7 908 return HTTP_OK ;
ansond 36:debaeb6006a7 909 }
ansond 36:debaeb6006a7 910
wolfSSL 22:4b9a4151cc73 911 HTTPResult HTTPClient::bAuth(void)
wolfSSL 22:4b9a4151cc73 912 {
wolfSSL 22:4b9a4151cc73 913 HTTPResult ret ;
wolfSSL 22:4b9a4151cc73 914 char b_auth[(int)((AUTHB_SIZE+3)*4/3+1)] ;
wolfSSL 22:4b9a4151cc73 915 char base64buff[AUTHB_SIZE+3] ;
wolfSSL 22:4b9a4151cc73 916
wolfSSL 22:4b9a4151cc73 917 ret = send("Authorization: Basic ") ;
wolfSSL 22:4b9a4151cc73 918 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 919 sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
wolfSSL 27:5d4739eae63e 920 DBG("bAuth: %s", base64buff) ;
wolfSSL 22:4b9a4151cc73 921 base64enc(b_auth, base64buff) ;
wolfSSL 22:4b9a4151cc73 922 b_auth[strlen(b_auth)+1] = '\0' ;
wolfSSL 22:4b9a4151cc73 923 b_auth[strlen(b_auth)] = '\n' ;
wolfSSL 22:4b9a4151cc73 924 DBG("b_auth:%s", b_auth) ;
wolfSSL 22:4b9a4151cc73 925 ret = send(b_auth) ;
wolfSSL 22:4b9a4151cc73 926 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 927 return HTTP_OK ;
wolfSSL 22:4b9a4151cc73 928 }