Skip to content

Commit 9ab55ef

Browse files
committed
New API's to read header name and value as String's
1 parent 6251547 commit 9ab55ef

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

HttpClient.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,68 @@ int HttpClient::read()
372372
return ret;
373373
}
374374

375+
bool HttpClient::headerAvailable()
376+
{
377+
// clear the currently store header line
378+
iHeaderLine = "";
379+
380+
while (!endOfHeadersReached())
381+
{
382+
// read a byte from the header
383+
int c = readHeader();
384+
385+
if (c == '\r' || c == '\n')
386+
{
387+
if (iHeaderLine.length())
388+
{
389+
// end of the line, all done
390+
break;
391+
}
392+
else
393+
{
394+
// ignore any CR or LF characters
395+
continue;
396+
}
397+
}
398+
399+
// append byte to header line
400+
iHeaderLine += (char)c;
401+
}
402+
403+
return (iHeaderLine.length() > 0);
404+
}
405+
406+
String HttpClient::readHeaderName()
407+
{
408+
int colonIndex = iHeaderLine.indexOf(':');
409+
410+
if (colonIndex == -1)
411+
{
412+
return "";
413+
}
414+
415+
return iHeaderLine.substring(0, colonIndex);
416+
}
417+
418+
String HttpClient::readHeaderValue()
419+
{
420+
int colonIndex = iHeaderLine.indexOf(':');
421+
int startIndex = colonIndex + 1;
422+
423+
if (colonIndex == -1)
424+
{
425+
return "";
426+
}
427+
428+
// trim any leading whitespace
429+
while (startIndex < (int)iHeaderLine.length() && isSpace(iHeaderLine[startIndex]))
430+
{
431+
startIndex++;
432+
}
433+
434+
return iHeaderLine.substring(startIndex);
435+
}
436+
375437
int HttpClient::read(uint8_t *buf, size_t size)
376438
{
377439
int ret =iClient->read(buf, size);

HttpClient.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ class HttpClient : public Client
142142
*/
143143
int responseStatusCode();
144144

145+
/** Check if a header is available to be read.
146+
Use readHeaderName() to read header name, and readHeaderValue() to
147+
read the header value
148+
*/
149+
bool headerAvailable();
150+
151+
/** Read the name of the current response header.
152+
Returns empty string if a header is not available.
153+
*/
154+
String readHeaderName();
155+
156+
/** Read the vallue of the current response header.
157+
Returns empty string if a header is not available.
158+
*/
159+
String readHeaderValue();
160+
145161
/** Read the next character of the response headers.
146162
This functions in the same way as read() but to be used when reading
147163
through the headers. Check whether or not the end of the headers has
@@ -256,6 +272,7 @@ class HttpClient : public Client
256272
// How far through a Content-Length header prefix we are
257273
const char* iContentLengthPtr;
258274
uint32_t iHttpResponseTimeout;
275+
String iHeaderLine;
259276
};
260277

261278
#endif

0 commit comments

Comments
 (0)