HTTPClient
Fork of HTTPClient by
HTTPClient.cpp@19:1211b05ba323, 2014-10-10 (annotated)
- Committer:
- fangbao
- Date:
- Fri Oct 10 09:41:01 2014 +0000
- Revision:
- 19:1211b05ba323
- Parent:
- 18:277279a1891e
Fix an issue: the server might send back "Access-Control-Allow-Credentials: true" in the Response Headers. The length of the key in that header is 33 bytes, which is larger than the size of the 'key' buffer (32 bytes).
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 |
fangbao | 19:1211b05ba323 | 44 | #define KEYVALUE_SIZE 48 |
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 | |
donatien | 16:1f743885e7de | 85 | HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 86 | { |
donatien | 16:1f743885e7de | 87 | return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 16:1f743885e7de | 88 | } |
donatien | 16:1f743885e7de | 89 | |
donatien | 16:1f743885e7de | 90 | HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 91 | { |
donatien | 16:1f743885e7de | 92 | return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); |
donatien | 16:1f743885e7de | 93 | } |
donatien | 16:1f743885e7de | 94 | |
donatien | 16:1f743885e7de | 95 | |
donatien | 0:2ccb9960a044 | 96 | int HTTPClient::getHTTPResponseCode() |
donatien | 0:2ccb9960a044 | 97 | { |
donatien | 0:2ccb9960a044 | 98 | return m_httpResponseCode; |
donatien | 0:2ccb9960a044 | 99 | } |
donatien | 0:2ccb9960a044 | 100 | |
donatien | 5:791fc3dcb6c4 | 101 | #define CHECK_CONN_ERR(ret) \ |
donatien | 5:791fc3dcb6c4 | 102 | do{ \ |
donatien | 7:4e39864f7b15 | 103 | if(ret) { \ |
donatien | 7:4e39864f7b15 | 104 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 105 | ERR("Connection error (%d)", ret); \ |
donatien | 11:390362de8c3f | 106 | return HTTP_CONN; \ |
donatien | 5:791fc3dcb6c4 | 107 | } \ |
donatien | 5:791fc3dcb6c4 | 108 | } while(0) |
donatien | 5:791fc3dcb6c4 | 109 | |
donatien | 5:791fc3dcb6c4 | 110 | #define PRTCL_ERR() \ |
donatien | 5:791fc3dcb6c4 | 111 | do{ \ |
donatien | 7:4e39864f7b15 | 112 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 113 | ERR("Protocol error"); \ |
donatien | 11:390362de8c3f | 114 | return HTTP_PRTCL; \ |
donatien | 5:791fc3dcb6c4 | 115 | } while(0) |
donatien | 0:2ccb9960a044 | 116 | |
donatien | 12:89d09a6db00a | 117 | HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request |
donatien | 12:89d09a6db00a | 118 | { |
donatien | 0:2ccb9960a044 | 119 | m_httpResponseCode = 0; //Invalidate code |
donatien | 0:2ccb9960a044 | 120 | m_timeout = timeout; |
donatien | 16:1f743885e7de | 121 | |
donatien | 16:1f743885e7de | 122 | pDataIn->writeReset(); |
donatien | 16:1f743885e7de | 123 | if( pDataOut ) |
donatien | 16:1f743885e7de | 124 | { |
donatien | 16:1f743885e7de | 125 | pDataOut->readReset(); |
donatien | 16:1f743885e7de | 126 | } |
donatien | 0:2ccb9960a044 | 127 | |
donatien | 0:2ccb9960a044 | 128 | char scheme[8]; |
donatien | 0:2ccb9960a044 | 129 | uint16_t port; |
donatien | 0:2ccb9960a044 | 130 | char host[32]; |
donatien | 0:2ccb9960a044 | 131 | char path[64]; |
donatien | 0:2ccb9960a044 | 132 | //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) |
donatien | 11:390362de8c3f | 133 | HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); |
donatien | 11:390362de8c3f | 134 | if(res != HTTP_OK) |
donatien | 0:2ccb9960a044 | 135 | { |
donatien | 11:390362de8c3f | 136 | ERR("parseURL returned %d", res); |
donatien | 11:390362de8c3f | 137 | return res; |
donatien | 0:2ccb9960a044 | 138 | } |
donatien | 0:2ccb9960a044 | 139 | |
donatien | 0:2ccb9960a044 | 140 | if(port == 0) //TODO do handle HTTPS->443 |
donatien | 0:2ccb9960a044 | 141 | { |
donatien | 0:2ccb9960a044 | 142 | port = 80; |
donatien | 0:2ccb9960a044 | 143 | } |
donatien | 0:2ccb9960a044 | 144 | |
donatien | 0:2ccb9960a044 | 145 | DBG("Scheme: %s", scheme); |
donatien | 0:2ccb9960a044 | 146 | DBG("Host: %s", host); |
donatien | 0:2ccb9960a044 | 147 | DBG("Port: %d", port); |
donatien | 0:2ccb9960a044 | 148 | DBG("Path: %s", path); |
donatien | 0:2ccb9960a044 | 149 | |
donatien | 7:4e39864f7b15 | 150 | //Connect |
donatien | 7:4e39864f7b15 | 151 | DBG("Connecting socket to server"); |
donatien | 11:390362de8c3f | 152 | int ret = m_sock.connect(host, port); |
donatien | 0:2ccb9960a044 | 153 | if (ret < 0) |
donatien | 0:2ccb9960a044 | 154 | { |
donatien | 7:4e39864f7b15 | 155 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 156 | ERR("Could not connect"); |
donatien | 11:390362de8c3f | 157 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 158 | } |
donatien | 0:2ccb9960a044 | 159 | |
donatien | 0:2ccb9960a044 | 160 | //Send request |
donatien | 0:2ccb9960a044 | 161 | DBG("Sending request"); |
donatien | 9:ff30cc189191 | 162 | char buf[CHUNK_SIZE]; |
donatien | 16:1f743885e7de | 163 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
donatien | 9:ff30cc189191 | 164 | snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request |
donatien | 9:ff30cc189191 | 165 | ret = send(buf); |
donatien | 0:2ccb9960a044 | 166 | if(ret) |
donatien | 0:2ccb9960a044 | 167 | { |
donatien | 7:4e39864f7b15 | 168 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 169 | ERR("Could not write request"); |
donatien | 11:390362de8c3f | 170 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 171 | } |
donatien | 0:2ccb9960a044 | 172 | |
donatien | 0:2ccb9960a044 | 173 | //Send all headers |
donatien | 0:2ccb9960a044 | 174 | |
donatien | 0:2ccb9960a044 | 175 | //Send default headers |
donatien | 0:2ccb9960a044 | 176 | DBG("Sending headers"); |
donatien | 16:1f743885e7de | 177 | if( pDataOut != NULL ) |
donatien | 0:2ccb9960a044 | 178 | { |
donatien | 0:2ccb9960a044 | 179 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 180 | { |
donatien | 0:2ccb9960a044 | 181 | ret = send("Transfer-Encoding: chunked\r\n"); |
donatien | 5:791fc3dcb6c4 | 182 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 183 | } |
donatien | 0:2ccb9960a044 | 184 | else |
donatien | 0:2ccb9960a044 | 185 | { |
donatien | 9:ff30cc189191 | 186 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
donatien | 9:ff30cc189191 | 187 | ret = send(buf); |
donatien | 5:791fc3dcb6c4 | 188 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 189 | } |
donatien | 0:2ccb9960a044 | 190 | char type[48]; |
donatien | 11:390362de8c3f | 191 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) |
donatien | 0:2ccb9960a044 | 192 | { |
donatien | 9:ff30cc189191 | 193 | snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
donatien | 9:ff30cc189191 | 194 | ret = send(buf); |
donatien | 5:791fc3dcb6c4 | 195 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 196 | } |
donatien | 17:679e15a3d3db | 197 | |
donatien | 17:679e15a3d3db | 198 | //Send specific headers |
donatien | 17:679e15a3d3db | 199 | while( pDataOut->getHeader(buf, sizeof(buf) - 3) ) //must have space left for CRLF + 0 terminating char |
donatien | 17:679e15a3d3db | 200 | { |
donatien | 17:679e15a3d3db | 201 | size_t headerlen = strlen(buf); |
donatien | 17:679e15a3d3db | 202 | snprintf(buf + headerlen, sizeof(buf) - headerlen, "\r\n"); |
donatien | 17:679e15a3d3db | 203 | ret = send(buf); |
donatien | 17:679e15a3d3db | 204 | CHECK_CONN_ERR(ret); |
donatien | 17:679e15a3d3db | 205 | } |
donatien | 17:679e15a3d3db | 206 | } |
donatien | 17:679e15a3d3db | 207 | |
donatien | 17:679e15a3d3db | 208 | //Send specific headers |
donatien | 17:679e15a3d3db | 209 | while( pDataIn->getHeader(buf, sizeof(buf) - 3) ) |
donatien | 17:679e15a3d3db | 210 | { |
donatien | 17:679e15a3d3db | 211 | size_t headerlen = strlen(buf); |
donatien | 17:679e15a3d3db | 212 | snprintf(buf + headerlen, sizeof(buf) - headerlen, "\r\n"); |
donatien | 17:679e15a3d3db | 213 | ret = send(buf); |
donatien | 17:679e15a3d3db | 214 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 215 | } |
donatien | 9:ff30cc189191 | 216 | |
donatien | 0:2ccb9960a044 | 217 | //Close headers |
donatien | 0:2ccb9960a044 | 218 | DBG("Headers sent"); |
donatien | 0:2ccb9960a044 | 219 | ret = send("\r\n"); |
donatien | 5:791fc3dcb6c4 | 220 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 221 | |
donatien | 4:c071b05ac026 | 222 | size_t trfLen; |
donatien | 9:ff30cc189191 | 223 | |
donatien | 16:1f743885e7de | 224 | //Send data (if available) |
donatien | 16:1f743885e7de | 225 | if( pDataOut != NULL ) |
donatien | 0:2ccb9960a044 | 226 | { |
donatien | 0:2ccb9960a044 | 227 | DBG("Sending data"); |
donatien | 0:2ccb9960a044 | 228 | while(true) |
donatien | 0:2ccb9960a044 | 229 | { |
donatien | 0:2ccb9960a044 | 230 | size_t writtenLen = 0; |
donatien | 0:2ccb9960a044 | 231 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
donatien | 0:2ccb9960a044 | 232 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 233 | { |
donatien | 0:2ccb9960a044 | 234 | //Write chunk header |
donatien | 9:ff30cc189191 | 235 | char chunkHeader[16]; |
donatien | 9:ff30cc189191 | 236 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
donatien | 9:ff30cc189191 | 237 | ret = send(chunkHeader); |
donatien | 5:791fc3dcb6c4 | 238 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 239 | } |
donatien | 0:2ccb9960a044 | 240 | else if( trfLen == 0 ) |
donatien | 0:2ccb9960a044 | 241 | { |
donatien | 0:2ccb9960a044 | 242 | break; |
donatien | 0:2ccb9960a044 | 243 | } |
donatien | 0:2ccb9960a044 | 244 | if( trfLen != 0 ) |
donatien | 0:2ccb9960a044 | 245 | { |
donatien | 0:2ccb9960a044 | 246 | ret = send(buf, trfLen); |
donatien | 5:791fc3dcb6c4 | 247 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 248 | } |
donatien | 0:2ccb9960a044 | 249 | |
donatien | 0:2ccb9960a044 | 250 | if( pDataOut->getIsChunked() ) |
donatien | 0:2ccb9960a044 | 251 | { |
donatien | 0:2ccb9960a044 | 252 | ret = send("\r\n"); //Chunk-terminating CRLF |
donatien | 5:791fc3dcb6c4 | 253 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 254 | } |
donatien | 0:2ccb9960a044 | 255 | else |
donatien | 0:2ccb9960a044 | 256 | { |
donatien | 0:2ccb9960a044 | 257 | writtenLen += trfLen; |
donatien | 0:2ccb9960a044 | 258 | if( writtenLen >= pDataOut->getDataLen() ) |
donatien | 0:2ccb9960a044 | 259 | { |
donatien | 0:2ccb9960a044 | 260 | break; |
donatien | 0:2ccb9960a044 | 261 | } |
donatien | 0:2ccb9960a044 | 262 | } |
donatien | 0:2ccb9960a044 | 263 | |
donatien | 0:2ccb9960a044 | 264 | if( trfLen == 0 ) |
donatien | 0:2ccb9960a044 | 265 | { |
donatien | 0:2ccb9960a044 | 266 | break; |
donatien | 0:2ccb9960a044 | 267 | } |
donatien | 0:2ccb9960a044 | 268 | } |
donatien | 0:2ccb9960a044 | 269 | } |
donatien | 9:ff30cc189191 | 270 | |
donatien | 0:2ccb9960a044 | 271 | //Receive response |
donatien | 0:2ccb9960a044 | 272 | DBG("Receiving response"); |
donatien | 17:679e15a3d3db | 273 | ret = recv(buf, 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
donatien | 5:791fc3dcb6c4 | 274 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 275 | |
donatien | 0:2ccb9960a044 | 276 | buf[trfLen] = '\0'; |
donatien | 0:2ccb9960a044 | 277 | |
donatien | 17:679e15a3d3db | 278 | //Make sure we got the first response line |
donatien | 17:679e15a3d3db | 279 | char* crlfPtr = NULL; |
donatien | 17:679e15a3d3db | 280 | while( true ) |
donatien | 17:679e15a3d3db | 281 | { |
donatien | 17:679e15a3d3db | 282 | crlfPtr = strstr(buf, "\r\n"); |
donatien | 0:2ccb9960a044 | 283 | if(crlfPtr == NULL) |
donatien | 0:2ccb9960a044 | 284 | { |
donatien | 17:679e15a3d3db | 285 | if( trfLen < CHUNK_SIZE - 1 ) |
donatien | 17:679e15a3d3db | 286 | { |
donatien | 17:679e15a3d3db | 287 | size_t newTrfLen; |
donatien | 17:679e15a3d3db | 288 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 17:679e15a3d3db | 289 | trfLen += newTrfLen; |
donatien | 17:679e15a3d3db | 290 | buf[trfLen] = '\0'; |
donatien | 17:679e15a3d3db | 291 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
donatien | 17:679e15a3d3db | 292 | CHECK_CONN_ERR(ret); |
donatien | 17:679e15a3d3db | 293 | continue; |
donatien | 17:679e15a3d3db | 294 | } |
donatien | 17:679e15a3d3db | 295 | else |
donatien | 17:679e15a3d3db | 296 | { |
donatien | 5:791fc3dcb6c4 | 297 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 298 | } |
donatien | 17:679e15a3d3db | 299 | } |
donatien | 17:679e15a3d3db | 300 | break; |
donatien | 17:679e15a3d3db | 301 | } |
donatien | 0:2ccb9960a044 | 302 | |
donatien | 4:c071b05ac026 | 303 | int crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 304 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 305 | |
donatien | 0:2ccb9960a044 | 306 | //Parse HTTP response |
donatien | 17:679e15a3d3db | 307 | //if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) |
donatien | 17:679e15a3d3db | 308 | if(crlfPos > 13) |
donatien | 17:679e15a3d3db | 309 | { |
donatien | 17:679e15a3d3db | 310 | buf[13] = '\0'; |
donatien | 17:679e15a3d3db | 311 | } |
donatien | 17:679e15a3d3db | 312 | if( sscanf(buf, "HTTP/%*d.%*d %d", &m_httpResponseCode) != 1 ) //Kludge for newlib nano |
donatien | 0:2ccb9960a044 | 313 | { |
donatien | 0:2ccb9960a044 | 314 | //Cannot match string, error |
donatien | 0:2ccb9960a044 | 315 | ERR("Not a correct HTTP answer : %s\n", buf); |
donatien | 5:791fc3dcb6c4 | 316 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 317 | } |
donatien | 0:2ccb9960a044 | 318 | |
donatien | 16:1f743885e7de | 319 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) |
donatien | 0:2ccb9960a044 | 320 | { |
donatien | 16:1f743885e7de | 321 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
donatien | 0:2ccb9960a044 | 322 | WARN("Response code %d", m_httpResponseCode); |
donatien | 5:791fc3dcb6c4 | 323 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 324 | } |
donatien | 0:2ccb9960a044 | 325 | |
donatien | 0:2ccb9960a044 | 326 | DBG("Reading headers"); |
donatien | 0:2ccb9960a044 | 327 | |
donatien | 4:c071b05ac026 | 328 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 329 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 330 | |
donatien | 4:c071b05ac026 | 331 | size_t recvContentLength = 0; |
donatien | 4:c071b05ac026 | 332 | bool recvChunked = false; |
donatien | 17:679e15a3d3db | 333 | bool recvLengthUnknown = true; |
donatien | 0:2ccb9960a044 | 334 | //Now get headers |
donatien | 0:2ccb9960a044 | 335 | while( true ) |
donatien | 0:2ccb9960a044 | 336 | { |
donatien | 0:2ccb9960a044 | 337 | crlfPtr = strstr(buf, "\r\n"); |
donatien | 0:2ccb9960a044 | 338 | if(crlfPtr == NULL) |
donatien | 0:2ccb9960a044 | 339 | { |
donatien | 4:c071b05ac026 | 340 | if( trfLen < CHUNK_SIZE - 1 ) |
donatien | 0:2ccb9960a044 | 341 | { |
donatien | 0:2ccb9960a044 | 342 | size_t newTrfLen; |
donatien | 0:2ccb9960a044 | 343 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 0:2ccb9960a044 | 344 | trfLen += newTrfLen; |
donatien | 0:2ccb9960a044 | 345 | buf[trfLen] = '\0'; |
donatien | 4:c071b05ac026 | 346 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
donatien | 5:791fc3dcb6c4 | 347 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 348 | continue; |
donatien | 0:2ccb9960a044 | 349 | } |
donatien | 0:2ccb9960a044 | 350 | else |
donatien | 0:2ccb9960a044 | 351 | { |
donatien | 5:791fc3dcb6c4 | 352 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 353 | } |
donatien | 0:2ccb9960a044 | 354 | } |
donatien | 0:2ccb9960a044 | 355 | |
donatien | 0:2ccb9960a044 | 356 | crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 357 | |
donatien | 0:2ccb9960a044 | 358 | if(crlfPos == 0) //End of headers |
donatien | 0:2ccb9960a044 | 359 | { |
donatien | 0:2ccb9960a044 | 360 | DBG("Headers read"); |
donatien | 4:c071b05ac026 | 361 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 362 | trfLen -= 2; |
donatien | 0:2ccb9960a044 | 363 | break; |
donatien | 0:2ccb9960a044 | 364 | } |
donatien | 0:2ccb9960a044 | 365 | |
donatien | 0:2ccb9960a044 | 366 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 367 | |
fangbao | 19:1211b05ba323 | 368 | char key[KEYVALUE_SIZE]; |
fangbao | 19:1211b05ba323 | 369 | char value[KEYVALUE_SIZE]; |
donatien | 0:2ccb9960a044 | 370 | |
donatien | 17:679e15a3d3db | 371 | //key[31] = '\0'; |
donatien | 17:679e15a3d3db | 372 | //value[31] = '\0'; |
donatien | 17:679e15a3d3db | 373 | |
fangbao | 19:1211b05ba323 | 374 | memset(key, 0, KEYVALUE_SIZE); |
fangbao | 19:1211b05ba323 | 375 | memset(value, 0, KEYVALUE_SIZE); |
donatien | 4:c071b05ac026 | 376 | |
donatien | 17:679e15a3d3db | 377 | //int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); |
donatien | 17:679e15a3d3db | 378 | |
donatien | 17:679e15a3d3db | 379 | int n = 0; |
donatien | 17:679e15a3d3db | 380 | |
donatien | 17:679e15a3d3db | 381 | char* keyEnd = strchr(buf, ':'); |
donatien | 17:679e15a3d3db | 382 | if(keyEnd != NULL) |
donatien | 17:679e15a3d3db | 383 | { |
donatien | 17:679e15a3d3db | 384 | *keyEnd = '\0'; |
fangbao | 19:1211b05ba323 | 385 | if(strlen(buf) < KEYVALUE_SIZE) |
donatien | 17:679e15a3d3db | 386 | { |
donatien | 18:277279a1891e | 387 | strcpy(key, buf); |
donatien | 17:679e15a3d3db | 388 | n++; |
donatien | 17:679e15a3d3db | 389 | char* valueStart = keyEnd + 2; |
donatien | 17:679e15a3d3db | 390 | if( (valueStart - buf) < crlfPos ) |
donatien | 17:679e15a3d3db | 391 | { |
fangbao | 19:1211b05ba323 | 392 | if(strlen(valueStart) < KEYVALUE_SIZE) |
donatien | 18:277279a1891e | 393 | { |
donatien | 18:277279a1891e | 394 | strcpy(value, valueStart); |
donatien | 17:679e15a3d3db | 395 | n++; |
donatien | 17:679e15a3d3db | 396 | } |
donatien | 17:679e15a3d3db | 397 | } |
donatien | 17:679e15a3d3db | 398 | } |
donatien | 17:679e15a3d3db | 399 | } |
donatien | 0:2ccb9960a044 | 400 | if ( n == 2 ) |
donatien | 0:2ccb9960a044 | 401 | { |
donatien | 0:2ccb9960a044 | 402 | DBG("Read header : %s: %s\n", key, value); |
donatien | 0:2ccb9960a044 | 403 | if( !strcmp(key, "Content-Length") ) |
donatien | 0:2ccb9960a044 | 404 | { |
donatien | 0:2ccb9960a044 | 405 | sscanf(value, "%d", &recvContentLength); |
donatien | 17:679e15a3d3db | 406 | recvLengthUnknown = false; |
donatien | 0:2ccb9960a044 | 407 | pDataIn->setDataLen(recvContentLength); |
donatien | 0:2ccb9960a044 | 408 | } |
donatien | 0:2ccb9960a044 | 409 | else if( !strcmp(key, "Transfer-Encoding") ) |
donatien | 0:2ccb9960a044 | 410 | { |
donatien | 0:2ccb9960a044 | 411 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) |
donatien | 0:2ccb9960a044 | 412 | { |
donatien | 0:2ccb9960a044 | 413 | recvChunked = true; |
donatien | 17:679e15a3d3db | 414 | recvLengthUnknown = false; |
donatien | 0:2ccb9960a044 | 415 | pDataIn->setIsChunked(true); |
donatien | 0:2ccb9960a044 | 416 | } |
donatien | 0:2ccb9960a044 | 417 | } |
donatien | 0:2ccb9960a044 | 418 | else if( !strcmp(key, "Content-Type") ) |
donatien | 0:2ccb9960a044 | 419 | { |
donatien | 0:2ccb9960a044 | 420 | pDataIn->setDataType(value); |
donatien | 0:2ccb9960a044 | 421 | } |
donatien | 0:2ccb9960a044 | 422 | |
donatien | 4:c071b05ac026 | 423 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
donatien | 0:2ccb9960a044 | 424 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 425 | |
donatien | 0:2ccb9960a044 | 426 | } |
donatien | 0:2ccb9960a044 | 427 | else |
donatien | 0:2ccb9960a044 | 428 | { |
donatien | 0:2ccb9960a044 | 429 | ERR("Could not parse header"); |
donatien | 5:791fc3dcb6c4 | 430 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 431 | } |
donatien | 0:2ccb9960a044 | 432 | |
donatien | 0:2ccb9960a044 | 433 | } |
donatien | 0:2ccb9960a044 | 434 | |
donatien | 0:2ccb9960a044 | 435 | //Receive data |
donatien | 0:2ccb9960a044 | 436 | DBG("Receiving data"); |
donatien | 0:2ccb9960a044 | 437 | while(true) |
donatien | 0:2ccb9960a044 | 438 | { |
donatien | 0:2ccb9960a044 | 439 | size_t readLen = 0; |
donatien | 0:2ccb9960a044 | 440 | |
donatien | 0:2ccb9960a044 | 441 | if( recvChunked ) |
donatien | 0:2ccb9960a044 | 442 | { |
donatien | 0:2ccb9960a044 | 443 | //Read chunk header |
donatien | 14:2744e0c0e527 | 444 | bool foundCrlf; |
donatien | 14:2744e0c0e527 | 445 | do |
donatien | 0:2ccb9960a044 | 446 | { |
donatien | 14:2744e0c0e527 | 447 | foundCrlf = false; |
donatien | 14:2744e0c0e527 | 448 | crlfPos=0; |
donatien | 14:2744e0c0e527 | 449 | buf[trfLen]=0; |
donatien | 14:2744e0c0e527 | 450 | if(trfLen >= 2) |
donatien | 0:2ccb9960a044 | 451 | { |
donatien | 14:2744e0c0e527 | 452 | for(; crlfPos < trfLen - 2; crlfPos++) |
donatien | 14:2744e0c0e527 | 453 | { |
donatien | 14:2744e0c0e527 | 454 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) |
donatien | 14:2744e0c0e527 | 455 | { |
donatien | 14:2744e0c0e527 | 456 | foundCrlf = true; |
donatien | 14:2744e0c0e527 | 457 | break; |
donatien | 14:2744e0c0e527 | 458 | } |
donatien | 14:2744e0c0e527 | 459 | } |
donatien | 0:2ccb9960a044 | 460 | } |
donatien | 14:2744e0c0e527 | 461 | if(!foundCrlf) //Try to read more |
donatien | 0:2ccb9960a044 | 462 | { |
donatien | 14:2744e0c0e527 | 463 | if( trfLen < CHUNK_SIZE ) |
donatien | 14:2744e0c0e527 | 464 | { |
donatien | 14:2744e0c0e527 | 465 | size_t newTrfLen; |
donatien | 14:2744e0c0e527 | 466 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 14:2744e0c0e527 | 467 | trfLen += newTrfLen; |
donatien | 14:2744e0c0e527 | 468 | CHECK_CONN_ERR(ret); |
donatien | 14:2744e0c0e527 | 469 | continue; |
donatien | 14:2744e0c0e527 | 470 | } |
donatien | 14:2744e0c0e527 | 471 | else |
donatien | 14:2744e0c0e527 | 472 | { |
donatien | 14:2744e0c0e527 | 473 | PRTCL_ERR(); |
donatien | 14:2744e0c0e527 | 474 | } |
donatien | 0:2ccb9960a044 | 475 | } |
donatien | 14:2744e0c0e527 | 476 | } while(!foundCrlf); |
donatien | 0:2ccb9960a044 | 477 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 478 | int n = sscanf(buf, "%x", &readLen); |
donatien | 0:2ccb9960a044 | 479 | if(n!=1) |
donatien | 0:2ccb9960a044 | 480 | { |
donatien | 15:5ad07f90e895 | 481 | ERR("Could not read chunk length"); |
donatien | 5:791fc3dcb6c4 | 482 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 483 | } |
donatien | 0:2ccb9960a044 | 484 | |
donatien | 4:c071b05ac026 | 485 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
donatien | 0:2ccb9960a044 | 486 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 487 | |
donatien | 0:2ccb9960a044 | 488 | if( readLen == 0 ) |
donatien | 0:2ccb9960a044 | 489 | { |
donatien | 0:2ccb9960a044 | 490 | //Last chunk |
donatien | 0:2ccb9960a044 | 491 | break; |
donatien | 0:2ccb9960a044 | 492 | } |
donatien | 0:2ccb9960a044 | 493 | } |
donatien | 0:2ccb9960a044 | 494 | else |
donatien | 0:2ccb9960a044 | 495 | { |
donatien | 0:2ccb9960a044 | 496 | readLen = recvContentLength; |
donatien | 0:2ccb9960a044 | 497 | } |
donatien | 0:2ccb9960a044 | 498 | |
donatien | 17:679e15a3d3db | 499 | DBG("Retrieving %d bytes (%d bytes in buffer)", readLen, trfLen); |
donatien | 0:2ccb9960a044 | 500 | |
donatien | 0:2ccb9960a044 | 501 | do |
donatien | 0:2ccb9960a044 | 502 | { |
donatien | 17:679e15a3d3db | 503 | if(recvLengthUnknown ) |
donatien | 17:679e15a3d3db | 504 | { |
donatien | 17:679e15a3d3db | 505 | readLen = trfLen; |
donatien | 17:679e15a3d3db | 506 | } |
donatien | 0:2ccb9960a044 | 507 | pDataIn->write(buf, MIN(trfLen, readLen)); |
donatien | 17:679e15a3d3db | 508 | if(!recvLengthUnknown) |
donatien | 0:2ccb9960a044 | 509 | { |
donatien | 17:679e15a3d3db | 510 | if( trfLen > readLen ) |
donatien | 17:679e15a3d3db | 511 | { |
donatien | 17:679e15a3d3db | 512 | memmove(buf, &buf[readLen], trfLen - readLen); |
donatien | 17:679e15a3d3db | 513 | trfLen -= readLen; |
donatien | 17:679e15a3d3db | 514 | readLen = 0; |
donatien | 17:679e15a3d3db | 515 | } |
donatien | 17:679e15a3d3db | 516 | else |
donatien | 17:679e15a3d3db | 517 | { |
donatien | 17:679e15a3d3db | 518 | readLen -= trfLen; |
donatien | 17:679e15a3d3db | 519 | } |
donatien | 0:2ccb9960a044 | 520 | } |
donatien | 0:2ccb9960a044 | 521 | else |
donatien | 0:2ccb9960a044 | 522 | { |
donatien | 17:679e15a3d3db | 523 | trfLen = 0; |
donatien | 0:2ccb9960a044 | 524 | } |
donatien | 0:2ccb9960a044 | 525 | |
donatien | 17:679e15a3d3db | 526 | if(readLen || recvLengthUnknown) |
donatien | 0:2ccb9960a044 | 527 | { |
donatien | 0:2ccb9960a044 | 528 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
donatien | 17:679e15a3d3db | 529 | if(recvLengthUnknown && (ret == HTTP_CLOSED)) |
donatien | 17:679e15a3d3db | 530 | { |
donatien | 17:679e15a3d3db | 531 | //Write and exit |
donatien | 17:679e15a3d3db | 532 | pDataIn->write(buf, trfLen); |
donatien | 17:679e15a3d3db | 533 | break; |
donatien | 17:679e15a3d3db | 534 | } |
donatien | 5:791fc3dcb6c4 | 535 | CHECK_CONN_ERR(ret); |
donatien | 17:679e15a3d3db | 536 | if(recvLengthUnknown && (trfLen == 0)) |
donatien | 17:679e15a3d3db | 537 | { |
donatien | 17:679e15a3d3db | 538 | break; |
donatien | 17:679e15a3d3db | 539 | } |
donatien | 0:2ccb9960a044 | 540 | } |
donatien | 17:679e15a3d3db | 541 | } while(readLen || recvLengthUnknown); |
donatien | 0:2ccb9960a044 | 542 | |
donatien | 0:2ccb9960a044 | 543 | if( recvChunked ) |
donatien | 0:2ccb9960a044 | 544 | { |
donatien | 0:2ccb9960a044 | 545 | if(trfLen < 2) |
donatien | 0:2ccb9960a044 | 546 | { |
donatien | 0:2ccb9960a044 | 547 | size_t newTrfLen; |
donatien | 0:2ccb9960a044 | 548 | //Read missing chars to find end of chunk |
donatien | 14:2744e0c0e527 | 549 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
donatien | 5:791fc3dcb6c4 | 550 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 551 | trfLen += newTrfLen; |
donatien | 0:2ccb9960a044 | 552 | } |
donatien | 0:2ccb9960a044 | 553 | if( (buf[0] != '\r') || (buf[1] != '\n') ) |
donatien | 0:2ccb9960a044 | 554 | { |
donatien | 0:2ccb9960a044 | 555 | ERR("Format error"); |
donatien | 5:791fc3dcb6c4 | 556 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 557 | } |
donatien | 0:2ccb9960a044 | 558 | memmove(buf, &buf[2], trfLen - 2); |
donatien | 0:2ccb9960a044 | 559 | trfLen -= 2; |
donatien | 0:2ccb9960a044 | 560 | } |
donatien | 0:2ccb9960a044 | 561 | else |
donatien | 0:2ccb9960a044 | 562 | { |
donatien | 0:2ccb9960a044 | 563 | break; |
donatien | 0:2ccb9960a044 | 564 | } |
donatien | 0:2ccb9960a044 | 565 | |
donatien | 0:2ccb9960a044 | 566 | } |
donatien | 0:2ccb9960a044 | 567 | |
donatien | 7:4e39864f7b15 | 568 | m_sock.close(); |
donatien | 0:2ccb9960a044 | 569 | DBG("Completed HTTP transaction"); |
donatien | 0:2ccb9960a044 | 570 | |
donatien | 11:390362de8c3f | 571 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 572 | } |
donatien | 0:2ccb9960a044 | 573 | |
donatien | 11:390362de8c3f | 574 | HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure |
donatien | 0:2ccb9960a044 | 575 | { |
donatien | 0:2ccb9960a044 | 576 | DBG("Trying to read between %d and %d bytes", minLen, maxLen); |
donatien | 0:2ccb9960a044 | 577 | size_t readLen = 0; |
donatien | 12:89d09a6db00a | 578 | |
donatien | 12:89d09a6db00a | 579 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 580 | { |
donatien | 12:89d09a6db00a | 581 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 582 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 583 | } |
donatien | 12:89d09a6db00a | 584 | |
donatien | 7:4e39864f7b15 | 585 | int ret; |
donatien | 7:4e39864f7b15 | 586 | while(readLen < maxLen) |
donatien | 0:2ccb9960a044 | 587 | { |
donatien | 7:4e39864f7b15 | 588 | if(readLen < minLen) |
donatien | 0:2ccb9960a044 | 589 | { |
donatien | 12:89d09a6db00a | 590 | DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); |
donatien | 13:be61104f4e91 | 591 | m_sock.set_blocking(false, m_timeout); |
donatien | 12:89d09a6db00a | 592 | ret = m_sock.receive_all(buf + readLen, minLen - readLen); |
donatien | 0:2ccb9960a044 | 593 | } |
donatien | 7:4e39864f7b15 | 594 | else |
donatien | 7:4e39864f7b15 | 595 | { |
donatien | 12:89d09a6db00a | 596 | DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); |
donatien | 13:be61104f4e91 | 597 | m_sock.set_blocking(false, 0); |
donatien | 12:89d09a6db00a | 598 | ret = m_sock.receive(buf + readLen, maxLen - readLen); |
donatien | 7:4e39864f7b15 | 599 | } |
donatien | 7:4e39864f7b15 | 600 | |
donatien | 0:2ccb9960a044 | 601 | if( ret > 0) |
donatien | 0:2ccb9960a044 | 602 | { |
donatien | 0:2ccb9960a044 | 603 | readLen += ret; |
donatien | 0:2ccb9960a044 | 604 | } |
donatien | 0:2ccb9960a044 | 605 | else if( ret == 0 ) |
donatien | 0:2ccb9960a044 | 606 | { |
donatien | 7:4e39864f7b15 | 607 | break; |
donatien | 7:4e39864f7b15 | 608 | } |
donatien | 7:4e39864f7b15 | 609 | else |
donatien | 7:4e39864f7b15 | 610 | { |
donatien | 12:89d09a6db00a | 611 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 612 | { |
donatien | 12:89d09a6db00a | 613 | ERR("Connection error (recv returned %d)", ret); |
donatien | 12:89d09a6db00a | 614 | *pReadLen = readLen; |
donatien | 12:89d09a6db00a | 615 | return HTTP_CONN; |
donatien | 12:89d09a6db00a | 616 | } |
donatien | 12:89d09a6db00a | 617 | else |
donatien | 12:89d09a6db00a | 618 | { |
donatien | 12:89d09a6db00a | 619 | break; |
donatien | 12:89d09a6db00a | 620 | } |
donatien | 12:89d09a6db00a | 621 | } |
donatien | 12:89d09a6db00a | 622 | |
donatien | 12:89d09a6db00a | 623 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 624 | { |
donatien | 12:89d09a6db00a | 625 | break; |
donatien | 7:4e39864f7b15 | 626 | } |
donatien | 7:4e39864f7b15 | 627 | } |
donatien | 7:4e39864f7b15 | 628 | DBG("Read %d bytes", readLen); |
donatien | 7:4e39864f7b15 | 629 | *pReadLen = readLen; |
donatien | 11:390362de8c3f | 630 | return HTTP_OK; |
donatien | 7:4e39864f7b15 | 631 | } |
donatien | 7:4e39864f7b15 | 632 | |
donatien | 11:390362de8c3f | 633 | HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure |
donatien | 7:4e39864f7b15 | 634 | { |
donatien | 7:4e39864f7b15 | 635 | if(len == 0) |
donatien | 7:4e39864f7b15 | 636 | { |
donatien | 7:4e39864f7b15 | 637 | len = strlen(buf); |
donatien | 7:4e39864f7b15 | 638 | } |
donatien | 7:4e39864f7b15 | 639 | DBG("Trying to write %d bytes", len); |
donatien | 7:4e39864f7b15 | 640 | size_t writtenLen = 0; |
donatien | 12:89d09a6db00a | 641 | |
donatien | 12:89d09a6db00a | 642 | if(!m_sock.is_connected()) |
donatien | 12:89d09a6db00a | 643 | { |
donatien | 12:89d09a6db00a | 644 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 645 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 646 | } |
donatien | 7:4e39864f7b15 | 647 | |
donatien | 13:be61104f4e91 | 648 | m_sock.set_blocking(false, m_timeout); |
donatien | 12:89d09a6db00a | 649 | int ret = m_sock.send_all(buf, len); |
donatien | 12:89d09a6db00a | 650 | if(ret > 0) |
donatien | 7:4e39864f7b15 | 651 | { |
donatien | 12:89d09a6db00a | 652 | writtenLen += ret; |
donatien | 12:89d09a6db00a | 653 | } |
donatien | 12:89d09a6db00a | 654 | else if( ret == 0 ) |
donatien | 12:89d09a6db00a | 655 | { |
donatien | 12:89d09a6db00a | 656 | WARN("Connection was closed by server"); |
donatien | 12:89d09a6db00a | 657 | return HTTP_CLOSED; //Connection was closed by server |
donatien | 12:89d09a6db00a | 658 | } |
donatien | 12:89d09a6db00a | 659 | else |
donatien | 12:89d09a6db00a | 660 | { |
donatien | 12:89d09a6db00a | 661 | ERR("Connection error (send returned %d)", ret); |
donatien | 12:89d09a6db00a | 662 | return HTTP_CONN; |
donatien | 12:89d09a6db00a | 663 | } |
donatien | 7:4e39864f7b15 | 664 | |
donatien | 0:2ccb9960a044 | 665 | DBG("Written %d bytes", writtenLen); |
donatien | 11:390362de8c3f | 666 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 667 | } |
donatien | 0:2ccb9960a044 | 668 | |
donatien | 11:390362de8c3f | 669 | 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 | 670 | { |
donatien | 0:2ccb9960a044 | 671 | char* schemePtr = (char*) url; |
donatien | 0:2ccb9960a044 | 672 | char* hostPtr = (char*) strstr(url, "://"); |
donatien | 0:2ccb9960a044 | 673 | if(hostPtr == NULL) |
donatien | 0:2ccb9960a044 | 674 | { |
donatien | 0:2ccb9960a044 | 675 | WARN("Could not find host"); |
donatien | 11:390362de8c3f | 676 | return HTTP_PARSE; //URL is invalid |
donatien | 0:2ccb9960a044 | 677 | } |
donatien | 0:2ccb9960a044 | 678 | |
donatien | 0:2ccb9960a044 | 679 | if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 680 | { |
donatien | 0:2ccb9960a044 | 681 | WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); |
donatien | 11:390362de8c3f | 682 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 683 | } |
donatien | 0:2ccb9960a044 | 684 | memcpy(scheme, schemePtr, hostPtr - schemePtr); |
donatien | 0:2ccb9960a044 | 685 | scheme[hostPtr - schemePtr] = '\0'; |
donatien | 0:2ccb9960a044 | 686 | |
donatien | 0:2ccb9960a044 | 687 | hostPtr+=3; |
donatien | 0:2ccb9960a044 | 688 | |
donatien | 0:2ccb9960a044 | 689 | size_t hostLen = 0; |
donatien | 0:2ccb9960a044 | 690 | |
donatien | 0:2ccb9960a044 | 691 | char* portPtr = strchr(hostPtr, ':'); |
donatien | 0:2ccb9960a044 | 692 | if( portPtr != NULL ) |
donatien | 0:2ccb9960a044 | 693 | { |
donatien | 0:2ccb9960a044 | 694 | hostLen = portPtr - hostPtr; |
donatien | 0:2ccb9960a044 | 695 | portPtr++; |
donatien | 8:45c8da29a1cf | 696 | if( sscanf(portPtr, "%hu", port) != 1) |
donatien | 0:2ccb9960a044 | 697 | { |
donatien | 0:2ccb9960a044 | 698 | WARN("Could not find port"); |
donatien | 11:390362de8c3f | 699 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 700 | } |
donatien | 0:2ccb9960a044 | 701 | } |
donatien | 0:2ccb9960a044 | 702 | else |
donatien | 0:2ccb9960a044 | 703 | { |
donatien | 0:2ccb9960a044 | 704 | *port=0; |
donatien | 0:2ccb9960a044 | 705 | } |
donatien | 0:2ccb9960a044 | 706 | char* pathPtr = strchr(hostPtr, '/'); |
donatien | 0:2ccb9960a044 | 707 | if( hostLen == 0 ) |
donatien | 0:2ccb9960a044 | 708 | { |
donatien | 0:2ccb9960a044 | 709 | hostLen = pathPtr - hostPtr; |
donatien | 0:2ccb9960a044 | 710 | } |
donatien | 0:2ccb9960a044 | 711 | |
donatien | 0:2ccb9960a044 | 712 | if( maxHostLen < hostLen + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 713 | { |
donatien | 0:2ccb9960a044 | 714 | WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); |
donatien | 11:390362de8c3f | 715 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 716 | } |
donatien | 0:2ccb9960a044 | 717 | memcpy(host, hostPtr, hostLen); |
donatien | 0:2ccb9960a044 | 718 | host[hostLen] = '\0'; |
donatien | 0:2ccb9960a044 | 719 | |
donatien | 0:2ccb9960a044 | 720 | size_t pathLen; |
donatien | 0:2ccb9960a044 | 721 | char* fragmentPtr = strchr(hostPtr, '#'); |
donatien | 0:2ccb9960a044 | 722 | if(fragmentPtr != NULL) |
donatien | 0:2ccb9960a044 | 723 | { |
donatien | 0:2ccb9960a044 | 724 | pathLen = fragmentPtr - pathPtr; |
donatien | 0:2ccb9960a044 | 725 | } |
donatien | 0:2ccb9960a044 | 726 | else |
donatien | 0:2ccb9960a044 | 727 | { |
donatien | 0:2ccb9960a044 | 728 | pathLen = strlen(pathPtr); |
donatien | 0:2ccb9960a044 | 729 | } |
donatien | 0:2ccb9960a044 | 730 | |
donatien | 0:2ccb9960a044 | 731 | if( maxPathLen < pathLen + 1 ) //including NULL-terminating char |
donatien | 0:2ccb9960a044 | 732 | { |
donatien | 0:2ccb9960a044 | 733 | WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); |
donatien | 11:390362de8c3f | 734 | return HTTP_PARSE; |
donatien | 0:2ccb9960a044 | 735 | } |
donatien | 0:2ccb9960a044 | 736 | memcpy(path, pathPtr, pathLen); |
donatien | 0:2ccb9960a044 | 737 | path[pathLen] = '\0'; |
donatien | 0:2ccb9960a044 | 738 | |
donatien | 11:390362de8c3f | 739 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 740 | } |