Fork HTTPClient and Modfiy code for mbed 6.0

Dependents:   mbed-demo-http-get-json

Committer:
WiredHome
Date:
Thu Dec 29 20:14:53 2016 +0000
Revision:
38:2ef07232f65c
Parent:
37:74c1c4527f70
Child:
41:37e7df011c1f
Added a method to translate a return code to a text message

Who changed what in which revision?

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