Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

malformed request when max option is not defined #2

Open
wants to merge 3 commits into from

1 participant

@mef

Example from README.md (basic use, step 4) used to crash the process, whereas running examples.basic.js worked fine.

It turned out that this was because when option "max" is omitted, the XML did not encode any document, as variable length only got defined when max exists.

Attached code fixes this by defining the variable length when max is not set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 7 deletions.
  1. +21 −5 lib/searchresult.js
  2. +4 −2 lib/server.js
View
26 lib/searchresult.js
@@ -86,6 +86,8 @@ SearchResult.prototype.toXML = function toXML(options) {
else
length = (max > this.documents.length) ? this.documents.length : max;
}
+ else
+ length = (!ids) ? this.documents.length : ids.length
var result = SearchResult.XML_OPEN;
@@ -97,10 +99,10 @@ SearchResult.prototype.toXML = function toXML(options) {
doc = (ids) ? this.documentHash[ids[i]] : this.documents[i];
if(doc) {
- result += "<document id=\"" +doc.id+ "\">";
- result += "<snippet>" +doc.snippet+ "</snippet>";
- result += "<url>" +doc.url+ "</url>";
- result += "<title>" +doc.title+ "</title>";
+ result += "<document id=\"" + escape(doc.id) + "\">";
+ result += "<snippet>" + escape(doc.snippet) + "</snippet>";
+ result += "<url>" + escape(doc.url) + "</url>";
+ result += "<title>" + escape(doc.title) + "</title>";
result += "</document>";
}
}
@@ -155,6 +157,20 @@ SearchResult.XML_QUERY_OPEN = "<query>";
SearchResult.XML_QUERY_CLOSE = "</query>";
SearchResult.XML_CLOSE = "</searchresult>";
+function escape(txt)
+{
+
+ if (typeof txt == 'string')
+ {
+ return txt.replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;');
+ }
+ else
+ return txt;
+}
+
function serialize(_obj)
{
switch (typeof _obj)
@@ -192,4 +208,4 @@ function serialize(_obj)
return 'UNKNOWN';
break;
}
-};
+};
View
6 lib/server.js
@@ -17,8 +17,10 @@ function validateAlgorithm(algorithm) {
* source — By Source Clustering
*/
+ var validAlgorithms = ['lingo', 'stc', 'kmeans'];
+
algorithm = algorithm.toLowerCase();
- if(algorithm!="lingo" || algorithm!="stc" || algorithm!="kmeans")
+ if(validAlgorithms.indexOf(algorithm) == -1)
algorithm = "lingo";
return algorithm;
};
@@ -167,4 +169,4 @@ DocumentClusteringServer.prototype.makeRequest = function makeRequest(search_res
req.write(data);
req.end();
-};
+};
Something went wrong with that request. Please try again.