Skip to content

New signatures to ESP8266WebServer::sendContent #2567

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
wellrats opened this issue Oct 1, 2016 · 5 comments · Fixed by #6341
Closed

New signatures to ESP8266WebServer::sendContent #2567

wellrats opened this issue Oct 1, 2016 · 5 comments · Fixed by #6341
Labels
component: web level: easy type: enhancement waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@wellrats
Copy link

wellrats commented Oct 1, 2016

Hi all,
Would it be possible in next version of this lib insert another signatures for
ESP8266WebServer::sendContent as follow

void ESP8266WebServer::sendContent(const char* content, size_t len);
void ESP8266WebServer::sendContent(const char* content)

Useful when parsing a char* buffer like a HTML template from a file, and sending little pieces of this buffer to client after parsed. Call <server_instance>.client().write( ... ) directly is not an option, because final length of parsed buffer is unknown and encoding is chunked

Thanks in advance

The implementation.

void ESP8266WebServer::sendContent(const char* content, size_t len) {
  const char * footer = "\r\n";
  if(_chunked) {
    char * chunkSize = (char *)malloc(11);
    if(chunkSize){
      sprintf(chunkSize, "%x%s", len, footer);
      _currentClient.write(chunkSize, strlen(chunkSize));
      free(chunkSize);
    }
  }
  _currentClient.write(content, len);
  if(_chunked){
    _currentClient.write(footer, 2);
  }
}

void ESP8266WebServer::sendContent(const char* content) {
  sendContent(content, strlen(content));
}

void ESP8266WebServer::sendContent(const String& content) {
  sendContent(content.c_str(), content.length());
}
@luc-github
Copy link
Contributor

Actually you do not need to specify size when sending if you use setContentLength(CONTENT_LENGTH_UNKNOWN); you just need to finish your sending by sendContent("");

you can see how it can be used with template on my project : https://github.com/luc-github/ESP3D/blob/master/esp3d/webinterface.cpp#L214-L394

one send part is here FYR: https://github.com/luc-github/ESP3D/blob/master/esp3d/webinterface.cpp#L379-L392

@wellrats
Copy link
Author

wellrats commented Oct 1, 2016

I understand that.

I just think it's not necessary duplicate my big char * buffer, turning it into a String to call sendContent.

@luc-github
Copy link
Contributor

luc-github commented Oct 1, 2016

Sorry I misunderstood the question - but I would say why duplicate ? why not use String only, instead of big char * buffer, this is what I do reading template files and processing them

@wellrats
Copy link
Author

wellrats commented Oct 1, 2016

OK. no problem.
The point is, more signatures means more flexibility.
The code posted above is working to String and char*

Just a suggestion.

@devyte
Copy link
Collaborator

devyte commented Oct 12, 2017

@wellrats could you please make a PR with your suggestion?

@devyte devyte added waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. component: web level: easy type: enhancement labels Oct 12, 2017
earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Jul 24, 2019
Fixes esp8266#2567

Allow the web server to send plain C strings instead of requring they be
encapsulated inside a String class object.  Saves memory vs. having to
convert C strings to Strings (i.e. duplication of data), overloads on
the efficient sendContent_P(char*) methods.
d-a-v pushed a commit that referenced this issue Jul 25, 2019
Fixes #2567

Allow the web server to send plain C strings instead of requring they be
encapsulated inside a String class object.  Saves memory vs. having to
convert C strings to Strings (i.e. duplication of data), overloads on
the efficient sendContent_P(char*) methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: web level: easy type: enhancement waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants