Skip to content

Commit

Permalink
escape debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
zivillian committed Jul 7, 2022
1 parent 85298e8 commit 80ace83
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
private:
Print *_serial;
AsyncResponseStream *_response;
String escapeAmp(String text);
String escapeLt(String text);
String escape(String buffer, char oldValue, String newValue);
public:
WebPrint(Print *serial, AsyncResponseStream *reponse);
size_t write(uint8_t) override;
Expand Down
36 changes: 34 additions & 2 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,43 @@ WebPrint::WebPrint(Print *serial, AsyncResponseStream *response)
{}

size_t WebPrint::write(uint8_t arg){
_response->print("(char)arg");
if (arg == '<'){
_response->print("&lt;");
}
else if (arg == '&'){
_response->print("&amp;");
}
else{
_response->print((char)arg);
}
return _serial->write(arg);
}

size_t WebPrint::write(const uint8_t *buffer, size_t size){
_response->print((const char*)buffer);
_response->print(escapeLt(escapeAmp((const char*)buffer)));
return _serial->write(buffer, size);
}

String WebPrint::escapeAmp(String text){
return escape(text, '&', "&amp;");
}

String WebPrint::escapeLt(String text){
return escape(text, '<', "&lt;");
}

String WebPrint::escape(String text, char oldValue, String newValue){
auto pos = text.indexOf(oldValue);
if (pos < 0) return text;
String result = text.substring(0, pos) + newValue;
auto last = pos;
pos = text.indexOf(oldValue, pos + 1);
while(pos >= 0)
{
result += text.substring(last + 1, pos) + newValue;
last = pos;
pos = text.indexOf(oldValue, pos + 1);
}
result += text.substring(last + 1);
return result;
}

0 comments on commit 80ace83

Please sign in to comment.