Skip to content

Commit be91d96

Browse files
committed
improve debug out and error handling
1 parent e6c661e commit be91d96

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ void httpClient::begin(String host, uint16_t port, String url, bool https, Strin
8080
* called after the payload is handeld
8181
*/
8282
void httpClient::end(void) {
83-
if((!_reuse || !_canReuse) && connected()) {
84-
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop \n");
85-
_tcp->stop();
83+
if(connected()) {
84+
if(_reuse && _canReuse) {
85+
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n");
86+
} else {
87+
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n");
88+
_tcp->stop();
89+
}
8690
} else {
87-
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n");
91+
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");
8892
}
8993
}
9094

@@ -189,12 +193,16 @@ WiFiClient & httpClient::getStream(void) {
189193
/**
190194
* write all message body / payload to Stream
191195
* @param stream Stream *
192-
* @return bytes written
196+
* @return bytes written ( negative values are error codes )
193197
*/
194198
int httpClient::writeToStream(Stream * stream) {
195199

196200
if(!stream) {
197-
return -1;
201+
return HTTPC_ERROR_NO_STREAM;
202+
}
203+
204+
if(!connected()) {
205+
return HTTPC_ERROR_NOT_CONNECTED;
198206
}
199207

200208
// get lenght of document (is -1 when Server sends no Content-Length header)
@@ -219,14 +227,17 @@ int httpClient::writeToStream(Stream * stream) {
219227
if(len > 0) {
220228
len -= c;
221229
}
230+
231+
delay(0);
232+
} else {
233+
delay(1);
222234
}
223-
delay(1);
224235
}
225236

226-
DEBUG_HTTPCLIENT("[HTTP-Client] connection closed or file end.\n");
237+
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten);
227238

228239
if(_size && _size != bytesWritten) {
229-
DEBUG_HTTPCLIENT("[HTTP-Client] bytesWritten %d and size %d missmatch!.\n", bytesWritten, _size);
240+
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d missmatch!.\n", bytesWritten, _size);
230241
}
231242

232243
end();

libraries/ESP8266httpClient/src/ESP8266httpClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define HTTPC_ERROR_SEND_PAYLOAD_FAILD (-3)
4040
#define HTTPC_ERROR_NOT_CONNECTED (-4)
4141
#define HTTPC_ERROR_CONNECTION_LOST (-5)
42+
#define HTTPC_ERROR_NO_STREAM (-6)
4243

4344

4445
class httpClient {

0 commit comments

Comments
 (0)