mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Make skipResponseHeaders() optional, if contentLength() is called first
This commit is contained in:
parent
4b6f4dfa29
commit
255118660b
@ -384,6 +384,17 @@ int HttpClient::skipResponseHeaders()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int HttpClient::contentLength()
|
||||||
|
{
|
||||||
|
// skip the response headers, if they haven't been read already
|
||||||
|
if (!endOfHeadersReached())
|
||||||
|
{
|
||||||
|
skipResponseHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
return iContentLength;
|
||||||
|
}
|
||||||
|
|
||||||
bool HttpClient::endOfBodyReached()
|
bool HttpClient::endOfBodyReached()
|
||||||
{
|
{
|
||||||
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))
|
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))
|
||||||
|
@ -159,6 +159,7 @@ public:
|
|||||||
/** Check if a header is available to be read.
|
/** Check if a header is available to be read.
|
||||||
Use readHeaderName() to read header name, and readHeaderValue() to
|
Use readHeaderName() to read header name, and readHeaderValue() to
|
||||||
read the header value
|
read the header value
|
||||||
|
MUST be called after responseStatusCode() and before contentLength()
|
||||||
*/
|
*/
|
||||||
bool headerAvailable();
|
bool headerAvailable();
|
||||||
|
|
||||||
@ -177,6 +178,7 @@ public:
|
|||||||
through the headers. Check whether or not the end of the headers has
|
through the headers. Check whether or not the end of the headers has
|
||||||
been reached by calling endOfHeadersReached(), although after that point
|
been reached by calling endOfHeadersReached(), although after that point
|
||||||
this will still return data as read() would, but slightly less efficiently
|
this will still return data as read() would, but slightly less efficiently
|
||||||
|
MUST be called after responseStatusCode() and before contentLength()
|
||||||
@return The next character of the response headers
|
@return The next character of the response headers
|
||||||
*/
|
*/
|
||||||
int readHeader();
|
int readHeader();
|
||||||
@ -186,6 +188,7 @@ public:
|
|||||||
returned in the response. You can also use it after you've found all of
|
returned in the response. You can also use it after you've found all of
|
||||||
the headers you're interested in, and just want to get on with processing
|
the headers you're interested in, and just want to get on with processing
|
||||||
the body.
|
the body.
|
||||||
|
MUST be called after responseStatusCode()
|
||||||
@return HTTP_SUCCESS if successful, else an error code
|
@return HTTP_SUCCESS if successful, else an error code
|
||||||
*/
|
*/
|
||||||
int skipResponseHeaders();
|
int skipResponseHeaders();
|
||||||
@ -204,10 +207,12 @@ public:
|
|||||||
virtual bool completed() { return endOfBodyReached(); };
|
virtual bool completed() { return endOfBodyReached(); };
|
||||||
|
|
||||||
/** Return the length of the body.
|
/** Return the length of the body.
|
||||||
|
Also skips response headers if they have not been read already
|
||||||
|
MUST be called after responseStatusCode()
|
||||||
@return Length of the body, in bytes, or kNoContentLengthHeader if no
|
@return Length of the body, in bytes, or kNoContentLengthHeader if no
|
||||||
Content-Length header was returned by the server
|
Content-Length header was returned by the server
|
||||||
*/
|
*/
|
||||||
int contentLength() { return iContentLength; };
|
int contentLength();
|
||||||
|
|
||||||
// Inherited from Print
|
// Inherited from Print
|
||||||
// Note: 1st call to these indicates the user is sending the body, so if need
|
// Note: 1st call to these indicates the user is sending the body, so if need
|
||||||
|
@ -68,45 +68,44 @@ void loop()
|
|||||||
// similar "success" code (200-299) before carrying on,
|
// similar "success" code (200-299) before carrying on,
|
||||||
// but we'll print out whatever response we get
|
// but we'll print out whatever response we get
|
||||||
|
|
||||||
err = http.skipResponseHeaders();
|
// If you are interesting in the response headers, you
|
||||||
if (err >= 0)
|
// can read them here:
|
||||||
|
//while(http.headerAvailable())
|
||||||
|
//{
|
||||||
|
// String headerName = http.readHeaderName();
|
||||||
|
// String headerValue = http.readHeaderValue();
|
||||||
|
//}
|
||||||
|
|
||||||
|
int bodyLen = http.contentLength();
|
||||||
|
Serial.print("Content length is: ");
|
||||||
|
Serial.println(bodyLen);
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("Body returned follows:");
|
||||||
|
|
||||||
|
// Now we've got to the body, so we can print it out
|
||||||
|
unsigned long timeoutStart = millis();
|
||||||
|
char c;
|
||||||
|
// Whilst we haven't timed out & haven't reached the end of the body
|
||||||
|
while ( (http.connected() || http.available()) &&
|
||||||
|
(bodyLen > 0 || bodyLen != HttpClient::kNoContentLengthHeader) &&
|
||||||
|
((millis() - timeoutStart) < kNetworkTimeout) )
|
||||||
{
|
{
|
||||||
int bodyLen = http.contentLength();
|
if (http.available())
|
||||||
Serial.print("Content length is: ");
|
{
|
||||||
Serial.println(bodyLen);
|
c = http.read();
|
||||||
Serial.println();
|
// Print out this character
|
||||||
Serial.println("Body returned follows:");
|
Serial.print(c);
|
||||||
|
|
||||||
// Now we've got to the body, so we can print it out
|
bodyLen--;
|
||||||
unsigned long timeoutStart = millis();
|
// We read something, reset the timeout counter
|
||||||
char c;
|
timeoutStart = millis();
|
||||||
// Whilst we haven't timed out & haven't reached the end of the body
|
}
|
||||||
while ( (http.connected() || http.available()) &&
|
else
|
||||||
(bodyLen > 0 || bodyLen != HttpClient::kNoContentLengthHeader) &&
|
{
|
||||||
((millis() - timeoutStart) < kNetworkTimeout) )
|
// We haven't got any data, so let's pause to allow some to
|
||||||
{
|
// arrive
|
||||||
if (http.available())
|
delay(kNetworkDelay);
|
||||||
{
|
}
|
||||||
c = http.read();
|
|
||||||
// Print out this character
|
|
||||||
Serial.print(c);
|
|
||||||
|
|
||||||
bodyLen--;
|
|
||||||
// We read something, reset the timeout counter
|
|
||||||
timeoutStart = millis();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// We haven't got any data, so let's pause to allow some to
|
|
||||||
// arrive
|
|
||||||
delay(kNetworkDelay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Serial.print("Failed to skip response headers: ");
|
|
||||||
Serial.println(err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Loading…
x
Reference in New Issue
Block a user