mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Expose webserver's chunk api (#7134)
* expose hidden WebServer's chunked API
This commit is contained in:
parent
3db610f6d0
commit
d600cc7fa6
@ -186,12 +186,27 @@ void handleFileList() {
|
||||
Dir dir = filesystem->openDir(path);
|
||||
path.clear();
|
||||
|
||||
String output = "[";
|
||||
while (dir.next()) {
|
||||
File entry = dir.openFile("r");
|
||||
if (output != "[") {
|
||||
output += ',';
|
||||
// use HTTP/1.1 Chunked response to avoid building a huge temporary string
|
||||
if (!server.chunkedResponseModeStart(200, "text/json")) {
|
||||
server.send(505, FPSTR("text/html"), FPSTR("HTTP1.1 required"));
|
||||
return;
|
||||
}
|
||||
|
||||
// use the same string for every line
|
||||
String output;
|
||||
output.reserve(64);
|
||||
while (dir.next()) {
|
||||
|
||||
if (output.length()) {
|
||||
// send string from previous iteration
|
||||
// as an HTTP chunk
|
||||
server.sendContent(output);
|
||||
output = ',';
|
||||
} else {
|
||||
output = '[';
|
||||
}
|
||||
|
||||
File entry = dir.openFile("r");
|
||||
bool isDir = false;
|
||||
output += "{\"type\":\"";
|
||||
output += (isDir) ? "dir" : "file";
|
||||
@ -205,8 +220,10 @@ void handleFileList() {
|
||||
entry.close();
|
||||
}
|
||||
|
||||
// send last string
|
||||
output += "]";
|
||||
server.send(200, "text/json", output);
|
||||
server.sendContent(output);
|
||||
server.chunkedResponseFinalize();
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
|
@ -149,6 +149,24 @@ public:
|
||||
void sendContent(const char *content) { sendContent_P(content); }
|
||||
void sendContent(const char *content, size_t size) { sendContent_P(content, size); }
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static String credentialHash(const String& username, const String& realm, const String& password);
|
||||
|
||||
static String urlDecode(const String& text);
|
||||
|
Loading…
x
Reference in New Issue
Block a user