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