A HTTP/HTTPS Client for the mbed networking/CyaSSL ssl library

Dependents:   Anpi Example--Dropbox Example-Xively dropbox_access ... more

Fork of HTTPClient by Donatien Garnier

HTTP and HTTPS Client Class with wolfSSL, embedded SSL library.

/media/uploads/wolfSSL/wolfssl_logo.png

The class was forked from http://mbed.org/users/donatien/code/HTTPClient/

It, now, accepts url both with "http://" and "https://".

Allocate caller thread with 16kbytes or larger stack for "https" requests.

Rest of the API stays compatible with HTTPClient.

For more about the library, see http://www.wolfssl.com. http://wolfssl.com/yaSSL/Docs.html.

Extended methods:

Committer:
wolfSSL
Date:
Thu Apr 28 00:55:27 2016 +0000
Revision:
34:76aa4f4021c1
Parent:
33:77082c88748a
Sync with wolfSSL 3.9.0

Who changed what in which revision?

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