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