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