A HTTP Client for the mbed networking libraries, with PUT method

Fork of HTTPClient by Donatien Garnier

Committer:
stevep
Date:
Tue Aug 14 11:54:13 2012 +0000
Revision:
14:0463c8425d73
Parent:
13:be61104f4e91
Add HTTP PUT method.

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
donatien 7:4e39864f7b15 20 //Debug is disabled by default
donatien 13:be61104f4e91 21 #if 1
donatien 12:89d09a6db00a 22 //Enable debug
donatien 11:390362de8c3f 23 #include <cstdio>
donatien 13:be61104f4e91 24 //#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
donatien 13:be61104f4e91 25 #define DBG(x, ...)
donatien 11:390362de8c3f 26 #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
donatien 11:390362de8c3f 27 #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
donatien 12:89d09a6db00a 28
donatien 12:89d09a6db00a 29 #else
donatien 12:89d09a6db00a 30 //Disable debug
donatien 12:89d09a6db00a 31 #define DBG(x, ...)
donatien 12:89d09a6db00a 32 #define WARN(x, ...)
donatien 12:89d09a6db00a 33 #define ERR(x, ...)
donatien 12:89d09a6db00a 34
donatien 7:4e39864f7b15 35 #endif
donatien 0:2ccb9960a044 36
donatien 0:2ccb9960a044 37 #define HTTP_PORT 80
donatien 0:2ccb9960a044 38
donatien 11:390362de8c3f 39 #define OK 0
donatien 11:390362de8c3f 40
donatien 11:390362de8c3f 41 #define MIN(x,y) (((x)<(y))?(x):(y))
donatien 11:390362de8c3f 42 #define MAX(x,y) (((x)>(y))?(x):(y))
donatien 11:390362de8c3f 43
donatien 0:2ccb9960a044 44 #define CHUNK_SIZE 256
donatien 0:2ccb9960a044 45
donatien 0:2ccb9960a044 46 #include <cstring>
donatien 0:2ccb9960a044 47
donatien 11:390362de8c3f 48 #include "HTTPClient.h"
donatien 11:390362de8c3f 49
donatien 0:2ccb9960a044 50 HTTPClient::HTTPClient() :
donatien 7:4e39864f7b15 51 m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
donatien 0:2ccb9960a044 52 {
donatien 0:2ccb9960a044 53
donatien 0:2ccb9960a044 54 }
donatien 0:2ccb9960a044 55
donatien 0:2ccb9960a044 56 HTTPClient::~HTTPClient()
donatien 0:2ccb9960a044 57 {
donatien 0:2ccb9960a044 58
donatien 0:2ccb9960a044 59 }
donatien 0:2ccb9960a044 60
donatien 0:2ccb9960a044 61 #if 0
donatien 0:2ccb9960a044 62 void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
donatien 0:2ccb9960a044 63 {
donatien 0:2ccb9960a044 64 m_basicAuthUser = user;
donatien 0:2ccb9960a044 65 m_basicAuthPassword = password;
donatien 0:2ccb9960a044 66 }
donatien 0:2ccb9960a044 67 #endif
donatien 0:2ccb9960a044 68
donatien 12:89d09a6db00a 69 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 70 {
donatien 0:2ccb9960a044 71 return connect(url, HTTP_GET, NULL, pDataIn, timeout);
donatien 0:2ccb9960a044 72 }
donatien 0:2ccb9960a044 73
donatien 12:89d09a6db00a 74 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 75 {
donatien 0:2ccb9960a044 76 HTTPText str(result, maxResultLen);
donatien 0:2ccb9960a044 77 return get(url, &str, timeout);
donatien 0:2ccb9960a044 78 }
donatien 0:2ccb9960a044 79
donatien 12:89d09a6db00a 80 HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
donatien 0:2ccb9960a044 81 {
donatien 0:2ccb9960a044 82 return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
donatien 0:2ccb9960a044 83 }
donatien 0:2ccb9960a044 84
stevep 14:0463c8425d73 85 HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
stevep 14:0463c8425d73 86 {
stevep 14:0463c8425d73 87 return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
stevep 14:0463c8425d73 88 }
stevep 14:0463c8425d73 89
donatien 0:2ccb9960a044 90 int HTTPClient::getHTTPResponseCode()
donatien 0:2ccb9960a044 91 {
donatien 0:2ccb9960a044 92 return m_httpResponseCode;
donatien 0:2ccb9960a044 93 }
donatien 0:2ccb9960a044 94
donatien 5:791fc3dcb6c4 95 #define CHECK_CONN_ERR(ret) \
donatien 5:791fc3dcb6c4 96 do{ \
donatien 7:4e39864f7b15 97 if(ret) { \
donatien 7:4e39864f7b15 98 m_sock.close(); \
donatien 5:791fc3dcb6c4 99 ERR("Connection error (%d)", ret); \
donatien 11:390362de8c3f 100 return HTTP_CONN; \
donatien 5:791fc3dcb6c4 101 } \
donatien 5:791fc3dcb6c4 102 } while(0)
donatien 5:791fc3dcb6c4 103
donatien 5:791fc3dcb6c4 104 #define PRTCL_ERR() \
donatien 5:791fc3dcb6c4 105 do{ \
donatien 7:4e39864f7b15 106 m_sock.close(); \
donatien 5:791fc3dcb6c4 107 ERR("Protocol error"); \
donatien 11:390362de8c3f 108 return HTTP_PRTCL; \
donatien 5:791fc3dcb6c4 109 } while(0)
donatien 0:2ccb9960a044 110
donatien 12:89d09a6db00a 111 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
donatien 12:89d09a6db00a 112 {
donatien 0:2ccb9960a044 113 m_httpResponseCode = 0; //Invalidate code
donatien 0:2ccb9960a044 114 m_timeout = timeout;
donatien 0:2ccb9960a044 115
donatien 0:2ccb9960a044 116 char scheme[8];
donatien 0:2ccb9960a044 117 uint16_t port;
donatien 0:2ccb9960a044 118 char host[32];
donatien 0:2ccb9960a044 119 char path[64];
donatien 0:2ccb9960a044 120 //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?)
donatien 11:390362de8c3f 121 HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
donatien 11:390362de8c3f 122 if(res != HTTP_OK)
donatien 0:2ccb9960a044 123 {
donatien 11:390362de8c3f 124 ERR("parseURL returned %d", res);
donatien 11:390362de8c3f 125 return res;
donatien 0:2ccb9960a044 126 }
donatien 0:2ccb9960a044 127
donatien 0:2ccb9960a044 128 if(port == 0) //TODO do handle HTTPS->443
donatien 0:2ccb9960a044 129 {
donatien 0:2ccb9960a044 130 port = 80;
donatien 0:2ccb9960a044 131 }
donatien 0:2ccb9960a044 132
donatien 0:2ccb9960a044 133 DBG("Scheme: %s", scheme);
donatien 0:2ccb9960a044 134 DBG("Host: %s", host);
donatien 0:2ccb9960a044 135 DBG("Port: %d", port);
donatien 0:2ccb9960a044 136 DBG("Path: %s", path);
donatien 0:2ccb9960a044 137
donatien 7:4e39864f7b15 138 //Connect
donatien 7:4e39864f7b15 139 DBG("Connecting socket to server");
donatien 11:390362de8c3f 140 int ret = m_sock.connect(host, port);
donatien 0:2ccb9960a044 141 if (ret < 0)
donatien 0:2ccb9960a044 142 {
donatien 7:4e39864f7b15 143 m_sock.close();
donatien 0:2ccb9960a044 144 ERR("Could not connect");
donatien 11:390362de8c3f 145 return HTTP_CONN;
donatien 0:2ccb9960a044 146 }
donatien 0:2ccb9960a044 147
donatien 0:2ccb9960a044 148 //Send request
donatien 0:2ccb9960a044 149 DBG("Sending request");
donatien 9:ff30cc189191 150 char buf[CHUNK_SIZE];
stevep 14:0463c8425d73 151 const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":"";
donatien 9:ff30cc189191 152 snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
donatien 9:ff30cc189191 153 ret = send(buf);
donatien 0:2ccb9960a044 154 if(ret)
donatien 0:2ccb9960a044 155 {
donatien 7:4e39864f7b15 156 m_sock.close();
donatien 0:2ccb9960a044 157 ERR("Could not write request");
donatien 11:390362de8c3f 158 return HTTP_CONN;
donatien 0:2ccb9960a044 159 }
donatien 0:2ccb9960a044 160
donatien 0:2ccb9960a044 161 //Send all headers
donatien 0:2ccb9960a044 162
donatien 0:2ccb9960a044 163 //Send default headers
donatien 0:2ccb9960a044 164 DBG("Sending headers");
stevep 14:0463c8425d73 165 if( (method == HTTP_POST || method == HTTP_PUT) && (pDataOut != NULL) )
donatien 0:2ccb9960a044 166 {
donatien 0:2ccb9960a044 167 if( pDataOut->getIsChunked() )
donatien 0:2ccb9960a044 168 {
donatien 0:2ccb9960a044 169 ret = send("Transfer-Encoding: chunked\r\n");
donatien 5:791fc3dcb6c4 170 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 171 }
donatien 0:2ccb9960a044 172 else
donatien 0:2ccb9960a044 173 {
donatien 9:ff30cc189191 174 snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
donatien 9:ff30cc189191 175 ret = send(buf);
donatien 5:791fc3dcb6c4 176 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 177 }
donatien 0:2ccb9960a044 178 char type[48];
donatien 11:390362de8c3f 179 if( pDataOut->getDataType(type, 48) == HTTP_OK )
donatien 0:2ccb9960a044 180 {
donatien 9:ff30cc189191 181 snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
donatien 9:ff30cc189191 182 ret = send(buf);
donatien 5:791fc3dcb6c4 183 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 184 }
donatien 0:2ccb9960a044 185 }
donatien 9:ff30cc189191 186
donatien 0:2ccb9960a044 187 //Close headers
donatien 0:2ccb9960a044 188 DBG("Headers sent");
donatien 0:2ccb9960a044 189 ret = send("\r\n");
donatien 5:791fc3dcb6c4 190 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 191
donatien 4:c071b05ac026 192 size_t trfLen;
donatien 9:ff30cc189191 193
donatien 0:2ccb9960a044 194 //Send data (if POST)
stevep 14:0463c8425d73 195 if( (method == HTTP_POST || method == HTTP_PUT) && (pDataOut != NULL) )
donatien 0:2ccb9960a044 196 {
donatien 0:2ccb9960a044 197 DBG("Sending data");
donatien 0:2ccb9960a044 198 while(true)
donatien 0:2ccb9960a044 199 {
donatien 0:2ccb9960a044 200 size_t writtenLen = 0;
donatien 0:2ccb9960a044 201 pDataOut->read(buf, CHUNK_SIZE, &trfLen);
donatien 0:2ccb9960a044 202 if( pDataOut->getIsChunked() )
donatien 0:2ccb9960a044 203 {
donatien 0:2ccb9960a044 204 //Write chunk header
donatien 9:ff30cc189191 205 char chunkHeader[16];
donatien 9:ff30cc189191 206 snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
donatien 9:ff30cc189191 207 ret = send(chunkHeader);
donatien 5:791fc3dcb6c4 208 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 209 }
donatien 0:2ccb9960a044 210 else if( trfLen == 0 )
donatien 0:2ccb9960a044 211 {
donatien 0:2ccb9960a044 212 break;
donatien 0:2ccb9960a044 213 }
donatien 0:2ccb9960a044 214 if( trfLen != 0 )
donatien 0:2ccb9960a044 215 {
donatien 0:2ccb9960a044 216 ret = send(buf, trfLen);
donatien 5:791fc3dcb6c4 217 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 218 }
donatien 0:2ccb9960a044 219
donatien 0:2ccb9960a044 220 if( pDataOut->getIsChunked() )
donatien 0:2ccb9960a044 221 {
donatien 0:2ccb9960a044 222 ret = send("\r\n"); //Chunk-terminating CRLF
donatien 5:791fc3dcb6c4 223 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 224 }
donatien 0:2ccb9960a044 225 else
donatien 0:2ccb9960a044 226 {
donatien 0:2ccb9960a044 227 writtenLen += trfLen;
donatien 0:2ccb9960a044 228 if( writtenLen >= pDataOut->getDataLen() )
donatien 0:2ccb9960a044 229 {
donatien 0:2ccb9960a044 230 break;
donatien 0:2ccb9960a044 231 }
donatien 0:2ccb9960a044 232 }
donatien 0:2ccb9960a044 233
donatien 0:2ccb9960a044 234 if( trfLen == 0 )
donatien 0:2ccb9960a044 235 {
donatien 0:2ccb9960a044 236 break;
donatien 0:2ccb9960a044 237 }
donatien 0:2ccb9960a044 238 }
donatien 0:2ccb9960a044 239
donatien 0:2ccb9960a044 240 }
donatien 9:ff30cc189191 241
donatien 0:2ccb9960a044 242 //Receive response
donatien 0:2ccb9960a044 243 DBG("Receiving response");
donatien 4:c071b05ac026 244 ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
donatien 5:791fc3dcb6c4 245 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 246
donatien 0:2ccb9960a044 247 buf[trfLen] = '\0';
donatien 0:2ccb9960a044 248
donatien 0:2ccb9960a044 249 char* crlfPtr = strstr(buf, "\r\n");
donatien 0:2ccb9960a044 250 if(crlfPtr == NULL)
donatien 0:2ccb9960a044 251 {
donatien 5:791fc3dcb6c4 252 PRTCL_ERR();
donatien 0:2ccb9960a044 253 }
donatien 0:2ccb9960a044 254
donatien 4:c071b05ac026 255 int crlfPos = crlfPtr - buf;
donatien 0:2ccb9960a044 256 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 257
donatien 0:2ccb9960a044 258 //Parse HTTP response
donatien 0:2ccb9960a044 259 if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 )
donatien 0:2ccb9960a044 260 {
donatien 0:2ccb9960a044 261 //Cannot match string, error
donatien 0:2ccb9960a044 262 ERR("Not a correct HTTP answer : %s\n", buf);
donatien 5:791fc3dcb6c4 263 PRTCL_ERR();
donatien 0:2ccb9960a044 264 }
donatien 0:2ccb9960a044 265
donatien 0:2ccb9960a044 266 if(m_httpResponseCode != 200)
donatien 0:2ccb9960a044 267 {
donatien 0:2ccb9960a044 268 //Cannot match string, error
donatien 0:2ccb9960a044 269 WARN("Response code %d", m_httpResponseCode);
donatien 5:791fc3dcb6c4 270 PRTCL_ERR();
donatien 0:2ccb9960a044 271 }
donatien 0:2ccb9960a044 272
donatien 0:2ccb9960a044 273 DBG("Reading headers");
donatien 0:2ccb9960a044 274
donatien 4:c071b05ac026 275 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
donatien 0:2ccb9960a044 276 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 277
donatien 4:c071b05ac026 278 size_t recvContentLength = 0;
donatien 4:c071b05ac026 279 bool recvChunked = false;
donatien 0:2ccb9960a044 280 //Now get headers
donatien 0:2ccb9960a044 281 while( true )
donatien 0:2ccb9960a044 282 {
donatien 0:2ccb9960a044 283 crlfPtr = strstr(buf, "\r\n");
donatien 0:2ccb9960a044 284 if(crlfPtr == NULL)
donatien 0:2ccb9960a044 285 {
donatien 4:c071b05ac026 286 if( trfLen < CHUNK_SIZE - 1 )
donatien 0:2ccb9960a044 287 {
donatien 0:2ccb9960a044 288 size_t newTrfLen;
donatien 0:2ccb9960a044 289 ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
donatien 0:2ccb9960a044 290 trfLen += newTrfLen;
donatien 0:2ccb9960a044 291 buf[trfLen] = '\0';
donatien 4:c071b05ac026 292 DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
donatien 5:791fc3dcb6c4 293 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 294 continue;
donatien 0:2ccb9960a044 295 }
donatien 0:2ccb9960a044 296 else
donatien 0:2ccb9960a044 297 {
donatien 5:791fc3dcb6c4 298 PRTCL_ERR();
donatien 0:2ccb9960a044 299 }
donatien 0:2ccb9960a044 300 }
donatien 0:2ccb9960a044 301
donatien 0:2ccb9960a044 302 crlfPos = crlfPtr - buf;
donatien 0:2ccb9960a044 303
donatien 0:2ccb9960a044 304 if(crlfPos == 0) //End of headers
donatien 0:2ccb9960a044 305 {
donatien 0:2ccb9960a044 306 DBG("Headers read");
donatien 4:c071b05ac026 307 memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
donatien 0:2ccb9960a044 308 trfLen -= 2;
donatien 0:2ccb9960a044 309 break;
donatien 0:2ccb9960a044 310 }
donatien 0:2ccb9960a044 311
donatien 0:2ccb9960a044 312 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 313
donatien 4:c071b05ac026 314 char key[32];
donatien 4:c071b05ac026 315 char value[32];
donatien 0:2ccb9960a044 316
donatien 4:c071b05ac026 317 key[31] = '\0';
donatien 4:c071b05ac026 318 value[31] = '\0';
donatien 4:c071b05ac026 319
donatien 4:c071b05ac026 320 int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
donatien 0:2ccb9960a044 321 if ( n == 2 )
donatien 0:2ccb9960a044 322 {
donatien 0:2ccb9960a044 323 DBG("Read header : %s: %s\n", key, value);
donatien 0:2ccb9960a044 324 if( !strcmp(key, "Content-Length") )
donatien 0:2ccb9960a044 325 {
donatien 0:2ccb9960a044 326 sscanf(value, "%d", &recvContentLength);
donatien 0:2ccb9960a044 327 pDataIn->setDataLen(recvContentLength);
donatien 0:2ccb9960a044 328 }
donatien 0:2ccb9960a044 329 else if( !strcmp(key, "Transfer-Encoding") )
donatien 0:2ccb9960a044 330 {
donatien 0:2ccb9960a044 331 if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") )
donatien 0:2ccb9960a044 332 {
donatien 0:2ccb9960a044 333 recvChunked = true;
donatien 0:2ccb9960a044 334 pDataIn->setIsChunked(true);
donatien 0:2ccb9960a044 335 }
donatien 0:2ccb9960a044 336 }
donatien 0:2ccb9960a044 337 else if( !strcmp(key, "Content-Type") )
donatien 0:2ccb9960a044 338 {
donatien 0:2ccb9960a044 339 pDataIn->setDataType(value);
donatien 0:2ccb9960a044 340 }
donatien 0:2ccb9960a044 341
donatien 4:c071b05ac026 342 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
donatien 0:2ccb9960a044 343 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 344
donatien 0:2ccb9960a044 345 }
donatien 0:2ccb9960a044 346 else
donatien 0:2ccb9960a044 347 {
donatien 0:2ccb9960a044 348 ERR("Could not parse header");
donatien 5:791fc3dcb6c4 349 PRTCL_ERR();
donatien 0:2ccb9960a044 350 }
donatien 0:2ccb9960a044 351
donatien 0:2ccb9960a044 352 }
donatien 0:2ccb9960a044 353
donatien 0:2ccb9960a044 354 //Receive data
donatien 0:2ccb9960a044 355 DBG("Receiving data");
donatien 0:2ccb9960a044 356 while(true)
donatien 0:2ccb9960a044 357 {
donatien 0:2ccb9960a044 358 size_t readLen = 0;
donatien 0:2ccb9960a044 359
donatien 0:2ccb9960a044 360 if( recvChunked )
donatien 0:2ccb9960a044 361 {
donatien 0:2ccb9960a044 362 //Read chunk header
donatien 0:2ccb9960a044 363 crlfPos=0;
donatien 0:2ccb9960a044 364 for(crlfPos++; crlfPos < trfLen - 2; crlfPos++)
donatien 0:2ccb9960a044 365 {
donatien 0:2ccb9960a044 366 if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' )
donatien 0:2ccb9960a044 367 {
donatien 0:2ccb9960a044 368 break;
donatien 0:2ccb9960a044 369 }
donatien 0:2ccb9960a044 370 }
donatien 0:2ccb9960a044 371 if(crlfPos >= trfLen - 2) //Try to read more
donatien 0:2ccb9960a044 372 {
donatien 0:2ccb9960a044 373 if( trfLen < CHUNK_SIZE )
donatien 0:2ccb9960a044 374 {
donatien 0:2ccb9960a044 375 size_t newTrfLen;
donatien 0:2ccb9960a044 376 ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
donatien 0:2ccb9960a044 377 trfLen += newTrfLen;
donatien 5:791fc3dcb6c4 378 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 379 continue;
donatien 0:2ccb9960a044 380 }
donatien 0:2ccb9960a044 381 else
donatien 0:2ccb9960a044 382 {
donatien 5:791fc3dcb6c4 383 PRTCL_ERR();
donatien 0:2ccb9960a044 384 }
donatien 0:2ccb9960a044 385 }
donatien 0:2ccb9960a044 386 buf[crlfPos] = '\0';
donatien 0:2ccb9960a044 387 int n = sscanf(buf, "%x", &readLen);
donatien 0:2ccb9960a044 388 if(n!=1)
donatien 0:2ccb9960a044 389 {
donatien 0:2ccb9960a044 390 ERR("Could not read chunk length");
donatien 5:791fc3dcb6c4 391 PRTCL_ERR();
donatien 0:2ccb9960a044 392 }
donatien 0:2ccb9960a044 393
donatien 4:c071b05ac026 394 memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
donatien 0:2ccb9960a044 395 trfLen -= (crlfPos + 2);
donatien 0:2ccb9960a044 396
donatien 0:2ccb9960a044 397 if( readLen == 0 )
donatien 0:2ccb9960a044 398 {
donatien 0:2ccb9960a044 399 //Last chunk
donatien 0:2ccb9960a044 400 break;
donatien 0:2ccb9960a044 401 }
donatien 0:2ccb9960a044 402 }
donatien 0:2ccb9960a044 403 else
donatien 0:2ccb9960a044 404 {
donatien 0:2ccb9960a044 405 readLen = recvContentLength;
donatien 0:2ccb9960a044 406 }
donatien 0:2ccb9960a044 407
donatien 0:2ccb9960a044 408 DBG("Retrieving %d bytes", readLen);
donatien 0:2ccb9960a044 409
donatien 0:2ccb9960a044 410 do
donatien 0:2ccb9960a044 411 {
donatien 0:2ccb9960a044 412 pDataIn->write(buf, MIN(trfLen, readLen));
donatien 0:2ccb9960a044 413 if( trfLen > readLen )
donatien 0:2ccb9960a044 414 {
donatien 0:2ccb9960a044 415 memmove(buf, &buf[readLen], trfLen - readLen);
donatien 0:2ccb9960a044 416 trfLen -= readLen;
donatien 0:2ccb9960a044 417 readLen = 0;
donatien 0:2ccb9960a044 418 }
donatien 0:2ccb9960a044 419 else
donatien 0:2ccb9960a044 420 {
donatien 0:2ccb9960a044 421 readLen -= trfLen;
donatien 0:2ccb9960a044 422 }
donatien 0:2ccb9960a044 423
donatien 0:2ccb9960a044 424 if(readLen)
donatien 0:2ccb9960a044 425 {
donatien 0:2ccb9960a044 426 ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
donatien 5:791fc3dcb6c4 427 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 428 }
donatien 0:2ccb9960a044 429 } while(readLen);
donatien 0:2ccb9960a044 430
donatien 0:2ccb9960a044 431 if( recvChunked )
donatien 0:2ccb9960a044 432 {
donatien 0:2ccb9960a044 433 if(trfLen < 2)
donatien 0:2ccb9960a044 434 {
donatien 0:2ccb9960a044 435 size_t newTrfLen;
donatien 0:2ccb9960a044 436 //Read missing chars to find end of chunk
donatien 0:2ccb9960a044 437 ret = recv(buf, 2 - trfLen, CHUNK_SIZE, &newTrfLen);
donatien 5:791fc3dcb6c4 438 CHECK_CONN_ERR(ret);
donatien 0:2ccb9960a044 439 trfLen += newTrfLen;
donatien 0:2ccb9960a044 440 }
donatien 0:2ccb9960a044 441 if( (buf[0] != '\r') || (buf[1] != '\n') )
donatien 0:2ccb9960a044 442 {
donatien 0:2ccb9960a044 443 ERR("Format error");
donatien 5:791fc3dcb6c4 444 PRTCL_ERR();
donatien 0:2ccb9960a044 445 }
donatien 0:2ccb9960a044 446 memmove(buf, &buf[2], trfLen - 2);
donatien 0:2ccb9960a044 447 trfLen -= 2;
donatien 0:2ccb9960a044 448 }
donatien 0:2ccb9960a044 449 else
donatien 0:2ccb9960a044 450 {
donatien 0:2ccb9960a044 451 break;
donatien 0:2ccb9960a044 452 }
donatien 0:2ccb9960a044 453
donatien 0:2ccb9960a044 454 }
donatien 0:2ccb9960a044 455
donatien 7:4e39864f7b15 456 m_sock.close();
donatien 0:2ccb9960a044 457 DBG("Completed HTTP transaction");
donatien 0:2ccb9960a044 458
donatien 11:390362de8c3f 459 return HTTP_OK;
donatien 0:2ccb9960a044 460 }
donatien 0:2ccb9960a044 461
donatien 11:390362de8c3f 462 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
donatien 0:2ccb9960a044 463 {
donatien 0:2ccb9960a044 464 DBG("Trying to read between %d and %d bytes", minLen, maxLen);
donatien 0:2ccb9960a044 465 size_t readLen = 0;
donatien 12:89d09a6db00a 466
donatien 12:89d09a6db00a 467 if(!m_sock.is_connected())
donatien 12:89d09a6db00a 468 {
donatien 12:89d09a6db00a 469 WARN("Connection was closed by server");
donatien 12:89d09a6db00a 470 return HTTP_CLOSED; //Connection was closed by server
donatien 12:89d09a6db00a 471 }
donatien 12:89d09a6db00a 472
donatien 7:4e39864f7b15 473 int ret;
donatien 7:4e39864f7b15 474 while(readLen < maxLen)
donatien 0:2ccb9960a044 475 {
donatien 7:4e39864f7b15 476 if(readLen < minLen)
donatien 0:2ccb9960a044 477 {
donatien 12:89d09a6db00a 478 DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
donatien 13:be61104f4e91 479 m_sock.set_blocking(false, m_timeout);
donatien 12:89d09a6db00a 480 ret = m_sock.receive_all(buf + readLen, minLen - readLen);
donatien 0:2ccb9960a044 481 }
donatien 7:4e39864f7b15 482 else
donatien 7:4e39864f7b15 483 {
donatien 12:89d09a6db00a 484 DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
donatien 13:be61104f4e91 485 m_sock.set_blocking(false, 0);
donatien 12:89d09a6db00a 486 ret = m_sock.receive(buf + readLen, maxLen - readLen);
donatien 7:4e39864f7b15 487 }
donatien 7:4e39864f7b15 488
donatien 0:2ccb9960a044 489 if( ret > 0)
donatien 0:2ccb9960a044 490 {
donatien 0:2ccb9960a044 491 readLen += ret;
donatien 0:2ccb9960a044 492 }
donatien 0:2ccb9960a044 493 else if( ret == 0 )
donatien 0:2ccb9960a044 494 {
donatien 7:4e39864f7b15 495 break;
donatien 7:4e39864f7b15 496 }
donatien 7:4e39864f7b15 497 else
donatien 7:4e39864f7b15 498 {
donatien 12:89d09a6db00a 499 if(!m_sock.is_connected())
donatien 12:89d09a6db00a 500 {
donatien 12:89d09a6db00a 501 ERR("Connection error (recv returned %d)", ret);
donatien 12:89d09a6db00a 502 *pReadLen = readLen;
donatien 12:89d09a6db00a 503 return HTTP_CONN;
donatien 12:89d09a6db00a 504 }
donatien 12:89d09a6db00a 505 else
donatien 12:89d09a6db00a 506 {
donatien 12:89d09a6db00a 507 break;
donatien 12:89d09a6db00a 508 }
donatien 12:89d09a6db00a 509 }
donatien 12:89d09a6db00a 510
donatien 12:89d09a6db00a 511 if(!m_sock.is_connected())
donatien 12:89d09a6db00a 512 {
donatien 12:89d09a6db00a 513 break;
donatien 7:4e39864f7b15 514 }
donatien 7:4e39864f7b15 515 }
donatien 7:4e39864f7b15 516 DBG("Read %d bytes", readLen);
donatien 7:4e39864f7b15 517 *pReadLen = readLen;
donatien 11:390362de8c3f 518 return HTTP_OK;
donatien 7:4e39864f7b15 519 }
donatien 7:4e39864f7b15 520
donatien 11:390362de8c3f 521 HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
donatien 7:4e39864f7b15 522 {
donatien 7:4e39864f7b15 523 if(len == 0)
donatien 7:4e39864f7b15 524 {
donatien 7:4e39864f7b15 525 len = strlen(buf);
donatien 7:4e39864f7b15 526 }
donatien 7:4e39864f7b15 527 DBG("Trying to write %d bytes", len);
donatien 7:4e39864f7b15 528 size_t writtenLen = 0;
donatien 12:89d09a6db00a 529
donatien 12:89d09a6db00a 530 if(!m_sock.is_connected())
donatien 12:89d09a6db00a 531 {
donatien 12:89d09a6db00a 532 WARN("Connection was closed by server");
donatien 12:89d09a6db00a 533 return HTTP_CLOSED; //Connection was closed by server
donatien 12:89d09a6db00a 534 }
donatien 7:4e39864f7b15 535
donatien 13:be61104f4e91 536 m_sock.set_blocking(false, m_timeout);
donatien 12:89d09a6db00a 537 int ret = m_sock.send_all(buf, len);
donatien 12:89d09a6db00a 538 if(ret > 0)
donatien 7:4e39864f7b15 539 {
donatien 12:89d09a6db00a 540 writtenLen += ret;
donatien 12:89d09a6db00a 541 }
donatien 12:89d09a6db00a 542 else if( ret == 0 )
donatien 12:89d09a6db00a 543 {
donatien 12:89d09a6db00a 544 WARN("Connection was closed by server");
donatien 12:89d09a6db00a 545 return HTTP_CLOSED; //Connection was closed by server
donatien 12:89d09a6db00a 546 }
donatien 12:89d09a6db00a 547 else
donatien 12:89d09a6db00a 548 {
donatien 12:89d09a6db00a 549 ERR("Connection error (send returned %d)", ret);
donatien 12:89d09a6db00a 550 return HTTP_CONN;
donatien 12:89d09a6db00a 551 }
donatien 7:4e39864f7b15 552
donatien 0:2ccb9960a044 553 DBG("Written %d bytes", writtenLen);
donatien 11:390362de8c3f 554 return HTTP_OK;
donatien 0:2ccb9960a044 555 }
donatien 0:2ccb9960a044 556
donatien 11:390362de8c3f 557 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 558 {
donatien 0:2ccb9960a044 559 char* schemePtr = (char*) url;
donatien 0:2ccb9960a044 560 char* hostPtr = (char*) strstr(url, "://");
donatien 0:2ccb9960a044 561 if(hostPtr == NULL)
donatien 0:2ccb9960a044 562 {
donatien 0:2ccb9960a044 563 WARN("Could not find host");
donatien 11:390362de8c3f 564 return HTTP_PARSE; //URL is invalid
donatien 0:2ccb9960a044 565 }
donatien 0:2ccb9960a044 566
donatien 0:2ccb9960a044 567 if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char
donatien 0:2ccb9960a044 568 {
donatien 0:2ccb9960a044 569 WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
donatien 11:390362de8c3f 570 return HTTP_PARSE;
donatien 0:2ccb9960a044 571 }
donatien 0:2ccb9960a044 572 memcpy(scheme, schemePtr, hostPtr - schemePtr);
donatien 0:2ccb9960a044 573 scheme[hostPtr - schemePtr] = '\0';
donatien 0:2ccb9960a044 574
donatien 0:2ccb9960a044 575 hostPtr+=3;
donatien 0:2ccb9960a044 576
donatien 0:2ccb9960a044 577 size_t hostLen = 0;
donatien 0:2ccb9960a044 578
donatien 0:2ccb9960a044 579 char* portPtr = strchr(hostPtr, ':');
donatien 0:2ccb9960a044 580 if( portPtr != NULL )
donatien 0:2ccb9960a044 581 {
donatien 0:2ccb9960a044 582 hostLen = portPtr - hostPtr;
donatien 0:2ccb9960a044 583 portPtr++;
donatien 8:45c8da29a1cf 584 if( sscanf(portPtr, "%hu", port) != 1)
donatien 0:2ccb9960a044 585 {
donatien 0:2ccb9960a044 586 WARN("Could not find port");
donatien 11:390362de8c3f 587 return HTTP_PARSE;
donatien 0:2ccb9960a044 588 }
donatien 0:2ccb9960a044 589 }
donatien 0:2ccb9960a044 590 else
donatien 0:2ccb9960a044 591 {
donatien 0:2ccb9960a044 592 *port=0;
donatien 0:2ccb9960a044 593 }
donatien 0:2ccb9960a044 594 char* pathPtr = strchr(hostPtr, '/');
donatien 0:2ccb9960a044 595 if( hostLen == 0 )
donatien 0:2ccb9960a044 596 {
donatien 0:2ccb9960a044 597 hostLen = pathPtr - hostPtr;
donatien 0:2ccb9960a044 598 }
donatien 0:2ccb9960a044 599
donatien 0:2ccb9960a044 600 if( maxHostLen < hostLen + 1 ) //including NULL-terminating char
donatien 0:2ccb9960a044 601 {
donatien 0:2ccb9960a044 602 WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
donatien 11:390362de8c3f 603 return HTTP_PARSE;
donatien 0:2ccb9960a044 604 }
donatien 0:2ccb9960a044 605 memcpy(host, hostPtr, hostLen);
donatien 0:2ccb9960a044 606 host[hostLen] = '\0';
donatien 0:2ccb9960a044 607
donatien 0:2ccb9960a044 608 size_t pathLen;
donatien 0:2ccb9960a044 609 char* fragmentPtr = strchr(hostPtr, '#');
donatien 0:2ccb9960a044 610 if(fragmentPtr != NULL)
donatien 0:2ccb9960a044 611 {
donatien 0:2ccb9960a044 612 pathLen = fragmentPtr - pathPtr;
donatien 0:2ccb9960a044 613 }
donatien 0:2ccb9960a044 614 else
donatien 0:2ccb9960a044 615 {
donatien 0:2ccb9960a044 616 pathLen = strlen(pathPtr);
donatien 0:2ccb9960a044 617 }
donatien 0:2ccb9960a044 618
donatien 0:2ccb9960a044 619 if( maxPathLen < pathLen + 1 ) //including NULL-terminating char
donatien 0:2ccb9960a044 620 {
donatien 0:2ccb9960a044 621 WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
donatien 11:390362de8c3f 622 return HTTP_PARSE;
donatien 0:2ccb9960a044 623 }
donatien 0:2ccb9960a044 624 memcpy(path, pathPtr, pathLen);
donatien 0:2ccb9960a044 625 path[pathLen] = '\0';
donatien 0:2ccb9960a044 626
donatien 11:390362de8c3f 627 return HTTP_OK;
donatien 0:2ccb9960a044 628 }