1
0
Fork 0

Fix #1579: Handle special characters in html property browser

This commit is contained in:
Torsten Dreyer 2014-11-04 22:21:44 +01:00
parent 06e8fe747f
commit 0376ab3f28
2 changed files with 31 additions and 5 deletions

View file

@ -37,6 +37,32 @@ using std::vector;
namespace flightgear { namespace flightgear {
namespace http { namespace http {
// copied from http://stackoverflow.com/a/24315631
static void ReplaceAll(std::string & str, const std::string & from, const std::string & to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
}
static const std::string specialChars[][2] = {
{ "&", "&" },
{ "\"", """ },
{ "'", "'" },
{ "<", "&lt;" },
{ ">", "&gt;" },
};
static inline std::string htmlSpecialChars( const std::string & s )
{
string reply = s;
for( size_t i = 0; i < sizeof(specialChars)/sizeof(specialChars[0]); ++i )
ReplaceAll( reply, specialChars[i][0], specialChars[i][1] );
return reply;
}
class DOMElement { class DOMElement {
public: public:
virtual ~DOMElement() {} virtual ~DOMElement() {}
@ -201,7 +227,7 @@ static DOMElement * renderPropertyValueElement( SGPropertyNode_ptr node )
root = new DOMNode( "input" ); root = new DOMNode( "input" );
root->setAttribute( "type", "text" ); root->setAttribute( "type", "text" );
root->setAttribute( "name", node->getDisplayName() ); root->setAttribute( "name", node->getDisplayName() );
root->setAttribute( "value", value ); root->setAttribute( "value", htmlSpecialChars(value) );
root->setAttribute( "size", boost::lexical_cast<std::string>( len ) ); root->setAttribute( "size", boost::lexical_cast<std::string>( len ) );
root->setAttribute( "maxlength", "2047" ); root->setAttribute( "maxlength", "2047" );
} else { } else {
@ -212,7 +238,7 @@ static DOMElement * renderPropertyValueElement( SGPropertyNode_ptr node )
root->setAttribute( "cols", boost::lexical_cast<std::string>( cols ) ); root->setAttribute( "cols", boost::lexical_cast<std::string>( cols ) );
root->setAttribute( "rows", boost::lexical_cast<std::string>( rows ) ); root->setAttribute( "rows", boost::lexical_cast<std::string>( rows ) );
root->setAttribute( "maxlength", "2047" ); root->setAttribute( "maxlength", "2047" );
root->addChild( new DOMTextElement( value ) ); root->addChild( new DOMTextElement( htmlSpecialChars(value) ) );
} }
return root; return root;
@ -397,7 +423,7 @@ bool PropertyUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResp
e->setAttribute( "id", "currentvalue" ); e->setAttribute( "id", "currentvalue" );
e->addChild( new DOMTextElement( "Current Value: " ) ); e->addChild( new DOMTextElement( "Current Value: " ) );
e->addChild( new DOMTextElement( node->getStringValue() ) ); e->addChild( new DOMTextElement( htmlSpecialChars(node->getStringValue()) ) );
DOMNode * form = new DOMNode("form"); DOMNode * form = new DOMNode("form");
body->addChild( form ); body->addChild( form );

View file

@ -105,7 +105,7 @@ public:
Method = NotNull(connection->request_method); Method = NotNull(connection->request_method);
Uri = urlDecode(NotNull(connection->uri)); Uri = urlDecode(NotNull(connection->uri));
HttpVersion = NotNull(connection->http_version); HttpVersion = NotNull(connection->http_version);
QueryString = urlDecode(NotNull(connection->query_string)); QueryString = NotNull(connection->query_string);
remoteAddress = NotNull(connection->remote_ip); remoteAddress = NotNull(connection->remote_ip);
remotePort = connection->remote_port; remotePort = connection->remote_port;
@ -117,7 +117,7 @@ public:
for (string_list::iterator it = pairs.begin(); it != pairs.end(); ++it) { for (string_list::iterator it = pairs.begin(); it != pairs.end(); ++it) {
string_list nvp = split(*it, "="); string_list nvp = split(*it, "=");
if (nvp.size() != 2) continue; if (nvp.size() != 2) continue;
RequestVariables.insert(make_pair(nvp[0], nvp[1])); RequestVariables.insert(make_pair(urlDecode(nvp[0]), urlDecode(nvp[1])));
} }
for (int i = 0; i < connection->num_headers; i++) for (int i = 0; i < connection->num_headers; i++)