espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.42k stars 7.37k forks source link

webserver :Please add chunkedResponseModeStart / chunkedResponseFinalize #5080

Open fanfanlatulipe26 opened 3 years ago

fanfanlatulipe26 commented 3 years ago

Description:

webserver :Please add chunkedResponseModeStart / chunkedResponseFinalize These functions were added in a recent release for ESP8266WebServer and it will be nice to have them in the upcomming release for ESP32. If not , porting from ESP8266 to ESP32 becomes cumbersome. I developed a small derivated class to get them in rhe ESP32 environment. If it can help .....

Sketch:


#ifndef FS_WEBSERVER_H
#define FS_WEBSERVER_H

#include <WebServer.h>
class fs_WebServer : public WebServer
{
  public:
    fs_WebServer(int port = 80): WebServer(port) {
    }
   using WebServer:: send_P;
    void send_P(int code, PGM_P content_type, PGM_P content) {
      size_t contentLength = 0;

      if (content != NULL) {
        contentLength = strlen_P(content);
      }

      String header;
      char type[64];
      memccpy_P((void*)type, (PGM_VOID_P)content_type, 0, sizeof(type));
      _prepareHeader(header, code, (const char* )type, contentLength);
      _currentClientWrite(header.c_str(), header.length());
      if (contentLength) {  // if rajouté par FS ...........................+++++
        sendContent_P(content);
      }
    }

    bool chunkedResponseModeStart_P (int code, PGM_P content_type) {
      if (_currentVersion == 0)
        // no chunk mode in HTTP/1.0
        return false;
      setContentLength(CONTENT_LENGTH_UNKNOWN);
      send_P(code, content_type, "");
      return true;
    }
    bool chunkedResponseModeStart (int code, const char* content_type) {
      return chunkedResponseModeStart_P(code, content_type);
    }
    bool chunkedResponseModeStart (int code, const String& content_type) {
      return chunkedResponseModeStart_P(code, content_type.c_str());
    }
    void chunkedResponseFinalize () {
      sendContent(emptyString);
    }
};
#endif // FS_WEBSERVER_H
VojtechBartoska commented 3 years ago

Thanks @fanfanlatulipe26 . We will take a look and consider implementation.

tablatronix commented 2 years ago

Status of this?

fanfanlatulipe26 commented 2 years ago

I just updated the code of the derivative class using WebServer:: send_P; was added.

FrankBoesing commented 1 year ago

Any progress here?

VojtechBartoska commented 1 year ago

Hello, no progress. I'm adding this to next release and we will triage this and let you know about the resolution.

FrankBoesing commented 1 year ago

Great news!

armazenamentoURL commented 1 year ago

I really need this, I need to answer a gigantic (100mb) text file that is dynamically generated from memory. With esp8266 I sent with sendContent_P a few lines at a time and it worked great. I can't do this with esp32. Thanks.

fanfanlatulipe26 commented 1 year ago

@AlefRosa In the mean time, why don't you use my trick I gave at the beginning: just include in your sketch a file fs_Webserver.h containing the code I gave above and declare you server as fs_Webserver. You can see the use in the project BaliseDGAC_GPS_Logger where you will find the file fs_Webserver.h and the server declaration and use in the .ino

FrankBoesing commented 1 year ago

Do you know how to use the  RequestHandler (server->addHandler(new ... ) with your derived class?

Btw, the ESP32 Request handler uses different arguments fr its methods :(

FrankBoesing commented 1 year ago

An other missing method is:

void send(int code, const char *content_type, const char *content, size_t contentLength) Without, it uses an often superlfues strlen(), which is not optimal (esp with large content) Esp8266 has it.

fanfanlatulipe26 commented 1 year ago

Sorry, I never used RequestHandler . I had a look at the source code for RequestHandler .h and the ESP8266 version is really more C++ that the ESP32 one. I must admit that I am not C++ expert ... Didn't find any examples.

armazenamentoURL commented 1 year ago

@fanfanlatulipe26 thank you very much, i got it yesterday. As I need to use asyncwebserver, I studied it further and managed to use request response using chunk method. thank you so much again! Now I'm managing to generate 10mb files from memory registers and send them in parts as I make them.

chconnor commented 8 months ago

Also eagerly awaiting this important feature!

Thanks @fanfanlatulipe26 -- that code worked great. I had to add an additional constructor at the top:

fs_WebServer(IPAddress addr, int port): WebServer(addr, port){ }

fanfanlatulipe26 commented 8 months ago

Thanks @chconnor for the tip. I updated the code in the repository BaliseDGAC_GPS_Logger that I referenced above. It may help some one.

FarokhReza commented 7 months ago

Thanks for sharing it is great for solving some issues in the progress of migration from esp8266 to esp32!!

VojtechBartoska commented 7 months ago

Postponing to 3.1.0 Milestone, we will cover this during Webserver refactoring