Sfoglia il codice sorgente

Clean up response strings to remove invalid characters.

Bentley James Oakes 6 anni fa
parent
commit
8dc141e1a1
1 ha cambiato i file con 20 aggiunte e 1 eliminazioni
  1. 20 1
      httpwsd.js

+ 20 - 1
httpwsd.js

@@ -33,12 +33,31 @@ var workerIds2socketIds = {};
 
 
 /************************************ UTILS ***********************************/
+
+/** Remove invalid characters from a string. **/
+function __clean_string(s)
+{
+	if (s == undefined) {
+        return s;
+    }
+
+	s = JSON.stringify(s);
+	s = s.replace(/'/g, '');
+	s = s.replace(/"/g, '');
+	s = s.replace(/‘/g, '');
+	s = s.replace(/’/g, '');
+	s = s.replace(/\\/g, '\\');
+	s = s.replace(/\//g, '\/');
+	s = s.replace(/\\n/g, ' ');
+	return s;
+}
+
 /** Syntactic sugar to build and send HTTP responses **/
 function __respond(response, statusCode, reason, data, headers)
 {
 	response.writeHead(
 			statusCode,
-			JSON.stringify(reason),
+			__clean_string(reason),
 			(headers || {'Content-Type': 'text/plain',
 			'Access-Control-Allow-Origin': '*'}));