diff --git a/HttpClient.cpp b/HttpClient.cpp index 600820f..d969ce4 100644 --- a/HttpClient.cpp +++ b/HttpClient.cpp @@ -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); diff --git a/HttpClient.h b/HttpClient.h index 5d48008..e8312f8 100644 --- a/HttpClient.h +++ b/HttpClient.h @@ -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