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