HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

Committer:
takeuchiyoshihiko
Date:
Wed Aug 23 09:30:54 2017 +0000
Revision:
34:ce9d3c551150
Parent:
22:4b9a4151cc73
Child:
26:bf979804b653
for CyaSSL

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