HTTPClient for IEEE1888 (FIAP) Gateway
Dependents: Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW
Fork of HTTPClient by
HTTPClient.cpp@17:b3cc5bba2af4, 2013-02-17 (annotated)
- Committer:
- strysd
- Date:
- Sun Feb 17 09:59:36 2013 +0000
- Revision:
- 17:b3cc5bba2af4
- Parent:
- 16:1f743885e7de
marge changes from official HTTPClient repository
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:2ccb9960a044 | 1 | /* HTTPClient.cpp */ |
donatien | 10:e1351de84c16 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 10:e1351de84c16 | 3 | * |
donatien | 10:e1351de84c16 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 10:e1351de84c16 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 10:e1351de84c16 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 10:e1351de84c16 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 10:e1351de84c16 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 10:e1351de84c16 | 9 | * |
donatien | 10:e1351de84c16 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 10:e1351de84c16 | 11 | * substantial portions of the Software. |
donatien | 10:e1351de84c16 | 12 | * |
donatien | 10:e1351de84c16 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 10:e1351de84c16 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 10:e1351de84c16 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 10:e1351de84c16 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 10:e1351de84c16 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 10:e1351de84c16 | 18 | */ |
donatien | 0:2ccb9960a044 | 19 | |
donatien | 7:4e39864f7b15 | 20 | //Debug is disabled by default |
donatien | 16:1f743885e7de | 21 | #if 0 |
donatien | 12:89d09a6db00a | 22 | //Enable debug |
donatien | 11:390362de8c3f | 23 | #include <cstdio> |
donatien | 16:1f743885e7de | 24 | #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
donatien | 11:390362de8c3f | 25 | #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); |
donatien | 11:390362de8c3f | 26 | #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); |
donatien | 12:89d09a6db00a | 27 | |
donatien | 12:89d09a6db00a | 28 | #else |
donatien | 12:89d09a6db00a | 29 | //Disable debug |
donatien | 12:89d09a6db00a | 30 | #define DBG(x, ...) |
donatien | 12:89d09a6db00a | 31 | #define WARN(x, ...) |
donatien | 12:89d09a6db00a | 32 | #define ERR(x, ...) |
donatien | 12:89d09a6db00a | 33 | |
donatien | 7:4e39864f7b15 | 34 | #endif |
donatien | 0:2ccb9960a044 | 35 | |
donatien | 0:2ccb9960a044 | 36 | #define HTTP_PORT 80 |
donatien | 0:2ccb9960a044 | 37 | |
donatien | 11:390362de8c3f | 38 | #define OK 0 |
donatien | 11:390362de8c3f | 39 | |
donatien | 11:390362de8c3f | 40 | #define MIN(x,y) (((x)<(y))?(x):(y)) |
donatien | 11:390362de8c3f | 41 | #define MAX(x,y) (((x)>(y))?(x):(y)) |
donatien | 11:390362de8c3f | 42 | |
donatien | 0:2ccb9960a044 | 43 | #define CHUNK_SIZE 256 |
donatien | 0:2ccb9960a044 | 44 | |
donatien | 0:2ccb9960a044 | 45 | #include <cstring> |
donatien | 0:2ccb9960a044 | 46 | |
donatien | 11:390362de8c3f | 47 | #include "HTTPClient.h" |
donatien | 11:390362de8c3f | 48 | |
donatien | 0:2ccb9960a044 | 49 | HTTPClient::HTTPClient() : |
donatien | 7:4e39864f7b15 | 50 | m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0) |
donatien | 0:2ccb9960a044 | 51 | { |
donatien | 0:2ccb9960a044 | 52 | |
donatien | 0:2ccb9960a044 | 53 | } |
donatien | 0:2ccb9960a044 | 54 | |
donatien | 0:2ccb9960a044 | 55 | HTTPClient::~HTTPClient() |
donatien | 0:2ccb9960a044 | 56 | { |
donatien | 0:2ccb9960a044 | 57 | |
donatien | 0:2ccb9960a044 | 58 | } |
donatien | 0:2ccb9960a044 | 59 | |
donatien | 0:2ccb9960a044 | 60 | #if 0 |
donatien | 0:2ccb9960a044 | 61 | void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification |
donatien | 0:2ccb9960a044 | 62 | { |
donatien | 0:2ccb9960a044 | 63 | m_basicAuthUser = user; |
donatien | 0:2ccb9960a044 | 64 | m_basicAuthPassword = password; |
donatien | 0:2ccb9960a044 | 65 | } |
donatien | 0:2ccb9960a044 | 66 | #endif |
donatien | 0:2ccb9960a044 | 67 | |
donatien | 12:89d09a6db00a | 68 | HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 69 | { |
donatien | 0:2ccb9960a044 | 70 | return connect(url, HTTP_GET, NULL, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 71 | } |
donatien | 0:2ccb9960a044 | 72 | |
donatien | 12:89d09a6db00a | 73 | HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 74 | { |
donatien | 0:2ccb9960a044 | 75 | HTTPText str(result, maxResultLen); |
donatien | 0:2ccb9960a044 | 76 | return get(url, &str, timeout); |
donatien | 0:2ccb9960a044 | 77 | } |
donatien | 0:2ccb9960a044 | 78 | |
donatien | 12:89d09a6db00a | 79 | HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 80 | { |
donatien | 0:2ccb9960a044 | 81 | return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 82 | } |
donatien | 0:2ccb9960a044 | 83 | |
strysd | 17:b3cc5bba2af4 | 84 | //strysd add start |
strysd | 17:b3cc5bba2af4 | 85 | HTTPResult HTTPClient::postXML(const char* url,const char* SOAPAction, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
strysd | 17:b3cc5bba2af4 | 86 | { |
strysd | 17:b3cc5bba2af4 | 87 | return connectExt(url, HTTP_POST, SOAPAction, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
strysd | 17:b3cc5bba2af4 | 88 | } |
strysd | 17:b3cc5bba2af4 | 89 | //strysd add end |
strysd | 17:b3cc5bba2af4 | 90 | |
donatien | 16:1f743885e7de | 91 | HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 92 | { |
donatien | 16:1f743885e7de | 93 | return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 16:1f743885e7de | 94 | } |
donatien | 16:1f743885e7de | 95 | |
donatien | 16:1f743885e7de | 96 | HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 97 | { |
donatien | 16:1f743885e7de | 98 | return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); |
donatien | 16:1f743885e7de | 99 | } |
donatien | 16:1f743885e7de | 100 | |
donatien | 16:1f743885e7de | 101 | |
donatien | 0:2ccb9960a044 | 102 | int HTTPClient::getHTTPResponseCode() |
donatien | 0:2ccb9960a044 | 103 | { |
donatien | 0:2ccb9960a044 | 104 | return m_httpResponseCode; |
donatien | 0:2ccb9960a044 | 105 | } |
donatien | 0:2ccb9960a044 | 106 | |
donatien | 5:791fc3dcb6c4 | 107 | #define CHECK_CONN_ERR(ret) \ |
donatien | 5:791fc3dcb6c4 | 108 | do{ \ |
donatien | 7:4e39864f7b15 | 109 | if(ret) { \ |
donatien | 7:4e39864f7b15 | 110 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 111 | ERR("Connection error (%d)", ret); \ |
donatien | 11:390362de8c3f | 112 | return HTTP_CONN; \ |
donatien | 5:791fc3dcb6c4 | 113 | } \ |
donatien | 5:791fc3dcb6c4 | 114 | } while(0) |
donatien | 5:791fc3dcb6c4 | 115 | |
donatien | 5:791fc3dcb6c4 | 116 | #define PRTCL_ERR() \ |
donatien | 5:791fc3dcb6c4 | 117 | do{ \ |
donatien | 7:4e39864f7b15 | 118 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 119 | ERR("Protocol error"); \ |
donatien | 11:390362de8c3f | 120 | return HTTP_PRTCL; \ |
donatien | 5:791fc3dcb6c4 | 121 | } while(0) |
donatien | 0:2ccb9960a044 | 122 | |
donatien | 12:89d09a6db00a | 123 | HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request |
donatien | 12:89d09a6db00a | 124 | { |
donatien | 0:2ccb9960a044 | 125 | m_httpResponseCode = 0; //Invalidate code |
donatien | 0:2ccb9960a044 | 126 | m_timeout = timeout; |
donatien | 16:1f743885e7de | 127 | |
donatien | 16:1f743885e7de | 128 | pDataIn->writeReset(); |
donatien | 16:1f743885e7de | 129 | if( pDataOut ) |
donatien | 16:1f743885e7de | 130 | { |
donatien | 16:1f743885e7de | 131 | pDataOut->readReset(); |
donatien | 16:1f743885e7de | 132 | } |
donatien | 0:2ccb9960a044 | 133 | |
donatien | 0:2ccb9960a044 | 134 | char scheme[8]; |
donatien | 0:2ccb9960a044 | 135 | uint16_t port; |
strysd | 17:b3cc5bba2af4 | 136 | char host[64];//strysd change from 32 |
donatien | 0:2ccb9960a044 | 137 | char path[64]; |
donatien | 0:2ccb9960a044 | 138 | //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) |
donatien | 11:390362de8c3f | 139 | HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); |
donatien | 11:390362de8c3f | 140 | if(res != HTTP_OK) |
donatien | 0:2ccb9960a044 | 141 | { |
donatien | 11:390362de8c3f | 142 | ERR("parseURL returned %d", res); |
donatien | 11:390362de8c3f | 143 | return res; |
donatien | 0:2ccb9960a044 | 144 | } |
donatien | 0:2ccb9960a044 | 145 | |
donatien | 0:2ccb9960a044 | 146 | if(port == 0) //TODO do handle HTTPS->443 |
donatien | 0:2ccb9960a044 | 147 | { |
donatien | 0:2ccb9960a044 | 148 | port = 80; |
donatien | 0:2ccb9960a044 | 149 | } |
donatien | 0:2ccb9960a044 | 150 | |
donatien | 0:2ccb9960a044 | 151 | DBG("Scheme: %s", scheme); |
donatien | 0:2ccb9960a044 | 152 | DBG("Host: %s", host); |
donatien | 0:2ccb9960a044 | 153 | DBG("Port: %d", port); |
donatien | 0:2ccb9960a044 | 154 | DBG("Path: %s", path); |
donatien | 0:2ccb9960a044 | 155 | |
donatien | 7:4e39864f7b15 | 156 | //Connect |
donatien | 7:4e39864f7b15 | 157 | DBG("Connecting socket to server"); |
donatien | 11:390362de8c3f | 158 | int ret = m_sock.connect(host, port); |
donatien | 0:2ccb9960a044 | 159 | if (ret < 0) |
donatien | 0:2ccb9960a044 | 160 | { |
donatien | 7:4e39864f7b15 | 161 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 162 | ERR("Could not connect"); |
donatien | 11:390362de8c3f | 163 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 164 | } |
donatien | 0:2ccb9960a044 | 165 | |
donatien | 0:2ccb9960a044 | 166 | //Send request |
donatien | 0:2ccb9960a044 | 167 | DBG("Sending request"); |
donatien | 9:ff30cc189191 | 168 | char buf[CHUNK_SIZE]; |
donatien | 16:1f743885e7de | 169 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
donatien | 9:ff30cc189191 | 170 | snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request |
donatien | 9:ff30cc189191 | 171 | ret = send(buf); |
donatien | 0:2ccb9960a044 | 172 | if(ret) |
donatien | 0:2ccb9960a044 | 173 | { |
donatien | 7:4e39864f7b15 | 174 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 175 | ERR("Could not write request"); |
donatien | 11:390362de8c3f | 176 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 177 | } |
donatien | 0:2ccb9960a044 | 178 | |
donatien | 0:2ccb9960a044 | 179 | //Send all headers |
donatien | 0:2ccb9960a044 | 180 | |
donatien | 0:2ccb9960a044 | 181 | //Send default headers |
donatien | 0:2ccb9960a044 | 182 | DBG("Sending headers"); |
donatien | 16:1f743885e7de | 183 | if( pDataOut != NULL ) |
donatien | 0:2ccb9960a044 | 184 | { |
donatien | 0:2ccb9960a044 | 185 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 186 | { |
donatien | 0:2ccb9960a044 | 187 | ret = send("Transfer-Encoding: chunked\r\n"); |
donatien | 5:791fc3dcb6c4 | 188 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 189 | } |
donatien | 0:2ccb9960a044 | 190 | else |
donatien | 0:2ccb9960a044 | 191 | { |
donatien | 9:ff30cc189191 | 192 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
donatien | 9:ff30cc189191 | 193 | ret = send(buf); |
donatien | 5:791fc3dcb6c4 | 194 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 195 | } |
donatien | 0:2ccb9960a044 | 196 | char type[48]; |
donatien | 11:390362de8c3f | 197 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) |
donatien | 0:2ccb9960a044 | 198 | { |
donatien | 9:ff30cc189191 | 199 | snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
donatien | 9:ff30cc189191 | 200 | ret = send(buf); |
donatien | 5:791fc3dcb6c4 | 201 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 202 | } |
donatien | 0:2ccb9960a044 | 203 | } |
donatien | 9:ff30cc189191 | 204 | |
donatien | 0:2ccb9960a044 | 205 | //Close headers |
donatien | 0:2ccb9960a044 | 206 | DBG("Headers sent"); |
donatien | 0:2ccb9960a044 | 207 | ret = send("\r\n"); |
donatien | 5:791fc3dcb6c4 | 208 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 209 | |
donatien | 4:c071b05ac026 | 210 | size_t trfLen; |
donatien | 9:ff30cc189191 | 211 | |
donatien | 16:1f743885e7de | 212 | //Send data (if available) |
donatien | 16:1f743885e7de | 213 | if( pDataOut != NULL ) |
donatien | 0:2ccb9960a044 | 214 | { |
donatien | 0:2ccb9960a044 | 215 | DBG("Sending data"); |
donatien | 0:2ccb9960a044 | 216 | while(true) |
donatien | 0:2ccb9960a044 | 217 | { |
donatien | 0:2ccb9960a044 | 218 | size_t writtenLen = 0; |
donatien | 0:2ccb9960a044 | 219 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
donatien | 0:2ccb9960a044 | 220 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 221 | { |
donatien | 0:2ccb9960a044 | 222 | //Write chunk header |
donatien | 9:ff30cc189191 | 223 | char chunkHeader[16]; |
donatien | 9:ff30cc189191 | 224 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
donatien | 9:ff30cc189191 | 225 | ret = send(chunkHeader); |
donatien | 5:791fc3dcb6c4 | 226 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 227 | } |
donatien | 0:2ccb9960a044 | 228 | else if( trfLen == 0 ) |
donatien | 0:2ccb9960a044 | 229 | { |
donatien | 0:2ccb9960a044 | 230 | break; |
donatien | 0:2ccb9960a044 | 231 | } |
donatien | 0:2ccb9960a044 | 232 | if( trfLen != 0 ) |
donatien | 0:2ccb9960a044 | 233 | { |
donatien | 0:2ccb9960a044 | 234 | ret = send(buf, trfLen); |
donatien | 5:791fc3dcb6c4 | 235 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 236 | } |
donatien | 0:2ccb9960a044 | 237 | |
donatien | 0:2ccb9960a044 | 238 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 239 | { |
donatien | 0:2ccb9960a044 | 240 | ret = send("\r\n"); //Chunk-terminating CRLF |
donatien | 5:791fc3dcb6c4 | 241 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 242 | } |
donatien | 0:2ccb9960a044 | 243 | else |
donatien | 0:2ccb9960a044 | 244 | { |
donatien | 0:2ccb9960a044 | 245 | writtenLen += trfLen; |
donatien | 0:2ccb9960a044 | 246 | if( writtenLen >= pDataOut->getDataLen() ) |
donatien | 0:2ccb9960a044 | 247 | { |
donatien | 0:2ccb9960a044 | 248 | break; |
donatien | 0:2ccb9960a044 | 249 | } |
donatien | 0:2ccb9960a044 | 250 | } |
donatien | 0:2ccb9960a044 | 251 | |
donatien | 0:2ccb9960a044 | 252 | if( trfLen == 0 ) |
donatien | 0:2ccb9960a044 | 253 | { |
donatien | 0:2ccb9960a044 | 254 | break; |
donatien | 0:2ccb9960a044 | 255 | } |
donatien | 0:2ccb9960a044 | 256 | } |
donatien | 0:2ccb9960a044 | 257 | |
donatien | 0:2ccb9960a044 | 258 | } |
donatien | 9:ff30cc189191 | 259 | |
donatien | 0:2ccb9960a044 | 260 | //Receive response |
donatien | 0:2ccb9960a044 | 261 | DBG("Receiving response"); |
donatien | 4:c071b05ac026 | 262 | ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
donatien | 5:791fc3dcb6c4 | 263 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 264 | |
donatien | 0:2ccb9960a044 | 265 | buf[trfLen] = '\0'; |
donatien | 0:2ccb9960a044 | 266 | |
donatien | 0:2ccb9960a044 | 267 | char* crlfPtr = strstr(buf, "\r\n"); |
donatien | 0:2ccb9960a044 | 268 | if(crlfPtr == NULL) |
donatien | 0:2ccb9960a044 | 269 | { |
donatien | 5:791fc3dcb6c4 | 270 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 271 | } |
donatien | 0:2ccb9960a044 | 272 | |
donatien | 4:c071b05ac026 | 273 | int crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 274 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 275 | |
donatien | 0:2ccb9960a044 | 276 | //Parse HTTP response |
donatien | 0:2ccb9960a044 | 277 | if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) |
donatien | 0:2ccb9960a044 | 278 | { |
donatien | 0:2ccb9960a044 | 279 | //Cannot match string, error |
donatien | 0:2ccb9960a044 | 280 | ERR("Not a correct HTTP answer : %s\n", buf); |
donatien | 5:791fc3dcb6c4 | 281 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 282 | } |
donatien | 0:2ccb9960a044 | 283 | |
donatien | 16:1f743885e7de | 284 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) |
donatien | 0:2ccb9960a044 | 285 | { |
donatien | 16:1f743885e7de | 286 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
donatien | 0:2ccb9960a044 | 287 | WARN("Response code %d", m_httpResponseCode); |
donatien | 5:791fc3dcb6c4 | 288 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 289 | } |
donatien | 0:2ccb9960a044 | 290 | |
donatien | 0:2ccb9960a044 | 291 | DBG("Reading headers"); |
donatien | 0:2ccb9960a044 | 292 | |
donatien | 4:c071b05ac026 | 293 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 294 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 295 | |
donatien | 4:c071b05ac026 | 296 | size_t recvContentLength = 0; |
donatien | 4:c071b05ac026 | 297 | bool recvChunked = false; |
donatien | 0:2ccb9960a044 | 298 | //Now get headers |
donatien | 0:2ccb9960a044 | 299 | while( true ) |
donatien | 0:2ccb9960a044 | 300 | { |
donatien | 0:2ccb9960a044 | 301 | crlfPtr = strstr(buf, "\r\n"); |
donatien | 0:2ccb9960a044 | 302 | if(crlfPtr == NULL) |
donatien | 0:2ccb9960a044 | 303 | { |
donatien | 4:c071b05ac026 | 304 | if( trfLen < CHUNK_SIZE - 1 ) |
donatien | 0:2ccb9960a044 | 305 | { |
donatien | 0:2ccb9960a044 | 306 | size_t newTrfLen; |
donatien | 0:2ccb9960a044 | 307 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 0:2ccb9960a044 | 308 | trfLen += newTrfLen; |
donatien | 0:2ccb9960a044 | 309 | buf[trfLen] = '\0'; |
donatien | 4:c071b05ac026 | 310 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
donatien | 5:791fc3dcb6c4 | 311 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 312 | continue; |
donatien | 0:2ccb9960a044 | 313 | } |
donatien | 0:2ccb9960a044 | 314 | else |
donatien | 0:2ccb9960a044 | 315 | { |
donatien | 5:791fc3dcb6c4 | 316 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 317 | } |
donatien | 0:2ccb9960a044 | 318 | } |
donatien | 0:2ccb9960a044 | 319 | |
donatien | 0:2ccb9960a044 | 320 | crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 321 | |
donatien | 0:2ccb9960a044 | 322 | if(crlfPos == 0) //End of headers |
donatien | 0:2ccb9960a044 | 323 | { |
donatien | 0:2ccb9960a044 | 324 | DBG("Headers read"); |
donatien | 4:c071b05ac026 | 325 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 326 | trfLen -= 2; |
donatien | 0:2ccb9960a044 | 327 | break; |
donatien | 0:2ccb9960a044 | 328 | } |
donatien | 0:2ccb9960a044 | 329 | |
donatien | 0:2ccb9960a044 | 330 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 331 | |
donatien | 4:c071b05ac026 | 332 | char key[32]; |
donatien | 4:c071b05ac026 | 333 | char value[32]; |
donatien | 0:2ccb9960a044 | 334 | |
donatien | 4:c071b05ac026 | 335 | key[31] = '\0'; |
donatien | 4:c071b05ac026 | 336 | value[31] = '\0'; |
donatien | 4:c071b05ac026 | 337 | |
donatien | 4:c071b05ac026 | 338 | int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); |
donatien | 0:2ccb9960a044 | 339 | if ( n == 2 ) |
donatien | 0:2ccb9960a044 | 340 | { |
donatien | 0:2ccb9960a044 | 341 | DBG("Read header : %s: %s\n", key, value); |
donatien | 0:2ccb9960a044 | 342 | if( !strcmp(key, "Content-Length") ) |
donatien | 0:2ccb9960a044 | 343 | { |
donatien | 0:2ccb9960a044 | 344 | sscanf(value, "%d", &recvContentLength); |
donatien | 0:2ccb9960a044 | 345 | pDataIn->setDataLen(recvContentLength); |
donatien | 0:2ccb9960a044 | 346 | } |
donatien | 0:2ccb9960a044 | 347 | else if( !strcmp(key, "Transfer-Encoding") ) |
donatien | 0:2ccb9960a044 | 348 | { |
donatien | 0:2ccb9960a044 | 349 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) |
donatien | 0:2ccb9960a044 | 350 | { |
donatien | 0:2ccb9960a044 | 351 | recvChunked = true; |
donatien | 0:2ccb9960a044 | 352 | pDataIn->setIsChunked(true); |
donatien | 0:2ccb9960a044 | 353 | } |
donatien | 0:2ccb9960a044 | 354 | } |
donatien | 0:2ccb9960a044 | 355 | else if( !strcmp(key, "Content-Type") ) |
donatien | 0:2ccb9960a044 | 356 | { |
donatien | 0:2ccb9960a044 | 357 | pDataIn->setDataType(value); |
donatien | 0:2ccb9960a044 | 358 | } |
donatien | 0:2ccb9960a044 | 359 | |
donatien | 4:c071b05ac026 | 360 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 361 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 362 | |
donatien | 0:2ccb9960a044 | 363 | } |
donatien | 0:2ccb9960a044 | 364 | else |
donatien | 0:2ccb9960a044 | 365 | { |
donatien | 0:2ccb9960a044 | 366 | ERR("Could not parse header"); |
donatien | 5:791fc3dcb6c4 | 367 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 368 | } |
donatien | 0:2ccb9960a044 | 369 | |
donatien | 0:2ccb9960a044 | 370 | } |
donatien | 0:2ccb9960a044 | 371 | |
donatien | 0:2ccb9960a044 | 372 | //Receive data |
donatien | 0:2ccb9960a044 | 373 | DBG("Receiving data"); |
donatien | 0:2ccb9960a044 | 374 | while(true) |
donatien | 0:2ccb9960a044 | 375 | { |
donatien | 0:2ccb9960a044 | 376 | size_t readLen = 0; |
donatien | 0:2ccb9960a044 | 377 | |
donatien | 0:2ccb9960a044 | 378 | if( recvChunked ) |
donatien | 0:2ccb9960a044 | 379 | { |
donatien | 0:2ccb9960a044 | 380 | //Read chunk header |
donatien | 14:2744e0c0e527 | 381 | bool foundCrlf; |
donatien | 14:2744e0c0e527 | 382 | do |
donatien | 0:2ccb9960a044 | 383 | { |
donatien | 14:2744e0c0e527 | 384 | foundCrlf = false; |
donatien | 14:2744e0c0e527 | 385 | crlfPos=0; |
donatien | 14:2744e0c0e527 | 386 | buf[trfLen]=0; |
donatien | 14:2744e0c0e527 | 387 | if(trfLen >= 2) |
donatien | 0:2ccb9960a044 | 388 | { |
donatien | 14:2744e0c0e527 | 389 | for(; crlfPos < trfLen - 2; crlfPos++) |
donatien | 14:2744e0c0e527 | 390 | { |
donatien | 14:2744e0c0e527 | 391 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) |
donatien | 14:2744e0c0e527 | 392 | { |
donatien | 14:2744e0c0e527 | 393 | foundCrlf = true; |
donatien | 14:2744e0c0e527 | 394 | break; |
donatien | 14:2744e0c0e527 | 395 | } |
donatien | 14:2744e0c0e527 | 396 | } |
donatien | 0:2ccb9960a044 | 397 | } |
donatien | 14:2744e0c0e527 | 398 | if(!foundCrlf) //Try to read more |
donatien | 0:2ccb9960a044 | 399 | { |
donatien | 14:2744e0c0e527 | 400 | if( trfLen < CHUNK_SIZE ) |
donatien | 14:2744e0c0e527 | 401 | { |
donatien | 14:2744e0c0e527 | 402 | size_t newTrfLen; |
donatien | 14:2744e0c0e527 | 403 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 14:2744e0c0e527 | 404 | trfLen += newTrfLen; |
donatien | 14:2744e0c0e527 | 405 | CHECK_CONN_ERR(ret); |
donatien | 14:2744e0c0e527 | 406 | continue; |
donatien | 14:2744e0c0e527 | 407 | } |
donatien | 14:2744e0c0e527 | 408 | else |
donatien | 14:2744e0c0e527 | 409 | { |
donatien | 14:2744e0c0e527 | 410 | PRTCL_ERR(); |
donatien | 14:2744e0c0e527 | 411 | } |
donatien | 0:2ccb9960a044 | 412 | } |
donatien | 14:2744e0c0e527 | 413 | } while(!foundCrlf); |
donatien | 0:2ccb9960a044 | 414 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 415 | int n = sscanf(buf, "%x", &readLen); |
donatien | 0:2ccb9960a044 | 416 | if(n!=1) |
donatien | 0:2ccb9960a044 | 417 | { |
donatien | 15:5ad07f90e895 | 418 | ERR("Could not read chunk length"); |
donatien | 5:791fc3dcb6c4 | 419 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 420 | } |
donatien | 0:2ccb9960a044 | 421 | |
donatien | 4:c071b05ac026 | 422 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
donatien | 0:2ccb9960a044 | 423 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 424 | |
donatien | 0:2ccb9960a044 | 425 | if( readLen == 0 ) |
donatien | 0:2ccb9960a044 | 426 | { |
donatien | 0:2ccb9960a044 | 427 | //Last chunk |
donatien | 0:2ccb9960a044 | 428 | break; |
donatien | 0:2ccb9960a044 | 429 | } |
donatien | 0:2ccb9960a044 | 430 | } |
donatien | 0:2ccb9960a044 | 431 | else |
donatien | 0:2ccb9960a044 | 432 | { |
donatien | 0:2ccb9960a044 | 433 | readLen = recvContentLength; |
donatien | 0:2ccb9960a044 | 434 | } |
donatien | 0:2ccb9960a044 | 435 | |
donatien | 0:2ccb9960a044 | 436 | DBG("Retrieving %d bytes", readLen); |
donatien | 0:2ccb9960a044 | 437 | |
donatien | 0:2ccb9960a044 | 438 | do |
donatien | 0:2ccb9960a044 | 439 | { |
donatien | 0:2ccb9960a044 | 440 | pDataIn->write(buf, MIN(trfLen, readLen)); |
donatien | 0:2ccb9960a044 | 441 | if( trfLen > readLen ) |
donatien | 0:2ccb9960a044 | 442 | { |
donatien | 0:2ccb9960a044 | 443 | memmove(buf, &buf[readLen], trfLen - readLen); |
donatien | 0:2ccb9960a044 | 444 | trfLen -= readLen; |
donatien | 0:2ccb9960a044 | 445 | readLen = 0; |
donatien | 0:2ccb9960a044 | 446 | } |
donatien | 0:2ccb9960a044 | 447 | else |
donatien | 0:2ccb9960a044 | 448 | { |
donatien | 0:2ccb9960a044 | 449 | readLen -= trfLen; |
donatien | 0:2ccb9960a044 | 450 | } |
donatien | 0:2ccb9960a044 | 451 | |
donatien | 0:2ccb9960a044 | 452 | if(readLen) |
donatien | 0:2ccb9960a044 | 453 | { |
donatien | 0:2ccb9960a044 | 454 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
donatien | 5:791fc3dcb6c4 | 455 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 456 | } |
donatien | 0:2ccb9960a044 | 457 | } while(readLen); |
donatien | 0:2ccb9960a044 | 458 | |
donatien | 0:2ccb9960a044 | 459 | if( recvChunked ) |
donatien | 0:2ccb9960a044 | 460 | { |
donatien | 0:2ccb9960a044 | 461 | if(trfLen < 2) |
donatien | 0:2ccb9960a044 | 462 | { |
donatien | 0:2ccb9960a044 | 463 | size_t newTrfLen; |
donatien | 0:2ccb9960a044 | 464 | //Read missing chars to find end of chunk |
donatien | 14:2744e0c0e527 | 465 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 5:791fc3dcb6c4 | 466 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 467 | trfLen += newTrfLen; |
donatien | 0:2ccb9960a044 | 468 | } |
donatien | 0:2ccb9960a044 | 469 | if( (buf[0] != '\r') || (buf[1] != '\n') ) |
donatien | 0:2ccb9960a044 | 470 | { |
donatien | 0:2ccb9960a044 | 471 | ERR("Format error"); |
donatien | 5:791fc3dcb6c4 | 472 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 473 | } |
donatien | 0:2ccb9960a044 | 474 | memmove(buf, &buf[2], trfLen - 2); |
donatien | 0:2ccb9960a044 | 475 | trfLen -= 2; |
donatien | 0:2ccb9960a044 | 476 | } |
donatien | 0:2ccb9960a044 | 477 | else |
donatien | 0:2ccb9960a044 | 478 | { |
donatien | 0:2ccb9960a044 | 479 | break; |
donatien | 0:2ccb9960a044 | 480 | } |
donatien | 0:2ccb9960a044 | 481 | |
donatien | 0:2ccb9960a044 | 482 | } |
donatien | 0:2ccb9960a044 | 483 | |
donatien | 7:4e39864f7b15 | 484 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 485 | DBG("Completed HTTP transaction"); |
donatien | 0:2ccb9960a044 | 486 | |
donatien | 11:390362de8c3f | 487 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 488 | } |
donatien | 0:2ccb9960a044 | 489 | |
strysd | 17:b3cc5bba2af4 | 490 | //strysd add this function start |
strysd | 17:b3cc5bba2af4 | 491 | //mainly inherit from connect(), but some code is different, see programer name, "strysd" |
strysd | 17:b3cc5bba2af4 | 492 | HTTPResult HTTPClient::connectExt(const char* url, HTTP_METH method, const char* SOAPAction, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request |
strysd | 17:b3cc5bba2af4 | 493 | { |
strysd | 17:b3cc5bba2af4 | 494 | m_httpResponseCode = 0; //Invalidate code |
strysd | 17:b3cc5bba2af4 | 495 | m_timeout = timeout; |
strysd | 17:b3cc5bba2af4 | 496 | |
strysd | 17:b3cc5bba2af4 | 497 | pDataIn->writeReset(); |
strysd | 17:b3cc5bba2af4 | 498 | if( pDataOut ) |
strysd | 17:b3cc5bba2af4 | 499 | { |
strysd | 17:b3cc5bba2af4 | 500 | pDataOut->readReset(); |
strysd | 17:b3cc5bba2af4 | 501 | } |
strysd | 17:b3cc5bba2af4 | 502 | |
strysd | 17:b3cc5bba2af4 | 503 | char scheme[8]; |
strysd | 17:b3cc5bba2af4 | 504 | uint16_t port; |
strysd | 17:b3cc5bba2af4 | 505 | char host[64];//strysd change from 32 |
strysd | 17:b3cc5bba2af4 | 506 | char path[64]; |
strysd | 17:b3cc5bba2af4 | 507 | //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) |
strysd | 17:b3cc5bba2af4 | 508 | HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); |
strysd | 17:b3cc5bba2af4 | 509 | if(res != HTTP_OK) |
strysd | 17:b3cc5bba2af4 | 510 | { |
strysd | 17:b3cc5bba2af4 | 511 | ERR("parseURL returned %d", res); |
strysd | 17:b3cc5bba2af4 | 512 | return res; |
strysd | 17:b3cc5bba2af4 | 513 | } |
strysd | 17:b3cc5bba2af4 | 514 | |
strysd | 17:b3cc5bba2af4 | 515 | if(port == 0) //TODO do handle HTTPS->443 |
strysd | 17:b3cc5bba2af4 | 516 | { |
strysd | 17:b3cc5bba2af4 | 517 | port = 80; |
strysd | 17:b3cc5bba2af4 | 518 | } |
strysd | 17:b3cc5bba2af4 | 519 | |
strysd | 17:b3cc5bba2af4 | 520 | DBG("Scheme: %s", scheme); |
strysd | 17:b3cc5bba2af4 | 521 | DBG("Host: %s", host); |
strysd | 17:b3cc5bba2af4 | 522 | DBG("Port: %d", port); |
strysd | 17:b3cc5bba2af4 | 523 | DBG("Path: %s", path); |
strysd | 17:b3cc5bba2af4 | 524 | |
strysd | 17:b3cc5bba2af4 | 525 | //Connect |
strysd | 17:b3cc5bba2af4 | 526 | DBG("Connecting socket to server"); |
strysd | 17:b3cc5bba2af4 | 527 | int ret = m_sock.connect(host, port); |
strysd | 17:b3cc5bba2af4 | 528 | if (ret < 0) |
strysd | 17:b3cc5bba2af4 | 529 | { |
strysd | 17:b3cc5bba2af4 | 530 | m_sock.close(); |
strysd | 17:b3cc5bba2af4 | 531 | ERR("Could not connect"); |
strysd | 17:b3cc5bba2af4 | 532 | return HTTP_CONN; |
strysd | 17:b3cc5bba2af4 | 533 | } |
strysd | 17:b3cc5bba2af4 | 534 | |
strysd | 17:b3cc5bba2af4 | 535 | //Send request |
strysd | 17:b3cc5bba2af4 | 536 | DBG("Sending request"); |
strysd | 17:b3cc5bba2af4 | 537 | char buf[CHUNK_SIZE]; |
strysd | 17:b3cc5bba2af4 | 538 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
strysd | 17:b3cc5bba2af4 | 539 | snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request |
strysd | 17:b3cc5bba2af4 | 540 | ret = send(buf); |
strysd | 17:b3cc5bba2af4 | 541 | if(ret) |
strysd | 17:b3cc5bba2af4 | 542 | { |
strysd | 17:b3cc5bba2af4 | 543 | m_sock.close(); |
strysd | 17:b3cc5bba2af4 | 544 | ERR("Could not write request"); |
strysd | 17:b3cc5bba2af4 | 545 | return HTTP_CONN; |
strysd | 17:b3cc5bba2af4 | 546 | } |
strysd | 17:b3cc5bba2af4 | 547 | |
strysd | 17:b3cc5bba2af4 | 548 | //Send all headers |
strysd | 17:b3cc5bba2af4 | 549 | |
strysd | 17:b3cc5bba2af4 | 550 | //Send default headers |
strysd | 17:b3cc5bba2af4 | 551 | DBG("Sending headers"); |
strysd | 17:b3cc5bba2af4 | 552 | if( pDataOut != NULL ) |
strysd | 17:b3cc5bba2af4 | 553 | { |
strysd | 17:b3cc5bba2af4 | 554 | if( pDataOut->getIsChunked() ) |
strysd | 17:b3cc5bba2af4 | 555 | { |
strysd | 17:b3cc5bba2af4 | 556 | ret = send("Transfer-Encoding: chunked\r\n"); |
strysd | 17:b3cc5bba2af4 | 557 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 558 | } |
strysd | 17:b3cc5bba2af4 | 559 | else |
strysd | 17:b3cc5bba2af4 | 560 | { |
strysd | 17:b3cc5bba2af4 | 561 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
strysd | 17:b3cc5bba2af4 | 562 | ret = send(buf); |
strysd | 17:b3cc5bba2af4 | 563 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 564 | } |
strysd | 17:b3cc5bba2af4 | 565 | char type[48]; |
strysd | 17:b3cc5bba2af4 | 566 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) |
strysd | 17:b3cc5bba2af4 | 567 | { |
strysd | 17:b3cc5bba2af4 | 568 | //strysd change next line |
strysd | 17:b3cc5bba2af4 | 569 | //snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
strysd | 17:b3cc5bba2af4 | 570 | snprintf(buf, sizeof(buf), "Content-Type: text/xml;charset=UTF-8\r\n"); |
strysd | 17:b3cc5bba2af4 | 571 | |
strysd | 17:b3cc5bba2af4 | 572 | ret = send(buf); |
strysd | 17:b3cc5bba2af4 | 573 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 574 | //strysd add start |
strysd | 17:b3cc5bba2af4 | 575 | snprintf(buf, sizeof(buf), "SOAPAction: \"%s\"\r\n", type); |
strysd | 17:b3cc5bba2af4 | 576 | ret = send(buf); |
strysd | 17:b3cc5bba2af4 | 577 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 578 | //strysd add end |
strysd | 17:b3cc5bba2af4 | 579 | } |
strysd | 17:b3cc5bba2af4 | 580 | } |
strysd | 17:b3cc5bba2af4 | 581 | |
strysd | 17:b3cc5bba2af4 | 582 | //Close headers |
strysd | 17:b3cc5bba2af4 | 583 | DBG("Headers sent"); |
strysd | 17:b3cc5bba2af4 | 584 | ret = send("\r\n"); |
strysd | 17:b3cc5bba2af4 | 585 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 586 | |
strysd | 17:b3cc5bba2af4 | 587 | size_t trfLen; |
strysd | 17:b3cc5bba2af4 | 588 | |
strysd | 17:b3cc5bba2af4 | 589 | //Send data (if available) |
strysd | 17:b3cc5bba2af4 | 590 | if( pDataOut != NULL ) |
strysd | 17:b3cc5bba2af4 | 591 | { |
strysd | 17:b3cc5bba2af4 | 592 | DBG("Sending data"); |
strysd | 17:b3cc5bba2af4 | 593 | while(true) |
strysd | 17:b3cc5bba2af4 | 594 | { |
strysd | 17:b3cc5bba2af4 | 595 | size_t writtenLen = 0; |
strysd | 17:b3cc5bba2af4 | 596 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
strysd | 17:b3cc5bba2af4 | 597 | if( pDataOut->getIsChunked() ) |
strysd | 17:b3cc5bba2af4 | 598 | { |
strysd | 17:b3cc5bba2af4 | 599 | //Write chunk header |
strysd | 17:b3cc5bba2af4 | 600 | char chunkHeader[16]; |
strysd | 17:b3cc5bba2af4 | 601 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
strysd | 17:b3cc5bba2af4 | 602 | ret = send(chunkHeader); |
strysd | 17:b3cc5bba2af4 | 603 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 604 | } |
strysd | 17:b3cc5bba2af4 | 605 | else if( trfLen == 0 ) |
strysd | 17:b3cc5bba2af4 | 606 | { |
strysd | 17:b3cc5bba2af4 | 607 | break; |
strysd | 17:b3cc5bba2af4 | 608 | } |
strysd | 17:b3cc5bba2af4 | 609 | if( trfLen != 0 ) |
strysd | 17:b3cc5bba2af4 | 610 | { |
strysd | 17:b3cc5bba2af4 | 611 | ret = send(buf, trfLen); |
strysd | 17:b3cc5bba2af4 | 612 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 613 | } |
strysd | 17:b3cc5bba2af4 | 614 | |
strysd | 17:b3cc5bba2af4 | 615 | if( pDataOut->getIsChunked() ) |
strysd | 17:b3cc5bba2af4 | 616 | { |
strysd | 17:b3cc5bba2af4 | 617 | ret = send("\r\n"); //Chunk-terminating CRLF |
strysd | 17:b3cc5bba2af4 | 618 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 619 | } |
strysd | 17:b3cc5bba2af4 | 620 | else |
strysd | 17:b3cc5bba2af4 | 621 | { |
strysd | 17:b3cc5bba2af4 | 622 | writtenLen += trfLen; |
strysd | 17:b3cc5bba2af4 | 623 | if( writtenLen >= pDataOut->getDataLen() ) |
strysd | 17:b3cc5bba2af4 | 624 | { |
strysd | 17:b3cc5bba2af4 | 625 | break; |
strysd | 17:b3cc5bba2af4 | 626 | } |
strysd | 17:b3cc5bba2af4 | 627 | } |
strysd | 17:b3cc5bba2af4 | 628 | |
strysd | 17:b3cc5bba2af4 | 629 | if( trfLen == 0 ) |
strysd | 17:b3cc5bba2af4 | 630 | { |
strysd | 17:b3cc5bba2af4 | 631 | break; |
strysd | 17:b3cc5bba2af4 | 632 | } |
strysd | 17:b3cc5bba2af4 | 633 | } |
strysd | 17:b3cc5bba2af4 | 634 | |
strysd | 17:b3cc5bba2af4 | 635 | } |
strysd | 17:b3cc5bba2af4 | 636 | |
strysd | 17:b3cc5bba2af4 | 637 | //Receive response |
strysd | 17:b3cc5bba2af4 | 638 | DBG("Receiving response"); |
strysd | 17:b3cc5bba2af4 | 639 | ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
strysd | 17:b3cc5bba2af4 | 640 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 641 | |
strysd | 17:b3cc5bba2af4 | 642 | buf[trfLen] = '\0'; |
strysd | 17:b3cc5bba2af4 | 643 | |
strysd | 17:b3cc5bba2af4 | 644 | char* crlfPtr = strstr(buf, "\r\n"); |
strysd | 17:b3cc5bba2af4 | 645 | if(crlfPtr == NULL) |
strysd | 17:b3cc5bba2af4 | 646 | { |
strysd | 17:b3cc5bba2af4 | 647 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 648 | } |
strysd | 17:b3cc5bba2af4 | 649 | |
strysd | 17:b3cc5bba2af4 | 650 | int crlfPos = crlfPtr - buf; |
strysd | 17:b3cc5bba2af4 | 651 | buf[crlfPos] = '\0'; |
strysd | 17:b3cc5bba2af4 | 652 | |
strysd | 17:b3cc5bba2af4 | 653 | //Parse HTTP response |
strysd | 17:b3cc5bba2af4 | 654 | if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) |
strysd | 17:b3cc5bba2af4 | 655 | { |
strysd | 17:b3cc5bba2af4 | 656 | //Cannot match string, error |
strysd | 17:b3cc5bba2af4 | 657 | ERR("Not a correct HTTP answer : %s\n", buf); |
strysd | 17:b3cc5bba2af4 | 658 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 659 | } |
strysd | 17:b3cc5bba2af4 | 660 | |
strysd | 17:b3cc5bba2af4 | 661 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) |
strysd | 17:b3cc5bba2af4 | 662 | { |
strysd | 17:b3cc5bba2af4 | 663 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
strysd | 17:b3cc5bba2af4 | 664 | WARN("Response code %d", m_httpResponseCode); |
strysd | 17:b3cc5bba2af4 | 665 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 666 | } |
strysd | 17:b3cc5bba2af4 | 667 | |
strysd | 17:b3cc5bba2af4 | 668 | DBG("Reading headers"); |
strysd | 17:b3cc5bba2af4 | 669 | |
strysd | 17:b3cc5bba2af4 | 670 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
strysd | 17:b3cc5bba2af4 | 671 | trfLen -= (crlfPos + 2); |
strysd | 17:b3cc5bba2af4 | 672 | |
strysd | 17:b3cc5bba2af4 | 673 | size_t recvContentLength = 0; |
strysd | 17:b3cc5bba2af4 | 674 | bool recvChunked = false; |
strysd | 17:b3cc5bba2af4 | 675 | //Now get headers |
strysd | 17:b3cc5bba2af4 | 676 | while( true ) |
strysd | 17:b3cc5bba2af4 | 677 | { |
strysd | 17:b3cc5bba2af4 | 678 | crlfPtr = strstr(buf, "\r\n"); |
strysd | 17:b3cc5bba2af4 | 679 | if(crlfPtr == NULL) |
strysd | 17:b3cc5bba2af4 | 680 | { |
strysd | 17:b3cc5bba2af4 | 681 | if( trfLen < CHUNK_SIZE - 1 ) |
strysd | 17:b3cc5bba2af4 | 682 | { |
strysd | 17:b3cc5bba2af4 | 683 | size_t newTrfLen; |
strysd | 17:b3cc5bba2af4 | 684 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
strysd | 17:b3cc5bba2af4 | 685 | trfLen += newTrfLen; |
strysd | 17:b3cc5bba2af4 | 686 | buf[trfLen] = '\0'; |
strysd | 17:b3cc5bba2af4 | 687 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
strysd | 17:b3cc5bba2af4 | 688 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 689 | continue; |
strysd | 17:b3cc5bba2af4 | 690 | } |
strysd | 17:b3cc5bba2af4 | 691 | else |
strysd | 17:b3cc5bba2af4 | 692 | { |
strysd | 17:b3cc5bba2af4 | 693 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 694 | } |
strysd | 17:b3cc5bba2af4 | 695 | } |
strysd | 17:b3cc5bba2af4 | 696 | |
strysd | 17:b3cc5bba2af4 | 697 | crlfPos = crlfPtr - buf; |
strysd | 17:b3cc5bba2af4 | 698 | |
strysd | 17:b3cc5bba2af4 | 699 | if(crlfPos == 0) //End of headers |
strysd | 17:b3cc5bba2af4 | 700 | { |
strysd | 17:b3cc5bba2af4 | 701 | DBG("Headers read"); |
strysd | 17:b3cc5bba2af4 | 702 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
strysd | 17:b3cc5bba2af4 | 703 | trfLen -= 2; |
strysd | 17:b3cc5bba2af4 | 704 | break; |
strysd | 17:b3cc5bba2af4 | 705 | } |
strysd | 17:b3cc5bba2af4 | 706 | |
strysd | 17:b3cc5bba2af4 | 707 | buf[crlfPos] = '\0'; |
strysd | 17:b3cc5bba2af4 | 708 | |
strysd | 17:b3cc5bba2af4 | 709 | char key[32]; |
strysd | 17:b3cc5bba2af4 | 710 | char value[32]; |
strysd | 17:b3cc5bba2af4 | 711 | |
strysd | 17:b3cc5bba2af4 | 712 | key[31] = '\0'; |
strysd | 17:b3cc5bba2af4 | 713 | value[31] = '\0'; |
strysd | 17:b3cc5bba2af4 | 714 | |
strysd | 17:b3cc5bba2af4 | 715 | int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); |
strysd | 17:b3cc5bba2af4 | 716 | if ( n == 2 ) |
strysd | 17:b3cc5bba2af4 | 717 | { |
strysd | 17:b3cc5bba2af4 | 718 | DBG("Read header : %s: %s\n", key, value); |
strysd | 17:b3cc5bba2af4 | 719 | if( !strcmp(key, "Content-Length") ) |
strysd | 17:b3cc5bba2af4 | 720 | { |
strysd | 17:b3cc5bba2af4 | 721 | sscanf(value, "%d", &recvContentLength); |
strysd | 17:b3cc5bba2af4 | 722 | pDataIn->setDataLen(recvContentLength); |
strysd | 17:b3cc5bba2af4 | 723 | } |
strysd | 17:b3cc5bba2af4 | 724 | else if( !strcmp(key, "Transfer-Encoding") ) |
strysd | 17:b3cc5bba2af4 | 725 | { |
strysd | 17:b3cc5bba2af4 | 726 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) |
strysd | 17:b3cc5bba2af4 | 727 | { |
strysd | 17:b3cc5bba2af4 | 728 | recvChunked = true; |
strysd | 17:b3cc5bba2af4 | 729 | pDataIn->setIsChunked(true); |
strysd | 17:b3cc5bba2af4 | 730 | } |
strysd | 17:b3cc5bba2af4 | 731 | } |
strysd | 17:b3cc5bba2af4 | 732 | else if( !strcmp(key, "Content-Type") ) |
strysd | 17:b3cc5bba2af4 | 733 | { |
strysd | 17:b3cc5bba2af4 | 734 | pDataIn->setDataType(value); |
strysd | 17:b3cc5bba2af4 | 735 | } |
strysd | 17:b3cc5bba2af4 | 736 | |
strysd | 17:b3cc5bba2af4 | 737 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
strysd | 17:b3cc5bba2af4 | 738 | trfLen -= (crlfPos + 2); |
strysd | 17:b3cc5bba2af4 | 739 | |
strysd | 17:b3cc5bba2af4 | 740 | } |
strysd | 17:b3cc5bba2af4 | 741 | else |
strysd | 17:b3cc5bba2af4 | 742 | { |
strysd | 17:b3cc5bba2af4 | 743 | ERR("Could not parse header"); |
strysd | 17:b3cc5bba2af4 | 744 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 745 | } |
strysd | 17:b3cc5bba2af4 | 746 | |
strysd | 17:b3cc5bba2af4 | 747 | } |
strysd | 17:b3cc5bba2af4 | 748 | |
strysd | 17:b3cc5bba2af4 | 749 | //Receive data |
strysd | 17:b3cc5bba2af4 | 750 | DBG("Receiving data"); |
strysd | 17:b3cc5bba2af4 | 751 | while(true) |
strysd | 17:b3cc5bba2af4 | 752 | { |
strysd | 17:b3cc5bba2af4 | 753 | size_t readLen = 0; |
strysd | 17:b3cc5bba2af4 | 754 | |
strysd | 17:b3cc5bba2af4 | 755 | if( recvChunked ) |
strysd | 17:b3cc5bba2af4 | 756 | { |
strysd | 17:b3cc5bba2af4 | 757 | //Read chunk header |
strysd | 17:b3cc5bba2af4 | 758 | bool foundCrlf; |
strysd | 17:b3cc5bba2af4 | 759 | do |
strysd | 17:b3cc5bba2af4 | 760 | { |
strysd | 17:b3cc5bba2af4 | 761 | foundCrlf = false; |
strysd | 17:b3cc5bba2af4 | 762 | crlfPos=0; |
strysd | 17:b3cc5bba2af4 | 763 | buf[trfLen]=0; |
strysd | 17:b3cc5bba2af4 | 764 | if(trfLen >= 2) |
strysd | 17:b3cc5bba2af4 | 765 | { |
strysd | 17:b3cc5bba2af4 | 766 | for(; crlfPos < trfLen - 2; crlfPos++) |
strysd | 17:b3cc5bba2af4 | 767 | { |
strysd | 17:b3cc5bba2af4 | 768 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) |
strysd | 17:b3cc5bba2af4 | 769 | { |
strysd | 17:b3cc5bba2af4 | 770 | foundCrlf = true; |
strysd | 17:b3cc5bba2af4 | 771 | break; |
strysd | 17:b3cc5bba2af4 | 772 | } |
strysd | 17:b3cc5bba2af4 | 773 | } |
strysd | 17:b3cc5bba2af4 | 774 | } |
strysd | 17:b3cc5bba2af4 | 775 | if(!foundCrlf) //Try to read more |
strysd | 17:b3cc5bba2af4 | 776 | { |
strysd | 17:b3cc5bba2af4 | 777 | if( trfLen < CHUNK_SIZE ) |
strysd | 17:b3cc5bba2af4 | 778 | { |
strysd | 17:b3cc5bba2af4 | 779 | size_t newTrfLen; |
strysd | 17:b3cc5bba2af4 | 780 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
strysd | 17:b3cc5bba2af4 | 781 | trfLen += newTrfLen; |
strysd | 17:b3cc5bba2af4 | 782 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 783 | continue; |
strysd | 17:b3cc5bba2af4 | 784 | } |
strysd | 17:b3cc5bba2af4 | 785 | else |
strysd | 17:b3cc5bba2af4 | 786 | { |
strysd | 17:b3cc5bba2af4 | 787 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 788 | } |
strysd | 17:b3cc5bba2af4 | 789 | } |
strysd | 17:b3cc5bba2af4 | 790 | } while(!foundCrlf); |
strysd | 17:b3cc5bba2af4 | 791 | buf[crlfPos] = '\0'; |
strysd | 17:b3cc5bba2af4 | 792 | int n = sscanf(buf, "%x", &readLen); |
strysd | 17:b3cc5bba2af4 | 793 | if(n!=1) |
strysd | 17:b3cc5bba2af4 | 794 | { |
strysd | 17:b3cc5bba2af4 | 795 | ERR("Could not read chunk length"); |
strysd | 17:b3cc5bba2af4 | 796 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 797 | } |
strysd | 17:b3cc5bba2af4 | 798 | |
strysd | 17:b3cc5bba2af4 | 799 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
strysd | 17:b3cc5bba2af4 | 800 | trfLen -= (crlfPos + 2); |
strysd | 17:b3cc5bba2af4 | 801 | |
strysd | 17:b3cc5bba2af4 | 802 | if( readLen == 0 ) |
strysd | 17:b3cc5bba2af4 | 803 | { |
strysd | 17:b3cc5bba2af4 | 804 | //Last chunk |
strysd | 17:b3cc5bba2af4 | 805 | break; |
strysd | 17:b3cc5bba2af4 | 806 | } |
strysd | 17:b3cc5bba2af4 | 807 | } |
strysd | 17:b3cc5bba2af4 | 808 | else |
strysd | 17:b3cc5bba2af4 | 809 | { |
strysd | 17:b3cc5bba2af4 | 810 | readLen = recvContentLength; |
strysd | 17:b3cc5bba2af4 | 811 | } |
strysd | 17:b3cc5bba2af4 | 812 | |
strysd | 17:b3cc5bba2af4 | 813 | DBG("Retrieving %d bytes", readLen); |
strysd | 17:b3cc5bba2af4 | 814 | |
strysd | 17:b3cc5bba2af4 | 815 | do |
strysd | 17:b3cc5bba2af4 | 816 | { |
strysd | 17:b3cc5bba2af4 | 817 | pDataIn->write(buf, MIN(trfLen, readLen)); |
strysd | 17:b3cc5bba2af4 | 818 | if( trfLen > readLen ) |
strysd | 17:b3cc5bba2af4 | 819 | { |
strysd | 17:b3cc5bba2af4 | 820 | memmove(buf, &buf[readLen], trfLen - readLen); |
strysd | 17:b3cc5bba2af4 | 821 | trfLen -= readLen; |
strysd | 17:b3cc5bba2af4 | 822 | readLen = 0; |
strysd | 17:b3cc5bba2af4 | 823 | } |
strysd | 17:b3cc5bba2af4 | 824 | else |
strysd | 17:b3cc5bba2af4 | 825 | { |
strysd | 17:b3cc5bba2af4 | 826 | readLen -= trfLen; |
strysd | 17:b3cc5bba2af4 | 827 | } |
strysd | 17:b3cc5bba2af4 | 828 | |
strysd | 17:b3cc5bba2af4 | 829 | if(readLen) |
strysd | 17:b3cc5bba2af4 | 830 | { |
strysd | 17:b3cc5bba2af4 | 831 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
strysd | 17:b3cc5bba2af4 | 832 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 833 | } |
strysd | 17:b3cc5bba2af4 | 834 | } while(readLen); |
strysd | 17:b3cc5bba2af4 | 835 | |
strysd | 17:b3cc5bba2af4 | 836 | if( recvChunked ) |
strysd | 17:b3cc5bba2af4 | 837 | { |
strysd | 17:b3cc5bba2af4 | 838 | if(trfLen < 2) |
strysd | 17:b3cc5bba2af4 | 839 | { |
strysd | 17:b3cc5bba2af4 | 840 | size_t newTrfLen; |
strysd | 17:b3cc5bba2af4 | 841 | //Read missing chars to find end of chunk |
strysd | 17:b3cc5bba2af4 | 842 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
strysd | 17:b3cc5bba2af4 | 843 | CHECK_CONN_ERR(ret); |
strysd | 17:b3cc5bba2af4 | 844 | trfLen += newTrfLen; |
strysd | 17:b3cc5bba2af4 | 845 | } |
strysd | 17:b3cc5bba2af4 | 846 | if( (buf[0] != '\r') || (buf[1] != '\n') ) |
strysd | 17:b3cc5bba2af4 | 847 | { |
strysd | 17:b3cc5bba2af4 | 848 | ERR("Format error"); |
strysd | 17:b3cc5bba2af4 | 849 | PRTCL_ERR(); |
strysd | 17:b3cc5bba2af4 | 850 | } |
strysd | 17:b3cc5bba2af4 | 851 | memmove(buf, &buf[2], trfLen - 2); |
strysd | 17:b3cc5bba2af4 | 852 | trfLen -= 2; |
strysd | 17:b3cc5bba2af4 | 853 | } |
strysd | 17:b3cc5bba2af4 | 854 | else |
strysd | 17:b3cc5bba2af4 | 855 | { |
strysd | 17:b3cc5bba2af4 | 856 | break; |
strysd | 17:b3cc5bba2af4 | 857 | } |
strysd | 17:b3cc5bba2af4 | 858 | |
strysd | 17:b3cc5bba2af4 | 859 | } |
strysd | 17:b3cc5bba2af4 | 860 | |
strysd | 17:b3cc5bba2af4 | 861 | m_sock.close(); |
strysd | 17:b3cc5bba2af4 | 862 | DBG("Completed HTTP transaction"); |
strysd | 17:b3cc5bba2af4 | 863 | |
strysd | 17:b3cc5bba2af4 | 864 | return HTTP_OK; |
strysd | 17:b3cc5bba2af4 | 865 | } |
strysd | 17:b3cc5bba2af4 | 866 | //strysd add this function end |
strysd | 17:b3cc5bba2af4 | 867 | |
donatien | 11:390362de8c3f | 868 | HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure |
donatien | 0:2ccb9960a044 | 869 | { |
donatien | 0:2ccb9960a044 | 870 | DBG("Trying to read between %d and %d bytes", minLen, maxLen); |
donatien | 0:2ccb9960a044 | 871 | size_t readLen = 0; |
donatien | 12:89d09a6db00a | 872 | |
donatien | 12:89d09a6db00a | 873 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 874 | { |
donatien | 12:89d09a6db00a | 875 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 876 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 877 | } |
donatien | 12:89d09a6db00a | 878 | |
donatien | 7:4e39864f7b15 | 879 | int ret; |
donatien | 7:4e39864f7b15 | 880 | while(readLen < maxLen) |
donatien | 0:2ccb9960a044 | 881 | { |
donatien | 7:4e39864f7b15 | 882 | if(readLen < minLen) |
donatien | 0:2ccb9960a044 | 883 | { |
donatien | 12:89d09a6db00a | 884 | DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); |
donatien | 13:be61104f4e91 | 885 | m_sock.set_blocking(false, m_timeout); |
donatien | 12:89d09a6db00a | 886 | ret = m_sock.receive_all(buf + readLen, minLen - readLen); |
donatien | 0:2ccb9960a044 | 887 | } |
donatien | 7:4e39864f7b15 | 888 | else |
donatien | 7:4e39864f7b15 | 889 | { |
donatien | 12:89d09a6db00a | 890 | DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); |
donatien | 13:be61104f4e91 | 891 | m_sock.set_blocking(false, 0); |
donatien | 12:89d09a6db00a | 892 | ret = m_sock.receive(buf + readLen, maxLen - readLen); |
donatien | 7:4e39864f7b15 | 893 | } |
donatien | 7:4e39864f7b15 | 894 | |
donatien | 0:2ccb9960a044 | 895 | if( ret > 0) |
donatien | 0:2ccb9960a044 | 896 | { |
donatien | 0:2ccb9960a044 | 897 | readLen += ret; |
donatien | 0:2ccb9960a044 | 898 | } |
donatien | 0:2ccb9960a044 | 899 | else if( ret == 0 ) |
donatien | 0:2ccb9960a044 | 900 | { |
donatien | 7:4e39864f7b15 | 901 | break; |
donatien | 7:4e39864f7b15 | 902 | } |
donatien | 7:4e39864f7b15 | 903 | else |
donatien | 7:4e39864f7b15 | 904 | { |
donatien | 12:89d09a6db00a | 905 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 906 | { |
donatien | 12:89d09a6db00a | 907 | ERR("Connection error (recv returned %d)", ret); |
donatien | 12:89d09a6db00a | 908 | *pReadLen = readLen; |
donatien | 12:89d09a6db00a | 909 | return HTTP_CONN; |
donatien | 12:89d09a6db00a | 910 | } |
donatien | 12:89d09a6db00a | 911 | else |
donatien | 12:89d09a6db00a | 912 | { |
donatien | 12:89d09a6db00a | 913 | break; |
donatien | 12:89d09a6db00a | 914 | } |
donatien | 12:89d09a6db00a | 915 | } |
donatien | 12:89d09a6db00a | 916 | |
donatien | 12:89d09a6db00a | 917 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 918 | { |
donatien | 12:89d09a6db00a | 919 | break; |
donatien | 7:4e39864f7b15 | 920 | } |
donatien | 7:4e39864f7b15 | 921 | } |
donatien | 7:4e39864f7b15 | 922 | DBG("Read %d bytes", readLen); |
donatien | 7:4e39864f7b15 | 923 | *pReadLen = readLen; |
donatien | 11:390362de8c3f | 924 | return HTTP_OK; |
donatien | 7:4e39864f7b15 | 925 | } |
donatien | 7:4e39864f7b15 | 926 | |
donatien | 11:390362de8c3f | 927 | HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure |
donatien | 7:4e39864f7b15 | 928 | { |
donatien | 7:4e39864f7b15 | 929 | if(len == 0) |
donatien | 7:4e39864f7b15 | 930 | { |
donatien | 7:4e39864f7b15 | 931 | len = strlen(buf); |
donatien | 7:4e39864f7b15 | 932 | } |
donatien | 7:4e39864f7b15 | 933 | DBG("Trying to write %d bytes", len); |
donatien | 7:4e39864f7b15 | 934 | size_t writtenLen = 0; |
donatien | 12:89d09a6db00a | 935 | |
donatien | 12:89d09a6db00a | 936 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 937 | { |
donatien | 12:89d09a6db00a | 938 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 939 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 940 | } |
donatien | 7:4e39864f7b15 | 941 | |
donatien | 13:be61104f4e91 | 942 | m_sock.set_blocking(false, m_timeout); |
donatien | 12:89d09a6db00a | 943 | int ret = m_sock.send_all(buf, len); |
donatien | 12:89d09a6db00a | 944 | if(ret > 0) |
donatien | 7:4e39864f7b15 | 945 | { |
donatien | 12:89d09a6db00a | 946 | writtenLen += ret; |
donatien | 12:89d09a6db00a | 947 | } |
donatien | 12:89d09a6db00a | 948 | else if( ret == 0 ) |
donatien | 12:89d09a6db00a | 949 | { |
donatien | 12:89d09a6db00a | 950 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 951 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 952 | } |
donatien | 12:89d09a6db00a | 953 | else |
donatien | 12:89d09a6db00a | 954 | { |
donatien | 12:89d09a6db00a | 955 | ERR("Connection error (send returned %d)", ret); |
donatien | 12:89d09a6db00a | 956 | return HTTP_CONN; |
donatien | 12:89d09a6db00a | 957 | } |
donatien | 7:4e39864f7b15 | 958 | |
donatien | 0:2ccb9960a044 | 959 | DBG("Written %d bytes", writtenLen); |
donatien | 11:390362de8c3f | 960 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 961 | } |
donatien | 0:2ccb9960a044 | 962 | |
donatien | 11:390362de8c3f | 963 | 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 | 964 | { |
donatien | 0:2ccb9960a044 | 965 | char* schemePtr = (char*) url; |
donatien | 0:2ccb9960a044 | 966 | char* hostPtr = (char*) strstr(url, "://"); |
donatien | 0:2ccb9960a044 | 967 | if(hostPtr == NULL) |
donatien | 0:2ccb9960a044 | 968 | { |
donatien | 0:2ccb9960a044 | 969 | WARN("Could not find host"); |
donatien | 11:390362de8c3f | 970 | return HTTP_PARSE; //URL is invalid |
donatien | 0:2ccb9960a044 | 971 | } |
donatien | 0:2ccb9960a044 | 972 | |
donatien | 0:2ccb9960a044 | 973 | if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 974 | { |
donatien | 0:2ccb9960a044 | 975 | WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); |
donatien | 11:390362de8c3f | 976 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 977 | } |
donatien | 0:2ccb9960a044 | 978 | memcpy(scheme, schemePtr, hostPtr - schemePtr); |
donatien | 0:2ccb9960a044 | 979 | scheme[hostPtr - schemePtr] = '\0'; |
donatien | 0:2ccb9960a044 | 980 | |
donatien | 0:2ccb9960a044 | 981 | hostPtr+=3; |
donatien | 0:2ccb9960a044 | 982 | |
donatien | 0:2ccb9960a044 | 983 | size_t hostLen = 0; |
donatien | 0:2ccb9960a044 | 984 | |
donatien | 0:2ccb9960a044 | 985 | char* portPtr = strchr(hostPtr, ':'); |
donatien | 0:2ccb9960a044 | 986 | if( portPtr != NULL ) |
donatien | 0:2ccb9960a044 | 987 | { |
donatien | 0:2ccb9960a044 | 988 | hostLen = portPtr - hostPtr; |
donatien | 0:2ccb9960a044 | 989 | portPtr++; |
donatien | 8:45c8da29a1cf | 990 | if( sscanf(portPtr, "%hu", port) != 1) |
donatien | 0:2ccb9960a044 | 991 | { |
donatien | 0:2ccb9960a044 | 992 | WARN("Could not find port"); |
donatien | 11:390362de8c3f | 993 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 994 | } |
donatien | 0:2ccb9960a044 | 995 | } |
donatien | 0:2ccb9960a044 | 996 | else |
donatien | 0:2ccb9960a044 | 997 | { |
donatien | 0:2ccb9960a044 | 998 | *port=0; |
donatien | 0:2ccb9960a044 | 999 | } |
donatien | 0:2ccb9960a044 | 1000 | char* pathPtr = strchr(hostPtr, '/'); |
donatien | 0:2ccb9960a044 | 1001 | if( hostLen == 0 ) |
donatien | 0:2ccb9960a044 | 1002 | { |
donatien | 0:2ccb9960a044 | 1003 | hostLen = pathPtr - hostPtr; |
donatien | 0:2ccb9960a044 | 1004 | } |
donatien | 0:2ccb9960a044 | 1005 | |
donatien | 0:2ccb9960a044 | 1006 | if( maxHostLen < hostLen + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 1007 | { |
donatien | 0:2ccb9960a044 | 1008 | WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); |
donatien | 11:390362de8c3f | 1009 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 1010 | } |
donatien | 0:2ccb9960a044 | 1011 | memcpy(host, hostPtr, hostLen); |
donatien | 0:2ccb9960a044 | 1012 | host[hostLen] = '\0'; |
donatien | 0:2ccb9960a044 | 1013 | |
donatien | 0:2ccb9960a044 | 1014 | size_t pathLen; |
donatien | 0:2ccb9960a044 | 1015 | char* fragmentPtr = strchr(hostPtr, '#'); |
donatien | 0:2ccb9960a044 | 1016 | if(fragmentPtr != NULL) |
donatien | 0:2ccb9960a044 | 1017 | { |
donatien | 0:2ccb9960a044 | 1018 | pathLen = fragmentPtr - pathPtr; |
donatien | 0:2ccb9960a044 | 1019 | } |
donatien | 0:2ccb9960a044 | 1020 | else |
donatien | 0:2ccb9960a044 | 1021 | { |
donatien | 0:2ccb9960a044 | 1022 | pathLen = strlen(pathPtr); |
donatien | 0:2ccb9960a044 | 1023 | } |
donatien | 0:2ccb9960a044 | 1024 | |
donatien | 0:2ccb9960a044 | 1025 | if( maxPathLen < pathLen + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 1026 | { |
donatien | 0:2ccb9960a044 | 1027 | WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); |
donatien | 11:390362de8c3f | 1028 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 1029 | } |
donatien | 0:2ccb9960a044 | 1030 | memcpy(path, pathPtr, pathLen); |
donatien | 0:2ccb9960a044 | 1031 | path[pathLen] = '\0'; |
donatien | 0:2ccb9960a044 | 1032 | |
donatien | 11:390362de8c3f | 1033 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 1034 | } |