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