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