Fork HTTPClient and Modfiy code for mbed 6.0

Dependents:   mbed-demo-http-get-json

Committer:
WiredHome
Date:
Sun Mar 09 16:39:40 2014 +0000
Revision:
22:d6b08d9749d6
Parent:
21:d33f7e1ce811
Child:
24:eee214e3e806
Minor change to reduce compiler warnings.

Who changed what in which revision?

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