Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HTTPClient by
Diff: HTTPClient.cpp
- Revision:
- 20:4ea5255c1b04
- Parent:
- 19:bcbf0af9fac3
- Child:
- 21:d33f7e1ce811
diff -r bcbf0af9fac3 -r 4ea5255c1b04 HTTPClient.cpp --- a/HTTPClient.cpp Fri Jan 24 13:51:36 2014 +0000 +++ b/HTTPClient.cpp Sun Feb 02 16:47:06 2014 +0000 @@ -18,22 +18,21 @@ */ //Debug is disabled by default -#if 0 -//Enable debug +//#define DEBUG "HTCL" #include <cstdio> -#define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); -#define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); -#define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); - +#if (defined(DEBUG) && !defined(TARGET_LPC11U24)) +#define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); #else -//Disable debug -#define DBG(x, ...) +#define DBG(x, ...) #define WARN(x, ...) -#define ERR(x, ...) - +#define ERR(x, ...) +#define INFO(x, ...) #endif -#define HTTP_PORT 80 +//#define HTTP_PORT 80 #define OK 0 @@ -47,7 +46,7 @@ #include "HTTPClient.h" HTTPClient::HTTPClient() : -m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0) + m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0) { } @@ -59,8 +58,8 @@ void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification { - m_basicAuthUser = user; - m_basicAuthPassword = password; + m_basicAuthUser = user; + m_basicAuthPassword = password; } void HTTPClient::customHeaders(const char **headers, size_t pairs) @@ -72,34 +71,34 @@ HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { - return connect(url, HTTP_GET, NULL, pDataIn, timeout); + return connect(url, HTTP_GET, NULL, pDataIn, timeout); } HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { - HTTPText str(result, maxResultLen); - return get(url, &str, timeout); + HTTPText str(result, maxResultLen); + return get(url, &str, timeout); } HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { - return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout); + return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout); } HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { - return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); + return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); } HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking { - return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); + return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); } int HTTPClient::getHTTPResponseCode() { - return m_httpResponseCode; + return m_httpResponseCode; } #define CHECK_CONN_ERR(ret) \ @@ -119,592 +118,501 @@ } while(0) HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request -{ - m_httpResponseCode = 0; //Invalidate code - m_timeout = timeout; - - pDataIn->writeReset(); - if( pDataOut ) - { - pDataOut->readReset(); - } +{ + m_httpResponseCode = 0; //Invalidate code + m_timeout = timeout; + + pDataIn->writeReset(); + if( pDataOut ) { + pDataOut->readReset(); + } - char scheme[8]; - uint16_t port; - char host[32]; - char path[64]; - //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) - HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); - if(res != HTTP_OK) - { - ERR("parseURL returned %d", res); - return res; - } - - if(port == 0) //TODO do handle HTTPS->443 - { - port = 80; - } - - DBG("Scheme: %s", scheme); - DBG("Host: %s", host); - DBG("Port: %d", port); - DBG("Path: %s", path); + char scheme[8]; + uint16_t port; + char host[32]; + char path[64]; + //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?) + HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); + if(res != HTTP_OK) { + ERR("parseURL returned %d", res); + return res; + } - //Connect - DBG("Connecting socket to server"); - int ret = m_sock.connect(host, port); - if (ret < 0) - { - m_sock.close(); - ERR("Could not connect"); - return HTTP_CONN; - } + if(port == 0) { //TODO do handle HTTPS->443 + port = 80; + } + + DBG("Scheme: %s", scheme); + DBG("Host: %s", host); + DBG("Port: %d", port); + DBG("Path: %s", path); - //Send request - DBG("Sending request"); - char buf[CHUNK_SIZE]; - const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; - snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request - ret = send(buf); - if(ret) - { - m_sock.close(); - ERR("Could not write request"); - return HTTP_CONN; - } - - // send authorization - if ((!m_basicAuthUser) && (!m_basicAuthPass)) { - strcpy(buf, "Authorization: "); - createauth(m_basicAuthUser, m_basicAuthPass, buf+strlen(buf), sizeof(buf)-strlen(buf)); - strcat(buf, "\r\n"); - - ret = send(buf); - if(ret) - { - m_sock.close(); - ERR("Could not write request"); - return HTTP_CONN; + //Connect + DBG("Connecting socket to server"); + int ret = m_sock.connect(host, port); + if (ret < 0) { + m_sock.close(); + ERR("Could not connect"); + return HTTP_CONN; } - } - //Send all headers - for (size_t nh = 0; nh < m_nCustomHeaders; ++nh) { - snprintf(buf, sizeof(buf), "%s: %s\r\n", m_customHeaders[nh], m_customHeaders[nh+1]); + //Send request + DBG("Sending request"); + char buf[CHUNK_SIZE]; + const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; + 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 ret = send(buf); - if(ret) - { - m_sock.close(); - ERR("Could not write request"); - return HTTP_CONN; - } - } - - //Send default headers - DBG("Sending headers"); - if( pDataOut != NULL ) - { - if( pDataOut->getIsChunked() ) - { - ret = send("Transfer-Encoding: chunked\r\n"); - CHECK_CONN_ERR(ret); + if (ret) { + m_sock.close(); + ERR("Could not write request"); + return HTTP_CONN; } - else - { - snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); - ret = send(buf); - CHECK_CONN_ERR(ret); + + // send authorization + if (m_basicAuthUser && m_basicAuthPassword) { + strcpy(buf, "Authorization: Basic "); + createauth(m_basicAuthUser, m_basicAuthPassword, buf+strlen(buf), sizeof(buf)-strlen(buf)); + strcat(buf, "\r\n"); + INFO(" (%s,%s) => (%s)", m_basicAuthUser, m_basicAuthPassword, buf); + ret = send(buf); + INFO(" ret = %d", ret); + if(ret) { + m_sock.close(); + ERR("Could not write request"); + return HTTP_CONN; + } } - char type[48]; - if( pDataOut->getDataType(type, 48) == HTTP_OK ) - { - snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); - ret = send(buf); - CHECK_CONN_ERR(ret); + + //Send all headers + for (size_t nh = 0; nh < m_nCustomHeaders * 2; nh+=2) { + INFO("hdr[%d] %s:", nh, m_customHeaders[nh]); + INFO(" %s", m_customHeaders[nh+1]); + snprintf(buf, sizeof(buf), "%s: %s\r\n", m_customHeaders[nh], m_customHeaders[nh+1]); + ret = send(buf); + if (ret) { + ERR("closing"); + wait_ms(50); + m_sock.close(); + ERR("Could not write request"); + return HTTP_CONN; + } + INFO(" hdr %d", ret); } - } - - //Close headers - DBG("Headers sent"); - ret = send("\r\n"); - CHECK_CONN_ERR(ret); - size_t trfLen; - - //Send data (if available) - if( pDataOut != NULL ) - { - DBG("Sending data"); - while(true) - { - size_t writtenLen = 0; - pDataOut->read(buf, CHUNK_SIZE, &trfLen); - if( pDataOut->getIsChunked() ) - { - //Write chunk header - char chunkHeader[16]; - snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding - ret = send(chunkHeader); - CHECK_CONN_ERR(ret); - } - else if( trfLen == 0 ) - { - break; - } - if( trfLen != 0 ) - { - ret = send(buf, trfLen); - CHECK_CONN_ERR(ret); - } + //Send default headers + DBG("Sending headers"); + if( pDataOut != NULL ) { + if( pDataOut->getIsChunked() ) { + ret = send("Transfer-Encoding: chunked\r\n"); + CHECK_CONN_ERR(ret); + } else { + snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); + ret = send(buf); + CHECK_CONN_ERR(ret); + } + char type[48]; + if( pDataOut->getDataType(type, 48) == HTTP_OK ) { + snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); + ret = send(buf); + CHECK_CONN_ERR(ret); + } + } + + //Close headers + DBG("Headers sent"); + ret = send("\r\n"); + CHECK_CONN_ERR(ret); + + size_t trfLen; - if( pDataOut->getIsChunked() ) - { - ret = send("\r\n"); //Chunk-terminating CRLF - CHECK_CONN_ERR(ret); - } - else - { - writtenLen += trfLen; - if( writtenLen >= pDataOut->getDataLen() ) - { - break; + //Send data (if available) + if( pDataOut != NULL ) { + DBG("Sending data"); + while(true) { + size_t writtenLen = 0; + pDataOut->read(buf, CHUNK_SIZE, &trfLen); + if( pDataOut->getIsChunked() ) { + //Write chunk header + char chunkHeader[16]; + snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding + ret = send(chunkHeader); + CHECK_CONN_ERR(ret); + } else if( trfLen == 0 ) { + break; + } + if( trfLen != 0 ) { + ret = send(buf, trfLen); + CHECK_CONN_ERR(ret); + } + + if( pDataOut->getIsChunked() ) { + ret = send("\r\n"); //Chunk-terminating CRLF + CHECK_CONN_ERR(ret); + } else { + writtenLen += trfLen; + if( writtenLen >= pDataOut->getDataLen() ) { + break; + } + } + + if( trfLen == 0 ) { + break; + } } - } + + } - if( trfLen == 0 ) - { - break; - } + //Receive response + DBG("Receiving response"); + ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes + CHECK_CONN_ERR(ret); + buf[trfLen] = '\0'; + INFO("Received \r\n(%s\r\n)", buf); + + char* crlfPtr = strstr(buf, "\r\n"); + if( crlfPtr == NULL) { + PRTCL_ERR(); } - } - - //Receive response - DBG("Receiving response"); - ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes - CHECK_CONN_ERR(ret); - - buf[trfLen] = '\0'; - - char* crlfPtr = strstr(buf, "\r\n"); - if(crlfPtr == NULL) - { - PRTCL_ERR(); - } - - int crlfPos = crlfPtr - buf; - buf[crlfPos] = '\0'; - - //Parse HTTP response - if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) - { - //Cannot match string, error - ERR("Not a correct HTTP answer : %s\n", buf); - PRTCL_ERR(); - } + int crlfPos = crlfPtr - buf; + buf[crlfPos] = '\0'; - if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) - { - //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers - WARN("Response code %d", m_httpResponseCode); - PRTCL_ERR(); - } - - DBG("Reading headers"); - - memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well - trfLen -= (crlfPos + 2); + //Parse HTTP response + if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) { + //Cannot match string, error + ERR("Not a correct HTTP answer : %s\n", buf); + PRTCL_ERR(); + } - size_t recvContentLength = 0; - bool recvChunked = false; - //Now get headers - while( true ) - { - crlfPtr = strstr(buf, "\r\n"); - if(crlfPtr == NULL) - { - if( trfLen < CHUNK_SIZE - 1 ) - { - size_t newTrfLen; - ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); - trfLen += newTrfLen; - buf[trfLen] = '\0'; - DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); - CHECK_CONN_ERR(ret); - continue; - } - else - { + if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) { + //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers + WARN("Response code %d", m_httpResponseCode); PRTCL_ERR(); - } } - crlfPos = crlfPtr - buf; + DBG("Reading headers"); + + memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well + trfLen -= (crlfPos + 2); - if(crlfPos == 0) //End of headers - { - DBG("Headers read"); - memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well - trfLen -= 2; - break; - } + size_t recvContentLength = 0; + bool recvChunked = false; + //Now get headers + while( true ) { + crlfPtr = strstr(buf, "\r\n"); + if(crlfPtr == NULL) { + if( trfLen < CHUNK_SIZE - 1 ) { + size_t newTrfLen; + ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); + trfLen += newTrfLen; + buf[trfLen] = '\0'; + DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); + CHECK_CONN_ERR(ret); + continue; + } else { + PRTCL_ERR(); + } + } - buf[crlfPos] = '\0'; - - char key[32]; - char value[32]; - - key[31] = '\0'; - value[31] = '\0'; + crlfPos = crlfPtr - buf; - int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); - if ( n == 2 ) - { - DBG("Read header : %s: %s\n", key, value); - if( !strcmp(key, "Content-Length") ) - { - sscanf(value, "%d", &recvContentLength); - pDataIn->setDataLen(recvContentLength); - } - else if( !strcmp(key, "Transfer-Encoding") ) - { - if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) - { - recvChunked = true; - pDataIn->setIsChunked(true); + if(crlfPos == 0) { //End of headers + DBG("Headers read"); + memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well + trfLen -= 2; + break; } - } - else if( !strcmp(key, "Content-Type") ) - { - pDataIn->setDataType(value); - } + + buf[crlfPos] = '\0'; + + char key[32]; + char value[32]; + + key[31] = '\0'; + value[31] = '\0'; - memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well - trfLen -= (crlfPos + 2); + int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); + if ( n == 2 ) { + DBG("Read header : %s: %s\n", key, value); + if( !strcmp(key, "Content-Length") ) { + sscanf(value, "%d", &recvContentLength); + pDataIn->setDataLen(recvContentLength); + } else if( !strcmp(key, "Transfer-Encoding") ) { + if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) { + recvChunked = true; + pDataIn->setIsChunked(true); + } + } else if( !strcmp(key, "Content-Type") ) { + pDataIn->setDataType(value); + } - } - else - { - ERR("Could not parse header"); - PRTCL_ERR(); + memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well + trfLen -= (crlfPos + 2); + + } else { + ERR("Could not parse header"); + PRTCL_ERR(); + } + } - } - - //Receive data - DBG("Receiving data"); - while(true) - { - size_t readLen = 0; + //Receive data + DBG("Receiving data"); + while(true) { + size_t readLen = 0; - if( recvChunked ) - { - //Read chunk header - bool foundCrlf; - do - { - foundCrlf = false; - crlfPos=0; - buf[trfLen]=0; - if(trfLen >= 2) - { - for(; crlfPos < trfLen - 2; crlfPos++) - { - if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) - { - foundCrlf = true; - break; + if( recvChunked ) { + //Read chunk header + bool foundCrlf; + do { + foundCrlf = false; + crlfPos=0; + buf[trfLen]=0; + if(trfLen >= 2) { + for(; crlfPos < trfLen - 2; crlfPos++) { + if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) { + foundCrlf = true; + break; + } + } + } + if(!foundCrlf) { //Try to read more + if( trfLen < CHUNK_SIZE ) { + size_t newTrfLen; + ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); + trfLen += newTrfLen; + CHECK_CONN_ERR(ret); + continue; + } else { + PRTCL_ERR(); + } + } + } while(!foundCrlf); + buf[crlfPos] = '\0'; + int n = sscanf(buf, "%x", &readLen); + if(n!=1) { + ERR("Could not read chunk length"); + PRTCL_ERR(); } - } + + memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more + trfLen -= (crlfPos + 2); + + if( readLen == 0 ) { + //Last chunk + break; + } + } else { + readLen = recvContentLength; } - if(!foundCrlf) //Try to read more - { - if( trfLen < CHUNK_SIZE ) - { - size_t newTrfLen; - ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); - trfLen += newTrfLen; - CHECK_CONN_ERR(ret); - continue; - } - else - { - PRTCL_ERR(); - } + + DBG("Retrieving %d bytes", readLen); + + do { + pDataIn->write(buf, MIN(trfLen, readLen)); + if( trfLen > readLen ) { + memmove(buf, &buf[readLen], trfLen - readLen); + trfLen -= readLen; + readLen = 0; + } else { + readLen -= trfLen; + } + + if(readLen) { + ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); + CHECK_CONN_ERR(ret); + } + } while(readLen); + + if( recvChunked ) { + if(trfLen < 2) { + size_t newTrfLen; + //Read missing chars to find end of chunk + ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); + CHECK_CONN_ERR(ret); + trfLen += newTrfLen; + } + if( (buf[0] != '\r') || (buf[1] != '\n') ) { + ERR("Format error"); + PRTCL_ERR(); + } + memmove(buf, &buf[2], trfLen - 2); + trfLen -= 2; + } else { + break; } - } while(!foundCrlf); - buf[crlfPos] = '\0'; - int n = sscanf(buf, "%x", &readLen); - if(n!=1) - { - ERR("Could not read chunk length"); - PRTCL_ERR(); - } - memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more - trfLen -= (crlfPos + 2); - - if( readLen == 0 ) - { - //Last chunk - break; - } - } - else - { - readLen = recvContentLength; } - DBG("Retrieving %d bytes", readLen); - - do - { - pDataIn->write(buf, MIN(trfLen, readLen)); - if( trfLen > readLen ) - { - memmove(buf, &buf[readLen], trfLen - readLen); - trfLen -= readLen; - readLen = 0; - } - else - { - readLen -= trfLen; - } - - if(readLen) - { - ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); - CHECK_CONN_ERR(ret); - } - } while(readLen); + m_sock.close(); + DBG("Completed HTTP transaction"); - if( recvChunked ) - { - if(trfLen < 2) - { - size_t newTrfLen; - //Read missing chars to find end of chunk - ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); - CHECK_CONN_ERR(ret); - trfLen += newTrfLen; - } - if( (buf[0] != '\r') || (buf[1] != '\n') ) - { - ERR("Format error"); - PRTCL_ERR(); - } - memmove(buf, &buf[2], trfLen - 2); - trfLen -= 2; - } - else - { - break; - } - - } - - m_sock.close(); - DBG("Completed HTTP transaction"); - - return HTTP_OK; + return HTTP_OK; } HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure { - DBG("Trying to read between %d and %d bytes", minLen, maxLen); - size_t readLen = 0; - - if(!m_sock.is_connected()) - { - WARN("Connection was closed by server"); - return HTTP_CLOSED; //Connection was closed by server - } - - int ret; - while(readLen < maxLen) - { - if(readLen < minLen) - { - DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); - m_sock.set_blocking(false, m_timeout); - ret = m_sock.receive_all(buf + readLen, minLen - readLen); - } - else - { - DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); - m_sock.set_blocking(false, 0); - ret = m_sock.receive(buf + readLen, maxLen - readLen); + DBG("Trying to read between %d and %d bytes", minLen, maxLen); + size_t readLen = 0; + + if (!m_sock.is_connected()) { + WARN("Connection was closed by server"); + return HTTP_CLOSED; //Connection was closed by server } - - if( ret > 0) - { - readLen += ret; - } - else if( ret == 0 ) - { - break; + + int ret; + while (readLen < maxLen) { + if (readLen < minLen) { + DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); + m_sock.set_blocking(false, m_timeout); + ret = m_sock.receive_all(buf + readLen, minLen - readLen); + } else { + DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); + m_sock.set_blocking(false, 0); + ret = m_sock.receive(buf + readLen, maxLen - readLen); + } + + if (ret > 0) { + readLen += ret; + } else if ( ret == 0 ) { + break; + } else { + if (!m_sock.is_connected()) { + ERR("Connection error (recv returned %d)", ret); + *pReadLen = readLen; + return HTTP_CONN; + } else { + break; + } + } + if (!m_sock.is_connected()) { + break; + } } - else - { - if(!m_sock.is_connected()) - { - ERR("Connection error (recv returned %d)", ret); - *pReadLen = readLen; - return HTTP_CONN; - } - else - { - break; - } - } - - if(!m_sock.is_connected()) - { - break; - } - } - DBG("Read %d bytes", readLen); - *pReadLen = readLen; - return HTTP_OK; + DBG("Read %d bytes", readLen); + *pReadLen = readLen; + return HTTP_OK; } HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure { - if(len == 0) - { - len = strlen(buf); - } - DBG("Trying to write %d bytes", len); - size_t writtenLen = 0; - - if(!m_sock.is_connected()) - { - WARN("Connection was closed by server"); - return HTTP_CLOSED; //Connection was closed by server - } - - m_sock.set_blocking(false, m_timeout); - int ret = m_sock.send_all(buf, len); - if(ret > 0) - { - writtenLen += ret; - } - else if( ret == 0 ) - { - WARN("Connection was closed by server"); - return HTTP_CLOSED; //Connection was closed by server - } - else - { - ERR("Connection error (send returned %d)", ret); - return HTTP_CONN; - } - - DBG("Written %d bytes", writtenLen); - return HTTP_OK; + if(len == 0) { + len = strlen(buf); + } + DBG("send(%s,%d)", buf, len); + size_t writtenLen = 0; + + if(!m_sock.is_connected()) { + WARN("Connection was closed by server"); + return HTTP_CLOSED; //Connection was closed by server + } + + m_sock.set_blocking(false, m_timeout); + int ret = m_sock.send_all(buf, len); + if(ret > 0) { + writtenLen += ret; + } else if( ret == 0 ) { + WARN("Connection was closed by server"); + return HTTP_CLOSED; //Connection was closed by server + } else { + ERR("Connection error (send returned %d)", ret); + return HTTP_CONN; + } + + DBG("Written %d bytes", writtenLen); + return HTTP_OK; } 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 { - char* schemePtr = (char*) url; - char* hostPtr = (char*) strstr(url, "://"); - if(hostPtr == NULL) - { - WARN("Could not find host"); - return HTTP_PARSE; //URL is invalid - } + char* schemePtr = (char*) url; + char* hostPtr = (char*) strstr(url, "://"); + if(hostPtr == NULL) { + WARN("Could not find host"); + return HTTP_PARSE; //URL is invalid + } + + if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char + WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); + return HTTP_PARSE; + } + memcpy(scheme, schemePtr, hostPtr - schemePtr); + scheme[hostPtr - schemePtr] = '\0'; - if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char - { - WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); - return HTTP_PARSE; - } - memcpy(scheme, schemePtr, hostPtr - schemePtr); - scheme[hostPtr - schemePtr] = '\0'; + hostPtr+=3; - hostPtr+=3; + size_t hostLen = 0; - size_t hostLen = 0; - - char* portPtr = strchr(hostPtr, ':'); - if( portPtr != NULL ) - { - hostLen = portPtr - hostPtr; - portPtr++; - if( sscanf(portPtr, "%hu", port) != 1) - { - WARN("Could not find port"); - return HTTP_PARSE; + char* portPtr = strchr(hostPtr, ':'); + if( portPtr != NULL ) { + hostLen = portPtr - hostPtr; + portPtr++; + if( sscanf(portPtr, "%hu", port) != 1) { + WARN("Could not find port"); + return HTTP_PARSE; + } + } else { + *port=0; } - } - else - { - *port=0; - } - char* pathPtr = strchr(hostPtr, '/'); - if( hostLen == 0 ) - { - hostLen = pathPtr - hostPtr; - } + char* pathPtr = strchr(hostPtr, '/'); + if( hostLen == 0 ) { + hostLen = pathPtr - hostPtr; + } - if( maxHostLen < hostLen + 1 ) //including NULL-terminating char - { - WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); - return HTTP_PARSE; - } - memcpy(host, hostPtr, hostLen); - host[hostLen] = '\0'; + if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char + WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); + return HTTP_PARSE; + } + memcpy(host, hostPtr, hostLen); + host[hostLen] = '\0'; - size_t pathLen; - char* fragmentPtr = strchr(hostPtr, '#'); - if(fragmentPtr != NULL) - { - pathLen = fragmentPtr - pathPtr; - } - else - { - pathLen = strlen(pathPtr); - } + size_t pathLen; + char* fragmentPtr = strchr(hostPtr, '#'); + if(fragmentPtr != NULL) { + pathLen = fragmentPtr - pathPtr; + } else { + pathLen = strlen(pathPtr); + } - if( maxPathLen < pathLen + 1 ) //including NULL-terminating char - { - WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); - return HTTP_PARSE; - } - memcpy(path, pathPtr, pathLen); - path[pathLen] = '\0'; + if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char + WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); + return HTTP_PARSE; + } + memcpy(path, pathPtr, pathLen); + path[pathLen] = '\0'; - return HTTP_OK; + return HTTP_OK; } -void HTTPClient::createauth (const char *user, const char *pwd, char *buf, int len) { +void HTTPClient::createauth (const char *user, const char *pwd, char *buf, int len) +{ char tmp[80]; - + snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf)); } // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) -int HTTPClient::base64enc(const char *input, unsigned int length, char *output, int len) { - static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - unsigned int c, c1, c2, c3; - - if (len < ((((length-1)/3)+1)<<2)) return -1; - for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { - c1 = ((((unsigned char)*((unsigned char *)&input[i])))); - c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; - c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; - - c = ((c1 & 0xFC) >> 2); - output[j+0] = base64[c]; - c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); - output[j+1] = base64[c]; - c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); - output[j+2] = (length>i+1)?base64[c]:'='; - c = (c3 & 0x3F); - output[j+3] = (length>i+2)?base64[c]:'='; - } - output[(((length-1)/3)+1)<<2] = '\0'; - return 0; +int HTTPClient::base64enc(const char *input, unsigned int length, char *output, int len) +{ + static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + unsigned int c, c1, c2, c3; + + if (len < ((((length-1)/3)+1)<<2)) return -1; + for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { + c1 = ((((unsigned char)*((unsigned char *)&input[i])))); + c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; + c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; + + c = ((c1 & 0xFC) >> 2); + output[j+0] = base64[c]; + c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); + output[j+1] = base64[c]; + c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); + output[j+2] = (length>i+1)?base64[c]:'='; + c = (c3 & 0x3F); + output[j+3] = (length>i+2)?base64[c]:'='; + } + output[(((length-1)/3)+1)<<2] = '\0'; + return 0; }