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