HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

Committer:
wolfSSL
Date:
Thu Apr 17 12:23:47 2014 +0000
Revision:
21:14ecc2b2e282
Parent:
20:bec882d85856
Child:
22:4b9a4151cc73
CyaSSL 2.9.4

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