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