@@ -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 */
8282void 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 */
194198int 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 ();
0 commit comments