-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Avoid large reallocations in webserver's header response #8873
Draft
d-a-v
wants to merge
4
commits into
esp8266:master
Choose a base branch
from
d-a-v:webserverwithoutholes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−45
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,7 +274,7 @@ void ESP8266WebServerTemplate<ServerType>::serveStatic(const char* uri, FS& fs, | |
if(is_file) { | ||
_addRequestHandler(new StaticFileRequestHandler<ServerType>(fs, path, uri, cache_header)); | ||
} else { | ||
_addRequestHandler(new StaticDirectoryRequestHandler<ServerType>(fs, path, uri, cache_header)); | ||
_addRequestHandler(new StaticDirectoryRequestHandler<ServerType>(fs, path, uri, cache_header)); | ||
} | ||
} | ||
|
||
|
@@ -420,18 +420,12 @@ void ESP8266WebServerTemplate<ServerType>::stop() { | |
} | ||
|
||
template <typename ServerType> | ||
void ESP8266WebServerTemplate<ServerType>::sendHeader(const String& name, const String& value, bool first) { | ||
String headerLine = name; | ||
headerLine += F(": "); | ||
headerLine += value; | ||
headerLine += "\r\n"; | ||
|
||
if (first) { | ||
_responseHeaders = headerLine + _responseHeaders; | ||
} | ||
else { | ||
_responseHeaders += headerLine; | ||
} | ||
template <typename S1, typename S2> | ||
void ESP8266WebServerTemplate<ServerType>::sendHeader(S1 name, S2 value, bool first) { | ||
if (first) | ||
_userHeaders.emplace_front(std::pair(name, value)); | ||
else | ||
_userHeaders.emplace_back(std::pair(name, value)); | ||
} | ||
|
||
template <typename ServerType> | ||
|
@@ -440,44 +434,80 @@ void ESP8266WebServerTemplate<ServerType>::setContentLength(const size_t content | |
} | ||
|
||
template <typename ServerType> | ||
void ESP8266WebServerTemplate<ServerType>::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) { | ||
response = String(F("HTTP/1.")) + String(_currentVersion) + ' '; | ||
response += String(code); | ||
response += ' '; | ||
response += responseCodeToString(code); | ||
response += "\r\n"; | ||
bool ESP8266WebServerTemplate<ServerType>::_streamIt (Stream& s) { | ||
int len = s.streamRemaining(); | ||
int sent = s.sendAll(&_currentClient); | ||
if (sent == len) | ||
return true; | ||
DBGWS("HTTPServer: error: sent %zd on %u bytes\n", sent, len); | ||
return false; | ||
} | ||
|
||
template <typename ServerType> | ||
template <typename K, typename V> | ||
bool ESP8266WebServerTemplate<ServerType>::_streamHeader (K name, V value) | ||
{ | ||
return _streamIt(name) && _streamItC(F(": ")) && _streamIt(value) && _streamItC(F("\r\n")); | ||
} | ||
|
||
template <typename ServerType> | ||
bool ESP8266WebServerTemplate<ServerType>::_sendHeader(int code, const char* content_type, size_t contentLength) { | ||
if ( !_streamItC(F("HTTP/1.")) | ||
|| !_streamIt(String(_currentVersion)) | ||
|| !_streamItC(F(" ")) | ||
|| !_streamIt(String(code)) | ||
|| !_streamItC(F(" ")) | ||
|| !_streamIt(responseCodeToString(code)) | ||
|| !_streamItC(F("\r\n"))) | ||
{ | ||
return false; | ||
} | ||
|
||
using namespace mime; | ||
if (!content_type) | ||
content_type = mimeTable[html].mimeType; | ||
if (!_streamHeader(F("Content-Type"), content_type)) | ||
return false; | ||
|
||
sendHeader(String(F("Content-Type")), String(FPSTR(content_type)), true); | ||
if (_contentLength == CONTENT_LENGTH_NOT_SET) { | ||
sendHeader(String(FPSTR(Content_Length)), String(contentLength)); | ||
if (!_streamHeader(Content_Length, String(contentLength))) | ||
return false; | ||
} else if (_contentLength != CONTENT_LENGTH_UNKNOWN) { | ||
sendHeader(String(FPSTR(Content_Length)), String(_contentLength)); | ||
} else if(_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion){ //HTTP/1.1 or above client | ||
//let's do chunked | ||
_chunked = true; | ||
sendHeader(String(F("Accept-Ranges")),String(F("none"))); | ||
sendHeader(String(F("Transfer-Encoding")),String(F("chunked"))); | ||
if (!_streamHeader(Content_Length, String(_contentLength))) | ||
return false; | ||
} else if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion){ //HTTP/1.1 or above client | ||
//let's do chunked | ||
_chunked = true; | ||
if (!_streamHeader(F("Accept-Ranges"), F("none")) || !_streamHeader(F("Transfer-Encoding"), F("chunked"))) | ||
return false; | ||
} | ||
|
||
if (_corsEnabled) { | ||
sendHeader(String(F("Access-Control-Allow-Origin")), String("*")); | ||
if (!_streamHeader(F("Access-Control-Allow-Origin"), F("*"))) | ||
return false; | ||
} | ||
|
||
if (_keepAlive && _server.hasClient()) { // Disable keep alive if another client is waiting. | ||
_keepAlive = false; | ||
if (_keepAlive && _server.hasClient()) { | ||
// Disable keep alive if another client is waiting. | ||
_keepAlive = false; | ||
} | ||
if (!_streamHeader(F("Connection"), String(_keepAlive ? F("keep-alive") : F("close")))) { | ||
return false; | ||
} | ||
sendHeader(String(F("Connection")), String(_keepAlive ? F("keep-alive") : F("close"))); | ||
if (_keepAlive) { | ||
sendHeader(String(F("Keep-Alive")), String(F("timeout=")) + HTTP_MAX_CLOSE_WAIT); | ||
if (!_streamHeader(F("Keep-Alive"), String(F("timeout=")) + HTTP_MAX_CLOSE_WAIT)) | ||
return false; | ||
} | ||
|
||
for (const auto& kv: _userHeaders) | ||
if (!_streamHeader(kv.first, kv.second)) | ||
return false; | ||
_userHeaders.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will these be cleared if streaming failed? N.B. I got the impression the current implementation of the webserver does hold some data way longer than needed as opening a web page may sometimes free up quite a bit of memory. (maybe POST data of a previous request???) |
||
|
||
response += _responseHeaders; | ||
response += "\r\n"; | ||
_responseHeaders = ""; | ||
if (!_streamItC(F("\r\n"))) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
template <typename ServerType> | ||
|
@@ -503,13 +533,10 @@ void ESP8266WebServerTemplate<ServerType>::sendContent(const String& content) { | |
|
||
template <typename ServerType> | ||
void ESP8266WebServerTemplate<ServerType>::send(int code, const char* content_type, Stream* stream, size_t content_length /*= 0*/) { | ||
String header; | ||
if (content_length == 0) | ||
content_length = std::max((ssize_t)0, stream->streamRemaining()); | ||
_prepareHeader(header, code, content_type, content_length); | ||
size_t sent = StreamConstPtr(header).sendAll(&_currentClient); | ||
if (sent != header.length()) | ||
DBGWS("HTTPServer: error: sent %zd on %u bytes\n", sent, header.length()); | ||
if (!_sendHeader(code, content_type, content_length)) | ||
return; | ||
if (content_length) | ||
return sendContent(stream, content_length); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit
at a glance, we have a bunch of typed pairs that generate multiple emplace_... for every sendHeader variant
I'd consider using perfect forward and a specific type we already have