HTTPClient for CyaSSL

Dependencies:   CyaSSL

Dependents:   kintone-POST kintone-POST

Fork of HTTPClient by wolf SSL

Committer:
wolfSSL
Date:
Mon Apr 07 23:30:35 2014 +0000
Revision:
17:c73d8e61d391
Parent:
16:1f743885e7de
Child:
18:d89df40b4cf3
Added SSL by CyaSSL library; Added header method; Improved send buffer; Added retry connection

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