wandenberg / nginx-push-stream-module

A pure stream http push technology for your Nginx setup. Comet made easy and really scalable.
Other
2.22k stars 295 forks source link

"Uncaught Invalid JSON" when using LongPolling and message contains "}{" #254

Closed mnaglqu closed 6 years ago

mnaglqu commented 7 years ago

When client uses LongPolling and receives a message containing "}{" anywhere in data, message parsing fails with JS error in console: Uncaught Invalid JSON.

Examples for our system:

Example 1: Sending chat message "one two" - this is received correctly in client: {"id":6,"channel":"94c98803d4_eb_b2708889e11f2931eea7f7c9b0ce6ec6","text":"{\"t\":\"TM\",\"i\":\"169106488647894106\",\"d\":[[\"name\",\"value\"],[\"cid\",\"d16cd19f\"],[\"conversationid\",\"d16cd19f\"],[\"datechanged\",\"2017-03-16 06:57:29\"],[\"format\",\"T\"],[\"type\",\"M\"],[\"userid\",\"293f35cd\"],[\"files\",\"\"],[\"params\",\"\"],[\"text\",\"one two\"]]}","tag":"13","time":"Thu, 16 Mar 2017 13:57:29 GMT"}

Example 2: Sending chat message "{one}{two}" - this is results in exception in client Uncaught Invalid JSON: {"id":4,"channel":"94c98803d4_eb_b2708889e11f2931eea7f7c9b0ce6ec6","text":"{\"t\":\"TM\",\"i\":\"169106488647900345\",\"d\":[[\"name\",\"value\"],[\"cid\",\"d16cd19f\"],[\"conversationid\",\"d16cd19f\"],[\"datechanged\",\"2017-03-16 06:59:36\"],[\"format\",\"T\"],[\"type\",\"M\"],[\"userid\",\"293f35cd\"],[\"files\",\"\"],[\"params\",\"\"],[\"text\",\"{one}

Expected behavior is to receive the complete message which is a valid JSON: {"id":4,"channel":"94c98803d4_eb_b2708889e11f2931eea7f7c9b0ce6ec6","text":"{\"t\":\"TM\",\"i\":\"169106488647900345\",\"d\":[[\"name\",\"value\"],[\"cid\",\"d16cd19f\"],[\"conversationid\",\"d16cd19f\"],[\"datechanged\",\"2017-03-16 06:59:36\"],[\"format\",\"T\"],[\"type\",\"M\"],[\"userid\",\"293f35cd\"],[\"files\",\"\"],[\"params\",\"\"],[\"text\",\"{one}{two}\"]]}","tag":"13","time":"Thu, 16 Mar 2017 13:57:29 GMT"}

Note the premature end of JSON in example 2. The JSON was ended after first occurence of "}{" in data. The reason is in pushstream.js line 855 where individual messages are parsed from response by replacing "}{" by newline and then splitting by newline.

var messages = isArray(responseText) ? responseText : responseText.replace(/\}\{/g, "}\r\n{").split("\r\n");

Characters "}" and "{" and their combination "}{" are valid inside JSON values and are not required to be escaped, so a substring "}{" can appear anywhere in data.

wandenberg commented 7 years ago

@mnaglqu the pushstream.js client is made in a way that it requires that the text is escaped, no matter if the "}{" combination is valid or not inside a JSON value. Any client that you use must be compliance with the server. So, is just a matter of adjust the server/text with the client, or the other way around. But, to use the pushstream.js you have to ensure that the server is properly configured and the text message is valid for it. Take a look on the examples.