Skip to content

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

Closed
@vlastahajek

Description

@vlastahajek

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions