Skip to content

Commit 2551186

Browse files
committed
Make skipResponseHeaders() optional, if contentLength() is called first
1 parent 4b6f4df commit 2551186

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

HttpClient.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ int HttpClient::skipResponseHeaders()
384384
}
385385
}
386386

387+
int HttpClient::contentLength()
388+
{
389+
// skip the response headers, if they haven't been read already
390+
if (!endOfHeadersReached())
391+
{
392+
skipResponseHeaders();
393+
}
394+
395+
return iContentLength;
396+
}
397+
387398
bool HttpClient::endOfBodyReached()
388399
{
389400
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))

HttpClient.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class HttpClient : public Client
159159
/** Check if a header is available to be read.
160160
Use readHeaderName() to read header name, and readHeaderValue() to
161161
read the header value
162+
MUST be called after responseStatusCode() and before contentLength()
162163
*/
163164
bool headerAvailable();
164165

@@ -177,6 +178,7 @@ class HttpClient : public Client
177178
through the headers. Check whether or not the end of the headers has
178179
been reached by calling endOfHeadersReached(), although after that point
179180
this will still return data as read() would, but slightly less efficiently
181+
MUST be called after responseStatusCode() and before contentLength()
180182
@return The next character of the response headers
181183
*/
182184
int readHeader();
@@ -186,6 +188,7 @@ class HttpClient : public Client
186188
returned in the response. You can also use it after you've found all of
187189
the headers you're interested in, and just want to get on with processing
188190
the body.
191+
MUST be called after responseStatusCode()
189192
@return HTTP_SUCCESS if successful, else an error code
190193
*/
191194
int skipResponseHeaders();
@@ -204,10 +207,12 @@ class HttpClient : public Client
204207
virtual bool completed() { return endOfBodyReached(); };
205208

206209
/** Return the length of the body.
210+
Also skips response headers if they have not been read already
211+
MUST be called after responseStatusCode()
207212
@return Length of the body, in bytes, or kNoContentLengthHeader if no
208213
Content-Length header was returned by the server
209214
*/
210-
int contentLength() { return iContentLength; };
215+
int contentLength();
211216

212217
// Inherited from Print
213218
// Note: 1st call to these indicates the user is sending the body, so if need

examples/SimpleHttpExample/SimpleHttpExample.ino

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -68,45 +68,44 @@ void loop()
6868
// similar "success" code (200-299) before carrying on,
6969
// but we'll print out whatever response we get
7070

71-
err = http.skipResponseHeaders();
72-
if (err >= 0)
73-
{
74-
int bodyLen = http.contentLength();
75-
Serial.print("Content length is: ");
76-
Serial.println(bodyLen);
77-
Serial.println();
78-
Serial.println("Body returned follows:");
79-
80-
// Now we've got to the body, so we can print it out
81-
unsigned long timeoutStart = millis();
82-
char c;
83-
// Whilst we haven't timed out & haven't reached the end of the body
84-
while ( (http.connected() || http.available()) &&
85-
(bodyLen > 0 || bodyLen != HttpClient::kNoContentLengthHeader) &&
86-
((millis() - timeoutStart) < kNetworkTimeout) )
87-
{
88-
if (http.available())
89-
{
90-
c = http.read();
91-
// Print out this character
92-
Serial.print(c);
93-
94-
bodyLen--;
95-
// We read something, reset the timeout counter
96-
timeoutStart = millis();
97-
}
98-
else
99-
{
100-
// We haven't got any data, so let's pause to allow some to
101-
// arrive
102-
delay(kNetworkDelay);
103-
}
104-
}
105-
}
106-
else
71+
// If you are interesting in the response headers, you
72+
// can read them here:
73+
//while(http.headerAvailable())
74+
//{
75+
// String headerName = http.readHeaderName();
76+
// String headerValue = http.readHeaderValue();
77+
//}
78+
79+
int bodyLen = http.contentLength();
80+
Serial.print("Content length is: ");
81+
Serial.println(bodyLen);
82+
Serial.println();
83+
Serial.println("Body returned follows:");
84+
85+
// Now we've got to the body, so we can print it out
86+
unsigned long timeoutStart = millis();
87+
char c;
88+
// Whilst we haven't timed out & haven't reached the end of the body
89+
while ( (http.connected() || http.available()) &&
90+
(bodyLen > 0 || bodyLen != HttpClient::kNoContentLengthHeader) &&
91+
((millis() - timeoutStart) < kNetworkTimeout) )
10792
{
108-
Serial.print("Failed to skip response headers: ");
109-
Serial.println(err);
93+
if (http.available())
94+
{
95+
c = http.read();
96+
// Print out this character
97+
Serial.print(c);
98+
99+
bodyLen--;
100+
// We read something, reset the timeout counter
101+
timeoutStart = millis();
102+
}
103+
else
104+
{
105+
// We haven't got any data, so let's pause to allow some to
106+
// arrive
107+
delay(kNetworkDelay);
108+
}
110109
}
111110
}
112111
else

0 commit comments

Comments
 (0)