Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
HTTPClient.cpp@43:12a6ae712bcd, 2017-07-20 (annotated)
- Committer:
- WiredHome
- Date:
- Thu Jul 20 10:56:03 2017 +0000
- Revision:
- 43:12a6ae712bcd
- Parent:
- 42:ab259a9d1d36
- Child:
- 44:0e3b0849b2c7
Small memory reduction in ::FormatCTime(),; Minor tweaks to debug information.
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 | |
WiredHome | 42:ab259a9d1d36 | 20 | //#include "Utility.h" // private memory manager |
WiredHome | 36:a5c13e512b78 | 21 | #ifndef UTILITY_H |
WiredHome | 36:a5c13e512b78 | 22 | #define swMalloc malloc // use the standard |
WiredHome | 36:a5c13e512b78 | 23 | #define swFree free |
WiredHome | 36:a5c13e512b78 | 24 | #endif |
WiredHome | 36:a5c13e512b78 | 25 | |
donatien | 7:4e39864f7b15 | 26 | //Debug is disabled by default |
WiredHome | 34:3556275bebf3 | 27 | //#define DEBUG "HTCL" |
donatien | 11:390362de8c3f | 28 | #include <cstdio> |
WiredHome | 20:4ea5255c1b04 | 29 | #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) |
WiredHome | 35:d9e2d1c96b75 | 30 | #define DBG(x, ...) std::printf("[DBG %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 35:d9e2d1c96b75 | 31 | #define WARN(x, ...) std::printf("[WRN %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 35:d9e2d1c96b75 | 32 | #define ERR(x, ...) std::printf("[ERR %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 35:d9e2d1c96b75 | 33 | #define INFO(x, ...) std::printf("[INF %s %3d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
donatien | 12:89d09a6db00a | 34 | #else |
WiredHome | 20:4ea5255c1b04 | 35 | #define DBG(x, ...) |
donatien | 12:89d09a6db00a | 36 | #define WARN(x, ...) |
WiredHome | 20:4ea5255c1b04 | 37 | #define ERR(x, ...) |
WiredHome | 20:4ea5255c1b04 | 38 | #define INFO(x, ...) |
donatien | 7:4e39864f7b15 | 39 | #endif |
donatien | 0:2ccb9960a044 | 40 | |
WiredHome | 20:4ea5255c1b04 | 41 | //#define HTTP_PORT 80 |
donatien | 0:2ccb9960a044 | 42 | |
donatien | 11:390362de8c3f | 43 | #define OK 0 |
donatien | 11:390362de8c3f | 44 | |
donatien | 11:390362de8c3f | 45 | #define MIN(x,y) (((x)<(y))?(x):(y)) |
donatien | 11:390362de8c3f | 46 | #define MAX(x,y) (((x)>(y))?(x):(y)) |
donatien | 11:390362de8c3f | 47 | |
donatien | 0:2ccb9960a044 | 48 | #define CHUNK_SIZE 256 |
WiredHome | 31:96da7c08b5d0 | 49 | #define MAXLEN_VALUE 120 /* Max URL and Max Value for Name:Value */ |
donatien | 0:2ccb9960a044 | 50 | |
donatien | 0:2ccb9960a044 | 51 | #include <cstring> |
donatien | 0:2ccb9960a044 | 52 | |
donatien | 11:390362de8c3f | 53 | #include "HTTPClient.h" |
donatien | 11:390362de8c3f | 54 | |
donatien | 0:2ccb9960a044 | 55 | HTTPClient::HTTPClient() : |
WiredHome | 25:76084defa790 | 56 | m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), |
WiredHome | 25:76084defa790 | 57 | m_nCustomHeaders(0), m_httpResponseCode(0), |
WiredHome | 24:eee214e3e806 | 58 | m_maxredirections(1), m_location(NULL) |
donatien | 0:2ccb9960a044 | 59 | { |
donatien | 0:2ccb9960a044 | 60 | |
donatien | 0:2ccb9960a044 | 61 | } |
donatien | 0:2ccb9960a044 | 62 | |
donatien | 0:2ccb9960a044 | 63 | HTTPClient::~HTTPClient() |
donatien | 0:2ccb9960a044 | 64 | { |
WiredHome | 41:37e7df011c1f | 65 | if (m_basicAuthUser) |
WiredHome | 41:37e7df011c1f | 66 | swFree(m_basicAuthUser); |
WiredHome | 41:37e7df011c1f | 67 | if (m_basicAuthPassword) |
WiredHome | 41:37e7df011c1f | 68 | swFree(m_basicAuthPassword); |
WiredHome | 36:a5c13e512b78 | 69 | if (m_location) // if any redirection was involved, clean up after it. |
WiredHome | 36:a5c13e512b78 | 70 | swFree(m_location); |
WiredHome | 36:a5c13e512b78 | 71 | m_location = NULL; // this step isn't necessary... |
donatien | 0:2ccb9960a044 | 72 | } |
donatien | 0:2ccb9960a044 | 73 | |
WiredHome | 38:2ef07232f65c | 74 | |
WiredHome | 38:2ef07232f65c | 75 | const char * HTTPClient::GetErrorMessage(HTTPResult res) |
WiredHome | 38:2ef07232f65c | 76 | { |
WiredHome | 38:2ef07232f65c | 77 | const char * msg[HTTP_CLOSED+1] = { |
WiredHome | 38:2ef07232f65c | 78 | "HTTP OK", ///<Success |
WiredHome | 38:2ef07232f65c | 79 | "HTTP Processing", ///<Processing |
WiredHome | 38:2ef07232f65c | 80 | "HTTP URL Parse error", ///<url Parse error |
WiredHome | 38:2ef07232f65c | 81 | "HTTP DNS error", ///<Could not resolve name |
WiredHome | 38:2ef07232f65c | 82 | "HTTP Protocol error", ///<Protocol error |
WiredHome | 38:2ef07232f65c | 83 | "HTTP 404 Not Found", ///<HTTP 404 Error |
WiredHome | 38:2ef07232f65c | 84 | "HTTP 403 Refused", ///<HTTP 403 Error |
WiredHome | 38:2ef07232f65c | 85 | "HTTP ### Error", ///<HTTP xxx error |
WiredHome | 38:2ef07232f65c | 86 | "HTTP Timeout", ///<Connection timeout |
WiredHome | 38:2ef07232f65c | 87 | "HTTP Connection error", ///<Connection error |
WiredHome | 38:2ef07232f65c | 88 | "HTTP Closed by remote host" ///<Connection was closed by remote host |
WiredHome | 38:2ef07232f65c | 89 | }; |
WiredHome | 38:2ef07232f65c | 90 | if (res <= HTTP_CLOSED) |
WiredHome | 38:2ef07232f65c | 91 | return msg[res]; |
WiredHome | 38:2ef07232f65c | 92 | else |
WiredHome | 38:2ef07232f65c | 93 | return "HTTP Unknown Code"; |
WiredHome | 38:2ef07232f65c | 94 | }; |
WiredHome | 38:2ef07232f65c | 95 | |
WiredHome | 38:2ef07232f65c | 96 | |
donatien | 0:2ccb9960a044 | 97 | void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification |
donatien | 0:2ccb9960a044 | 98 | { |
WiredHome | 24:eee214e3e806 | 99 | #if 1 |
WiredHome | 21:d33f7e1ce811 | 100 | if (m_basicAuthUser) |
WiredHome | 36:a5c13e512b78 | 101 | swFree(m_basicAuthUser); |
WiredHome | 36:a5c13e512b78 | 102 | m_basicAuthUser = (char *)swMalloc(strlen(user)+1); |
WiredHome | 21:d33f7e1ce811 | 103 | strcpy(m_basicAuthUser, user); |
WiredHome | 21:d33f7e1ce811 | 104 | if (m_basicAuthPassword) |
WiredHome | 36:a5c13e512b78 | 105 | swFree(m_basicAuthPassword); |
WiredHome | 36:a5c13e512b78 | 106 | m_basicAuthPassword = (char *)swMalloc(strlen(password)+1); |
WiredHome | 24:eee214e3e806 | 107 | strcpy(m_basicAuthPassword, password); |
WiredHome | 24:eee214e3e806 | 108 | #else |
WiredHome | 20:4ea5255c1b04 | 109 | m_basicAuthUser = user; |
WiredHome | 20:4ea5255c1b04 | 110 | m_basicAuthPassword = password; |
WiredHome | 24:eee214e3e806 | 111 | #endif |
donatien | 0:2ccb9960a044 | 112 | } |
vwochnik | 19:bcbf0af9fac3 | 113 | |
vwochnik | 19:bcbf0af9fac3 | 114 | void HTTPClient::customHeaders(const char **headers, size_t pairs) |
vwochnik | 19:bcbf0af9fac3 | 115 | { |
vwochnik | 19:bcbf0af9fac3 | 116 | m_customHeaders = headers; |
vwochnik | 19:bcbf0af9fac3 | 117 | m_nCustomHeaders = pairs; |
vwochnik | 19:bcbf0af9fac3 | 118 | } |
vwochnik | 19:bcbf0af9fac3 | 119 | |
donatien | 0:2ccb9960a044 | 120 | |
donatien | 12:89d09a6db00a | 121 | HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 122 | { |
WiredHome | 33:d4d1475bafc0 | 123 | INFO("url: %s, timeout: %d", url, timeout); |
WiredHome | 20:4ea5255c1b04 | 124 | return connect(url, HTTP_GET, NULL, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 125 | } |
donatien | 0:2ccb9960a044 | 126 | |
donatien | 12:89d09a6db00a | 127 | HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 128 | { |
WiredHome | 43:12a6ae712bcd | 129 | INFO("url: %s, maxResultLen %d, timeout: %d", url, maxResultLen, timeout); |
WiredHome | 20:4ea5255c1b04 | 130 | HTTPText str(result, maxResultLen); |
WiredHome | 20:4ea5255c1b04 | 131 | return get(url, &str, timeout); |
donatien | 0:2ccb9960a044 | 132 | } |
donatien | 0:2ccb9960a044 | 133 | |
donatien | 12:89d09a6db00a | 134 | HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 135 | { |
WiredHome | 20:4ea5255c1b04 | 136 | return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 137 | } |
donatien | 0:2ccb9960a044 | 138 | |
donatien | 16:1f743885e7de | 139 | HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 140 | { |
WiredHome | 20:4ea5255c1b04 | 141 | return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 16:1f743885e7de | 142 | } |
donatien | 16:1f743885e7de | 143 | |
donatien | 16:1f743885e7de | 144 | HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 145 | { |
WiredHome | 20:4ea5255c1b04 | 146 | return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); |
donatien | 16:1f743885e7de | 147 | } |
donatien | 16:1f743885e7de | 148 | |
donatien | 16:1f743885e7de | 149 | |
donatien | 0:2ccb9960a044 | 150 | int HTTPClient::getHTTPResponseCode() |
donatien | 0:2ccb9960a044 | 151 | { |
WiredHome | 20:4ea5255c1b04 | 152 | return m_httpResponseCode; |
donatien | 0:2ccb9960a044 | 153 | } |
donatien | 0:2ccb9960a044 | 154 | |
WiredHome | 24:eee214e3e806 | 155 | void HTTPClient::setMaxRedirections(int i) |
WiredHome | 24:eee214e3e806 | 156 | { |
WiredHome | 24:eee214e3e806 | 157 | if (i < 1) |
WiredHome | 24:eee214e3e806 | 158 | i = 1; |
WiredHome | 24:eee214e3e806 | 159 | m_maxredirections = i; |
WiredHome | 24:eee214e3e806 | 160 | } |
WiredHome | 24:eee214e3e806 | 161 | |
donatien | 5:791fc3dcb6c4 | 162 | #define CHECK_CONN_ERR(ret) \ |
donatien | 5:791fc3dcb6c4 | 163 | do{ \ |
donatien | 7:4e39864f7b15 | 164 | if(ret) { \ |
donatien | 7:4e39864f7b15 | 165 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 166 | ERR("Connection error (%d)", ret); \ |
donatien | 11:390362de8c3f | 167 | return HTTP_CONN; \ |
donatien | 5:791fc3dcb6c4 | 168 | } \ |
donatien | 5:791fc3dcb6c4 | 169 | } while(0) |
donatien | 5:791fc3dcb6c4 | 170 | |
donatien | 5:791fc3dcb6c4 | 171 | #define PRTCL_ERR() \ |
donatien | 5:791fc3dcb6c4 | 172 | do{ \ |
donatien | 7:4e39864f7b15 | 173 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 174 | ERR("Protocol error"); \ |
donatien | 11:390362de8c3f | 175 | return HTTP_PRTCL; \ |
donatien | 5:791fc3dcb6c4 | 176 | } while(0) |
donatien | 0:2ccb9960a044 | 177 | |
donatien | 12:89d09a6db00a | 178 | HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request |
WiredHome | 20:4ea5255c1b04 | 179 | { |
WiredHome | 20:4ea5255c1b04 | 180 | m_httpResponseCode = 0; //Invalidate code |
WiredHome | 20:4ea5255c1b04 | 181 | m_timeout = timeout; |
WiredHome | 20:4ea5255c1b04 | 182 | |
WiredHome | 33:d4d1475bafc0 | 183 | INFO("connect(%s,%d,...,%d)", url, method, timeout); |
WiredHome | 20:4ea5255c1b04 | 184 | pDataIn->writeReset(); |
WiredHome | 20:4ea5255c1b04 | 185 | if( pDataOut ) { |
WiredHome | 20:4ea5255c1b04 | 186 | pDataOut->readReset(); |
WiredHome | 20:4ea5255c1b04 | 187 | } |
donatien | 0:2ccb9960a044 | 188 | |
WiredHome | 20:4ea5255c1b04 | 189 | char scheme[8]; |
WiredHome | 20:4ea5255c1b04 | 190 | uint16_t port; |
WiredHome | 20:4ea5255c1b04 | 191 | char host[32]; |
WiredHome | 31:96da7c08b5d0 | 192 | char path[MAXLEN_VALUE]; |
WiredHome | 24:eee214e3e806 | 193 | size_t recvContentLength = 0; |
WiredHome | 24:eee214e3e806 | 194 | bool recvChunked = false; |
WiredHome | 35:d9e2d1c96b75 | 195 | size_t crlfPos = 0; |
WiredHome | 24:eee214e3e806 | 196 | char buf[CHUNK_SIZE]; |
WiredHome | 24:eee214e3e806 | 197 | size_t trfLen; |
WiredHome | 24:eee214e3e806 | 198 | int ret = 0; |
WiredHome | 24:eee214e3e806 | 199 | |
WiredHome | 24:eee214e3e806 | 200 | int maxRedirect = m_maxredirections; |
donatien | 0:2ccb9960a044 | 201 | |
WiredHome | 24:eee214e3e806 | 202 | while (maxRedirect--) { |
WiredHome | 24:eee214e3e806 | 203 | bool takeRedirect = false; |
WiredHome | 24:eee214e3e806 | 204 | |
WiredHome | 24:eee214e3e806 | 205 | INFO("parse: [%s]", url); |
WiredHome | 24:eee214e3e806 | 206 | //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) |
WiredHome | 24:eee214e3e806 | 207 | HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); |
WiredHome | 24:eee214e3e806 | 208 | if(res != HTTP_OK) { |
WiredHome | 24:eee214e3e806 | 209 | ERR("parseURL returned %d", res); |
WiredHome | 24:eee214e3e806 | 210 | return res; |
WiredHome | 24:eee214e3e806 | 211 | } |
donatien | 0:2ccb9960a044 | 212 | |
WiredHome | 24:eee214e3e806 | 213 | if(port == 0) { //TODO do handle HTTPS->443 |
WiredHome | 24:eee214e3e806 | 214 | port = 80; |
WiredHome | 24:eee214e3e806 | 215 | } |
WiredHome | 20:4ea5255c1b04 | 216 | |
WiredHome | 24:eee214e3e806 | 217 | DBG("Scheme: %s", scheme); |
WiredHome | 24:eee214e3e806 | 218 | DBG("Host: %s", host); |
WiredHome | 24:eee214e3e806 | 219 | DBG("Port: %d", port); |
WiredHome | 24:eee214e3e806 | 220 | DBG("Path: %s", path); |
WiredHome | 24:eee214e3e806 | 221 | |
WiredHome | 24:eee214e3e806 | 222 | //Connect |
WiredHome | 24:eee214e3e806 | 223 | DBG("Connecting socket to server"); |
WiredHome | 24:eee214e3e806 | 224 | ret = m_sock.connect(host, port); |
WiredHome | 24:eee214e3e806 | 225 | if (ret < 0) { |
WiredHome | 20:4ea5255c1b04 | 226 | m_sock.close(); |
WiredHome | 24:eee214e3e806 | 227 | ERR("Could not connect"); |
WiredHome | 20:4ea5255c1b04 | 228 | return HTTP_CONN; |
WiredHome | 20:4ea5255c1b04 | 229 | } |
WiredHome | 20:4ea5255c1b04 | 230 | |
WiredHome | 36:a5c13e512b78 | 231 | // Send request |
WiredHome | 24:eee214e3e806 | 232 | DBG("Sending request"); |
WiredHome | 24:eee214e3e806 | 233 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
WiredHome | 36:a5c13e512b78 | 234 | snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s:%d\r\n", meth, path, host, port); //Write request |
WiredHome | 31:96da7c08b5d0 | 235 | INFO(" buf{%s}", buf); |
WiredHome | 20:4ea5255c1b04 | 236 | ret = send(buf); |
WiredHome | 20:4ea5255c1b04 | 237 | if (ret) { |
WiredHome | 20:4ea5255c1b04 | 238 | m_sock.close(); |
WiredHome | 20:4ea5255c1b04 | 239 | ERR("Could not write request"); |
WiredHome | 20:4ea5255c1b04 | 240 | return HTTP_CONN; |
WiredHome | 20:4ea5255c1b04 | 241 | } |
donatien | 0:2ccb9960a044 | 242 | |
WiredHome | 24:eee214e3e806 | 243 | // send authorization |
WiredHome | 25:76084defa790 | 244 | INFO("send auth (if defined)"); |
WiredHome | 24:eee214e3e806 | 245 | if (m_basicAuthUser && m_basicAuthPassword) { |
WiredHome | 24:eee214e3e806 | 246 | strcpy(buf, "Authorization: Basic "); |
WiredHome | 24:eee214e3e806 | 247 | createauth(m_basicAuthUser, m_basicAuthPassword, buf+strlen(buf), sizeof(buf)-strlen(buf)); |
WiredHome | 24:eee214e3e806 | 248 | strcat(buf, "\r\n"); |
WiredHome | 24:eee214e3e806 | 249 | INFO(" (%s,%s) => (%s)", m_basicAuthUser, m_basicAuthPassword, buf); |
WiredHome | 20:4ea5255c1b04 | 250 | ret = send(buf); |
WiredHome | 24:eee214e3e806 | 251 | INFO(" ret = %d", ret); |
WiredHome | 24:eee214e3e806 | 252 | if(ret) { |
WiredHome | 24:eee214e3e806 | 253 | m_sock.close(); |
WiredHome | 24:eee214e3e806 | 254 | ERR("Could not write request"); |
WiredHome | 24:eee214e3e806 | 255 | return HTTP_CONN; |
WiredHome | 24:eee214e3e806 | 256 | } |
WiredHome | 20:4ea5255c1b04 | 257 | } |
WiredHome | 20:4ea5255c1b04 | 258 | |
WiredHome | 36:a5c13e512b78 | 259 | // Send all headers |
WiredHome | 25:76084defa790 | 260 | INFO("Send custom header(s) %d (if any)", m_nCustomHeaders); |
WiredHome | 24:eee214e3e806 | 261 | for (size_t nh = 0; nh < m_nCustomHeaders * 2; nh+=2) { |
WiredHome | 36:a5c13e512b78 | 262 | INFO("hdr[%2d] %s: %s", nh, m_customHeaders[nh], m_customHeaders[nh+1]); |
WiredHome | 24:eee214e3e806 | 263 | snprintf(buf, sizeof(buf), "%s: %s\r\n", m_customHeaders[nh], m_customHeaders[nh+1]); |
WiredHome | 24:eee214e3e806 | 264 | ret = send(buf); |
WiredHome | 24:eee214e3e806 | 265 | if (ret) { |
WiredHome | 24:eee214e3e806 | 266 | ERR("closing"); |
WiredHome | 24:eee214e3e806 | 267 | wait_ms(50); |
WiredHome | 24:eee214e3e806 | 268 | m_sock.close(); |
WiredHome | 24:eee214e3e806 | 269 | ERR("Could not write request"); |
WiredHome | 24:eee214e3e806 | 270 | return HTTP_CONN; |
WiredHome | 24:eee214e3e806 | 271 | } |
WiredHome | 36:a5c13e512b78 | 272 | INFO(" send() returned %d", ret); |
WiredHome | 24:eee214e3e806 | 273 | } |
donatien | 0:2ccb9960a044 | 274 | |
WiredHome | 24:eee214e3e806 | 275 | //Send default headers |
WiredHome | 24:eee214e3e806 | 276 | DBG("Sending headers"); |
WiredHome | 24:eee214e3e806 | 277 | if( pDataOut != NULL ) { |
WiredHome | 20:4ea5255c1b04 | 278 | if( pDataOut->getIsChunked() ) { |
WiredHome | 24:eee214e3e806 | 279 | ret = send("Transfer-Encoding: chunked\r\n"); |
WiredHome | 20:4ea5255c1b04 | 280 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 281 | } else { |
WiredHome | 24:eee214e3e806 | 282 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
WiredHome | 24:eee214e3e806 | 283 | ret = send(buf); |
WiredHome | 20:4ea5255c1b04 | 284 | CHECK_CONN_ERR(ret); |
WiredHome | 20:4ea5255c1b04 | 285 | } |
WiredHome | 24:eee214e3e806 | 286 | char type[48]; |
WiredHome | 24:eee214e3e806 | 287 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) { |
WiredHome | 24:eee214e3e806 | 288 | snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
WiredHome | 24:eee214e3e806 | 289 | ret = send(buf); |
WiredHome | 20:4ea5255c1b04 | 290 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 291 | } |
WiredHome | 24:eee214e3e806 | 292 | } |
WiredHome | 24:eee214e3e806 | 293 | |
WiredHome | 24:eee214e3e806 | 294 | //Close headers |
WiredHome | 24:eee214e3e806 | 295 | DBG("Headers sent"); |
WiredHome | 24:eee214e3e806 | 296 | ret = send("\r\n"); |
WiredHome | 24:eee214e3e806 | 297 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 298 | |
WiredHome | 24:eee214e3e806 | 299 | //Send data (if available) |
WiredHome | 24:eee214e3e806 | 300 | if( pDataOut != NULL ) { |
WiredHome | 37:74c1c4527f70 | 301 | DBG("Sending data [%s]", buf); |
WiredHome | 24:eee214e3e806 | 302 | while(true) { |
WiredHome | 24:eee214e3e806 | 303 | size_t writtenLen = 0; |
WiredHome | 24:eee214e3e806 | 304 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
WiredHome | 37:74c1c4527f70 | 305 | DBG(" trfLen: %d", trfLen); |
WiredHome | 24:eee214e3e806 | 306 | if( pDataOut->getIsChunked() ) { |
WiredHome | 24:eee214e3e806 | 307 | //Write chunk header |
WiredHome | 24:eee214e3e806 | 308 | char chunkHeader[16]; |
WiredHome | 24:eee214e3e806 | 309 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
WiredHome | 24:eee214e3e806 | 310 | ret = send(chunkHeader); |
WiredHome | 24:eee214e3e806 | 311 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 312 | } else if( trfLen == 0 ) { |
WiredHome | 24:eee214e3e806 | 313 | break; |
WiredHome | 24:eee214e3e806 | 314 | } |
WiredHome | 24:eee214e3e806 | 315 | if( trfLen != 0 ) { |
WiredHome | 24:eee214e3e806 | 316 | ret = send(buf, trfLen); |
WiredHome | 24:eee214e3e806 | 317 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 318 | } |
WiredHome | 24:eee214e3e806 | 319 | |
WiredHome | 24:eee214e3e806 | 320 | if( pDataOut->getIsChunked() ) { |
WiredHome | 24:eee214e3e806 | 321 | ret = send("\r\n"); //Chunk-terminating CRLF |
WiredHome | 24:eee214e3e806 | 322 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 323 | } else { |
WiredHome | 24:eee214e3e806 | 324 | writtenLen += trfLen; |
WiredHome | 24:eee214e3e806 | 325 | if( writtenLen >= pDataOut->getDataLen() ) { |
WiredHome | 24:eee214e3e806 | 326 | break; |
WiredHome | 24:eee214e3e806 | 327 | } |
WiredHome | 24:eee214e3e806 | 328 | } |
WiredHome | 24:eee214e3e806 | 329 | |
WiredHome | 24:eee214e3e806 | 330 | if( trfLen == 0 ) { |
WiredHome | 20:4ea5255c1b04 | 331 | break; |
WiredHome | 20:4ea5255c1b04 | 332 | } |
WiredHome | 20:4ea5255c1b04 | 333 | } |
WiredHome | 20:4ea5255c1b04 | 334 | |
donatien | 0:2ccb9960a044 | 335 | } |
WiredHome | 20:4ea5255c1b04 | 336 | |
WiredHome | 24:eee214e3e806 | 337 | //Receive response |
WiredHome | 24:eee214e3e806 | 338 | DBG("Receiving response"); |
WiredHome | 25:76084defa790 | 339 | //ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
WiredHome | 25:76084defa790 | 340 | ret = recv(buf, 1, CHUNK_SIZE - 1, &trfLen); // recommended by Rob Noble to avoid timeout wait |
WiredHome | 24:eee214e3e806 | 341 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 342 | buf[trfLen] = '\0'; |
WiredHome | 24:eee214e3e806 | 343 | INFO("Received \r\n(%s\r\n)", buf); |
donatien | 0:2ccb9960a044 | 344 | |
WiredHome | 24:eee214e3e806 | 345 | char* crlfPtr = strstr(buf, "\r\n"); |
WiredHome | 24:eee214e3e806 | 346 | if( crlfPtr == NULL) { |
WiredHome | 24:eee214e3e806 | 347 | PRTCL_ERR(); |
WiredHome | 20:4ea5255c1b04 | 348 | } |
donatien | 0:2ccb9960a044 | 349 | |
WiredHome | 20:4ea5255c1b04 | 350 | crlfPos = crlfPtr - buf; |
WiredHome | 20:4ea5255c1b04 | 351 | buf[crlfPos] = '\0'; |
WiredHome | 20:4ea5255c1b04 | 352 | |
WiredHome | 24:eee214e3e806 | 353 | //Parse HTTP response |
WiredHome | 24:eee214e3e806 | 354 | if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) { |
WiredHome | 24:eee214e3e806 | 355 | //Cannot match string, error |
WiredHome | 26:58b6fe9f596b | 356 | ERR("Not a correct HTTP answer : {%s}\n", buf); |
WiredHome | 24:eee214e3e806 | 357 | PRTCL_ERR(); |
WiredHome | 24:eee214e3e806 | 358 | } |
donatien | 0:2ccb9960a044 | 359 | |
WiredHome | 24:eee214e3e806 | 360 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) { |
WiredHome | 24:eee214e3e806 | 361 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
WiredHome | 24:eee214e3e806 | 362 | WARN("Response code %d", m_httpResponseCode); |
WiredHome | 20:4ea5255c1b04 | 363 | PRTCL_ERR(); |
WiredHome | 20:4ea5255c1b04 | 364 | } |
WiredHome | 20:4ea5255c1b04 | 365 | |
WiredHome | 24:eee214e3e806 | 366 | DBG("Reading headers"); |
WiredHome | 24:eee214e3e806 | 367 | |
WiredHome | 24:eee214e3e806 | 368 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
WiredHome | 24:eee214e3e806 | 369 | trfLen -= (crlfPos + 2); |
WiredHome | 24:eee214e3e806 | 370 | |
WiredHome | 24:eee214e3e806 | 371 | recvContentLength = 0; |
WiredHome | 24:eee214e3e806 | 372 | recvChunked = false; |
WiredHome | 24:eee214e3e806 | 373 | //Now get headers |
WiredHome | 24:eee214e3e806 | 374 | while( true ) { |
WiredHome | 24:eee214e3e806 | 375 | crlfPtr = strstr(buf, "\r\n"); |
WiredHome | 24:eee214e3e806 | 376 | if(crlfPtr == NULL) { |
WiredHome | 24:eee214e3e806 | 377 | if( trfLen < CHUNK_SIZE - 1 ) { |
WiredHome | 24:eee214e3e806 | 378 | size_t newTrfLen = 0; |
WiredHome | 24:eee214e3e806 | 379 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
WiredHome | 24:eee214e3e806 | 380 | trfLen += newTrfLen; |
WiredHome | 24:eee214e3e806 | 381 | buf[trfLen] = '\0'; |
WiredHome | 24:eee214e3e806 | 382 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
WiredHome | 24:eee214e3e806 | 383 | CHECK_CONN_ERR(ret); |
WiredHome | 24:eee214e3e806 | 384 | continue; |
WiredHome | 24:eee214e3e806 | 385 | } else { |
WiredHome | 24:eee214e3e806 | 386 | PRTCL_ERR(); |
WiredHome | 24:eee214e3e806 | 387 | } |
WiredHome | 24:eee214e3e806 | 388 | } |
WiredHome | 24:eee214e3e806 | 389 | |
WiredHome | 24:eee214e3e806 | 390 | crlfPos = crlfPtr - buf; |
WiredHome | 24:eee214e3e806 | 391 | |
WiredHome | 24:eee214e3e806 | 392 | if(crlfPos == 0) { //End of headers |
WiredHome | 24:eee214e3e806 | 393 | DBG("Headers read"); |
WiredHome | 24:eee214e3e806 | 394 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
WiredHome | 24:eee214e3e806 | 395 | trfLen -= 2; |
WiredHome | 24:eee214e3e806 | 396 | break; |
WiredHome | 24:eee214e3e806 | 397 | } |
WiredHome | 24:eee214e3e806 | 398 | |
WiredHome | 24:eee214e3e806 | 399 | buf[crlfPos] = '\0'; |
WiredHome | 24:eee214e3e806 | 400 | |
WiredHome | 24:eee214e3e806 | 401 | char key[32]; |
WiredHome | 31:96da7c08b5d0 | 402 | char value[MAXLEN_VALUE]; |
WiredHome | 24:eee214e3e806 | 403 | |
WiredHome | 24:eee214e3e806 | 404 | key[31] = '\0'; |
WiredHome | 31:96da7c08b5d0 | 405 | value[MAXLEN_VALUE - 1] = '\0'; |
WiredHome | 24:eee214e3e806 | 406 | |
WiredHome | 31:96da7c08b5d0 | 407 | int n = sscanf(buf, "%31[^:]: %160[^\r\n]", key, value); |
WiredHome | 24:eee214e3e806 | 408 | if ( n == 2 ) { |
WiredHome | 25:76084defa790 | 409 | DBG("Read header : %s: %s", key, value); |
WiredHome | 24:eee214e3e806 | 410 | if( !strcmp(key, "Content-Length") ) { |
WiredHome | 24:eee214e3e806 | 411 | sscanf(value, "%d", &recvContentLength); |
WiredHome | 24:eee214e3e806 | 412 | pDataIn->setDataLen(recvContentLength); |
WiredHome | 24:eee214e3e806 | 413 | } else if( !strcmp(key, "Transfer-Encoding") ) { |
WiredHome | 24:eee214e3e806 | 414 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) { |
WiredHome | 24:eee214e3e806 | 415 | recvChunked = true; |
WiredHome | 24:eee214e3e806 | 416 | pDataIn->setIsChunked(true); |
WiredHome | 24:eee214e3e806 | 417 | } |
WiredHome | 24:eee214e3e806 | 418 | } else if( !strcmp(key, "Content-Type") ) { |
WiredHome | 24:eee214e3e806 | 419 | pDataIn->setDataType(value); |
WiredHome | 24:eee214e3e806 | 420 | } else if ( !strcmp(key, "Location") ) { |
WiredHome | 36:a5c13e512b78 | 421 | if (m_location) { |
WiredHome | 36:a5c13e512b78 | 422 | swFree(m_location); |
WiredHome | 36:a5c13e512b78 | 423 | } |
WiredHome | 36:a5c13e512b78 | 424 | m_location = (char *)swMalloc(strlen(value)+1); |
WiredHome | 24:eee214e3e806 | 425 | if (m_location) { |
WiredHome | 24:eee214e3e806 | 426 | strcpy(m_location,value); |
WiredHome | 24:eee214e3e806 | 427 | url = m_location; |
WiredHome | 24:eee214e3e806 | 428 | INFO("Following redirect[%d] to [%s]", maxRedirect, url); |
WiredHome | 24:eee214e3e806 | 429 | m_sock.close(); |
WiredHome | 24:eee214e3e806 | 430 | takeRedirect = true; |
WiredHome | 24:eee214e3e806 | 431 | break; // exit the while(true) header to follow the redirect |
WiredHome | 36:a5c13e512b78 | 432 | } else { |
WiredHome | 36:a5c13e512b78 | 433 | ERR("Could not allocate memory for %s", key); |
WiredHome | 24:eee214e3e806 | 434 | } |
WiredHome | 24:eee214e3e806 | 435 | } |
WiredHome | 24:eee214e3e806 | 436 | |
WiredHome | 24:eee214e3e806 | 437 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
WiredHome | 24:eee214e3e806 | 438 | trfLen -= (crlfPos + 2); |
WiredHome | 24:eee214e3e806 | 439 | } else { |
WiredHome | 24:eee214e3e806 | 440 | ERR("Could not parse header"); |
WiredHome | 24:eee214e3e806 | 441 | PRTCL_ERR(); |
WiredHome | 24:eee214e3e806 | 442 | } |
WiredHome | 24:eee214e3e806 | 443 | |
WiredHome | 24:eee214e3e806 | 444 | } // while(true) // get headers |
WiredHome | 24:eee214e3e806 | 445 | if (!takeRedirect) |
WiredHome | 24:eee214e3e806 | 446 | break; |
WiredHome | 24:eee214e3e806 | 447 | } // while (maxRedirect) |
donatien | 0:2ccb9960a044 | 448 | |
WiredHome | 20:4ea5255c1b04 | 449 | //Receive data |
WiredHome | 20:4ea5255c1b04 | 450 | DBG("Receiving data"); |
WiredHome | 20:4ea5255c1b04 | 451 | while(true) { |
WiredHome | 20:4ea5255c1b04 | 452 | size_t readLen = 0; |
donatien | 0:2ccb9960a044 | 453 | |
WiredHome | 20:4ea5255c1b04 | 454 | if( recvChunked ) { |
WiredHome | 20:4ea5255c1b04 | 455 | //Read chunk header |
WiredHome | 20:4ea5255c1b04 | 456 | bool foundCrlf; |
WiredHome | 20:4ea5255c1b04 | 457 | do { |
WiredHome | 20:4ea5255c1b04 | 458 | foundCrlf = false; |
WiredHome | 20:4ea5255c1b04 | 459 | crlfPos=0; |
WiredHome | 20:4ea5255c1b04 | 460 | buf[trfLen]=0; |
WiredHome | 20:4ea5255c1b04 | 461 | if(trfLen >= 2) { |
WiredHome | 20:4ea5255c1b04 | 462 | for(; crlfPos < trfLen - 2; crlfPos++) { |
WiredHome | 20:4ea5255c1b04 | 463 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) { |
WiredHome | 20:4ea5255c1b04 | 464 | foundCrlf = true; |
WiredHome | 20:4ea5255c1b04 | 465 | break; |
WiredHome | 20:4ea5255c1b04 | 466 | } |
WiredHome | 20:4ea5255c1b04 | 467 | } |
WiredHome | 20:4ea5255c1b04 | 468 | } |
WiredHome | 20:4ea5255c1b04 | 469 | if(!foundCrlf) { //Try to read more |
WiredHome | 20:4ea5255c1b04 | 470 | if( trfLen < CHUNK_SIZE ) { |
WiredHome | 22:d6b08d9749d6 | 471 | size_t newTrfLen = 0; |
WiredHome | 20:4ea5255c1b04 | 472 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
WiredHome | 20:4ea5255c1b04 | 473 | trfLen += newTrfLen; |
WiredHome | 20:4ea5255c1b04 | 474 | CHECK_CONN_ERR(ret); |
WiredHome | 20:4ea5255c1b04 | 475 | continue; |
WiredHome | 20:4ea5255c1b04 | 476 | } else { |
WiredHome | 20:4ea5255c1b04 | 477 | PRTCL_ERR(); |
WiredHome | 20:4ea5255c1b04 | 478 | } |
WiredHome | 20:4ea5255c1b04 | 479 | } |
WiredHome | 20:4ea5255c1b04 | 480 | } while(!foundCrlf); |
WiredHome | 20:4ea5255c1b04 | 481 | buf[crlfPos] = '\0'; |
WiredHome | 20:4ea5255c1b04 | 482 | int n = sscanf(buf, "%x", &readLen); |
WiredHome | 20:4ea5255c1b04 | 483 | if(n!=1) { |
WiredHome | 20:4ea5255c1b04 | 484 | ERR("Could not read chunk length"); |
WiredHome | 20:4ea5255c1b04 | 485 | PRTCL_ERR(); |
donatien | 14:2744e0c0e527 | 486 | } |
WiredHome | 20:4ea5255c1b04 | 487 | |
WiredHome | 20:4ea5255c1b04 | 488 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
WiredHome | 20:4ea5255c1b04 | 489 | trfLen -= (crlfPos + 2); |
WiredHome | 20:4ea5255c1b04 | 490 | |
WiredHome | 20:4ea5255c1b04 | 491 | if( readLen == 0 ) { |
WiredHome | 20:4ea5255c1b04 | 492 | //Last chunk |
WiredHome | 20:4ea5255c1b04 | 493 | break; |
WiredHome | 20:4ea5255c1b04 | 494 | } |
WiredHome | 20:4ea5255c1b04 | 495 | } else { |
WiredHome | 20:4ea5255c1b04 | 496 | readLen = recvContentLength; |
donatien | 0:2ccb9960a044 | 497 | } |
WiredHome | 20:4ea5255c1b04 | 498 | |
WiredHome | 20:4ea5255c1b04 | 499 | DBG("Retrieving %d bytes", readLen); |
WiredHome | 20:4ea5255c1b04 | 500 | |
WiredHome | 20:4ea5255c1b04 | 501 | do { |
WiredHome | 29:9ee96efc1c20 | 502 | INFO("write %d,%d: %s", trfLen, readLen, buf); |
WiredHome | 20:4ea5255c1b04 | 503 | pDataIn->write(buf, MIN(trfLen, readLen)); |
WiredHome | 20:4ea5255c1b04 | 504 | if( trfLen > readLen ) { |
WiredHome | 20:4ea5255c1b04 | 505 | memmove(buf, &buf[readLen], trfLen - readLen); |
WiredHome | 20:4ea5255c1b04 | 506 | trfLen -= readLen; |
WiredHome | 20:4ea5255c1b04 | 507 | readLen = 0; |
WiredHome | 20:4ea5255c1b04 | 508 | } else { |
WiredHome | 20:4ea5255c1b04 | 509 | readLen -= trfLen; |
WiredHome | 20:4ea5255c1b04 | 510 | } |
WiredHome | 20:4ea5255c1b04 | 511 | |
WiredHome | 20:4ea5255c1b04 | 512 | if(readLen) { |
WiredHome | 20:4ea5255c1b04 | 513 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
WiredHome | 20:4ea5255c1b04 | 514 | CHECK_CONN_ERR(ret); |
WiredHome | 43:12a6ae712bcd | 515 | INFO("recv'd next chunk ret: %d", ret); |
WiredHome | 20:4ea5255c1b04 | 516 | } |
WiredHome | 20:4ea5255c1b04 | 517 | } while(readLen); |
WiredHome | 20:4ea5255c1b04 | 518 | |
WiredHome | 20:4ea5255c1b04 | 519 | if( recvChunked ) { |
WiredHome | 20:4ea5255c1b04 | 520 | if(trfLen < 2) { |
WiredHome | 20:4ea5255c1b04 | 521 | size_t newTrfLen; |
WiredHome | 20:4ea5255c1b04 | 522 | //Read missing chars to find end of chunk |
WiredHome | 29:9ee96efc1c20 | 523 | INFO("read chunk"); |
WiredHome | 20:4ea5255c1b04 | 524 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
WiredHome | 20:4ea5255c1b04 | 525 | CHECK_CONN_ERR(ret); |
WiredHome | 20:4ea5255c1b04 | 526 | trfLen += newTrfLen; |
WiredHome | 43:12a6ae712bcd | 527 | INFO("recv'd next chunk ret: %d", ret); |
WiredHome | 20:4ea5255c1b04 | 528 | } |
WiredHome | 20:4ea5255c1b04 | 529 | if( (buf[0] != '\r') || (buf[1] != '\n') ) { |
WiredHome | 20:4ea5255c1b04 | 530 | ERR("Format error"); |
WiredHome | 20:4ea5255c1b04 | 531 | PRTCL_ERR(); |
WiredHome | 20:4ea5255c1b04 | 532 | } |
WiredHome | 20:4ea5255c1b04 | 533 | memmove(buf, &buf[2], trfLen - 2); |
WiredHome | 20:4ea5255c1b04 | 534 | trfLen -= 2; |
WiredHome | 20:4ea5255c1b04 | 535 | } else { |
WiredHome | 20:4ea5255c1b04 | 536 | break; |
donatien | 0:2ccb9960a044 | 537 | } |
donatien | 0:2ccb9960a044 | 538 | } |
WiredHome | 20:4ea5255c1b04 | 539 | m_sock.close(); |
WiredHome | 20:4ea5255c1b04 | 540 | DBG("Completed HTTP transaction"); |
WiredHome | 20:4ea5255c1b04 | 541 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 542 | } |
donatien | 0:2ccb9960a044 | 543 | |
donatien | 11:390362de8c3f | 544 | HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure |
donatien | 0:2ccb9960a044 | 545 | { |
WiredHome | 20:4ea5255c1b04 | 546 | DBG("Trying to read between %d and %d bytes", minLen, maxLen); |
WiredHome | 20:4ea5255c1b04 | 547 | size_t readLen = 0; |
WiredHome | 20:4ea5255c1b04 | 548 | |
WiredHome | 20:4ea5255c1b04 | 549 | if (!m_sock.is_connected()) { |
WiredHome | 20:4ea5255c1b04 | 550 | WARN("Connection was closed by server"); |
WiredHome | 20:4ea5255c1b04 | 551 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 7:4e39864f7b15 | 552 | } |
WiredHome | 20:4ea5255c1b04 | 553 | |
WiredHome | 20:4ea5255c1b04 | 554 | int ret; |
WiredHome | 20:4ea5255c1b04 | 555 | while (readLen < maxLen) { |
WiredHome | 20:4ea5255c1b04 | 556 | if (readLen < minLen) { |
WiredHome | 29:9ee96efc1c20 | 557 | DBG("Trying to read at most %4d bytes [Blocking] %d,%d", minLen - readLen, minLen, readLen); |
WiredHome | 20:4ea5255c1b04 | 558 | m_sock.set_blocking(false, m_timeout); |
WiredHome | 20:4ea5255c1b04 | 559 | ret = m_sock.receive_all(buf + readLen, minLen - readLen); |
WiredHome | 20:4ea5255c1b04 | 560 | } else { |
WiredHome | 29:9ee96efc1c20 | 561 | DBG("Trying to read at most %4d bytes [Not blocking] %d,%d", maxLen - readLen, maxLen, readLen); |
WiredHome | 20:4ea5255c1b04 | 562 | m_sock.set_blocking(false, 0); |
WiredHome | 20:4ea5255c1b04 | 563 | ret = m_sock.receive(buf + readLen, maxLen - readLen); |
WiredHome | 20:4ea5255c1b04 | 564 | } |
WiredHome | 20:4ea5255c1b04 | 565 | |
WiredHome | 20:4ea5255c1b04 | 566 | if (ret > 0) { |
WiredHome | 20:4ea5255c1b04 | 567 | readLen += ret; |
WiredHome | 20:4ea5255c1b04 | 568 | } else if ( ret == 0 ) { |
WiredHome | 20:4ea5255c1b04 | 569 | break; |
WiredHome | 20:4ea5255c1b04 | 570 | } else { |
WiredHome | 20:4ea5255c1b04 | 571 | if (!m_sock.is_connected()) { |
WiredHome | 20:4ea5255c1b04 | 572 | ERR("Connection error (recv returned %d)", ret); |
WiredHome | 20:4ea5255c1b04 | 573 | *pReadLen = readLen; |
WiredHome | 20:4ea5255c1b04 | 574 | return HTTP_CONN; |
WiredHome | 20:4ea5255c1b04 | 575 | } else { |
WiredHome | 20:4ea5255c1b04 | 576 | break; |
WiredHome | 20:4ea5255c1b04 | 577 | } |
WiredHome | 20:4ea5255c1b04 | 578 | } |
WiredHome | 20:4ea5255c1b04 | 579 | if (!m_sock.is_connected()) { |
WiredHome | 20:4ea5255c1b04 | 580 | break; |
WiredHome | 20:4ea5255c1b04 | 581 | } |
donatien | 7:4e39864f7b15 | 582 | } |
WiredHome | 20:4ea5255c1b04 | 583 | DBG("Read %d bytes", readLen); |
WiredHome | 29:9ee96efc1c20 | 584 | buf[readLen] = '\0'; // DS makes it easier to see what's new. |
WiredHome | 20:4ea5255c1b04 | 585 | *pReadLen = readLen; |
WiredHome | 20:4ea5255c1b04 | 586 | return HTTP_OK; |
donatien | 7:4e39864f7b15 | 587 | } |
donatien | 7:4e39864f7b15 | 588 | |
WiredHome | 35:d9e2d1c96b75 | 589 | HTTPResult HTTPClient::send(const char* buf, size_t len) //0 on success, err code on failure |
donatien | 7:4e39864f7b15 | 590 | { |
WiredHome | 20:4ea5255c1b04 | 591 | if(len == 0) { |
WiredHome | 20:4ea5255c1b04 | 592 | len = strlen(buf); |
WiredHome | 20:4ea5255c1b04 | 593 | } |
WiredHome | 37:74c1c4527f70 | 594 | DBG("send(\r\n%s,%d)", buf, len); |
WiredHome | 20:4ea5255c1b04 | 595 | size_t writtenLen = 0; |
WiredHome | 20:4ea5255c1b04 | 596 | |
WiredHome | 20:4ea5255c1b04 | 597 | if(!m_sock.is_connected()) { |
WiredHome | 20:4ea5255c1b04 | 598 | WARN("Connection was closed by server"); |
WiredHome | 20:4ea5255c1b04 | 599 | return HTTP_CLOSED; //Connection was closed by server |
WiredHome | 20:4ea5255c1b04 | 600 | } |
WiredHome | 37:74c1c4527f70 | 601 | DBG("a"); |
WiredHome | 20:4ea5255c1b04 | 602 | m_sock.set_blocking(false, m_timeout); |
WiredHome | 37:74c1c4527f70 | 603 | DBG("b"); |
WiredHome | 20:4ea5255c1b04 | 604 | int ret = m_sock.send_all(buf, len); |
WiredHome | 20:4ea5255c1b04 | 605 | if(ret > 0) { |
WiredHome | 20:4ea5255c1b04 | 606 | writtenLen += ret; |
WiredHome | 20:4ea5255c1b04 | 607 | } else if( ret == 0 ) { |
WiredHome | 20:4ea5255c1b04 | 608 | WARN("Connection was closed by server"); |
WiredHome | 20:4ea5255c1b04 | 609 | return HTTP_CLOSED; //Connection was closed by server |
WiredHome | 20:4ea5255c1b04 | 610 | } else { |
WiredHome | 20:4ea5255c1b04 | 611 | ERR("Connection error (send returned %d)", ret); |
WiredHome | 20:4ea5255c1b04 | 612 | return HTTP_CONN; |
WiredHome | 20:4ea5255c1b04 | 613 | } |
WiredHome | 20:4ea5255c1b04 | 614 | DBG("Written %d bytes", writtenLen); |
WiredHome | 20:4ea5255c1b04 | 615 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 616 | } |
donatien | 0:2ccb9960a044 | 617 | |
donatien | 11:390362de8c3f | 618 | 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 | 619 | { |
WiredHome | 20:4ea5255c1b04 | 620 | char* schemePtr = (char*) url; |
WiredHome | 20:4ea5255c1b04 | 621 | char* hostPtr = (char*) strstr(url, "://"); |
WiredHome | 43:12a6ae712bcd | 622 | INFO("parseURL(%s,%p,%d,%s,%d,%d,%p,%d", |
WiredHome | 37:74c1c4527f70 | 623 | url, scheme, maxSchemeLen, host, maxHostLen, *port, path, maxPathLen); |
WiredHome | 43:12a6ae712bcd | 624 | if (hostPtr == NULL) { |
WiredHome | 20:4ea5255c1b04 | 625 | WARN("Could not find host"); |
WiredHome | 20:4ea5255c1b04 | 626 | return HTTP_PARSE; //URL is invalid |
WiredHome | 20:4ea5255c1b04 | 627 | } |
WiredHome | 20:4ea5255c1b04 | 628 | |
WiredHome | 43:12a6ae712bcd | 629 | if ( (uint16_t)maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char |
WiredHome | 20:4ea5255c1b04 | 630 | WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); |
WiredHome | 20:4ea5255c1b04 | 631 | return HTTP_PARSE; |
WiredHome | 20:4ea5255c1b04 | 632 | } |
WiredHome | 20:4ea5255c1b04 | 633 | memcpy(scheme, schemePtr, hostPtr - schemePtr); |
WiredHome | 20:4ea5255c1b04 | 634 | scheme[hostPtr - schemePtr] = '\0'; |
donatien | 0:2ccb9960a044 | 635 | |
WiredHome | 20:4ea5255c1b04 | 636 | hostPtr+=3; |
donatien | 0:2ccb9960a044 | 637 | |
WiredHome | 20:4ea5255c1b04 | 638 | size_t hostLen = 0; |
donatien | 0:2ccb9960a044 | 639 | |
WiredHome | 20:4ea5255c1b04 | 640 | char* portPtr = strchr(hostPtr, ':'); |
WiredHome | 20:4ea5255c1b04 | 641 | if( portPtr != NULL ) { |
WiredHome | 20:4ea5255c1b04 | 642 | hostLen = portPtr - hostPtr; |
WiredHome | 20:4ea5255c1b04 | 643 | portPtr++; |
WiredHome | 20:4ea5255c1b04 | 644 | if( sscanf(portPtr, "%hu", port) != 1) { |
WiredHome | 20:4ea5255c1b04 | 645 | WARN("Could not find port"); |
WiredHome | 20:4ea5255c1b04 | 646 | return HTTP_PARSE; |
WiredHome | 20:4ea5255c1b04 | 647 | } |
WiredHome | 20:4ea5255c1b04 | 648 | } else { |
WiredHome | 20:4ea5255c1b04 | 649 | *port=0; |
donatien | 0:2ccb9960a044 | 650 | } |
WiredHome | 37:74c1c4527f70 | 651 | INFO(" hostPtr: %s", hostPtr); |
WiredHome | 37:74c1c4527f70 | 652 | INFO(" hostLen: %d", hostLen); |
WiredHome | 20:4ea5255c1b04 | 653 | char* pathPtr = strchr(hostPtr, '/'); |
WiredHome | 20:4ea5255c1b04 | 654 | if( hostLen == 0 ) { |
WiredHome | 20:4ea5255c1b04 | 655 | hostLen = pathPtr - hostPtr; |
WiredHome | 20:4ea5255c1b04 | 656 | } |
donatien | 0:2ccb9960a044 | 657 | |
WiredHome | 20:4ea5255c1b04 | 658 | if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char |
WiredHome | 20:4ea5255c1b04 | 659 | WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); |
WiredHome | 20:4ea5255c1b04 | 660 | return HTTP_PARSE; |
WiredHome | 20:4ea5255c1b04 | 661 | } |
WiredHome | 20:4ea5255c1b04 | 662 | memcpy(host, hostPtr, hostLen); |
WiredHome | 20:4ea5255c1b04 | 663 | host[hostLen] = '\0'; |
donatien | 0:2ccb9960a044 | 664 | |
WiredHome | 20:4ea5255c1b04 | 665 | size_t pathLen; |
WiredHome | 20:4ea5255c1b04 | 666 | char* fragmentPtr = strchr(hostPtr, '#'); |
WiredHome | 20:4ea5255c1b04 | 667 | if(fragmentPtr != NULL) { |
WiredHome | 20:4ea5255c1b04 | 668 | pathLen = fragmentPtr - pathPtr; |
WiredHome | 20:4ea5255c1b04 | 669 | } else { |
WiredHome | 20:4ea5255c1b04 | 670 | pathLen = strlen(pathPtr); |
WiredHome | 20:4ea5255c1b04 | 671 | } |
donatien | 0:2ccb9960a044 | 672 | |
WiredHome | 20:4ea5255c1b04 | 673 | if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char |
WiredHome | 20:4ea5255c1b04 | 674 | WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); |
WiredHome | 20:4ea5255c1b04 | 675 | return HTTP_PARSE; |
WiredHome | 20:4ea5255c1b04 | 676 | } |
WiredHome | 20:4ea5255c1b04 | 677 | memcpy(path, pathPtr, pathLen); |
WiredHome | 20:4ea5255c1b04 | 678 | path[pathLen] = '\0'; |
donatien | 0:2ccb9960a044 | 679 | |
WiredHome | 20:4ea5255c1b04 | 680 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 681 | } |
kazushi2008 | 18:cf5d7427a9ec | 682 | |
WiredHome | 20:4ea5255c1b04 | 683 | void HTTPClient::createauth (const char *user, const char *pwd, char *buf, int len) |
WiredHome | 20:4ea5255c1b04 | 684 | { |
kazushi2008 | 18:cf5d7427a9ec | 685 | char tmp[80]; |
WiredHome | 20:4ea5255c1b04 | 686 | |
kazushi2008 | 18:cf5d7427a9ec | 687 | snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); |
kazushi2008 | 18:cf5d7427a9ec | 688 | base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf)); |
kazushi2008 | 18:cf5d7427a9ec | 689 | } |
kazushi2008 | 18:cf5d7427a9ec | 690 | |
kazushi2008 | 18:cf5d7427a9ec | 691 | // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
WiredHome | 20:4ea5255c1b04 | 692 | int HTTPClient::base64enc(const char *input, unsigned int length, char *output, int len) |
WiredHome | 20:4ea5255c1b04 | 693 | { |
WiredHome | 20:4ea5255c1b04 | 694 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
WiredHome | 20:4ea5255c1b04 | 695 | unsigned int c, c1, c2, c3; |
WiredHome | 20:4ea5255c1b04 | 696 | |
WiredHome | 35:d9e2d1c96b75 | 697 | if ((uint16_t)len < ((((length-1)/3)+1)<<2)) return -1; |
WiredHome | 20:4ea5255c1b04 | 698 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
WiredHome | 20:4ea5255c1b04 | 699 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
WiredHome | 20:4ea5255c1b04 | 700 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
WiredHome | 20:4ea5255c1b04 | 701 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
WiredHome | 20:4ea5255c1b04 | 702 | |
WiredHome | 20:4ea5255c1b04 | 703 | c = ((c1 & 0xFC) >> 2); |
WiredHome | 20:4ea5255c1b04 | 704 | output[j+0] = base64[c]; |
WiredHome | 20:4ea5255c1b04 | 705 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
WiredHome | 20:4ea5255c1b04 | 706 | output[j+1] = base64[c]; |
WiredHome | 20:4ea5255c1b04 | 707 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
WiredHome | 20:4ea5255c1b04 | 708 | output[j+2] = (length>i+1)?base64[c]:'='; |
WiredHome | 20:4ea5255c1b04 | 709 | c = (c3 & 0x3F); |
WiredHome | 20:4ea5255c1b04 | 710 | output[j+3] = (length>i+2)?base64[c]:'='; |
WiredHome | 20:4ea5255c1b04 | 711 | } |
WiredHome | 20:4ea5255c1b04 | 712 | output[(((length-1)/3)+1)<<2] = '\0'; |
WiredHome | 20:4ea5255c1b04 | 713 | return 0; |
kazushi2008 | 18:cf5d7427a9ec | 714 | } |