HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

Committer:
wolfSSL
Date:
Wed Dec 03 05:14:23 2014 +0000
Revision:
28:25b5d3720bd1
Parent:
27:5d4739eae63e
Child:
30:a9ecee69c6b5
timing after request sent;

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 28:25b5d3720bd1 326 wait(0.1) ;
wolfSSL 28:25b5d3720bd1 327
wolfSSL 18:d89df40b4cf3 328 //Send all headers
donatien 0:2ccb9960a044 329
wolfSSL 18:d89df40b4cf3 330 //Send default headers
wolfSSL 18:d89df40b4cf3 331 DBG("Sending headers");
wolfSSL 27:5d4739eae63e 332 if(m_basicAuthUser) {
wolfSSL 27:5d4739eae63e 333 bAuth() ; /* send out Basic Auth header */
wolfSSL 27:5d4739eae63e 334 }
wolfSSL 18:d89df40b4cf3 335 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 336 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 337 ret = send("Transfer-Encoding: chunked\r\n");
wolfSSL 18:d89df40b4cf3 338 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 339 } else {
wolfSSL 18:d89df40b4cf3 340 snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
wolfSSL 22:4b9a4151cc73 341 DBG("Content buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 342 ret = send(buf);
wolfSSL 18:d89df40b4cf3 343 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 344 }
wolfSSL 18:d89df40b4cf3 345 char type[48];
wolfSSL 18:d89df40b4cf3 346 if( pDataOut->getDataType(type, 48) == HTTP_OK ) {
wolfSSL 18:d89df40b4cf3 347 snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
wolfSSL 18:d89df40b4cf3 348 ret = send(buf);
wolfSSL 18:d89df40b4cf3 349 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 350 }
wolfSSL 18:d89df40b4cf3 351 }
wolfSSL 18:d89df40b4cf3 352
wolfSSL 18:d89df40b4cf3 353 //Add user headers
wolfSSL 18:d89df40b4cf3 354 if(header) {
wolfSSL 27:5d4739eae63e 355 ret = send((char *)header);
donatien 5:791fc3dcb6c4 356 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 357 }
donatien 0:2ccb9960a044 358
wolfSSL 18:d89df40b4cf3 359 //Close headers
wolfSSL 18:d89df40b4cf3 360 DBG("Headers sent");
wolfSSL 18:d89df40b4cf3 361 ret = send("\r\n");
wolfSSL 18:d89df40b4cf3 362 CHECK_CONN_ERR(ret);
wolfSSL 17:c73d8e61d391 363
wolfSSL 18:d89df40b4cf3 364 size_t trfLen;
donatien 0:2ccb9960a044 365
wolfSSL 18:d89df40b4cf3 366 //Send data (if available)
wolfSSL 18:d89df40b4cf3 367 if( pDataOut != NULL ) {
wolfSSL 18:d89df40b4cf3 368 DBG("Sending data");
wolfSSL 18:d89df40b4cf3 369 while(true) {
wolfSSL 18:d89df40b4cf3 370 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 371 pDataOut->read(buf, CHUNK_SIZE, &trfLen);
wolfSSL 18:d89df40b4cf3 372 buf[trfLen] = 0x0 ;
wolfSSL 18:d89df40b4cf3 373 DBG("buf:%s", buf) ;
wolfSSL 18:d89df40b4cf3 374 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 375 //Write chunk header
wolfSSL 22:4b9a4151cc73 376 char chunkHeader[64];
wolfSSL 18:d89df40b4cf3 377 snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
wolfSSL 18:d89df40b4cf3 378 ret = send(chunkHeader);
wolfSSL 18:d89df40b4cf3 379 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 380 } else if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 381 DBG("trfLen==0") ;
wolfSSL 18:d89df40b4cf3 382 break;
wolfSSL 18:d89df40b4cf3 383 }
wolfSSL 22:4b9a4151cc73 384 DBG("trfLen 1=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 385 if( trfLen != 0 ) {
wolfSSL 22:4b9a4151cc73 386 DBG("Sending 1") ;
wolfSSL 18:d89df40b4cf3 387 ret = send(buf, trfLen);
wolfSSL 22:4b9a4151cc73 388 DBG("Sent 1") ;
wolfSSL 18:d89df40b4cf3 389 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 390 }
donatien 0:2ccb9960a044 391
wolfSSL 18:d89df40b4cf3 392 if( pDataOut->getIsChunked() ) {
wolfSSL 18:d89df40b4cf3 393 ret = send("\r\n"); //Chunk-terminating CRLF
wolfSSL 18:d89df40b4cf3 394 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 395 } else {
wolfSSL 18:d89df40b4cf3 396 writtenLen += trfLen;
wolfSSL 18:d89df40b4cf3 397 if( writtenLen >= pDataOut->getDataLen() ) {
wolfSSL 22:4b9a4151cc73 398 DBG("writtenLen=%d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 399 break;
wolfSSL 18:d89df40b4cf3 400 }
wolfSSL 22:4b9a4151cc73 401 DBG("writtenLen+=trfLen = %d", writtenLen) ;
wolfSSL 18:d89df40b4cf3 402 }
wolfSSL 22:4b9a4151cc73 403 DBG("trfLen 2=%d", trfLen) ;
wolfSSL 18:d89df40b4cf3 404 if( trfLen == 0 ) {
wolfSSL 22:4b9a4151cc73 405 DBG("trfLen == 0") ;
wolfSSL 18:d89df40b4cf3 406 break;
wolfSSL 18:d89df40b4cf3 407 }
wolfSSL 18:d89df40b4cf3 408 }
donatien 0:2ccb9960a044 409
wolfSSL 18:d89df40b4cf3 410 }
wolfSSL 18:d89df40b4cf3 411 ret = flush() ; // flush the send buffer ;
wolfSSL 18:d89df40b4cf3 412 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 413
wolfSSL 18:d89df40b4cf3 414 //Receive response
wolfSSL 18:d89df40b4cf3 415 DBG("Receiving response");
wolfSSL 18:d89df40b4cf3 416
wolfSSL 18:d89df40b4cf3 417 ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
wolfSSL 18:d89df40b4cf3 418 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 419
wolfSSL 18:d89df40b4cf3 420 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 421
wolfSSL 18:d89df40b4cf3 422 char* crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 423 if(crlfPtr == NULL) {
donatien 5:791fc3dcb6c4 424 PRTCL_ERR();
donatien 0:2ccb9960a044 425 }
donatien 0:2ccb9960a044 426
wolfSSL 18:d89df40b4cf3 427 int crlfPos = crlfPtr - buf;
donatien 0:2ccb9960a044 428 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 429
wolfSSL 18:d89df40b4cf3 430 //Parse HTTP response
wolfSSL 18:d89df40b4cf3 431 if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) {
wolfSSL 18:d89df40b4cf3 432 //Cannot match string, error
wolfSSL 18:d89df40b4cf3 433 ERR("Not a correct HTTP answer : %s\n", buf);
wolfSSL 18:d89df40b4cf3 434 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 435 }
donatien 4:c071b05ac026 436
wolfSSL 27:5d4739eae63e 437 if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) {
wolfSSL 18:d89df40b4cf3 438 //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers
wolfSSL 18:d89df40b4cf3 439 WARN("Response code %d", m_httpResponseCode);
wolfSSL 18:d89df40b4cf3 440 PRTCL_ERR();
donatien 0:2ccb9960a044 441 }
donatien 0:2ccb9960a044 442
wolfSSL 18:d89df40b4cf3 443 DBG("Reading headers");
donatien 0:2ccb9960a044 444
wolfSSL 18:d89df40b4cf3 445 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 446 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 447
wolfSSL 18:d89df40b4cf3 448 size_t recvContentLength = 0;
wolfSSL 18:d89df40b4cf3 449 bool recvChunked = false;
wolfSSL 18:d89df40b4cf3 450 //Now get headers
wolfSSL 18:d89df40b4cf3 451 while( true ) {
wolfSSL 18:d89df40b4cf3 452 crlfPtr = strstr(buf, "\r\n");
wolfSSL 18:d89df40b4cf3 453 if(crlfPtr == NULL) {
wolfSSL 18:d89df40b4cf3 454 if( trfLen < CHUNK_SIZE - 1 ) {
wolfSSL 18:d89df40b4cf3 455 size_t newTrfLen;
wolfSSL 18:d89df40b4cf3 456 ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 457 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 458 buf[trfLen] = '\0';
wolfSSL 18:d89df40b4cf3 459 DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
wolfSSL 18:d89df40b4cf3 460 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 461 continue;
wolfSSL 18:d89df40b4cf3 462 } else {
wolfSSL 18:d89df40b4cf3 463 PRTCL_ERR();
donatien 14:2744e0c0e527 464 }
wolfSSL 18:d89df40b4cf3 465 }
wolfSSL 18:d89df40b4cf3 466
wolfSSL 18:d89df40b4cf3 467 crlfPos = crlfPtr - buf;
wolfSSL 18:d89df40b4cf3 468
wolfSSL 18:d89df40b4cf3 469 if(crlfPos == 0) { //End of headers
wolfSSL 18:d89df40b4cf3 470 DBG("Headers read");
wolfSSL 18:d89df40b4cf3 471 memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 472 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 473 break;
donatien 0:2ccb9960a044 474 }
wolfSSL 18:d89df40b4cf3 475
wolfSSL 18:d89df40b4cf3 476 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 477
wolfSSL 18:d89df40b4cf3 478 char key[32];
wolfSSL 18:d89df40b4cf3 479 char value[32];
wolfSSL 18:d89df40b4cf3 480
wolfSSL 18:d89df40b4cf3 481 key[31] = '\0';
wolfSSL 18:d89df40b4cf3 482 value[31] = '\0';
wolfSSL 18:d89df40b4cf3 483
wolfSSL 18:d89df40b4cf3 484 int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
wolfSSL 18:d89df40b4cf3 485 if ( n == 2 ) {
wolfSSL 18:d89df40b4cf3 486 DBG("Read header : %s: %s\n", key, value);
wolfSSL 18:d89df40b4cf3 487 if( !strcmp(key, "Content-Length") ) {
wolfSSL 18:d89df40b4cf3 488 sscanf(value, "%d", &recvContentLength);
wolfSSL 18:d89df40b4cf3 489 pDataIn->setDataLen(recvContentLength);
wolfSSL 18:d89df40b4cf3 490 } else if( !strcmp(key, "Transfer-Encoding") ) {
wolfSSL 18:d89df40b4cf3 491 if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) {
wolfSSL 18:d89df40b4cf3 492 recvChunked = true;
wolfSSL 18:d89df40b4cf3 493 pDataIn->setIsChunked(true);
wolfSSL 18:d89df40b4cf3 494 }
wolfSSL 18:d89df40b4cf3 495 } else if( !strcmp(key, "Content-Type") ) {
wolfSSL 18:d89df40b4cf3 496 pDataIn->setDataType(value);
wolfSSL 27:5d4739eae63e 497 } else if( !strcmp(key, "location") && redirect_url) {
wolfSSL 27:5d4739eae63e 498 sscanf(buf, "%31[^:]: %128[^\r\n]", key, redirect_url);
wolfSSL 27:5d4739eae63e 499 DBG("Redirect %s: %s", key, redirect_url) ;
wolfSSL 27:5d4739eae63e 500 redirect = 1 ;
wolfSSL 18:d89df40b4cf3 501 }
wolfSSL 18:d89df40b4cf3 502 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
wolfSSL 18:d89df40b4cf3 503 trfLen -= (crlfPos + 2);
wolfSSL 18:d89df40b4cf3 504
wolfSSL 18:d89df40b4cf3 505 } else {
wolfSSL 18:d89df40b4cf3 506 ERR("Could not parse header");
donatien 14:2744e0c0e527 507 PRTCL_ERR();
donatien 0:2ccb9960a044 508 }
donatien 0:2ccb9960a044 509
donatien 0:2ccb9960a044 510 }
donatien 0:2ccb9960a044 511
wolfSSL 18:d89df40b4cf3 512 //Receive data
wolfSSL 18:d89df40b4cf3 513 DBG("Receiving data");
wolfSSL 18:d89df40b4cf3 514
wolfSSL 18:d89df40b4cf3 515 while(true) {
wolfSSL 18:d89df40b4cf3 516 size_t readLen = 0;
donatien 0:2ccb9960a044 517
wolfSSL 18:d89df40b4cf3 518 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 519 //Read chunk header
wolfSSL 18:d89df40b4cf3 520 bool foundCrlf;
wolfSSL 18:d89df40b4cf3 521 do {
wolfSSL 18:d89df40b4cf3 522 foundCrlf = false;
wolfSSL 18:d89df40b4cf3 523 crlfPos=0;
wolfSSL 18:d89df40b4cf3 524 buf[trfLen]=0;
wolfSSL 18:d89df40b4cf3 525 if(trfLen >= 2) {
wolfSSL 18:d89df40b4cf3 526 for(; crlfPos < trfLen - 2; crlfPos++) {
wolfSSL 18:d89df40b4cf3 527 if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) {
wolfSSL 18:d89df40b4cf3 528 foundCrlf = true;
wolfSSL 18:d89df40b4cf3 529 break;
wolfSSL 18:d89df40b4cf3 530 }
wolfSSL 18:d89df40b4cf3 531 }
wolfSSL 18:d89df40b4cf3 532 }
wolfSSL 18:d89df40b4cf3 533 if(!foundCrlf) { //Try to read more
wolfSSL 18:d89df40b4cf3 534 if( trfLen < CHUNK_SIZE ) {
wolfSSL 18:d89df40b4cf3 535 size_t newTrfLen;
wolfSSL 18:d89df40b4cf3 536 ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 537 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 538 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 539 continue;
wolfSSL 18:d89df40b4cf3 540 } else {
wolfSSL 18:d89df40b4cf3 541 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 542 }
wolfSSL 18:d89df40b4cf3 543 }
wolfSSL 18:d89df40b4cf3 544 } while(!foundCrlf);
wolfSSL 18:d89df40b4cf3 545 buf[crlfPos] = '\0';
wolfSSL 18:d89df40b4cf3 546 int n = sscanf(buf, "%x", &readLen);
wolfSSL 18:d89df40b4cf3 547 if(n!=1) {
wolfSSL 18:d89df40b4cf3 548 ERR("Could not read chunk length");
wolfSSL 18:d89df40b4cf3 549 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 550 }
wolfSSL 18:d89df40b4cf3 551
wolfSSL 18:d89df40b4cf3 552 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
wolfSSL 18:d89df40b4cf3 553 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 554
wolfSSL 18:d89df40b4cf3 555 if( readLen == 0 ) {
wolfSSL 18:d89df40b4cf3 556 //Last chunk
wolfSSL 18:d89df40b4cf3 557 break;
wolfSSL 18:d89df40b4cf3 558 }
wolfSSL 18:d89df40b4cf3 559 } else {
wolfSSL 18:d89df40b4cf3 560 readLen = recvContentLength;
wolfSSL 18:d89df40b4cf3 561 }
wolfSSL 18:d89df40b4cf3 562
wolfSSL 18:d89df40b4cf3 563 DBG("Retrieving %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 564
wolfSSL 18:d89df40b4cf3 565 do {
wolfSSL 18:d89df40b4cf3 566 pDataIn->write(buf, MIN(trfLen, readLen));
wolfSSL 18:d89df40b4cf3 567 if( trfLen > readLen ) {
wolfSSL 18:d89df40b4cf3 568 memmove(buf, &buf[readLen], trfLen - readLen);
wolfSSL 18:d89df40b4cf3 569 trfLen -= readLen;
wolfSSL 18:d89df40b4cf3 570 readLen = 0;
wolfSSL 18:d89df40b4cf3 571 } else {
wolfSSL 18:d89df40b4cf3 572 readLen -= trfLen;
wolfSSL 18:d89df40b4cf3 573 }
donatien 0:2ccb9960a044 574
wolfSSL 18:d89df40b4cf3 575 if(readLen) {
wolfSSL 18:d89df40b4cf3 576 ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
wolfSSL 18:d89df40b4cf3 577 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 578 }
wolfSSL 18:d89df40b4cf3 579 } while(readLen);
wolfSSL 18:d89df40b4cf3 580
wolfSSL 18:d89df40b4cf3 581 if( recvChunked ) {
wolfSSL 18:d89df40b4cf3 582 if(trfLen < 2) {
wolfSSL 18:d89df40b4cf3 583 size_t newTrfLen;
wolfSSL 18:d89df40b4cf3 584 //Read missing chars to find end of chunk
wolfSSL 18:d89df40b4cf3 585 ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen);
wolfSSL 18:d89df40b4cf3 586 CHECK_CONN_ERR(ret);
wolfSSL 18:d89df40b4cf3 587 trfLen += newTrfLen;
wolfSSL 18:d89df40b4cf3 588 }
wolfSSL 18:d89df40b4cf3 589 if( (buf[0] != '\r') || (buf[1] != '\n') ) {
wolfSSL 18:d89df40b4cf3 590 ERR("Format error");
wolfSSL 18:d89df40b4cf3 591 PRTCL_ERR();
wolfSSL 18:d89df40b4cf3 592 }
wolfSSL 18:d89df40b4cf3 593 memmove(buf, &buf[2], trfLen - 2);
wolfSSL 18:d89df40b4cf3 594 trfLen -= 2;
wolfSSL 18:d89df40b4cf3 595 } else {
wolfSSL 18:d89df40b4cf3 596 break;
wolfSSL 18:d89df40b4cf3 597 }
wolfSSL 18:d89df40b4cf3 598
donatien 0:2ccb9960a044 599 }
wolfSSL 20:bec882d85856 600 cyassl_free() ;
wolfSSL 18:d89df40b4cf3 601 m_sock.close();
wolfSSL 18:d89df40b4cf3 602 DBG("Completed HTTP transaction");
wolfSSL 27:5d4739eae63e 603 if(redirect)return HTTP_REDIRECT ;
wolfSSL 27:5d4739eae63e 604 else return HTTP_OK;
donatien 0:2ccb9960a044 605 }
donatien 0:2ccb9960a044 606
wolfSSL 19:1e2f05809eb1 607 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
donatien 0:2ccb9960a044 608 {
wolfSSL 18:d89df40b4cf3 609 DBG("Trying to read between %d and %d bytes", minLen, maxLen);
wolfSSL 18:d89df40b4cf3 610 size_t readLen = 0;
wolfSSL 28:25b5d3720bd1 611 maxLen = maxLen == 0 ? 1 : maxLen ;
wolfSSL 18:d89df40b4cf3 612 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 613 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 614 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 615 }
wolfSSL 18:d89df40b4cf3 616
wolfSSL 18:d89df40b4cf3 617 int ret;
wolfSSL 18:d89df40b4cf3 618
wolfSSL 18:d89df40b4cf3 619 if(port == HTTPS_PORT) {
wolfSSL 18:d89df40b4cf3 620 DBG("Enter CyaSSL_read") ;
wolfSSL 18:d89df40b4cf3 621
wolfSSL 18:d89df40b4cf3 622 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 623 readLen = CyaSSL_read(ssl, buf, maxLen);
wolfSSL 18:d89df40b4cf3 624 if (readLen > 0) {
wolfSSL 18:d89df40b4cf3 625 buf[readLen] = 0;
wolfSSL 18:d89df40b4cf3 626 DBG("CyaSSL_read:%s\n", buf);
wolfSSL 18:d89df40b4cf3 627 } else {
wolfSSL 18:d89df40b4cf3 628 ERR("CyaSSL_read, ret = %d", readLen) ;
wolfSSL 18:d89df40b4cf3 629 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 630 }
wolfSSL 18:d89df40b4cf3 631 DBG("Read %d bytes", readLen);
wolfSSL 18:d89df40b4cf3 632 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 633 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 634 }
wolfSSL 18:d89df40b4cf3 635
wolfSSL 18:d89df40b4cf3 636 while(readLen < maxLen) {
wolfSSL 18:d89df40b4cf3 637 if(readLen < minLen) {
wolfSSL 18:d89df40b4cf3 638 DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
wolfSSL 18:d89df40b4cf3 639 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 640 ret = m_sock.receive_all(buf + readLen, minLen - readLen);
wolfSSL 18:d89df40b4cf3 641 } else {
wolfSSL 18:d89df40b4cf3 642 DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
wolfSSL 18:d89df40b4cf3 643 m_sock.set_blocking(false, 0);
wolfSSL 18:d89df40b4cf3 644 ret = m_sock.receive(buf + readLen, maxLen - readLen);
wolfSSL 18:d89df40b4cf3 645 }
wolfSSL 18:d89df40b4cf3 646
wolfSSL 18:d89df40b4cf3 647 if( ret > 0) {
wolfSSL 18:d89df40b4cf3 648 readLen += ret;
wolfSSL 18:d89df40b4cf3 649 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 650 break;
wolfSSL 18:d89df40b4cf3 651 } else {
wolfSSL 18:d89df40b4cf3 652 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 653 ERR("Connection error (recv returned %d)", ret);
wolfSSL 18:d89df40b4cf3 654 *pReadLen = readLen;
wolfSSL 18:d89df40b4cf3 655 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 656 } else {
wolfSSL 18:d89df40b4cf3 657 break;
wolfSSL 18:d89df40b4cf3 658 }
wolfSSL 18:d89df40b4cf3 659 }
wolfSSL 18:d89df40b4cf3 660
wolfSSL 18:d89df40b4cf3 661 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 662 break;
wolfSSL 18:d89df40b4cf3 663 }
wolfSSL 17:c73d8e61d391 664 }
wolfSSL 17:c73d8e61d391 665 DBG("Read %d bytes", readLen);
wolfSSL 17:c73d8e61d391 666 *pReadLen = readLen;
wolfSSL 17:c73d8e61d391 667 return HTTP_OK;
donatien 7:4e39864f7b15 668 }
donatien 7:4e39864f7b15 669
wolfSSL 19:1e2f05809eb1 670 HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
donatien 7:4e39864f7b15 671 {
wolfSSL 18:d89df40b4cf3 672 HTTPResult ret ;
wolfSSL 18:d89df40b4cf3 673 int cp_len ;
wolfSSL 18:d89df40b4cf3 674
wolfSSL 18:d89df40b4cf3 675 if(len == 0) {
wolfSSL 18:d89df40b4cf3 676 len = strlen(buf);
wolfSSL 17:c73d8e61d391 677 }
wolfSSL 17:c73d8e61d391 678
wolfSSL 18:d89df40b4cf3 679 do {
wolfSSL 22:4b9a4151cc73 680
wolfSSL 18:d89df40b4cf3 681 if((SEND_BUF_SIZE - (send_buf_p - send_buf)) >= len) {
wolfSSL 18:d89df40b4cf3 682 cp_len = len ;
wolfSSL 18:d89df40b4cf3 683 } else {
wolfSSL 22:4b9a4151cc73 684 cp_len = SEND_BUF_SIZE - (send_buf_p - send_buf) ;
wolfSSL 18:d89df40b4cf3 685 }
wolfSSL 22:4b9a4151cc73 686 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 687 memcpy(send_buf_p, buf, cp_len) ;
wolfSSL 18:d89df40b4cf3 688 send_buf_p += cp_len ;
wolfSSL 18:d89df40b4cf3 689 len -= cp_len ;
wolfSSL 18:d89df40b4cf3 690
wolfSSL 18:d89df40b4cf3 691 if(send_buf_p == send_buf + SEND_BUF_SIZE) {
wolfSSL 22:4b9a4151cc73 692 if(port == HTTPS_PORT){
wolfSSL 22:4b9a4151cc73 693 ERR("HTTPClient::send buffer overflow");
wolfSSL 22:4b9a4151cc73 694 return HTTP_ERROR ;
wolfSSL 22:4b9a4151cc73 695 }
wolfSSL 18:d89df40b4cf3 696 ret = flush() ;
wolfSSL 18:d89df40b4cf3 697 if(ret)return(ret) ;
wolfSSL 18:d89df40b4cf3 698 }
wolfSSL 18:d89df40b4cf3 699 } while(len) ;
wolfSSL 18:d89df40b4cf3 700 return HTTP_OK ;
wolfSSL 17:c73d8e61d391 701 }
wolfSSL 17:c73d8e61d391 702
wolfSSL 19:1e2f05809eb1 703 HTTPResult HTTPClient::flush() //0 on success, err code on failure
wolfSSL 17:c73d8e61d391 704 {
wolfSSL 18:d89df40b4cf3 705 int len ;
wolfSSL 18:d89df40b4cf3 706 char * buf ;
wolfSSL 18:d89df40b4cf3 707
wolfSSL 18:d89df40b4cf3 708 buf = send_buf ;
wolfSSL 18:d89df40b4cf3 709 len = send_buf_p - send_buf ;
wolfSSL 18:d89df40b4cf3 710 send_buf_p = send_buf ; // reset send buffer
wolfSSL 18:d89df40b4cf3 711
wolfSSL 18:d89df40b4cf3 712 DBG("Trying to write %d bytes:%s\n", len, buf);
wolfSSL 18:d89df40b4cf3 713 size_t writtenLen = 0;
wolfSSL 18:d89df40b4cf3 714
wolfSSL 18:d89df40b4cf3 715 if(!m_sock.is_connected()) {
wolfSSL 18:d89df40b4cf3 716 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 717 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 17:c73d8e61d391 718 }
wolfSSL 18:d89df40b4cf3 719
wolfSSL 18:d89df40b4cf3 720 if(port == HTTPS_PORT) {
wolfSSL 18:d89df40b4cf3 721 DBG("Enter CyaSSL_write") ;
wolfSSL 18:d89df40b4cf3 722 if (CyaSSL_write(ssl, buf, len) != len) {
wolfSSL 18:d89df40b4cf3 723 ERR("SSL_write failed");
wolfSSL 18:d89df40b4cf3 724 return HTTP_ERROR ;
wolfSSL 18:d89df40b4cf3 725 }
wolfSSL 18:d89df40b4cf3 726 DBG("Written %d bytes", writtenLen);
wolfSSL 18:d89df40b4cf3 727 return HTTP_OK;
wolfSSL 18:d89df40b4cf3 728 }
wolfSSL 18:d89df40b4cf3 729 m_sock.set_blocking(false, m_timeout);
wolfSSL 18:d89df40b4cf3 730 int ret = m_sock.send_all(buf, len);
wolfSSL 18:d89df40b4cf3 731 if(ret > 0) {
wolfSSL 18:d89df40b4cf3 732 writtenLen += ret;
wolfSSL 18:d89df40b4cf3 733 } else if( ret == 0 ) {
wolfSSL 18:d89df40b4cf3 734 WARN("Connection was closed by server");
wolfSSL 18:d89df40b4cf3 735 return HTTP_CLOSED; //Connection was closed by server
wolfSSL 18:d89df40b4cf3 736 } else {
wolfSSL 18:d89df40b4cf3 737 ERR("Connection error (send returned %d)", ret);
wolfSSL 18:d89df40b4cf3 738 return HTTP_CONN;
wolfSSL 18:d89df40b4cf3 739 }
wolfSSL 18:d89df40b4cf3 740
wolfSSL 17:c73d8e61d391 741 DBG("Written %d bytes", writtenLen);
wolfSSL 17:c73d8e61d391 742 return HTTP_OK;
donatien 0:2ccb9960a044 743 }
donatien 0:2ccb9960a044 744
wolfSSL 19:1e2f05809eb1 745 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 746 {
wolfSSL 18:d89df40b4cf3 747 char* schemePtr = (char*) url;
wolfSSL 18:d89df40b4cf3 748 char* hostPtr = (char*) strstr(url, "://");
wolfSSL 18:d89df40b4cf3 749 if(hostPtr == NULL) {
wolfSSL 18:d89df40b4cf3 750 WARN("Could not find host");
wolfSSL 18:d89df40b4cf3 751 return HTTP_PARSE; //URL is invalid
wolfSSL 18:d89df40b4cf3 752 }
wolfSSL 18:d89df40b4cf3 753
wolfSSL 18:d89df40b4cf3 754 if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 755 WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
wolfSSL 18:d89df40b4cf3 756 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 757 }
wolfSSL 18:d89df40b4cf3 758 memcpy(scheme, schemePtr, hostPtr - schemePtr);
wolfSSL 18:d89df40b4cf3 759 scheme[hostPtr - schemePtr] = '\0';
donatien 0:2ccb9960a044 760
wolfSSL 18:d89df40b4cf3 761 hostPtr+=3;
donatien 0:2ccb9960a044 762
wolfSSL 18:d89df40b4cf3 763 size_t hostLen = 0;
donatien 0:2ccb9960a044 764
wolfSSL 18:d89df40b4cf3 765 char* portPtr = strchr(hostPtr, ':');
wolfSSL 18:d89df40b4cf3 766 if( portPtr != NULL ) {
wolfSSL 18:d89df40b4cf3 767 hostLen = portPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 768 portPtr++;
wolfSSL 18:d89df40b4cf3 769 if( sscanf(portPtr, "%hu", port) != 1) {
wolfSSL 18:d89df40b4cf3 770 WARN("Could not find port");
wolfSSL 18:d89df40b4cf3 771 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 772 }
wolfSSL 18:d89df40b4cf3 773 } else {
wolfSSL 18:d89df40b4cf3 774 *port=0;
donatien 0:2ccb9960a044 775 }
wolfSSL 18:d89df40b4cf3 776 char* pathPtr = strchr(hostPtr, '/');
wolfSSL 18:d89df40b4cf3 777 if( hostLen == 0 ) {
wolfSSL 18:d89df40b4cf3 778 hostLen = pathPtr - hostPtr;
wolfSSL 18:d89df40b4cf3 779 }
donatien 0:2ccb9960a044 780
wolfSSL 18:d89df40b4cf3 781 if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 782 WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
wolfSSL 18:d89df40b4cf3 783 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 784 }
wolfSSL 18:d89df40b4cf3 785 memcpy(host, hostPtr, hostLen);
wolfSSL 18:d89df40b4cf3 786 host[hostLen] = '\0';
donatien 0:2ccb9960a044 787
wolfSSL 18:d89df40b4cf3 788 size_t pathLen;
wolfSSL 18:d89df40b4cf3 789 char* fragmentPtr = strchr(hostPtr, '#');
wolfSSL 18:d89df40b4cf3 790 if(fragmentPtr != NULL) {
wolfSSL 18:d89df40b4cf3 791 pathLen = fragmentPtr - pathPtr;
wolfSSL 18:d89df40b4cf3 792 } else {
wolfSSL 18:d89df40b4cf3 793 pathLen = strlen(pathPtr);
wolfSSL 18:d89df40b4cf3 794 }
donatien 0:2ccb9960a044 795
wolfSSL 18:d89df40b4cf3 796 if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char
wolfSSL 18:d89df40b4cf3 797 WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
wolfSSL 18:d89df40b4cf3 798 return HTTP_PARSE;
wolfSSL 18:d89df40b4cf3 799 }
wolfSSL 18:d89df40b4cf3 800 memcpy(path, pathPtr, pathLen);
wolfSSL 18:d89df40b4cf3 801 path[pathLen] = '\0';
donatien 0:2ccb9960a044 802
wolfSSL 18:d89df40b4cf3 803 return HTTP_OK;
donatien 0:2ccb9960a044 804 }
wolfSSL 22:4b9a4151cc73 805
wolfSSL 22:4b9a4151cc73 806 HTTPResult HTTPClient::bAuth(void)
wolfSSL 22:4b9a4151cc73 807 {
wolfSSL 22:4b9a4151cc73 808 HTTPResult ret ;
wolfSSL 22:4b9a4151cc73 809 char b_auth[(int)((AUTHB_SIZE+3)*4/3+1)] ;
wolfSSL 22:4b9a4151cc73 810 char base64buff[AUTHB_SIZE+3] ;
wolfSSL 22:4b9a4151cc73 811
wolfSSL 22:4b9a4151cc73 812 ret = send("Authorization: Basic ") ;
wolfSSL 22:4b9a4151cc73 813 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 814 sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ;
wolfSSL 27:5d4739eae63e 815 DBG("bAuth: %s", base64buff) ;
wolfSSL 22:4b9a4151cc73 816 base64enc(b_auth, base64buff) ;
wolfSSL 22:4b9a4151cc73 817 b_auth[strlen(b_auth)+1] = '\0' ;
wolfSSL 22:4b9a4151cc73 818 b_auth[strlen(b_auth)] = '\n' ;
wolfSSL 22:4b9a4151cc73 819 DBG("b_auth:%s", b_auth) ;
wolfSSL 22:4b9a4151cc73 820 ret = send(b_auth) ;
wolfSSL 22:4b9a4151cc73 821 CHECK_CONN_ERR(ret);
wolfSSL 22:4b9a4151cc73 822 return HTTP_OK ;
wolfSSL 22:4b9a4151cc73 823 }