Skip to content

HTTPClient::getString() freezes on empty response without included Content-length header #3586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vlastahajek opened this issue Dec 19, 2019 · 4 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@vlastahajek
Copy link
Contributor

Hardware:

Board: DOIT ESP32 DEVKIT V1
Core Installation version: 1.0.4
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600
Computer OS: Windows 10

Description:

HTTPClient::getString() freezes on empty response without included Content-length header, mostly in case of HTTP status code 204, when connection reuse is on and server doesn't forces connection close.
This happens when server is written in Go or simply with netcat. Server in NodeJS forces connection close on timeout.

Server snippet for Go:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/write", HandlePost)
	fmt.Println("Listening on 666")
	http.ListenAndServe(":666", nil)
}

func HandlePost(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Got request")
	if r.Method == http.MethodPost {
		w.WriteHeader(http.StatusNoContent)
	} else {
		http.NotFound(w, r)
	}
}

To run this, save it in the file named server.go and and start: go run server.go

Or use netcat with following response:

HTTP/1.1 204 No Content
Connection: keep-alive

Save it (including the last empty line) in file named resp.txt and start: nc -l -p 666 <resp.txt

When connection reuse is set to false, it crashes when connecting to Go server:

[E][WiFiClient.cpp:436] read(): fail on fd 55, errno: 104, "Connection reset by peer"
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d2080  PS      : 0x00060030  A0      : 0x800d248d  A1      : 0x3ffb1cc0  
A2      : 0x00000000  A3      : 0x3ffb1d1f  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x3ffc1474  A7      : 0x00000068  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x40084f34  A12     : 0x00000050  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000008  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x400d2080:0x3ffb1cc0 0x400d248a:0x3ffb1ce0 0x4015e136:0x3ffb1d10 0x400d7189:0x3ffb1d40 0x400d7221:0x3ffb1d60 0x400d496d:0x3ffb1d80 0x400d5447:0x3ffb1e20 0x400d549b:0x3ffb1e60 0x400d54bf:0x3ffb1e80 0x400d1cd6:0x3ffb1ea0 0x400d81b1:0x3ffb1fb0 0x40088b9d:0x3ffb1fd0

x400d2080: WiFiClientRxBuffer::read(unsigned char*, unsigned int) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi\src\WiFiClient.cpp line 107
0x400d248a: WiFiClient::read(unsigned char*, unsigned int) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi\src\WiFiClient.cpp line 434
0x4015e136: WiFiClient::read() at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi\src\WiFiClient.cpp line 345
0x400d7189: Stream::timedRead() at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32\Stream.cpp line 36
0x400d7221: Stream::readStringUntil(char) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32\Stream.cpp line 287
0x400d496d: HTTPClient::handleHeaderResponse() at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 1118
0x400d5447: HTTPClient::sendRequest(char const*, unsigned char*, unsigned int) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 573
0x400d549b: HTTPClient::POST(unsigned char*, unsigned int) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 493
0x400d54bf: HTTPClient::POST(String) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\HTTPClient\src\HTTPClient.cpp line 498
0x400d1cd6: loop() at C:\Users\vlast\OneDrive\Dokumenty\Arduino\Esp32\HTTPIssueReproductionInflux/HTTPIssueReproductionInflux.ino line 69
0x400d81b1: loopTask(void*) at C:\Users\vlast\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32\main.cpp line 19
0x40088b9d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Sketch: (leave the backquotes for code formatting)

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>

#include <HTTPClient.h>

#define SERVER_URL "http://192.168.88.96:666"

WiFiMulti wifiMulti;

void setup() {

    Serial.begin(115200);
    Serial.println();

  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP("SSID","PASSWORD");

  Serial.print("Connecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();
}

void loop() {
    // wait for WiFi connection
    if(WiFi.status() == WL_CONNECTED) {

        HTTPClient http;
        //http.setReuse(false);
        
        String url = SERVER_URL;
        url += "/write";
        http.begin(url); //HTTP
        Serial.print("Sending POST to ");
        Serial.println(url);
        
        http.addHeader("Content-Type", "text/plain");

        String point = "test POST";
        int httpCode = http.POST(point);

        // httpCode will be negative on error
        if(httpCode > 0) {
            Serial.printf(" StatusCode: %d\n", httpCode);
            String payload = http.getString();
            Serial.println(" Payload:");
            Serial.println(payload);
        } else {
            Serial.printf("Request failed, error: %s\n", http.errorToString(httpCode).c_str());
        }       
        http.end();
    }

    delay(5000);
}

Debug Messages:

[D][HTTPClient.cpp:276] beginInternal(): host: 192.168.88.96 port: 666 url: /write
Sending POST to http://192.168.88.96:666/write
[D][HTTPClient.cpp:1025] connect():  connected to 192.168.88.96:666
[D][HTTPClient.cpp:1158] handleHeaderResponse(): code: 204
 StatusCode: 204
@maizy
Copy link

maizy commented Jan 7, 2020

seems like the same issue as in #2667

@stale
Copy link

stale bot commented Mar 7, 2020

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Mar 7, 2020
@stale
Copy link

stale bot commented Mar 21, 2020

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Mar 21, 2020
@stale
Copy link

stale bot commented Apr 4, 2020

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

2 participants