esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
16k stars 13.33k forks source link

net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) while trying to send huge data/image. #8906

Closed kayakaantuna closed 1 year ago

kayakaantuna commented 1 year ago

Basic Infos

Platform

Settings in IDE

Problem Description

I'm using this library to upload about 500kb jpg image to the "http://ESPIPaddress:80/" address, however I get "net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)" from the chrome browser. I only can see %10 of the image before I get the error.

Since its a huge data for ESP8266, I'm getting the image from another microchip using SPI communication. So I'm using server.sendContent(); function. When I setup an PHP server I can see the image perfectly but I want this to be standalone and not dependent to the PHP server I open from OS.

Here is the related code:

MCVE Sketch


if(POSTcheck){
    server.setContentLength(u_image_size);
    String header = String(F("HTTP/1.1 200 OK\r\n"));
    header += String(F("Content-Type: image/jpg\r\n"));
    header += String(F("Content-Length: "));
    header += String(u_image_size);
    header += String(F("\r\n"));
    header += String(F("Connection: Close\r\n\r\n"));
    uint32_t total_length = head.length() + tail.length() + u_image_size;
    server.sendContent(header.c_str(), header.length());
    POSTcheck = false;
    }

    if(uart_rx_idx_ctr % UART_RX_PACKET_SIZE == 0)
      { 
        esp_counter++;
        server.sendContent_P((char *)&buf, UART_RX_PACKET_SIZE);
        is_buf_full = 0;
        conti = false;
        interrupts();
        Serial.write(ready_to_receive);
      }
      else
      {
        esp_counter++;
        uint16_t remain = uart_rx_idx_ctr % UART_RX_PACKET_SIZE;
        uint8_t packet_num = remain / SERVER_SEND_PACKET_SIZE;
        uint8_t remain_mod = remain % SERVER_SEND_PACKET_SIZE;
        uint8_t i = 0;
        server.sendContent_P((char *)&buf, remain);
        delay(500);
        is_buf_full = 0;

        finit = true;
        PHPupload = true;

      }  

Basically, when SPI data length reaches to max RAM size it enters this function and send it to the server.

Debug Messages

kayakaantuna commented 1 year ago

Update: I found the issue, after I changed these values it worked perfectly.

Inside "ESP8266WebServer.h":

define HTTP_MAX_DATA_WAIT 5000000 //ms to wait for the client to send the request

define HTTP_MAX_POST_WAIT 5000000 //ms to wait for POST data to arrive

define HTTP_MAX_SEND_WAIT 5000000 //ms to wait for data chunk to be ACKed

define HTTP_MAX_CLOSE_WAIT 2000000 //ms to wait for the client to close the connection

I believe that the current version only maintains connection for 2 seconds, which is certainly insufficient for me. After changing these definitions values, it began to wait somewhat longer, which resolved my issue.