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 Aug 27 21:00:02 2014 +0000
Revision:
30:6fef375c94e6
Parent:
29:2d96cc752d19
Child:
31:0675a342e45c
synced with HTTPClient-BasicAuth library updates

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 29:2d96cc752d19 20 // DMA: Added tunable to adapt size of larger input URLs
ansond 29:2d96cc752d19 21 #define MAX_URL_HOSTNAME_LENGTH 128
ansond 29:2d96cc752d19 22 #define MAX_URL_PATH_LENGTH 128
donatien 0:2ccb9960a044 23
donatien 7:4e39864f7b15 24 //Debug is disabled by default
donatien 16:1f743885e7de 25 #if 0
donatien 12:89d09a6db00a 26 //Enable debug
donatien 11:390362de8c3f 27 #include <cstdio>
wolfSSL 18:d89df40b4cf3 28 #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
wolfSSL 18:d89df40b4cf3 29 #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
wolfSSL 18:d89df40b4cf3 30 #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
donatien 12:89d09a6db00a 31
donatien 12:89d09a6db00a 32 #else
donatien 12:89d09a6db00a 33 //Disable debug
wolfSSL 18:d89df40b4cf3 34 #define DBG(x, ...)
donatien 12:89d09a6db00a 35 #define WARN(x, ...)
wolfSSL 18:d89df40b4cf3 36 #define ERR(x, ...)
donatien 12:89d09a6db00a 37
donatien 7:4e39864f7b15 38 #endif
donatien 0:2ccb9960a044 39
donatien 0:2ccb9960a044 40 #define HTTP_PORT 80
wolfSSL 17:c73d8e61d391 41 #define HTTPS_PORT 443
donatien 0:2ccb9960a044 42
donatien 11:390362de8c3f 43 #define OK 0
donatien 11:390362de8c3f 44
donatien 11:390362de8c3f 45 #define MIN(x,y) (((x)<(y))?(x):(y))
donatien 11:390362de8c3f 46 #define MAX(x,y) (((x)>(y))?(x):(y))
donatien 11:390362de8c3f 47
wolfSSL 17:c73d8e61d391 48 #include <cstring>
donatien 0:2ccb9960a044 49
wolfSSL 17:c73d8e61d391 50 #include <../CyaSSL/cyassl/ctaocrypt/settings.h>
wolfSSL 17:c73d8e61d391 51 #include <../CyaSSL/cyassl/ctaocrypt/types.h>
wolfSSL 17:c73d8e61d391 52 #include <../CyaSSL/cyassl/internal.h>
wolfSSL 17:c73d8e61d391 53 #include <../CyaSSL/cyassl/ssl.h>
donatien 0:2ccb9960a044 54
donatien 11:390362de8c3f 55 #include "HTTPClient.h"
wolfSSL 17:c73d8e61d391 56 #include "TCPSocketConnection.h"
wolfSSL 17:c73d8e61d391 57
wolfSSL 19:1e2f05809eb1 58 static TCPSocketConnection m_sock;
wolfSSL 17:c73d8e61d391 59 #define CHUNK_SIZE 256
wolfSSL 17:c73d8e61d391 60 #define SEND_BUF_SIZE 512
wolfSSL 17:c73d8e61d391 61 static char send_buf[SEND_BUF_SIZE] ;
wolfSSL 17:c73d8e61d391 62 static char *send_buf_p ;
wolfSSL 17:c73d8e61d391 63
wolfSSL 17:c73d8e61d391 64 static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 17:c73d8e61d391 65 {
wolfSSL 17:c73d8e61d391 66 int n ;
wolfSSL 17:c73d8e61d391 67 int i ;
wolfSSL 18:d89df40b4cf3 68 #define RECV_RETRY 3
wolfSSL 22:4b9a4151cc73 69
wolfSSL 17:c73d8e61d391 70 for(i=0; i<RECV_RETRY; i++) {
wolfSSL 17:c73d8e61d391 71 n = m_sock.receive(buf, sz) ;
wolfSSL 17:c73d8e61d391 72 if(n >= 0)return n ;
wolfSSL 22:4b9a4151cc73 73 wait(0.2) ;
wolfSSL 17:c73d8e61d391 74 }
wolfSSL 17:c73d8e61d391 75 ERR("SocketReceive:%d/%d\n", n, sz) ;
wolfSSL 17:c73d8e61d391 76 return n ;
wolfSSL 17:c73d8e61d391 77 }
wolfSSL 17:c73d8e61d391 78
wolfSSL 17:c73d8e61d391 79 static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx)
wolfSSL 17:c73d8e61d391 80 {
wolfSSL 17:c73d8e61d391 81 int n ;
wolfSSL 22:4b9a4151cc73 82
wolfSSL 22:4b9a4151cc73 83 wait(0.1) ;
wolfSSL 17:c73d8e61d391 84 n = m_sock.send(buf, sz);
wolfSSL 17:c73d8e61d391 85 if(n > 0) {
wolfSSL 22:4b9a4151cc73 86 wait(0.3) ;
wolfSSL 17:c73d8e61d391 87 return n ;
wolfSSL 17:c73d8e61d391 88 } else ERR("SocketSend:%d/%d\n", n, sz);
wolfSSL 17:c73d8e61d391 89 return n ;
wolfSSL 17:c73d8e61d391 90 }
donatien 11:390362de8c3f 91
wolfSSL 22:4b9a4151cc73 92 static void base64enc(char *out, const char *in) {
wolfSSL 22:4b9a4151cc73 93 const char code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ;
wolfSSL 22:4b9a4151cc73 94 int i = 0, x = 0, l = 0;
wolfSSL 22:4b9a4151cc73 95
wolfSSL 22:4b9a4151cc73 96 for (; *in; in++) {
wolfSSL 22:4b9a4151cc73 97 x = x << 8 | *in;
wolfSSL 22:4b9a4151cc73 98 for (l += 8; l >= 6; l -= 6) {
wolfSSL 22:4b9a4151cc73 99 out[i++] = code[(x >> (l - 6)) & 0x3f];
wolfSSL 22:4b9a4151cc73 100 }
wolfSSL 22:4b9a4151cc73 101 }
wolfSSL 22:4b9a4151cc73 102 if (l > 0) {
wolfSSL 22:4b9a4151cc73 103 x <<= 6 - l;
wolfSSL 22:4b9a4151cc73 104 out[i++] = code[x & 0x3f];
wolfSSL 22:4b9a4151cc73 105 }
wolfSSL 22:4b9a4151cc73 106 for (; i % 4;) {
wolfSSL 22:4b9a4151cc73 107 out[i++] = '=';
wolfSSL 22:4b9a4151cc73 108 }
wolfSSL 22:4b9a4151cc73 109 out[i] = '\0' ;
wolfSSL 22:4b9a4151cc73 110 }
wolfSSL 22:4b9a4151cc73 111
donatien 0:2ccb9960a044 112 HTTPClient::HTTPClient() :
wolfSSL 18:d89df40b4cf3 113 m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
donatien 0:2ccb9960a044 114 {
wolfSSL 22:4b9a4151cc73 115
wolfSSL 22:4b9a4151cc73 116 /* CyaSSL_Debugging_ON() ; */
wolfSSL 22:4b9a4151cc73 117
wolfSSL 18:d89df40b4cf3 118 ctx = 0 ;
wolfSSL 18:d89df40b4cf3 119 ssl = 0 ;
wolfSSL 22:4b9a4151cc73 120 SSLver = 3 ;
wolfSSL 27:5d4739eae63e 121 m_basicAuthUser = NULL ;
wolfSSL 27:5d4739eae63e 122 redirect_url = NULL ;
wolfSSL 27:5d4739eae63e 123 redirect = 0 ;
wolfSSL 27:5d4739eae63e 124 header = NULL ;
donatien 0:2ccb9960a044 125 }
donatien 0:2ccb9960a044 126
donatien 0:2ccb9960a044 127 HTTPClient::~HTTPClient()
donatien 0:2ccb9960a044 128 {
donatien 0:2ccb9960a044 129
donatien 0:2ccb9960a044 130 }
donatien 0:2ccb9960a044 131
wolfSSL 22:4b9a4151cc73 132 HTTPResult HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
donatien 0:2ccb9960a044 133 {
wolfSSL 22:4b9a4151cc73 134 #define AUTHB_SIZE 128
wolfSSL 22:4b9a4151cc73 135 if((strlen(user) + strlen(password)) >= AUTHB_SIZE)
wolfSSL 22:4b9a4151cc73 136 return HTTP_ERROR ;
ansond 30:6fef375c94e6 137
ansond 30:6fef375c94e6 138 if (m_basicAuthUser) free((void *)m_basicAuthUser);
ansond 30:6fef375c94e6 139 if (user != NULL) {
ansond 30:6fef375c94e6 140 m_basicAuthUser = (char *)malloc(strlen(user)+1);
ansond 30:6fef375c94e6 141 strcpy((char *)m_basicAuthUser, user);
ansond 30:6fef375c94e6 142 }
ansond 30:6fef375c94e6 143
ansond 30:6fef375c94e6 144 if (m_basicAuthPassword) free((void *)m_basicAuthPassword);
ansond 30:6fef375c94e6 145 if (password != NULL) {
ansond 30:6fef375c94e6 146 m_basicAuthPassword = (char *)malloc(strlen(password)+1);
ansond 30:6fef375c94e6 147 strcpy((char *)m_basicAuthPassword, password);
ansond 30:6fef375c94e6 148 }
ansond 30:6fef375c94e6 149
wolfSSL 22:4b9a4151cc73 150 return HTTP_OK ;
donatien 0:2ccb9960a044 151 }
donatien 0:2ccb9960a044 152
donatien 12:89d09a6db00a 153 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 154 {
wolfSSL 18:d89df40b4cf3 155 return connect(url, HTTP_GET, NULL, pDataIn, timeout);
donatien 0:2ccb9960a044 156 }
donatien 0:2ccb9960a044 157
donatien 12:89d09a6db00a 158 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 159 {
wolfSSL 18:d89df40b4cf3 160 HTTPText str(result, maxResultLen);
wolfSSL 18:d89df40b4cf3 161 return get(url, &str, timeout);
donatien 0:2ccb9960a044 162 }
donatien 0:2ccb9960a044 163
donatien 12:89d09a6db00a 164 HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 165 {
wolfSSL 18:d89df40b4cf3 166 return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
donatien 0:2ccb9960a044 167 }
donatien 0:2ccb9960a044 168
donatien 16:1f743885e7de 169 HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 16:1f743885e7de 170 {
wolfSSL 18:d89df40b4cf3 171 return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
donatien 16:1f743885e7de 172 }
donatien 16:1f743885e7de 173
donatien 16:1f743885e7de 174 HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 16:1f743885e7de 175 {
wolfSSL 18:d89df40b4cf3 176 return connect(url, HTTP_DELETE, NULL, pDataIn, timeout);
donatien 16:1f743885e7de 177 }
donatien 16:1f743885e7de 178
donatien 16:1f743885e7de 179
donatien 0:2ccb9960a044 180 int HTTPClient::getHTTPResponseCode()
donatien 0:2ccb9960a044 181 {
wolfSSL 18:d89df40b4cf3 182 return m_httpResponseCode;
donatien 0:2ccb9960a044 183 }
donatien 0:2ccb9960a044 184
wolfSSL 27:5d4739eae63e 185 void HTTPClient::setHeader(const char * h)
wolfSSL 17:c73d8e61d391 186 {
wolfSSL 17:c73d8e61d391 187 header = h ;
wolfSSL 17:c73d8e61d391 188 }
wolfSSL 17:c73d8e61d391 189
wolfSSL 27:5d4739eae63e 190 void HTTPClient::setLocationBuf(char * url, int size)
wolfSSL 27:5d4739eae63e 191 {
wolfSSL 27:5d4739eae63e 192 redirect_url = url ;
wolfSSL 27:5d4739eae63e 193 redirect_url_size = size ;
wolfSSL 27:5d4739eae63e 194 }
wolfSSL 27:5d4739eae63e 195
wolfSSL 22:4b9a4151cc73 196 HTTPResult HTTPClient::setSSLversion(int minorV)
wolfSSL 22:4b9a4151cc73 197 {
wolfSSL 22:4b9a4151cc73 198 if((minorV>=0) && (minorV<=3))
wolfSSL 22:4b9a4151cc73 199 SSLver = minorV ;
wolfSSL 22:4b9a4151cc73 200 else return HTTP_ERROR ;
wolfSSL 22:4b9a4151cc73 201 return HTTP_OK ;
wolfSSL 22:4b9a4151cc73 202 }
wolfSSL 22:4b9a4151cc73 203
wolfSSL 17:c73d8e61d391 204
donatien 5:791fc3dcb6c4 205 #define CHECK_CONN_ERR(ret) \
donatien 5:791fc3dcb6c4 206 do{ \
donatien 7:4e39864f7b15 207 if(ret) { \
wolfSSL 17:c73d8e61d391 208 cyassl_free() ;\
donatien 7:4e39864f7b15 209 m_sock.close(); \
donatien 5:791fc3dcb6c4 210 ERR("Connection error (%d)", ret); \
donatien 11:390362de8c3f 211 return HTTP_CONN; \
donatien 5:791fc3dcb6c4 212 } \
donatien 5:791fc3dcb6c4 213 } while(0)
donatien 5:791fc3dcb6c4 214
donatien 5:791fc3dcb6c4 215 #define PRTCL_ERR() \
donatien 5:791fc3dcb6c4 216 do{ \
wolfSSL 17:c73d8e61d391 217 cyassl_free() ;\
donatien 7:4e39864f7b15 218 m_sock.close(); \
donatien 5:791fc3dcb6c4 219 ERR("Protocol error"); \
donatien 11:390362de8c3f 220 return HTTP_PRTCL; \
donatien 5:791fc3dcb6c4 221 } while(0)
donatien 0:2ccb9960a044 222
wolfSSL 17:c73d8e61d391 223 void HTTPClient::cyassl_free(void)
wolfSSL 17:c73d8e61d391 224 {
wolfSSL 19:1e2f05809eb1 225 if(ssl) {
wolfSSL 17:c73d8e61d391 226 CyaSSL_free(ssl) ;
wolfSSL 19:1e2f05809eb1 227 ssl = NULL ;
wolfSSL 19:1e2f05809eb1 228 }
wolfSSL 19:1e2f05809eb1 229 if(ctx) {
wolfSSL 17:c73d8e61d391 230 CyaSSL_CTX_free(ctx) ;
wolfSSL 19:1e2f05809eb1 231 ctx = NULL ;
wolfSSL 19:1e2f05809eb1 232 }
wolfSSL 22:4b9a4151cc73 233 CyaSSL_Cleanup() ;
wolfSSL 22:4b9a4151cc73 234 }
wolfSSL 17:c73d8e61d391 235
donatien 12:89d09a6db00a 236 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
wolfSSL 18:d89df40b4cf3 237 {
wolfSSL 22:4b9a4151cc73 238 CYASSL_METHOD * SSLmethod ;
wolfSSL 18:d89df40b4cf3 239 m_httpResponseCode = 0; //Invalidate code
wolfSSL 18:d89df40b4cf3 240 m_timeout = timeout;
wolfSSL 27:5d4739eae63e 241 redirect = 0 ;
wolfSSL 27:5d4739eae63e 242
wolfSSL 18:d89df40b4cf3 243 pDataIn->writeReset();
wolfSSL 18:d89df40b4cf3 244 if( pDataOut ) {
wolfSSL 18:d89df40b4cf3 245 pDataOut->readReset();
wolfSSL 18:d89df40b4cf3 246 }
wolfSSL 17:c73d8e61d391 247
wolfSSL 18:d89df40b4cf3 248 char scheme[8];
ansond 29:2d96cc752d19 249 char host[MAX_URL_HOSTNAME_LENGTH];
ansond 29:2d96cc752d19 250 char path[MAX_URL_PATH_LENGTH];
wolfSSL 18:d89df40b4cf3 251
wolfSSL 18:d89df40b4cf3 252 int ret ;
donatien 0:2ccb9960a044 253
wolfSSL 18:d89df40b4cf3 254 //First we need to parse the url (http[s]://host[:port][/[path]])
wolfSSL 18:d89df40b4cf3 255 HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
wolfSSL 18:d89df40b4cf3 256 if(res != HTTP_OK) {
wolfSSL 18:d89df40b4cf3 257 ERR("parseURL returned %d", res);
wolfSSL 18:d89df40b4cf3 258 return res;
wolfSSL 18:d89df40b4cf3 259 }
donatien 0:2ccb9960a044 260
wolfSSL 22:4b9a4151cc73 261 if(port == 0) {
wolfSSL 18:d89df40b4cf3 262 if(strcmp(scheme, "http") == 0)
wolfSSL 18:d89df40b4cf3 263 port = HTTP_PORT ;
wolfSSL 18:d89df40b4cf3 264 else if(strcmp(scheme, "https") == 0)
wolfSSL 18:d89df40b4cf3 265 port = HTTPS_PORT ;
wolfSSL 18:d89df40b4cf3 266 }
donatien 0:2ccb9960a044 267
wolfSSL 18:d89df40b4cf3 268 DBG("Scheme: %s", scheme);
wolfSSL 18:d89df40b4cf3 269 DBG("Host: %s", host);
wolfSSL 18:d89df40b4cf3 270 DBG("Port: %d", port);
wolfSSL 18:d89df40b4cf3 271 DBG("Path: %s", path);
wolfSSL 17:c73d8e61d391 272
wolfSSL 18:d89df40b4cf3 273 //Connect
wolfSSL 18:d89df40b4cf3 274 DBG("Connecting socket to server");
wolfSSL 18:d89df40b4cf3 275
wolfSSL 18:d89df40b4cf3 276 #define MAX_RETRY 5
wolfSSL 18:d89df40b4cf3 277 int retry ;
donatien 0:2ccb9960a044 278
wolfSSL 18:d89df40b4cf3 279 for(retry=0; retry<MAX_RETRY; retry++) {
wolfSSL 18:d89df40b4cf3 280 int ret = m_sock.connect(host, port);
wolfSSL 18:d89df40b4cf3 281 if(ret == 0)break ;
wolfSSL 17:c73d8e61d391 282 }
wolfSSL 18:d89df40b4cf3 283 if(retry == MAX_RETRY) {
wolfSSL 18:d89df40b4cf3 284 m_sock.close();
wolfSSL 18:d89df40b4cf3 285 ERR("Could not connect");
wolfSSL 18:d89df40b4cf3 286 return HTTP_CONN;
wolfSSL 17:c73d8e61d391 287 }
wolfSSL 17:c73d8e61d391 288
wolfSSL 18:d89df40b4cf3 289 if(port == HTTPS_PORT) {
wolfSSL 22:4b9a4151cc73 290
wolfSSL 18:d89df40b4cf3 291 /* Start SSL connect */
wolfSSL 27:5d4739eae63e 292 DBG("SSLver=%d", SSLver) ;
wolfSSL 19:1e2f05809eb1 293 if(ctx == NULL) {
wolfSSL 22:4b9a4151cc73 294 switch(SSLver) {
wolfSSL 22:4b9a4151cc73 295 case 0 : SSLmethod = CyaSSLv3_client_method() ; break ;
wolfSSL 22:4b9a4151cc73 296 case 1 : SSLmethod = CyaTLSv1_client_method() ; break ;
wolfSSL 22:4b9a4151cc73 297 case 2 : SSLmethod = CyaTLSv1_1_client_method() ; break ;
wolfSSL 22:4b9a4151cc73 298 case 3 : SSLmethod = CyaTLSv1_2_client_method() ; break ;
wolfSSL 22:4b9a4151cc73 299 }
wolfSSL 22:4b9a4151cc73 300 ctx = CyaSSL_CTX_new((CYASSL_METHOD *)SSLmethod);
wolfSSL 19:1e2f05809eb1 301 if (ctx == NULL) {
wolfSSL 19:1e2f05809eb1 302 ERR("unable to get ctx");
wolfSSL 19:1e2f05809eb1 303 return HTTP_CONN;
wolfSSL 19:1e2f05809eb1 304 }
wolfSSL 19:1e2f05809eb1 305 CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL 19:1e2f05809eb1 306 CyaSSL_SetIORecv(ctx, SocketReceive) ;
wolfSSL 19:1e2f05809eb1 307 CyaSSL_SetIOSend(ctx, SocketSend) ;
wolfSSL 18:d89df40b4cf3 308 }
wolfSSL 18:d89df40b4cf3 309 if (ssl == NULL) {
wolfSSL 19:1e2f05809eb1 310 ssl = CyaSSL_new(ctx);
wolfSSL 19:1e2f05809eb1 311 if (ssl == NULL) {
wolfSSL 19:1e2f05809eb1 312 ERR("unable to get SSL object");
wolfSSL 19:1e2f05809eb1 313 cyassl_free() ;
wolfSSL 19:1e2f05809eb1 314 return HTTP_CONN;
wolfSSL 19:1e2f05809eb1 315 }
wolfSSL 18:d89df40b4cf3 316 }
donatien 0:2ccb9960a044 317
wolfSSL 18:d89df40b4cf3 318 DBG("ctx=%x, ssl=%x, ssl->ctx->CBIORecv, CBIOSend=%x, %x\n",
wolfSSL 18:d89df40b4cf3 319 ctx, ssl, SocketReceive, SocketSend ) ;
wolfSSL 18:d89df40b4cf3 320 if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
wolfSSL 18:d89df40b4cf3 321 ERR("SSL_connect failed");
wolfSSL 18:d89df40b4cf3 322 cyassl_free() ;
wolfSSL 18:d89df40b4cf3 323 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 324 }
wolfSSL 18:d89df40b4cf3 325 } /* SSL connect complete */
donatien 0:2ccb9960a044 326
wolfSSL 18:d89df40b4cf3 327 //Send request
wolfSSL 18:d89df40b4cf3 328 DBG("Sending request");
wolfSSL 18:d89df40b4cf3 329 char buf[CHUNK_SIZE];
ansond 30:6fef375c94e6 330 memset(buf,0,CHUNK_SIZE);
wolfSSL 18:d89df40b4cf3 331 send_buf_p = send_buf ; // Reset send buffer ;
wolfSSL 18:d89df40b4cf3 332
wolfSSL 18:d89df40b4cf3 333 const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":"";
wolfSSL 18:d89df40b4cf3 334 snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
wolfSSL 18:d89df40b4cf3 335 ret = send(buf);
wolfSSL 18:d89df40b4cf3 336 if(ret) {
wolfSSL 18:d89df40b4cf3 337 m_sock.close();
wolfSSL 18:d89df40b4cf3 338 ERR("Could not write request");
wolfSSL 18:d89df40b4cf3 339 return HTTP_CONN;
donatien 0:2ccb9960a044 340 }
wolfSSL 17:c73d8e61d391 341
wolfSSL 18:d89df40b4cf3 342 //Send all headers
donatien 0:2ccb9960a044 343
wolfSSL 18:d89df40b4cf3 344 //Send default headers
wolfSSL 18:d89df40b4cf3 345 DBG("Sending headers");
wolfSSL 27:5d4739eae63e 346 if(m_basicAuthUser) {
wolfSSL 27:5d4739eae63e 347 bAuth() ; /* send out Basic Auth header */
wolfSSL 27:5d4739eae63e 348 }
wolfSSL 18:d89df40b4cf3 349 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 350 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 351 ret = send("Transfer-Encoding: chunked\r\n");
wolfSSL 18:d89df40b4cf3 352 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 353 } else {
wolfSSL 18:d89df40b4cf3 354 snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
wolfSSL 22:4b9a4151cc73 355 DBG("Content buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 356 ret = send(buf);
wolfSSL 18:d89df40b4cf3 357 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 358 }
wolfSSL 18:d89df40b4cf3 359 char type[48];
wolfSSL 18:d89df40b4cf3 360 if( pDataOut->getDataType(type, 48) == HTTP_OK ) {
wolfSSL 18:d89df40b4cf3 361 snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
wolfSSL 18:d89df40b4cf3 362 ret = send(buf);
wolfSSL 18:d89df40b4cf3 363 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 364 }
wolfSSL 18:d89df40b4cf3 365 }
wolfSSL 18:d89df40b4cf3 366
wolfSSL 18:d89df40b4cf3 367 //Add user headers
wolfSSL 18:d89df40b4cf3 368 if(header) {
wolfSSL 27:5d4739eae63e 369 ret = send((char *)header);
donatien 5:791fc3dcb6c4 370 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 371 }
donatien 0:2ccb9960a044 372
wolfSSL 18:d89df40b4cf3 373 //Close headers
wolfSSL 18:d89df40b4cf3 374 DBG("Headers sent");
wolfSSL 18:d89df40b4cf3 375 ret = send("\r\n");
wolfSSL 18:d89df40b4cf3 376 CHECK_CONN_ERR(ret);
wolfSSL 17:c73d8e61d391 377
wolfSSL 18:d89df40b4cf3 378 size_t trfLen;
donatien 0:2ccb9960a044 379
wolfSSL 18:d89df40b4cf3 380 //Send data (if available)
wolfSSL 18:d89df40b4cf3 381 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 382 DBG("Sending data");
wolfSSL 18:d89df40b4cf3 383 while(true) {
wolfSSL 18:d89df40b4cf3 384 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 385 pDataOut->read(buf, CHUNK_SIZE, &trfLen);
wolfSSL 18:d89df40b4cf3 386 buf[trfLen] = 0x0 ;
wolfSSL 18:d89df40b4cf3 387 DBG("buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 388 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 389 //Write chunk header
wolfSSL 22:4b9a4151cc73 390 char chunkHeader[64];
wolfSSL 18:d89df40b4cf3 391 snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
wolfSSL 18:d89df40b4cf3 392 ret = send(chunkHeader);
wolfSSL 18:d89df40b4cf3 393 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 394 } else if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 395 DBG("trfLen==0") ;
wolfSSL 18:d89df40b4cf3 396 break;
wolfSSL 18:d89df40b4cf3 397 }
wolfSSL 22:4b9a4151cc73 398 DBG("trfLen 1=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 399 if( trfLen != 0 ) {
wolfSSL 22:4b9a4151cc73 400 DBG("Sending 1") ;
wolfSSL 18:d89df40b4cf3 401 ret = send(buf, trfLen);
wolfSSL 22:4b9a4151cc73 402 DBG("Sent 1") ;
wolfSSL 18:d89df40b4cf3 403 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 404 }
donatien 0:2ccb9960a044 405
wolfSSL 18:d89df40b4cf3 406 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 407 ret = send("\r\n"); //Chunk-terminating CRLF
wolfSSL 18:d89df40b4cf3 408 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 409 } else {
wolfSSL 18:d89df40b4cf3 410 writtenLen += trfLen;
wolfSSL 18:d89df40b4cf3 411 if( writtenLen >= pDataOut->getDataLen() ) {
wolfSSL 22:4b9a4151cc73 412 DBG("writtenLen=%d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 413 break;
wolfSSL 18:d89df40b4cf3 414 }
wolfSSL 22:4b9a4151cc73 415 DBG("writtenLen+=trfLen = %d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 416 }
wolfSSL 22:4b9a4151cc73 417 DBG("trfLen 2=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 418 if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 419 DBG("trfLen == 0") ;
wolfSSL 18:d89df40b4cf3 420 break;
wolfSSL 18:d89df40b4cf3 421 }
wolfSSL 18:d89df40b4cf3 422 }
donatien 0:2ccb9960a044 423
wolfSSL 18:d89df40b4cf3 424 }
wolfSSL 18:d89df40b4cf3 425 ret = flush() ; // flush the send buffer ;
wolfSSL 18:d89df40b4cf3 426 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 427
wolfSSL 18:d89df40b4cf3 428 //Receive response
wolfSSL 18:d89df40b4cf3 429 DBG("Receiving response");
wolfSSL 18:d89df40b4cf3 430
wolfSSL 18:d89df40b4cf3 431 ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
wolfSSL 18:d89df40b4cf3 432 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 433
wolfSSL 18:d89df40b4cf3 434 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 435
wolfSSL 18:d89df40b4cf3 436 char* crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 437 if(crlfPtr == NULL) {
donatien 5:791fc3dcb6c4 438 PRTCL_ERR();
donatien 0:2ccb9960a044 439 }
donatien 0:2ccb9960a044 440
wolfSSL 18:d89df40b4cf3 441 int crlfPos = crlfPtr - buf;
donatien 0:2ccb9960a044 442 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 443
wolfSSL 18:d89df40b4cf3 444 //Parse HTTP response
wolfSSL 18:d89df40b4cf3 445 if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) {
wolfSSL 18:d89df40b4cf3 446 //Cannot match string, error
wolfSSL 18:d89df40b4cf3 447 ERR("Not a correct HTTP answer : %s\n", buf);
wolfSSL 18:d89df40b4cf3 448 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 449 }
donatien 4:c071b05ac026 450
wolfSSL 27:5d4739eae63e 451 if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) {
wolfSSL 18:d89df40b4cf3 452 //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers
wolfSSL 18:d89df40b4cf3 453 WARN("Response code %d", m_httpResponseCode);
wolfSSL 18:d89df40b4cf3 454 PRTCL_ERR();
donatien 0:2ccb9960a044 455 }
donatien 0:2ccb9960a044 456
wolfSSL 18:d89df40b4cf3 457 DBG("Reading headers");
donatien 0:2ccb9960a044 458
wolfSSL 18:d89df40b4cf3 459 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 460 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 461
wolfSSL 18:d89df40b4cf3 462 size_t recvContentLength = 0;
wolfSSL 18:d89df40b4cf3 463 bool recvChunked = false;
wolfSSL 18:d89df40b4cf3 464 //Now get headers
wolfSSL 18:d89df40b4cf3 465 while( true ) {
wolfSSL 18:d89df40b4cf3 466 crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 467 if(crlfPtr == NULL) {
wolfSSL 18:d89df40b4cf3 468 if( trfLen < CHUNK_SIZE - 1 ) {
ansond 30:6fef375c94e6 469 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 470 ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 471 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 472 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 473 DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
wolfSSL 18:d89df40b4cf3 474 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 475 continue;
wolfSSL 18:d89df40b4cf3 476 } else {
wolfSSL 18:d89df40b4cf3 477 PRTCL_ERR();
donatien 14:2744e0c0e527 478 }
wolfSSL 18:d89df40b4cf3 479 }
wolfSSL 18:d89df40b4cf3 480
wolfSSL 18:d89df40b4cf3 481 crlfPos = crlfPtr - buf;
wolfSSL 18:d89df40b4cf3 482
wolfSSL 18:d89df40b4cf3 483 if(crlfPos == 0) { //End of headers
wolfSSL 18:d89df40b4cf3 484 DBG("Headers read");
wolfSSL 18:d89df40b4cf3 485 memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 486 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 487 break;
donatien 0:2ccb9960a044 488 }
wolfSSL 18:d89df40b4cf3 489
wolfSSL 18:d89df40b4cf3 490 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 491
ansond 30:6fef375c94e6 492 char key[41];
ansond 30:6fef375c94e6 493 char value[41];
wolfSSL 18:d89df40b4cf3 494
ansond 30:6fef375c94e6 495 memset(key,0,41);
ansond 30:6fef375c94e6 496 memset(value,0,41);
wolfSSL 18:d89df40b4cf3 497
ansond 30:6fef375c94e6 498 int n = sscanf(buf, "%40[^:]: %40[^\r\n]", key, value);
wolfSSL 18:d89df40b4cf3 499 if ( n == 2 ) {
wolfSSL 18:d89df40b4cf3 500 DBG("Read header : %s: %s\n", key, value);
wolfSSL 18:d89df40b4cf3 501 if( !strcmp(key, "Content-Length") ) {
wolfSSL 18:d89df40b4cf3 502 sscanf(value, "%d", &recvContentLength);
wolfSSL 18:d89df40b4cf3 503 pDataIn->setDataLen(recvContentLength);
wolfSSL 18:d89df40b4cf3 504 } else if( !strcmp(key, "Transfer-Encoding") ) {
wolfSSL 18:d89df40b4cf3 505 if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) {
wolfSSL 18:d89df40b4cf3 506 recvChunked = true;
wolfSSL 18:d89df40b4cf3 507 pDataIn->setIsChunked(true);
wolfSSL 18:d89df40b4cf3 508 }
wolfSSL 18:d89df40b4cf3 509 } else if( !strcmp(key, "Content-Type") ) {
wolfSSL 18:d89df40b4cf3 510 pDataIn->setDataType(value);
wolfSSL 27:5d4739eae63e 511 } else if( !strcmp(key, "location") && redirect_url) {
wolfSSL 27:5d4739eae63e 512 sscanf(buf, "%31[^:]: %128[^\r\n]", key, redirect_url);
wolfSSL 27:5d4739eae63e 513 DBG("Redirect %s: %s", key, redirect_url) ;
wolfSSL 27:5d4739eae63e 514 redirect = 1 ;
wolfSSL 18:d89df40b4cf3 515 }
wolfSSL 18:d89df40b4cf3 516 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 517 trfLen -= (crlfPos + 2);
wolfSSL 18:d89df40b4cf3 518
wolfSSL 18:d89df40b4cf3 519 } else {
wolfSSL 18:d89df40b4cf3 520 ERR("Could not parse header");
donatien 14:2744e0c0e527 521 PRTCL_ERR();
donatien 0:2ccb9960a044 522 }
donatien 0:2ccb9960a044 523
donatien 0:2ccb9960a044 524 }
donatien 0:2ccb9960a044 525
wolfSSL 18:d89df40b4cf3 526 //Receive data
wolfSSL 18:d89df40b4cf3 527 DBG("Receiving data");
wolfSSL 18:d89df40b4cf3 528
wolfSSL 18:d89df40b4cf3 529 while(true) {
wolfSSL 18:d89df40b4cf3 530 size_t readLen = 0;
donatien 0:2ccb9960a044 531
wolfSSL 18:d89df40b4cf3 532 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 533 //Read chunk header
wolfSSL 18:d89df40b4cf3 534 bool foundCrlf;
wolfSSL 18:d89df40b4cf3 535 do {
wolfSSL 18:d89df40b4cf3 536 foundCrlf = false;
wolfSSL 18:d89df40b4cf3 537 crlfPos=0;
wolfSSL 18:d89df40b4cf3 538 buf[trfLen]=0;
wolfSSL 18:d89df40b4cf3 539 if(trfLen >= 2) {
wolfSSL 18:d89df40b4cf3 540 for(; crlfPos < trfLen - 2; crlfPos++) {
wolfSSL 18:d89df40b4cf3 541 if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) {
wolfSSL 18:d89df40b4cf3 542 foundCrlf = true;
wolfSSL 18:d89df40b4cf3 543 break;
wolfSSL 18:d89df40b4cf3 544 }
wolfSSL 18:d89df40b4cf3 545 }
wolfSSL 18:d89df40b4cf3 546 }
wolfSSL 18:d89df40b4cf3 547 if(!foundCrlf) { //Try to read more
wolfSSL 18:d89df40b4cf3 548 if( trfLen < CHUNK_SIZE ) {
ansond 30:6fef375c94e6 549 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 550 ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 551 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 552 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 553 continue;
wolfSSL 18:d89df40b4cf3 554 } else {
wolfSSL 18:d89df40b4cf3 555 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 556 }
wolfSSL 18:d89df40b4cf3 557 }
wolfSSL 18:d89df40b4cf3 558 } while(!foundCrlf);
wolfSSL 18:d89df40b4cf3 559 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 560 int n = sscanf(buf, "%x", &readLen);
wolfSSL 18:d89df40b4cf3 561 if(n!=1) {
wolfSSL 18:d89df40b4cf3 562 ERR("Could not read chunk length");
wolfSSL 18:d89df40b4cf3 563 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 564 }
wolfSSL 18:d89df40b4cf3 565
wolfSSL 18:d89df40b4cf3 566 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
wolfSSL 18:d89df40b4cf3 567 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 568
wolfSSL 18:d89df40b4cf3 569 if( readLen == 0 ) {
wolfSSL 18:d89df40b4cf3 570 //Last chunk
wolfSSL 18:d89df40b4cf3 571 break;
wolfSSL 18:d89df40b4cf3 572 }
wolfSSL 18:d89df40b4cf3 573 } else {
wolfSSL 18:d89df40b4cf3 574 readLen = recvContentLength;
wolfSSL 18:d89df40b4cf3 575 }
wolfSSL 18:d89df40b4cf3 576
wolfSSL 18:d89df40b4cf3 577 DBG("Retrieving %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 578
wolfSSL 18:d89df40b4cf3 579 do {
wolfSSL 18:d89df40b4cf3 580 pDataIn->write(buf, MIN(trfLen, readLen));
wolfSSL 18:d89df40b4cf3 581 if( trfLen > readLen ) {
wolfSSL 18:d89df40b4cf3 582 memmove(buf, &buf[readLen], trfLen - readLen);
wolfSSL 18:d89df40b4cf3 583 trfLen -= readLen;
wolfSSL 18:d89df40b4cf3 584 readLen = 0;
wolfSSL 18:d89df40b4cf3 585 } else {
wolfSSL 18:d89df40b4cf3 586 readLen -= trfLen;
wolfSSL 18:d89df40b4cf3 587 }
donatien 0:2ccb9960a044 588
wolfSSL 18:d89df40b4cf3 589 if(readLen) {
wolfSSL 18:d89df40b4cf3 590 ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
wolfSSL 18:d89df40b4cf3 591 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 592 }
wolfSSL 18:d89df40b4cf3 593 } while(readLen);
wolfSSL 18:d89df40b4cf3 594
wolfSSL 18:d89df40b4cf3 595 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 596 if(trfLen < 2) {
ansond 30:6fef375c94e6 597 size_t newTrfLen = 0;
wolfSSL 18:d89df40b4cf3 598 //Read missing chars to find end of chunk
wolfSSL 18:d89df40b4cf3 599 ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 600 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 601 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 602 }
wolfSSL 18:d89df40b4cf3 603 if( (buf[0] != '\r') || (buf[1] != '\n') ) {
wolfSSL 18:d89df40b4cf3 604 ERR("Format error");
wolfSSL 18:d89df40b4cf3 605 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 606 }
wolfSSL 18:d89df40b4cf3 607 memmove(buf, &buf[2], trfLen - 2);
wolfSSL 18:d89df40b4cf3 608 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 609 } else {
wolfSSL 18:d89df40b4cf3 610 break;
wolfSSL 18:d89df40b4cf3 611 }
wolfSSL 18:d89df40b4cf3 612
donatien 0:2ccb9960a044 613 }
wolfSSL 20:bec882d85856 614 cyassl_free() ;
wolfSSL 18:d89df40b4cf3 615 m_sock.close();
wolfSSL 18:d89df40b4cf3 616 DBG("Completed HTTP transaction");
wolfSSL 27:5d4739eae63e 617 if(redirect)return HTTP_REDIRECT ;
wolfSSL 27:5d4739eae63e 618 else return HTTP_OK;
donatien 0:2ccb9960a044 619 }
donatien 0:2ccb9960a044 620
wolfSSL 19:1e2f05809eb1 621 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
donatien 0:2ccb9960a044 622 {
wolfSSL 18:d89df40b4cf3 623 DBG("Trying to read between %d and %d bytes", minLen, maxLen);
wolfSSL 18:d89df40b4cf3 624 size_t readLen = 0;
wolfSSL 18:d89df40b4cf3 625
wolfSSL 18:d89df40b4cf3 626 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 627 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 628 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 629 }
wolfSSL 18:d89df40b4cf3 630
wolfSSL 18:d89df40b4cf3 631 int ret;
wolfSSL 18:d89df40b4cf3 632
wolfSSL 18:d89df40b4cf3 633 if(port == HTTPS_PORT) {
wolfSSL 18:d89df40b4cf3 634 DBG("Enter CyaSSL_read") ;
wolfSSL 18:d89df40b4cf3 635
wolfSSL 18:d89df40b4cf3 636 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 637 readLen = CyaSSL_read(ssl, buf, maxLen);
wolfSSL 18:d89df40b4cf3 638 if (readLen > 0) {
wolfSSL 18:d89df40b4cf3 639 buf[readLen] = 0;
wolfSSL 18:d89df40b4cf3 640 DBG("CyaSSL_read:%s\n", buf);
wolfSSL 18:d89df40b4cf3 641 } else {
wolfSSL 18:d89df40b4cf3 642 ERR("CyaSSL_read, ret = %d", readLen) ;
wolfSSL 18:d89df40b4cf3 643 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 644 }
wolfSSL 18:d89df40b4cf3 645 DBG("Read %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 646 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 647 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 648 }
wolfSSL 18:d89df40b4cf3 649
wolfSSL 18:d89df40b4cf3 650 while(readLen < maxLen) {
wolfSSL 18:d89df40b4cf3 651 if(readLen < minLen) {
wolfSSL 18:d89df40b4cf3 652 DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
wolfSSL 18:d89df40b4cf3 653 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 654 ret = m_sock.receive_all(buf + readLen, minLen - readLen);
wolfSSL 18:d89df40b4cf3 655 } else {
wolfSSL 18:d89df40b4cf3 656 DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
wolfSSL 18:d89df40b4cf3 657 m_sock.set_blocking(false, 0);
wolfSSL 18:d89df40b4cf3 658 ret = m_sock.receive(buf + readLen, maxLen - readLen);
wolfSSL 18:d89df40b4cf3 659 }
wolfSSL 18:d89df40b4cf3 660
wolfSSL 18:d89df40b4cf3 661 if( ret > 0) {
wolfSSL 18:d89df40b4cf3 662 readLen += ret;
wolfSSL 18:d89df40b4cf3 663 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 664 break;
wolfSSL 18:d89df40b4cf3 665 } else {
wolfSSL 18:d89df40b4cf3 666 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 667 ERR("Connection error (recv returned %d)", ret);
wolfSSL 18:d89df40b4cf3 668 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 669 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 670 } else {
wolfSSL 18:d89df40b4cf3 671 break;
wolfSSL 18:d89df40b4cf3 672 }
wolfSSL 18:d89df40b4cf3 673 }
wolfSSL 18:d89df40b4cf3 674
wolfSSL 18:d89df40b4cf3 675 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 676 break;
wolfSSL 18:d89df40b4cf3 677 }
wolfSSL 17:c73d8e61d391 678 }
wolfSSL 17:c73d8e61d391 679 DBG("Read %d bytes", readLen);
wolfSSL 17:c73d8e61d391 680 *pReadLen = readLen;
wolfSSL 17:c73d8e61d391 681 return HTTP_OK;
donatien 7:4e39864f7b15 682 }
donatien 7:4e39864f7b15 683
wolfSSL 19:1e2f05809eb1 684 HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
donatien 7:4e39864f7b15 685 {
wolfSSL 18:d89df40b4cf3 686 HTTPResult ret ;
wolfSSL 18:d89df40b4cf3 687 int cp_len ;
wolfSSL 18:d89df40b4cf3 688
wolfSSL 18:d89df40b4cf3 689 if(len == 0) {
wolfSSL 18:d89df40b4cf3 690 len = strlen(buf);
wolfSSL 17:c73d8e61d391 691 }
wolfSSL 17:c73d8e61d391 692
wolfSSL 18:d89df40b4cf3 693 do {
wolfSSL 22:4b9a4151cc73 694
wolfSSL 18:d89df40b4cf3 695 if((SEND_BUF_SIZE - (send_buf_p - send_buf)) >= len) {
wolfSSL 18:d89df40b4cf3 696 cp_len = len ;
wolfSSL 18:d89df40b4cf3 697 } else {
wolfSSL 22:4b9a4151cc73 698 cp_len = SEND_BUF_SIZE - (send_buf_p - send_buf) ;
wolfSSL 18:d89df40b4cf3 699 }
wolfSSL 22:4b9a4151cc73 700 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 701 memcpy(send_buf_p, buf, cp_len) ;
wolfSSL 18:d89df40b4cf3 702 send_buf_p += cp_len ;
wolfSSL 18:d89df40b4cf3 703 len -= cp_len ;
wolfSSL 18:d89df40b4cf3 704
wolfSSL 18:d89df40b4cf3 705 if(send_buf_p == send_buf + SEND_BUF_SIZE) {
wolfSSL 22:4b9a4151cc73 706 if(port == HTTPS_PORT){
wolfSSL 22:4b9a4151cc73 707 ERR("HTTPClient::send buffer overflow");
wolfSSL 22:4b9a4151cc73 708 return HTTP_ERROR ;
wolfSSL 22:4b9a4151cc73 709 }
wolfSSL 18:d89df40b4cf3 710 ret = flush() ;
wolfSSL 18:d89df40b4cf3 711 if(ret)return(ret) ;
wolfSSL 18:d89df40b4cf3 712 }
wolfSSL 18:d89df40b4cf3 713 } while(len) ;
wolfSSL 18:d89df40b4cf3 714 return HTTP_OK ;
wolfSSL 17:c73d8e61d391 715 }
wolfSSL 17:c73d8e61d391 716
wolfSSL 19:1e2f05809eb1 717 HTTPResult HTTPClient::flush() //0 on success, err code on failure
wolfSSL 17:c73d8e61d391 718 {
wolfSSL 18:d89df40b4cf3 719 int len ;
wolfSSL 18:d89df40b4cf3 720 char * buf ;
wolfSSL 18:d89df40b4cf3 721
wolfSSL 18:d89df40b4cf3 722 buf = send_buf ;
wolfSSL 18:d89df40b4cf3 723 len = send_buf_p - send_buf ;
wolfSSL 18:d89df40b4cf3 724 send_buf_p = send_buf ; // reset send buffer
wolfSSL 18:d89df40b4cf3 725
wolfSSL 18:d89df40b4cf3 726 DBG("Trying to write %d bytes:%s\n", len, buf);
wolfSSL 18:d89df40b4cf3 727 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 728
wolfSSL 18:d89df40b4cf3 729 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 730 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 731 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 17:c73d8e61d391 732 }
wolfSSL 18:d89df40b4cf3 733
wolfSSL 18:d89df40b4cf3 734 if(port == HTTPS_PORT) {
wolfSSL 18:d89df40b4cf3 735 DBG("Enter CyaSSL_write") ;
wolfSSL 18:d89df40b4cf3 736 if (CyaSSL_write(ssl, buf, len) != len) {
wolfSSL 18:d89df40b4cf3 737 ERR("SSL_write failed");
wolfSSL 18:d89df40b4cf3 738 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 739 }
wolfSSL 18:d89df40b4cf3 740 DBG("Written %d bytes", writtenLen);
wolfSSL 18:d89df40b4cf3 741 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 742 }
wolfSSL 18:d89df40b4cf3 743 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 744 int ret = m_sock.send_all(buf, len);
wolfSSL 18:d89df40b4cf3 745 if(ret > 0) {
wolfSSL 18:d89df40b4cf3 746 writtenLen += ret;
wolfSSL 18:d89df40b4cf3 747 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 748 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 749 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 750 } else {
wolfSSL 18:d89df40b4cf3 751 ERR("Connection error (send returned %d)", ret);
wolfSSL 18:d89df40b4cf3 752 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 753 }
wolfSSL 18:d89df40b4cf3 754
wolfSSL 17:c73d8e61d391 755 DBG("Written %d bytes", writtenLen);
wolfSSL 17:c73d8e61d391 756 return HTTP_OK;
donatien 0:2ccb9960a044 757 }
donatien 0:2ccb9960a044 758
wolfSSL 19:1e2f05809eb1 759 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 760 {
wolfSSL 18:d89df40b4cf3 761 char* schemePtr = (char*) url;
wolfSSL 18:d89df40b4cf3 762 char* hostPtr = (char*) strstr(url, "://");
wolfSSL 18:d89df40b4cf3 763 if(hostPtr == NULL) {
wolfSSL 18:d89df40b4cf3 764 WARN("Could not find host");
wolfSSL 18:d89df40b4cf3 765 return HTTP_PARSE; //URL is invalid
wolfSSL 18:d89df40b4cf3 766 }
wolfSSL 18:d89df40b4cf3 767
wolfSSL 18:d89df40b4cf3 768 if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 769 WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
wolfSSL 18:d89df40b4cf3 770 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 771 }
wolfSSL 18:d89df40b4cf3 772 memcpy(scheme, schemePtr, hostPtr - schemePtr);
wolfSSL 18:d89df40b4cf3 773 scheme[hostPtr - schemePtr] = '\0';
donatien 0:2ccb9960a044 774
wolfSSL 18:d89df40b4cf3 775 hostPtr+=3;
donatien 0:2ccb9960a044 776
wolfSSL 18:d89df40b4cf3 777 size_t hostLen = 0;
donatien 0:2ccb9960a044 778
wolfSSL 18:d89df40b4cf3 779 char* portPtr = strchr(hostPtr, ':');
wolfSSL 18:d89df40b4cf3 780 if( portPtr != NULL ) {
wolfSSL 18:d89df40b4cf3 781 hostLen = portPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 782 portPtr++;
wolfSSL 18:d89df40b4cf3 783 if( sscanf(portPtr, "%hu", port) != 1) {
wolfSSL 18:d89df40b4cf3 784 WARN("Could not find port");
wolfSSL 18:d89df40b4cf3 785 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 786 }
wolfSSL 18:d89df40b4cf3 787 } else {
wolfSSL 18:d89df40b4cf3 788 *port=0;
donatien 0:2ccb9960a044 789 }
wolfSSL 18:d89df40b4cf3 790 char* pathPtr = strchr(hostPtr, '/');
wolfSSL 18:d89df40b4cf3 791 if( hostLen == 0 ) {
wolfSSL 18:d89df40b4cf3 792 hostLen = pathPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 793 }
donatien 0:2ccb9960a044 794
wolfSSL 18:d89df40b4cf3 795 if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 796 WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
wolfSSL 18:d89df40b4cf3 797 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 798 }
wolfSSL 18:d89df40b4cf3 799 memcpy(host, hostPtr, hostLen);
wolfSSL 18:d89df40b4cf3 800 host[hostLen] = '\0';
donatien 0:2ccb9960a044 801
wolfSSL 18:d89df40b4cf3 802 size_t pathLen;
wolfSSL 18:d89df40b4cf3 803 char* fragmentPtr = strchr(hostPtr, '#');
wolfSSL 18:d89df40b4cf3 804 if(fragmentPtr != NULL) {
wolfSSL 18:d89df40b4cf3 805 pathLen = fragmentPtr - pathPtr;
wolfSSL 18:d89df40b4cf3 806 } else {
wolfSSL 18:d89df40b4cf3 807 pathLen = strlen(pathPtr);
wolfSSL 18:d89df40b4cf3 808 }
donatien 0:2ccb9960a044 809
wolfSSL 18:d89df40b4cf3 810 if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 811 WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
wolfSSL 18:d89df40b4cf3 812 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 813 }
wolfSSL 18:d89df40b4cf3 814 memcpy(path, pathPtr, pathLen);
wolfSSL 18:d89df40b4cf3 815 path[pathLen] = '\0';
donatien 0:2ccb9960a044 816
wolfSSL 18:d89df40b4cf3 817 return HTTP_OK;
donatien 0:2ccb9960a044 818 }
wolfSSL 22:4b9a4151cc73 819
wolfSSL 22:4b9a4151cc73 820 HTTPResult HTTPClient::bAuth(void)
wolfSSL 22:4b9a4151cc73 821 {
wolfSSL 22:4b9a4151cc73 822 HTTPResult ret ;
wolfSSL 22:4b9a4151cc73 823 char b_auth[(int)((AUTHB_SIZE+3)*4/3+1)] ;
wolfSSL 22:4b9a4151cc73 824 char base64buff[AUTHB_SIZE+3] ;
wolfSSL 22:4b9a4151cc73 825
wolfSSL 22:4b9a4151cc73 826 ret = send("Authorization: Basic ") ;
wolfSSL 22:4b9a4151cc73 827 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 828 sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
wolfSSL 27:5d4739eae63e 829 DBG("bAuth: %s", base64buff) ;
wolfSSL 22:4b9a4151cc73 830 base64enc(b_auth, base64buff) ;
wolfSSL 22:4b9a4151cc73 831 b_auth[strlen(b_auth)+1] = '\0' ;
wolfSSL 22:4b9a4151cc73 832 b_auth[strlen(b_auth)] = '\n' ;
wolfSSL 22:4b9a4151cc73 833 DBG("b_auth:%s", b_auth) ;
wolfSSL 22:4b9a4151cc73 834 ret = send(b_auth) ;
wolfSSL 22:4b9a4151cc73 835 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 836 return HTTP_OK ;
wolfSSL 22:4b9a4151cc73 837 }