1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-04-19 21:22:15 +03:00

New API's to read header name and value as String's

This commit is contained in:
Sandeep Mistry 2016-06-17 12:48:49 -04:00
parent 625154754a
commit 9ab55ef6e2
2 changed files with 79 additions and 0 deletions

View File

@ -372,6 +372,68 @@ int HttpClient::read()
return ret;
}
bool HttpClient::headerAvailable()
{
// clear the currently store header line
iHeaderLine = "";
while (!endOfHeadersReached())
{
// read a byte from the header
int c = readHeader();
if (c == '\r' || c == '\n')
{
if (iHeaderLine.length())
{
// end of the line, all done
break;
}
else
{
// ignore any CR or LF characters
continue;
}
}
// append byte to header line
iHeaderLine += (char)c;
}
return (iHeaderLine.length() > 0);
}
String HttpClient::readHeaderName()
{
int colonIndex = iHeaderLine.indexOf(':');
if (colonIndex == -1)
{
return "";
}
return iHeaderLine.substring(0, colonIndex);
}
String HttpClient::readHeaderValue()
{
int colonIndex = iHeaderLine.indexOf(':');
int startIndex = colonIndex + 1;
if (colonIndex == -1)
{
return "";
}
// trim any leading whitespace
while (startIndex < (int)iHeaderLine.length() && isSpace(iHeaderLine[startIndex]))
{
startIndex++;
}
return iHeaderLine.substring(startIndex);
}
int HttpClient::read(uint8_t *buf, size_t size)
{
int ret =iClient->read(buf, size);

View File

@ -142,6 +142,22 @@ public:
*/
int responseStatusCode();
/** Check if a header is available to be read.
Use readHeaderName() to read header name, and readHeaderValue() to
read the header value
*/
bool headerAvailable();
/** Read the name of the current response header.
Returns empty string if a header is not available.
*/
String readHeaderName();
/** Read the vallue of the current response header.
Returns empty string if a header is not available.
*/
String readHeaderValue();
/** Read the next character of the response headers.
This functions in the same way as read() but to be used when reading
through the headers. Check whether or not the end of the headers has
@ -256,6 +272,7 @@ protected:
// How far through a Content-Length header prefix we are
const char* iContentLengthPtr;
uint32_t iHttpResponseTimeout;
String iHeaderLine;
};
#endif