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@36:debaeb6006a7, 2014-09-17 (annotated)
- Committer:
- ansond
- Date:
- Wed Sep 17 21:36:14 2014 +0000
- Revision:
- 36:debaeb6006a7
- Parent:
- 35:16f0a44cc53e
- Child:
- 37:29941a3bae90
updates
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 | 36:debaeb6006a7 | 29 | #ifdef DEBUG_HTTP |
donatien | 12:89d09a6db00a | 30 | //Enable debug |
donatien | 11:390362de8c3f | 31 | #include <cstdio> |
wolfSSL | 18:d89df40b4cf3 | 32 | #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
wolfSSL | 18:d89df40b4cf3 | 33 | #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); |
wolfSSL | 18:d89df40b4cf3 | 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; |
wolfSSL | 17:c73d8e61d391 | 63 | #define CHUNK_SIZE 256 |
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 | { |
wolfSSL | 22:4b9a4151cc73 | 119 | |
wolfSSL | 22:4b9a4151cc73 | 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 | |
wolfSSL | 18:d89df40b4cf3 | 343 | DBG("ctx=%x, ssl=%x, ssl->ctx->CBIORecv, CBIOSend=%x, %x\n", |
wolfSSL | 18:d89df40b4cf3 | 344 | ctx, ssl, SocketReceive, SocketSend ) ; |
ansond | 33:b663a5bab72d | 345 | int result = CyaSSL_connect(ssl); |
ansond | 33:b663a5bab72d | 346 | if (result != SSL_SUCCESS) { |
wolfSSL | 18:d89df40b4cf3 | 347 | ERR("SSL_connect failed"); |
ansond | 33:b663a5bab72d | 348 | int err = CyaSSL_get_error(ssl, result); |
ansond | 33:b663a5bab72d | 349 | char errorString[80]; |
ansond | 33:b663a5bab72d | 350 | CyaSSL_ERR_error_string(err, errorString); |
ansond | 33:b663a5bab72d | 351 | ERR("SSL Error: %s (err=%d,status=%d)",errorString,err,result); |
wolfSSL | 18:d89df40b4cf3 | 352 | cyassl_free() ; |
wolfSSL | 18:d89df40b4cf3 | 353 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 354 | } |
wolfSSL | 18:d89df40b4cf3 | 355 | } /* SSL connect complete */ |
donatien | 0:2ccb9960a044 | 356 | |
wolfSSL | 18:d89df40b4cf3 | 357 | //Send request |
wolfSSL | 18:d89df40b4cf3 | 358 | DBG("Sending request"); |
wolfSSL | 18:d89df40b4cf3 | 359 | char buf[CHUNK_SIZE]; |
ansond | 30:6fef375c94e6 | 360 | memset(buf,0,CHUNK_SIZE); |
wolfSSL | 18:d89df40b4cf3 | 361 | send_buf_p = send_buf ; // Reset send buffer ; |
wolfSSL | 18:d89df40b4cf3 | 362 | |
wolfSSL | 18:d89df40b4cf3 | 363 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
ansond | 31:0675a342e45c | 364 | 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 | 365 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 366 | if(ret) { |
wolfSSL | 18:d89df40b4cf3 | 367 | m_sock.close(); |
wolfSSL | 18:d89df40b4cf3 | 368 | ERR("Could not write request"); |
wolfSSL | 18:d89df40b4cf3 | 369 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 370 | } |
wolfSSL | 17:c73d8e61d391 | 371 | |
wolfSSL | 18:d89df40b4cf3 | 372 | //Send all headers |
donatien | 0:2ccb9960a044 | 373 | |
wolfSSL | 18:d89df40b4cf3 | 374 | //Send default headers |
wolfSSL | 18:d89df40b4cf3 | 375 | DBG("Sending headers"); |
ansond | 31:0675a342e45c | 376 | if(m_basicAuthUser && m_basicAuthPassword) { |
ansond | 36:debaeb6006a7 | 377 | DBG("Sending basic auth header"); |
ansond | 36:debaeb6006a7 | 378 | bAuth() ; /* send out Basic Auth header */ |
ansond | 36:debaeb6006a7 | 379 | } |
ansond | 36:debaeb6006a7 | 380 | else if (m_oauthToken) { |
ansond | 36:debaeb6006a7 | 381 | DBG("Sending OAUTH header"); |
ansond | 36:debaeb6006a7 | 382 | tokenAuth(); /* send out OAUTH header */ |
wolfSSL | 27:5d4739eae63e | 383 | } |
wolfSSL | 18:d89df40b4cf3 | 384 | if( pDataOut != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 385 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 386 | ret = send("Transfer-Encoding: chunked\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 387 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 388 | } else { |
wolfSSL | 18:d89df40b4cf3 | 389 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
wolfSSL | 22:4b9a4151cc73 | 390 | DBG("Content buf:%s", buf) ; |
wolfSSL | 18:d89df40b4cf3 | 391 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 392 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 393 | } |
wolfSSL | 18:d89df40b4cf3 | 394 | char type[48]; |
wolfSSL | 18:d89df40b4cf3 | 395 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) { |
wolfSSL | 18:d89df40b4cf3 | 396 | snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
wolfSSL | 18:d89df40b4cf3 | 397 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 398 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 399 | } |
wolfSSL | 18:d89df40b4cf3 | 400 | } |
wolfSSL | 18:d89df40b4cf3 | 401 | |
wolfSSL | 18:d89df40b4cf3 | 402 | //Add user headers |
wolfSSL | 18:d89df40b4cf3 | 403 | if(header) { |
wolfSSL | 27:5d4739eae63e | 404 | ret = send((char *)header); |
donatien | 5:791fc3dcb6c4 | 405 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 406 | } |
donatien | 0:2ccb9960a044 | 407 | |
wolfSSL | 18:d89df40b4cf3 | 408 | //Close headers |
wolfSSL | 18:d89df40b4cf3 | 409 | DBG("Headers sent"); |
wolfSSL | 18:d89df40b4cf3 | 410 | ret = send("\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 411 | CHECK_CONN_ERR(ret); |
wolfSSL | 17:c73d8e61d391 | 412 | |
wolfSSL | 18:d89df40b4cf3 | 413 | size_t trfLen; |
donatien | 0:2ccb9960a044 | 414 | |
wolfSSL | 18:d89df40b4cf3 | 415 | //Send data (if available) |
wolfSSL | 18:d89df40b4cf3 | 416 | if( pDataOut != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 417 | DBG("Sending data"); |
wolfSSL | 18:d89df40b4cf3 | 418 | while(true) { |
wolfSSL | 18:d89df40b4cf3 | 419 | size_t writtenLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 420 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
wolfSSL | 18:d89df40b4cf3 | 421 | buf[trfLen] = 0x0 ; |
wolfSSL | 18:d89df40b4cf3 | 422 | DBG("buf:%s", buf) ; |
wolfSSL | 18:d89df40b4cf3 | 423 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 424 | //Write chunk header |
wolfSSL | 22:4b9a4151cc73 | 425 | char chunkHeader[64]; |
wolfSSL | 18:d89df40b4cf3 | 426 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
wolfSSL | 18:d89df40b4cf3 | 427 | ret = send(chunkHeader); |
wolfSSL | 18:d89df40b4cf3 | 428 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 429 | } else if( trfLen == 0 ) { |
wolfSSL | 22:4b9a4151cc73 | 430 | DBG("trfLen==0") ; |
wolfSSL | 18:d89df40b4cf3 | 431 | break; |
wolfSSL | 18:d89df40b4cf3 | 432 | } |
wolfSSL | 22:4b9a4151cc73 | 433 | DBG("trfLen 1=%d", trfLen) ; |
wolfSSL | 18:d89df40b4cf3 | 434 | if( trfLen != 0 ) { |
wolfSSL | 22:4b9a4151cc73 | 435 | DBG("Sending 1") ; |
wolfSSL | 18:d89df40b4cf3 | 436 | ret = send(buf, trfLen); |
wolfSSL | 22:4b9a4151cc73 | 437 | DBG("Sent 1") ; |
wolfSSL | 18:d89df40b4cf3 | 438 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 439 | } |
donatien | 0:2ccb9960a044 | 440 | |
wolfSSL | 18:d89df40b4cf3 | 441 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 442 | ret = send("\r\n"); //Chunk-terminating CRLF |
wolfSSL | 18:d89df40b4cf3 | 443 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 444 | } else { |
wolfSSL | 18:d89df40b4cf3 | 445 | writtenLen += trfLen; |
wolfSSL | 18:d89df40b4cf3 | 446 | if( writtenLen >= pDataOut->getDataLen() ) { |
wolfSSL | 22:4b9a4151cc73 | 447 | DBG("writtenLen=%d", writtenLen) ; |
wolfSSL | 18:d89df40b4cf3 | 448 | break; |
wolfSSL | 18:d89df40b4cf3 | 449 | } |
wolfSSL | 22:4b9a4151cc73 | 450 | DBG("writtenLen+=trfLen = %d", writtenLen) ; |
wolfSSL | 18:d89df40b4cf3 | 451 | } |
wolfSSL | 22:4b9a4151cc73 | 452 | DBG("trfLen 2=%d", trfLen) ; |
wolfSSL | 18:d89df40b4cf3 | 453 | if( trfLen == 0 ) { |
wolfSSL | 22:4b9a4151cc73 | 454 | DBG("trfLen == 0") ; |
wolfSSL | 18:d89df40b4cf3 | 455 | break; |
wolfSSL | 18:d89df40b4cf3 | 456 | } |
wolfSSL | 18:d89df40b4cf3 | 457 | } |
donatien | 0:2ccb9960a044 | 458 | |
wolfSSL | 18:d89df40b4cf3 | 459 | } |
wolfSSL | 18:d89df40b4cf3 | 460 | ret = flush() ; // flush the send buffer ; |
wolfSSL | 18:d89df40b4cf3 | 461 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 462 | |
wolfSSL | 18:d89df40b4cf3 | 463 | //Receive response |
wolfSSL | 18:d89df40b4cf3 | 464 | DBG("Receiving response"); |
wolfSSL | 18:d89df40b4cf3 | 465 | |
wolfSSL | 18:d89df40b4cf3 | 466 | ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
wolfSSL | 18:d89df40b4cf3 | 467 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 468 | |
wolfSSL | 18:d89df40b4cf3 | 469 | buf[trfLen] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 470 | |
wolfSSL | 18:d89df40b4cf3 | 471 | char* crlfPtr = strstr(buf, "\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 472 | if(crlfPtr == NULL) { |
donatien | 5:791fc3dcb6c4 | 473 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 474 | } |
donatien | 0:2ccb9960a044 | 475 | |
wolfSSL | 18:d89df40b4cf3 | 476 | int crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 477 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 478 | |
wolfSSL | 18:d89df40b4cf3 | 479 | //Parse HTTP response |
wolfSSL | 18:d89df40b4cf3 | 480 | if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) { |
wolfSSL | 18:d89df40b4cf3 | 481 | //Cannot match string, error |
wolfSSL | 18:d89df40b4cf3 | 482 | ERR("Not a correct HTTP answer : %s\n", buf); |
wolfSSL | 18:d89df40b4cf3 | 483 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 484 | } |
donatien | 4:c071b05ac026 | 485 | |
wolfSSL | 27:5d4739eae63e | 486 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) { |
wolfSSL | 18:d89df40b4cf3 | 487 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
wolfSSL | 18:d89df40b4cf3 | 488 | WARN("Response code %d", m_httpResponseCode); |
wolfSSL | 18:d89df40b4cf3 | 489 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 490 | } |
donatien | 0:2ccb9960a044 | 491 | |
wolfSSL | 18:d89df40b4cf3 | 492 | DBG("Reading headers"); |
donatien | 0:2ccb9960a044 | 493 | |
wolfSSL | 18:d89df40b4cf3 | 494 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 495 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 496 | |
wolfSSL | 18:d89df40b4cf3 | 497 | size_t recvContentLength = 0; |
wolfSSL | 18:d89df40b4cf3 | 498 | bool recvChunked = false; |
wolfSSL | 18:d89df40b4cf3 | 499 | //Now get headers |
wolfSSL | 18:d89df40b4cf3 | 500 | while( true ) { |
wolfSSL | 18:d89df40b4cf3 | 501 | crlfPtr = strstr(buf, "\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 502 | if(crlfPtr == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 503 | if( trfLen < CHUNK_SIZE - 1 ) { |
ansond | 30:6fef375c94e6 | 504 | size_t newTrfLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 505 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 506 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 507 | buf[trfLen] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 508 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
wolfSSL | 18:d89df40b4cf3 | 509 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 510 | continue; |
wolfSSL | 18:d89df40b4cf3 | 511 | } else { |
wolfSSL | 18:d89df40b4cf3 | 512 | PRTCL_ERR(); |
donatien | 14:2744e0c0e527 | 513 | } |
wolfSSL | 18:d89df40b4cf3 | 514 | } |
wolfSSL | 18:d89df40b4cf3 | 515 | |
wolfSSL | 18:d89df40b4cf3 | 516 | crlfPos = crlfPtr - buf; |
wolfSSL | 18:d89df40b4cf3 | 517 | |
wolfSSL | 18:d89df40b4cf3 | 518 | if(crlfPos == 0) { //End of headers |
wolfSSL | 18:d89df40b4cf3 | 519 | DBG("Headers read"); |
wolfSSL | 18:d89df40b4cf3 | 520 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 521 | trfLen -= 2; |
wolfSSL | 18:d89df40b4cf3 | 522 | break; |
donatien | 0:2ccb9960a044 | 523 | } |
wolfSSL | 18:d89df40b4cf3 | 524 | |
wolfSSL | 18:d89df40b4cf3 | 525 | buf[crlfPos] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 526 | |
ansond | 30:6fef375c94e6 | 527 | char key[41]; |
ansond | 30:6fef375c94e6 | 528 | char value[41]; |
wolfSSL | 18:d89df40b4cf3 | 529 | |
ansond | 30:6fef375c94e6 | 530 | memset(key,0,41); |
ansond | 30:6fef375c94e6 | 531 | memset(value,0,41); |
wolfSSL | 18:d89df40b4cf3 | 532 | |
ansond | 30:6fef375c94e6 | 533 | int n = sscanf(buf, "%40[^:]: %40[^\r\n]", key, value); |
wolfSSL | 18:d89df40b4cf3 | 534 | if ( n == 2 ) { |
wolfSSL | 18:d89df40b4cf3 | 535 | DBG("Read header : %s: %s\n", key, value); |
wolfSSL | 18:d89df40b4cf3 | 536 | if( !strcmp(key, "Content-Length") ) { |
wolfSSL | 18:d89df40b4cf3 | 537 | sscanf(value, "%d", &recvContentLength); |
wolfSSL | 18:d89df40b4cf3 | 538 | pDataIn->setDataLen(recvContentLength); |
wolfSSL | 18:d89df40b4cf3 | 539 | } else if( !strcmp(key, "Transfer-Encoding") ) { |
wolfSSL | 18:d89df40b4cf3 | 540 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) { |
wolfSSL | 18:d89df40b4cf3 | 541 | recvChunked = true; |
wolfSSL | 18:d89df40b4cf3 | 542 | pDataIn->setIsChunked(true); |
wolfSSL | 18:d89df40b4cf3 | 543 | } |
wolfSSL | 18:d89df40b4cf3 | 544 | } else if( !strcmp(key, "Content-Type") ) { |
wolfSSL | 18:d89df40b4cf3 | 545 | pDataIn->setDataType(value); |
wolfSSL | 27:5d4739eae63e | 546 | } else if( !strcmp(key, "location") && redirect_url) { |
ansond | 31:0675a342e45c | 547 | sscanf(buf, "%40[^:]: %128[^\r\n]", key, redirect_url); |
wolfSSL | 27:5d4739eae63e | 548 | DBG("Redirect %s: %s", key, redirect_url) ; |
wolfSSL | 27:5d4739eae63e | 549 | redirect = 1 ; |
wolfSSL | 18:d89df40b4cf3 | 550 | } |
wolfSSL | 18:d89df40b4cf3 | 551 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 552 | trfLen -= (crlfPos + 2); |
wolfSSL | 18:d89df40b4cf3 | 553 | |
wolfSSL | 18:d89df40b4cf3 | 554 | } else { |
wolfSSL | 18:d89df40b4cf3 | 555 | ERR("Could not parse header"); |
donatien | 14:2744e0c0e527 | 556 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 557 | } |
donatien | 0:2ccb9960a044 | 558 | |
donatien | 0:2ccb9960a044 | 559 | } |
donatien | 0:2ccb9960a044 | 560 | |
wolfSSL | 18:d89df40b4cf3 | 561 | //Receive data |
wolfSSL | 18:d89df40b4cf3 | 562 | DBG("Receiving data"); |
wolfSSL | 18:d89df40b4cf3 | 563 | |
wolfSSL | 18:d89df40b4cf3 | 564 | while(true) { |
wolfSSL | 18:d89df40b4cf3 | 565 | size_t readLen = 0; |
donatien | 0:2ccb9960a044 | 566 | |
wolfSSL | 18:d89df40b4cf3 | 567 | if( recvChunked ) { |
wolfSSL | 18:d89df40b4cf3 | 568 | //Read chunk header |
wolfSSL | 18:d89df40b4cf3 | 569 | bool foundCrlf; |
wolfSSL | 18:d89df40b4cf3 | 570 | do { |
wolfSSL | 18:d89df40b4cf3 | 571 | foundCrlf = false; |
wolfSSL | 18:d89df40b4cf3 | 572 | crlfPos=0; |
wolfSSL | 18:d89df40b4cf3 | 573 | buf[trfLen]=0; |
wolfSSL | 18:d89df40b4cf3 | 574 | if(trfLen >= 2) { |
wolfSSL | 18:d89df40b4cf3 | 575 | for(; crlfPos < trfLen - 2; crlfPos++) { |
wolfSSL | 18:d89df40b4cf3 | 576 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) { |
wolfSSL | 18:d89df40b4cf3 | 577 | foundCrlf = true; |
wolfSSL | 18:d89df40b4cf3 | 578 | break; |
wolfSSL | 18:d89df40b4cf3 | 579 | } |
wolfSSL | 18:d89df40b4cf3 | 580 | } |
wolfSSL | 18:d89df40b4cf3 | 581 | } |
wolfSSL | 18:d89df40b4cf3 | 582 | if(!foundCrlf) { //Try to read more |
wolfSSL | 18:d89df40b4cf3 | 583 | if( trfLen < CHUNK_SIZE ) { |
ansond | 30:6fef375c94e6 | 584 | size_t newTrfLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 585 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 586 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 587 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 588 | continue; |
wolfSSL | 18:d89df40b4cf3 | 589 | } else { |
wolfSSL | 18:d89df40b4cf3 | 590 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 591 | } |
wolfSSL | 18:d89df40b4cf3 | 592 | } |
wolfSSL | 18:d89df40b4cf3 | 593 | } while(!foundCrlf); |
wolfSSL | 18:d89df40b4cf3 | 594 | buf[crlfPos] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 595 | int n = sscanf(buf, "%x", &readLen); |
wolfSSL | 18:d89df40b4cf3 | 596 | if(n!=1) { |
wolfSSL | 18:d89df40b4cf3 | 597 | ERR("Could not read chunk length"); |
wolfSSL | 18:d89df40b4cf3 | 598 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 599 | } |
wolfSSL | 18:d89df40b4cf3 | 600 | |
wolfSSL | 18:d89df40b4cf3 | 601 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
wolfSSL | 18:d89df40b4cf3 | 602 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 603 | |
wolfSSL | 18:d89df40b4cf3 | 604 | if( readLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 605 | //Last chunk |
wolfSSL | 18:d89df40b4cf3 | 606 | break; |
wolfSSL | 18:d89df40b4cf3 | 607 | } |
wolfSSL | 18:d89df40b4cf3 | 608 | } else { |
wolfSSL | 18:d89df40b4cf3 | 609 | readLen = recvContentLength; |
wolfSSL | 18:d89df40b4cf3 | 610 | } |
wolfSSL | 18:d89df40b4cf3 | 611 | |
wolfSSL | 18:d89df40b4cf3 | 612 | DBG("Retrieving %d bytes", readLen); |
wolfSSL | 18:d89df40b4cf3 | 613 | |
wolfSSL | 18:d89df40b4cf3 | 614 | do { |
wolfSSL | 18:d89df40b4cf3 | 615 | pDataIn->write(buf, MIN(trfLen, readLen)); |
wolfSSL | 18:d89df40b4cf3 | 616 | if( trfLen > readLen ) { |
wolfSSL | 18:d89df40b4cf3 | 617 | memmove(buf, &buf[readLen], trfLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 618 | trfLen -= readLen; |
wolfSSL | 18:d89df40b4cf3 | 619 | readLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 620 | } else { |
wolfSSL | 18:d89df40b4cf3 | 621 | readLen -= trfLen; |
wolfSSL | 18:d89df40b4cf3 | 622 | } |
donatien | 0:2ccb9960a044 | 623 | |
wolfSSL | 18:d89df40b4cf3 | 624 | if(readLen) { |
wolfSSL | 18:d89df40b4cf3 | 625 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
wolfSSL | 18:d89df40b4cf3 | 626 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 627 | } |
wolfSSL | 18:d89df40b4cf3 | 628 | } while(readLen); |
wolfSSL | 18:d89df40b4cf3 | 629 | |
wolfSSL | 18:d89df40b4cf3 | 630 | if( recvChunked ) { |
wolfSSL | 18:d89df40b4cf3 | 631 | if(trfLen < 2) { |
ansond | 30:6fef375c94e6 | 632 | size_t newTrfLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 633 | //Read missing chars to find end of chunk |
wolfSSL | 18:d89df40b4cf3 | 634 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 635 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 636 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 637 | } |
wolfSSL | 18:d89df40b4cf3 | 638 | if( (buf[0] != '\r') || (buf[1] != '\n') ) { |
wolfSSL | 18:d89df40b4cf3 | 639 | ERR("Format error"); |
wolfSSL | 18:d89df40b4cf3 | 640 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 641 | } |
wolfSSL | 18:d89df40b4cf3 | 642 | memmove(buf, &buf[2], trfLen - 2); |
wolfSSL | 18:d89df40b4cf3 | 643 | trfLen -= 2; |
wolfSSL | 18:d89df40b4cf3 | 644 | } else { |
wolfSSL | 18:d89df40b4cf3 | 645 | break; |
wolfSSL | 18:d89df40b4cf3 | 646 | } |
wolfSSL | 18:d89df40b4cf3 | 647 | |
donatien | 0:2ccb9960a044 | 648 | } |
wolfSSL | 20:bec882d85856 | 649 | cyassl_free() ; |
wolfSSL | 18:d89df40b4cf3 | 650 | m_sock.close(); |
wolfSSL | 18:d89df40b4cf3 | 651 | DBG("Completed HTTP transaction"); |
wolfSSL | 27:5d4739eae63e | 652 | if(redirect)return HTTP_REDIRECT ; |
wolfSSL | 27:5d4739eae63e | 653 | else return HTTP_OK; |
donatien | 0:2ccb9960a044 | 654 | } |
donatien | 0:2ccb9960a044 | 655 | |
wolfSSL | 19:1e2f05809eb1 | 656 | HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure |
donatien | 0:2ccb9960a044 | 657 | { |
wolfSSL | 18:d89df40b4cf3 | 658 | DBG("Trying to read between %d and %d bytes", minLen, maxLen); |
wolfSSL | 18:d89df40b4cf3 | 659 | size_t readLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 660 | |
wolfSSL | 18:d89df40b4cf3 | 661 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 662 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 663 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 18:d89df40b4cf3 | 664 | } |
wolfSSL | 18:d89df40b4cf3 | 665 | |
wolfSSL | 18:d89df40b4cf3 | 666 | int ret; |
wolfSSL | 18:d89df40b4cf3 | 667 | |
wolfSSL | 18:d89df40b4cf3 | 668 | if(port == HTTPS_PORT) { |
wolfSSL | 18:d89df40b4cf3 | 669 | DBG("Enter CyaSSL_read") ; |
wolfSSL | 18:d89df40b4cf3 | 670 | |
wolfSSL | 18:d89df40b4cf3 | 671 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 672 | readLen = CyaSSL_read(ssl, buf, maxLen); |
wolfSSL | 18:d89df40b4cf3 | 673 | if (readLen > 0) { |
wolfSSL | 18:d89df40b4cf3 | 674 | buf[readLen] = 0; |
wolfSSL | 18:d89df40b4cf3 | 675 | DBG("CyaSSL_read:%s\n", buf); |
wolfSSL | 18:d89df40b4cf3 | 676 | } else { |
wolfSSL | 18:d89df40b4cf3 | 677 | ERR("CyaSSL_read, ret = %d", readLen) ; |
wolfSSL | 18:d89df40b4cf3 | 678 | return HTTP_ERROR ; |
wolfSSL | 18:d89df40b4cf3 | 679 | } |
wolfSSL | 18:d89df40b4cf3 | 680 | DBG("Read %d bytes", readLen); |
wolfSSL | 18:d89df40b4cf3 | 681 | *pReadLen = readLen; |
wolfSSL | 18:d89df40b4cf3 | 682 | return HTTP_OK; |
wolfSSL | 18:d89df40b4cf3 | 683 | } |
wolfSSL | 18:d89df40b4cf3 | 684 | |
wolfSSL | 18:d89df40b4cf3 | 685 | while(readLen < maxLen) { |
wolfSSL | 18:d89df40b4cf3 | 686 | if(readLen < minLen) { |
wolfSSL | 18:d89df40b4cf3 | 687 | DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 688 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 689 | ret = m_sock.receive_all(buf + readLen, minLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 690 | } else { |
wolfSSL | 18:d89df40b4cf3 | 691 | DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 692 | m_sock.set_blocking(false, 0); |
wolfSSL | 18:d89df40b4cf3 | 693 | ret = m_sock.receive(buf + readLen, maxLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 694 | } |
wolfSSL | 18:d89df40b4cf3 | 695 | |
wolfSSL | 18:d89df40b4cf3 | 696 | if( ret > 0) { |
wolfSSL | 18:d89df40b4cf3 | 697 | readLen += ret; |
wolfSSL | 18:d89df40b4cf3 | 698 | } else if( ret == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 699 | break; |
wolfSSL | 18:d89df40b4cf3 | 700 | } else { |
wolfSSL | 18:d89df40b4cf3 | 701 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 702 | ERR("Connection error (recv returned %d)", ret); |
wolfSSL | 18:d89df40b4cf3 | 703 | *pReadLen = readLen; |
wolfSSL | 18:d89df40b4cf3 | 704 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 705 | } else { |
wolfSSL | 18:d89df40b4cf3 | 706 | break; |
wolfSSL | 18:d89df40b4cf3 | 707 | } |
wolfSSL | 18:d89df40b4cf3 | 708 | } |
wolfSSL | 18:d89df40b4cf3 | 709 | |
wolfSSL | 18:d89df40b4cf3 | 710 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 711 | break; |
wolfSSL | 18:d89df40b4cf3 | 712 | } |
wolfSSL | 17:c73d8e61d391 | 713 | } |
wolfSSL | 17:c73d8e61d391 | 714 | DBG("Read %d bytes", readLen); |
wolfSSL | 17:c73d8e61d391 | 715 | *pReadLen = readLen; |
wolfSSL | 17:c73d8e61d391 | 716 | return HTTP_OK; |
donatien | 7:4e39864f7b15 | 717 | } |
donatien | 7:4e39864f7b15 | 718 | |
wolfSSL | 19:1e2f05809eb1 | 719 | HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure |
donatien | 7:4e39864f7b15 | 720 | { |
wolfSSL | 18:d89df40b4cf3 | 721 | HTTPResult ret ; |
wolfSSL | 18:d89df40b4cf3 | 722 | int cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 723 | |
wolfSSL | 18:d89df40b4cf3 | 724 | if(len == 0) { |
wolfSSL | 18:d89df40b4cf3 | 725 | len = strlen(buf); |
wolfSSL | 17:c73d8e61d391 | 726 | } |
wolfSSL | 17:c73d8e61d391 | 727 | |
wolfSSL | 18:d89df40b4cf3 | 728 | do { |
wolfSSL | 22:4b9a4151cc73 | 729 | |
wolfSSL | 18:d89df40b4cf3 | 730 | if((SEND_BUF_SIZE - (send_buf_p - send_buf)) >= len) { |
wolfSSL | 18:d89df40b4cf3 | 731 | cp_len = len ; |
wolfSSL | 18:d89df40b4cf3 | 732 | } else { |
wolfSSL | 22:4b9a4151cc73 | 733 | cp_len = SEND_BUF_SIZE - (send_buf_p - send_buf) ; |
wolfSSL | 18:d89df40b4cf3 | 734 | } |
wolfSSL | 22:4b9a4151cc73 | 735 | 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 | 736 | memcpy(send_buf_p, buf, cp_len) ; |
wolfSSL | 18:d89df40b4cf3 | 737 | send_buf_p += cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 738 | len -= cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 739 | |
wolfSSL | 18:d89df40b4cf3 | 740 | if(send_buf_p == send_buf + SEND_BUF_SIZE) { |
wolfSSL | 22:4b9a4151cc73 | 741 | if(port == HTTPS_PORT){ |
wolfSSL | 22:4b9a4151cc73 | 742 | ERR("HTTPClient::send buffer overflow"); |
wolfSSL | 22:4b9a4151cc73 | 743 | return HTTP_ERROR ; |
wolfSSL | 22:4b9a4151cc73 | 744 | } |
wolfSSL | 18:d89df40b4cf3 | 745 | ret = flush() ; |
wolfSSL | 18:d89df40b4cf3 | 746 | if(ret)return(ret) ; |
wolfSSL | 18:d89df40b4cf3 | 747 | } |
wolfSSL | 18:d89df40b4cf3 | 748 | } while(len) ; |
wolfSSL | 18:d89df40b4cf3 | 749 | return HTTP_OK ; |
wolfSSL | 17:c73d8e61d391 | 750 | } |
wolfSSL | 17:c73d8e61d391 | 751 | |
wolfSSL | 19:1e2f05809eb1 | 752 | HTTPResult HTTPClient::flush() //0 on success, err code on failure |
wolfSSL | 17:c73d8e61d391 | 753 | { |
wolfSSL | 18:d89df40b4cf3 | 754 | int len ; |
wolfSSL | 18:d89df40b4cf3 | 755 | char * buf ; |
wolfSSL | 18:d89df40b4cf3 | 756 | |
wolfSSL | 18:d89df40b4cf3 | 757 | buf = send_buf ; |
wolfSSL | 18:d89df40b4cf3 | 758 | len = send_buf_p - send_buf ; |
wolfSSL | 18:d89df40b4cf3 | 759 | send_buf_p = send_buf ; // reset send buffer |
wolfSSL | 18:d89df40b4cf3 | 760 | |
wolfSSL | 18:d89df40b4cf3 | 761 | DBG("Trying to write %d bytes:%s\n", len, buf); |
wolfSSL | 18:d89df40b4cf3 | 762 | size_t writtenLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 763 | |
wolfSSL | 18:d89df40b4cf3 | 764 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 765 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 766 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 17:c73d8e61d391 | 767 | } |
wolfSSL | 18:d89df40b4cf3 | 768 | |
wolfSSL | 18:d89df40b4cf3 | 769 | if(port == HTTPS_PORT) { |
wolfSSL | 18:d89df40b4cf3 | 770 | DBG("Enter CyaSSL_write") ; |
wolfSSL | 18:d89df40b4cf3 | 771 | if (CyaSSL_write(ssl, buf, len) != len) { |
wolfSSL | 18:d89df40b4cf3 | 772 | ERR("SSL_write failed"); |
wolfSSL | 18:d89df40b4cf3 | 773 | return HTTP_ERROR ; |
wolfSSL | 18:d89df40b4cf3 | 774 | } |
wolfSSL | 18:d89df40b4cf3 | 775 | DBG("Written %d bytes", writtenLen); |
wolfSSL | 18:d89df40b4cf3 | 776 | return HTTP_OK; |
wolfSSL | 18:d89df40b4cf3 | 777 | } |
wolfSSL | 18:d89df40b4cf3 | 778 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 779 | int ret = m_sock.send_all(buf, len); |
wolfSSL | 18:d89df40b4cf3 | 780 | if(ret > 0) { |
wolfSSL | 18:d89df40b4cf3 | 781 | writtenLen += ret; |
wolfSSL | 18:d89df40b4cf3 | 782 | } else if( ret == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 783 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 784 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 18:d89df40b4cf3 | 785 | } else { |
wolfSSL | 18:d89df40b4cf3 | 786 | ERR("Connection error (send returned %d)", ret); |
wolfSSL | 18:d89df40b4cf3 | 787 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 788 | } |
wolfSSL | 18:d89df40b4cf3 | 789 | |
wolfSSL | 17:c73d8e61d391 | 790 | DBG("Written %d bytes", writtenLen); |
wolfSSL | 17:c73d8e61d391 | 791 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 792 | } |
donatien | 0:2ccb9960a044 | 793 | |
wolfSSL | 19:1e2f05809eb1 | 794 | 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 | 795 | { |
wolfSSL | 18:d89df40b4cf3 | 796 | char* schemePtr = (char*) url; |
wolfSSL | 18:d89df40b4cf3 | 797 | char* hostPtr = (char*) strstr(url, "://"); |
wolfSSL | 18:d89df40b4cf3 | 798 | if(hostPtr == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 799 | WARN("Could not find host"); |
wolfSSL | 18:d89df40b4cf3 | 800 | return HTTP_PARSE; //URL is invalid |
wolfSSL | 18:d89df40b4cf3 | 801 | } |
wolfSSL | 18:d89df40b4cf3 | 802 | |
wolfSSL | 18:d89df40b4cf3 | 803 | if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 804 | WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); |
wolfSSL | 18:d89df40b4cf3 | 805 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 806 | } |
wolfSSL | 18:d89df40b4cf3 | 807 | memcpy(scheme, schemePtr, hostPtr - schemePtr); |
wolfSSL | 18:d89df40b4cf3 | 808 | scheme[hostPtr - schemePtr] = '\0'; |
donatien | 0:2ccb9960a044 | 809 | |
wolfSSL | 18:d89df40b4cf3 | 810 | hostPtr+=3; |
donatien | 0:2ccb9960a044 | 811 | |
wolfSSL | 18:d89df40b4cf3 | 812 | size_t hostLen = 0; |
donatien | 0:2ccb9960a044 | 813 | |
wolfSSL | 18:d89df40b4cf3 | 814 | char* portPtr = strchr(hostPtr, ':'); |
wolfSSL | 18:d89df40b4cf3 | 815 | if( portPtr != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 816 | hostLen = portPtr - hostPtr; |
wolfSSL | 18:d89df40b4cf3 | 817 | portPtr++; |
wolfSSL | 18:d89df40b4cf3 | 818 | if( sscanf(portPtr, "%hu", port) != 1) { |
wolfSSL | 18:d89df40b4cf3 | 819 | WARN("Could not find port"); |
wolfSSL | 18:d89df40b4cf3 | 820 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 821 | } |
wolfSSL | 18:d89df40b4cf3 | 822 | } else { |
wolfSSL | 18:d89df40b4cf3 | 823 | *port=0; |
donatien | 0:2ccb9960a044 | 824 | } |
wolfSSL | 18:d89df40b4cf3 | 825 | char* pathPtr = strchr(hostPtr, '/'); |
wolfSSL | 18:d89df40b4cf3 | 826 | if( hostLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 827 | hostLen = pathPtr - hostPtr; |
wolfSSL | 18:d89df40b4cf3 | 828 | } |
donatien | 0:2ccb9960a044 | 829 | |
wolfSSL | 18:d89df40b4cf3 | 830 | if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 831 | WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); |
wolfSSL | 18:d89df40b4cf3 | 832 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 833 | } |
wolfSSL | 18:d89df40b4cf3 | 834 | memcpy(host, hostPtr, hostLen); |
wolfSSL | 18:d89df40b4cf3 | 835 | host[hostLen] = '\0'; |
donatien | 0:2ccb9960a044 | 836 | |
wolfSSL | 18:d89df40b4cf3 | 837 | size_t pathLen; |
wolfSSL | 18:d89df40b4cf3 | 838 | char* fragmentPtr = strchr(hostPtr, '#'); |
wolfSSL | 18:d89df40b4cf3 | 839 | if(fragmentPtr != NULL) { |
wolfSSL | 18:d89df40b4cf3 | 840 | pathLen = fragmentPtr - pathPtr; |
wolfSSL | 18:d89df40b4cf3 | 841 | } else { |
wolfSSL | 18:d89df40b4cf3 | 842 | pathLen = strlen(pathPtr); |
wolfSSL | 18:d89df40b4cf3 | 843 | } |
donatien | 0:2ccb9960a044 | 844 | |
wolfSSL | 18:d89df40b4cf3 | 845 | if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 846 | WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); |
wolfSSL | 18:d89df40b4cf3 | 847 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 848 | } |
wolfSSL | 18:d89df40b4cf3 | 849 | memcpy(path, pathPtr, pathLen); |
wolfSSL | 18:d89df40b4cf3 | 850 | path[pathLen] = '\0'; |
donatien | 0:2ccb9960a044 | 851 | |
wolfSSL | 18:d89df40b4cf3 | 852 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 853 | } |
wolfSSL | 22:4b9a4151cc73 | 854 | |
ansond | 36:debaeb6006a7 | 855 | HTTPResult HTTPClient::tokenAuth(void) |
ansond | 36:debaeb6006a7 | 856 | { |
ansond | 36:debaeb6006a7 | 857 | HTTPResult ret ; |
ansond | 36:debaeb6006a7 | 858 | ret = send("Authorization: Bearer ") ; |
ansond | 36:debaeb6006a7 | 859 | CHECK_CONN_ERR(ret); |
ansond | 36:debaeb6006a7 | 860 | DBG("oauthToken: %s", m_oauthToken) ; |
ansond | 36:debaeb6006a7 | 861 | ret = send((char *)m_oauthToken) ; |
ansond | 36:debaeb6006a7 | 862 | CHECK_CONN_ERR(ret); |
ansond | 36:debaeb6006a7 | 863 | return HTTP_OK ; |
ansond | 36:debaeb6006a7 | 864 | } |
ansond | 36:debaeb6006a7 | 865 | |
wolfSSL | 22:4b9a4151cc73 | 866 | HTTPResult HTTPClient::bAuth(void) |
wolfSSL | 22:4b9a4151cc73 | 867 | { |
wolfSSL | 22:4b9a4151cc73 | 868 | HTTPResult ret ; |
wolfSSL | 22:4b9a4151cc73 | 869 | char b_auth[(int)((AUTHB_SIZE+3)*4/3+1)] ; |
wolfSSL | 22:4b9a4151cc73 | 870 | char base64buff[AUTHB_SIZE+3] ; |
wolfSSL | 22:4b9a4151cc73 | 871 | |
wolfSSL | 22:4b9a4151cc73 | 872 | ret = send("Authorization: Basic ") ; |
wolfSSL | 22:4b9a4151cc73 | 873 | CHECK_CONN_ERR(ret); |
wolfSSL | 22:4b9a4151cc73 | 874 | sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ; |
wolfSSL | 27:5d4739eae63e | 875 | DBG("bAuth: %s", base64buff) ; |
wolfSSL | 22:4b9a4151cc73 | 876 | base64enc(b_auth, base64buff) ; |
wolfSSL | 22:4b9a4151cc73 | 877 | b_auth[strlen(b_auth)+1] = '\0' ; |
wolfSSL | 22:4b9a4151cc73 | 878 | b_auth[strlen(b_auth)] = '\n' ; |
wolfSSL | 22:4b9a4151cc73 | 879 | DBG("b_auth:%s", b_auth) ; |
wolfSSL | 22:4b9a4151cc73 | 880 | ret = send(b_auth) ; |
wolfSSL | 22:4b9a4151cc73 | 881 | CHECK_CONN_ERR(ret); |
wolfSSL | 22:4b9a4151cc73 | 882 | return HTTP_OK ; |
wolfSSL | 22:4b9a4151cc73 | 883 | } |