my fork

Fork of HTTPClient by wolf SSL

Committer:
wolfSSL
Date:
Mon Jul 21 11:29:00 2014 +0000
Revision:
27:5d4739eae63e
Parent:
22:4b9a4151cc73
Child:
28:25b5d3720bd1
Minor fix

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