edit.html: multipart/form-data instead of urlencoded

This commit is contained in:
Hansi, dl9rdz 2020-04-14 09:45:53 +02:00
parent a28042da3d
commit 8a05698f44
4 changed files with 87 additions and 15 deletions

View File

@ -83,6 +83,16 @@ String readLine(Stream &stream) {
return s;
}
// Read line from file, without using dynamic memory allocation (String class)
// returns length line.
int readLine(Stream &stream, char *buffer, int maxlen) {
int n = stream.readBytesUntil('\n', buffer, maxlen);
buffer[n] = 0;
if(n <= 0) return 0;
if(buffer[n-1]=='\r') { buffer[n-1]=0; n--; }
return n;
}
// Replaces placeholder with LED state value
String processor(const String& var) {
Serial.println(var);
@ -200,6 +210,7 @@ const char *createQRGForm() {
i + 1, s.c_str());
}
strcat(ptr, "</table><input type=\"submit\" value=\"Update\"/></form></body></html>");
Serial.printf("QRG form: size=%d bytes\n",strlen(message));
return message;
}
@ -303,6 +314,7 @@ const char *createWIFIForm() {
i + 1, i < nNetworks ? networks[i].pw.c_str() : "");
}
strcat(ptr, "</table><input type=\"submit\" value=\"Update\"></input></form></body></html>");
Serial.printf("WIFI form: size=%d bytes\n",strlen(message));
return message;
}
@ -381,6 +393,7 @@ const char *createStatusForm() {
}
}
strcat(ptr, "</body></html>");
Serial.printf("Status form: size=%d bytes\n",strlen(message));
return message;
}
@ -557,6 +570,7 @@ const char *createConfigForm() {
}
}
strcat(ptr, "</table><input type=\"submit\" value=\"Update\"></input></form></body></html>");
Serial.printf("Config form: size=%d bytes\n",strlen(message));
return message;
}
@ -629,6 +643,7 @@ const char *createControlForm() {
strcat(ptr, "\"></input><br>");
}
strcat(ptr, "</form></body></html>");
Serial.printf("Control form: size=%d bytes\n",strlen(message));
return message;
}
@ -688,32 +703,60 @@ const char *createEditForm(String filename) {
strcat(ptr, filename.c_str());
strcat(ptr, "</title></head><body><form action=\"edit.html?file=");
strcat(ptr, filename.c_str());
strcat(ptr, "\" method=\"post\">");
strcat(ptr, "\" method=\"post\" enctype=\"multipart/form-data\">");
strcat(ptr, "<textarea name=\"text\" cols=\"80\" rows=\"40\">");
while (file.available()) {
String line = readLine(file); //file.readStringUntil('\n');
strcat(ptr, line.c_str()); strcat(ptr, "\n");
}
strcat(ptr, "</textarea><input type=\"submit\" value=\"Save\"></input></form></body></html>");
Serial.printf("Edit form: size=%d bytes\n",strlen(message));
return message;
}
const char *handleEditPost(AsyncWebServerRequest *request) {
Serial.println("Handling post request");
int params = request->params();
Serial.printf("Post:, %d params\n", params);
for(int i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
String name = p->name();
String value = p->value();
if(name.c_str()==NULL) { name=String("NULL"); }
if(value.c_str()==NULL) { value=String("NULL"); }
if(p->isFile()){
Serial.printf("_FILE[%s]: %s, size: %u\n", name.c_str(), value.c_str(), p->size());
} else if(p->isPost()){
Serial.printf("_POST[%s]: %s\n", name.c_str(), value.c_str());
} else {
Serial.printf("_GET[%s]: %s\n", name.c_str(), value.c_str());
}
}
AsyncWebParameter *filep = request->getParam("file");
if (!filep) return NULL;
String filename = filep->value();
Serial.printf("Writing file <%s>\n",filename.c_str());
AsyncWebParameter *textp = request->getParam("text", true);
if (!textp) return NULL;
Serial.printf("Parameter size is %d\n", textp->size());
Serial.printf("Multipart: %d contentlen=%d \n",
request->multipart(), request->contentLength());
String content = textp->value();
if(content.length()==0) {
Serial.println("File is empty. Not written.");
return NULL;
}
File file = SPIFFS.open("/" + filename, "w");
if (!file) {
Serial.println("There was an error opening the file '/" + filename + "'for writing");
return "";
}
file.print(content);
Serial.printf("File is open for writing, content is %d bytes\n", content.length());
int len = file.print(content);
file.close();
Serial.printf("Written: %d bytes\n", len);
if (strcmp(filename.c_str(), "screens.txt") == 0) {
// screens update => reload
forceReloadScreenConfig = true;
@ -730,6 +773,7 @@ const char *createUpdateForm(boolean run) {
strcat(ptr, "<input type=\"submit\" name=\"master\" value=\"Master-Update\"></input><br><input type=\"submit\" name=\"devel\" value=\"Devel-Update\">");
}
strcat(ptr, "</form></body></html>");
Serial.printf("Update form: size=%d bytes\n",strlen(message));
return message;
}
@ -879,8 +923,15 @@ void SetupAsyncServer() {
request->send(200, "text/html", createEditForm(request->getParam(0)->value()));
});
server.on("/edit.html", HTTP_POST, [](AsyncWebServerRequest * request) {
handleEditPost(request);
const char *ret = handleEditPost(request);
if(ret==NULL)
request->send(200, "text/html", "<html><head>ERROR</head><body><p>Something went wrong. Uploaded file is empty.</p></body></hhtml>");
else
request->send(200, "text/html", createEditForm(request->getParam(0)->value()));
},
NULL,
[](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
Serial.printf("post data: index=%d len=%d total=%d\n", index, len, total);
});
// Route to load style.css file

