Fork HTTPClient and Modfiy code for mbed 6.0

Dependents:   mbed-demo-http-get-json

Committer:
quxiaorui
Date:
Sun Nov 08 02:49:01 2020 +0000
Revision:
50:1c1409029b05
Parent:
49:c5abb7ae070b
Child:
51:a9ed5a6eee90
Modfiy code for mbed 6.0

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