Skip to content

Commit

Permalink
urlencode quotes in value Strings before sending
Browse files Browse the repository at this point in the history
- replace " with %22
- decoding is done browser side

Saving Strings from the config pages with quotes to EEPROM is not a problem nor using them in your Sketch.
However when sending them with other values in a json String to the browser(config pages) these extra quotes break the json format.
This is why we now urlencode the quotes before sending. The config page js scripts take care of decoding.
  • Loading branch information
Onno-Dirkzwager authored Mar 1, 2019
1 parent fbe44bd commit c42ab03
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/IOTAppStory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,9 +1488,15 @@ void IOTAppStory::servHdlAppInfo(AsyncWebServerRequest *request){
if(i > 0){
retHtml += F(",");
}

// urlencode " in value | replace " with %22
String value = (*fieldStruct[i].varPointer);
value.replace("\"", "%22");

// get PROGMEM json string and replace {*} with values
retHtml += FPSTR(HTTP_APP_INFO);
retHtml.replace(F("{l}"), String(fieldStruct[i].fieldLabel));
retHtml.replace(F("{v}"), String((*fieldStruct[i].varPointer)));
retHtml.replace(F("{v}"), value);
retHtml.replace(F("{n}"), String(i));
retHtml.replace(F("{m}"), String(fieldStruct[i].length));
retHtml.replace(F("{t}"), String(fieldStruct[i].type));
Expand Down

2 comments on commit c42ab03

@abaskin
Copy link

@abaskin abaskin commented on c42ab03 Mar 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the change and it does fix the problem with double quotes in the value. However in the process of testing I found the both line breaks and tabs were also causing problems. Looking at the code, the value is being embedding in a JSON object as a string value. Based on this I was able modify your change slightly to escape the value as a JSON string and now double quotes, line breaks and tabs are working properly.

// urlencode " in value | replace " with %22
String value = (*fieldStruct[i].varPointer);
// value.replace("\"", "%22");
value.replace("\\", "\\\\");
value.replace("\"", "\\\"");
value.replace("\n", "\\n");
value.replace("\r", "\\r");
value.replace("\t", "\\t");
value.replace("\b", "\\b");
value.replace("\f", "\\f");

@Onno-Dirkzwager
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes like it!
On devices like the esp it's always a challange(memory wise) to balance how far to go. These additions make sense. I beleave @CwlBroeders is prepairing a pull request as we type.

Please sign in to comment.