View File

@ -1,4 +1,4 @@
const char *version_name = "rdzTTGOsonde";
const char *version_id = "devel20200209";
const char *version_id = "devel20200414";
const int SPIFFS_MAJOR=2;
const int SPIFFS_MINOR=4;

View File

@ -6,7 +6,7 @@
#include "Display.h"
#include "Sonde.h"
extern String readLine(Stream &stream);
int readLine(Stream &stream, char *buffer, int maxlen);
extern const char *version_name;
extern const char *version_id;
@ -28,7 +28,7 @@ extern SemaphoreHandle_t axpSemaphore;
SPIClass spiDisp(HSPI);
const char *sondeTypeStr[NSondeTypes] = { "DFM6", "DFM9", "RS41", "RS92", "M10" };
const char *sondeTypeStr[NSondeTypes] = { "DFM6", "DFM9", "RS41", "RS92", "M10 " };
byte myIP_tiles[8*11];
static uint8_t ap_tile[8]={0x00,0x04,0x22,0x92, 0x92, 0x22, 0x04, 0x00};
@ -541,6 +541,7 @@ uint16_t MY_ILI9225::drawGFXChar(int16_t x, int16_t y, unsigned char c, uint16_t
char Display::buf[17];
char Display::lineBuf[Display::LINEBUFLEN];
RawDisplay *Display::rdis = NULL;
@ -772,9 +773,11 @@ int Display::countEntries(File f) {
int pos = f.position();
int n = 0;
while(1) {
String line = readLine(f); //f.readStringUntil('\n');
line.trim();
const char *c=line.c_str();
//String line = readLine(f); //f.readStringUntil('\n');
//line.trim();
//const char *c=line.c_str();
readLine(f, lineBuf, LINEBUFLEN);
const char *c = trim(lineBuf);
if(*c=='#') continue;
if(*c>='0'&&*c<='9') n++;
if(strchr(c,'=')) continue;
@ -804,11 +807,13 @@ void Display::initFromFile() {
int entrysize;
Serial.printf("Reading from /screens.txt. available=%d\n",d.available());
while(d.available()) {
Serial.printf("Unused stack: %d\n", uxTaskGetStackHighWaterMark(0));
//Serial.printf("Unused stack: %d\n", uxTaskGetStackHighWaterMark(0));
const char *ptr;
String line = readLine(d); // d.readStringUntil('\n');
line.trim();
const char *s = line.c_str();
readLine(d, lineBuf, LINEBUFLEN);
const char *s = trim(lineBuf);
// String line = readLine(d);
// line.trim();
// const char *s = line.c_str();
Serial.printf("Line: '%s'\n", s);
if(*s == '#') continue; // ignore comments
switch(what) {
@ -818,10 +823,11 @@ void Display::initFromFile() {
Serial.printf("Illegal start of screen: %s\n", s);
continue;
}
char *label = strdup(s+1);
entrysize = countEntries(d);
Serial.printf("Reading entry with %d elements\n", entrysize);
idx++;
int res = allocDispInfo(entrysize, &newlayouts[idx], strdup(s+1));
int res = allocDispInfo(entrysize, &newlayouts[idx], label);
Serial.printf("allocDispInfo: idx %d: label is %p - %s\n",idx,newlayouts[idx].label, newlayouts[idx].label);
if(res<0) {
Serial.println("Error allocating memory for disp info");

View File

@ -129,6 +129,21 @@ private:
int gpsDist; // -1: invalid
int gpsCourse, gpsDir, gpsBear; // 0..360; -1: invalid
boolean gpsCourseOld;
static const int LINEBUFLEN{ 255 };
static char lineBuf[LINEBUFLEN];
static const char *trim(char *s) {
char *ret = s;
while(*ret && isspace(*ret)) { ret++; }
int lastidx;
while(1) {
lastidx = strlen(ret)-1;
if(lastidx>=0 && isspace(ret[lastidx]))
ret[lastidx] = 0;
else
break;
}
return ret;
}
public:
void initFromFile();