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