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