my fork

Fork of HTTPClient by wolf SSL

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:47 2015 +0000
Revision:
31:7fd621b83b60
Parent:
30:a9ecee69c6b5
Child:
32:9aadb8a34e80
with wolfSSL 3.6.0

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