@@ -112,18 +112,27 @@ void disconnectWiFi() {
112112 g_mqttClient.disconnect ();
113113}
114114
115- ZeroHttpPump::StepResult httpConnectStep (const ZeroHttpPump::Request&, void *) {
115+ void closeHttpClient () {
116+ #if defined(ARDUINO_ARCH_ESP8266)
117+ g_httpClient.abort ();
118+ #else
116119 g_httpClient.stop ();
120+ #endif
121+ }
122+
123+ ZeroHttpPump::StepResult httpConnectStep (const ZeroHttpPump::Request&, void *) {
124+ closeHttpClient ();
117125 g_httpClient.setTimeout (kHttpIoTimeoutMs );
118126 const bool connected = g_httpAddressValid ? g_httpClient.connect (g_httpAddress, kHttpPort )
119127 : g_httpClient.connect (kHttpHost , kHttpPort );
120128 if (!connected) {
129+ closeHttpClient ();
121130 return ZeroHttpPump::kStepFailed ;
122131 }
123132 g_httpRequestPrepared = false ;
124133 g_httpResponseSawStatus = false ;
125134 g_httpResponseSuccess = false ;
126- g_httpReadDeadlineMs = millis () + kHttpIoTimeoutMs ;
135+ g_httpReadDeadlineMs = 0 ;
127136 g_httpStatusLineLength = 0 ;
128137 return ZeroHttpPump::kStepComplete ;
129138}
@@ -145,15 +154,19 @@ ZeroHttpPump::StepResult httpWriteStep(const ZeroHttpPump::Request& request, voi
145154 g_httpClient.print (" \r\n\r\n " );
146155 g_httpClient.write (reinterpret_cast <const uint8_t *>(request.body ), request.bodyLength );
147156 g_httpRequestPrepared = true ;
157+ g_httpReadDeadlineMs = millis () + kHttpIoTimeoutMs ;
148158 }
149159
150160 return ZeroHttpPump::kStepComplete ;
151161}
152162
153163ZeroHttpPump::StepResult httpReadStep (const ZeroHttpPump::Request&, void *) {
154164 if (!g_httpClient.connected () && !g_httpClient.available ()) {
155- return g_httpResponseSawStatus && g_httpResponseSuccess ? ZeroHttpPump::kStepComplete
156- : ZeroHttpPump::kStepFailed ;
165+ if (g_httpResponseSawStatus && g_httpResponseSuccess) {
166+ return ZeroHttpPump::kStepComplete ;
167+ }
168+ closeHttpClient ();
169+ return ZeroHttpPump::kStepFailed ;
157170 }
158171
159172 while (g_httpClient.available ()) {
@@ -170,7 +183,11 @@ ZeroHttpPump::StepResult httpReadStep(const ZeroHttpPump::Request&, void*) {
170183 (strncmp (g_httpStatusLine, " HTTP/1.1 2" , 10 ) == 0 ) ||
171184 (strncmp (g_httpStatusLine, " HTTP/1.0 2" , 10 ) == 0 );
172185 g_httpStatusLineLength = 0 ;
173- return g_httpResponseSuccess ? ZeroHttpPump::kStepComplete : ZeroHttpPump::kStepFailed ;
186+ if (g_httpResponseSuccess) {
187+ return ZeroHttpPump::kStepComplete ;
188+ }
189+ closeHttpClient ();
190+ return ZeroHttpPump::kStepFailed ;
174191 }
175192 }
176193 g_httpStatusLineLength = 0 ;
@@ -182,14 +199,15 @@ ZeroHttpPump::StepResult httpReadStep(const ZeroHttpPump::Request&, void*) {
182199 }
183200
184201 if (millis () > g_httpReadDeadlineMs) {
202+ closeHttpClient ();
185203 return ZeroHttpPump::kStepFailed ;
186204 }
187205
188206 return ZeroHttpPump::kStepPending ;
189207}
190208
191209ZeroHttpPump::StepResult httpCloseStep (const ZeroHttpPump::Request&, void *) {
192- g_httpClient. stop ();
210+ closeHttpClient ();
193211 g_httpRequestPrepared = false ;
194212 g_httpResponseSawStatus = false ;
195213 g_httpResponseSuccess = false ;
0 commit comments