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