From 633d977a613770032a65521a5c085d5db7a5d3da Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Thu, 16 Feb 2012 17:07:16 +0000 Subject: [PATCH] Gather all chunks of POST body before splitting. The boundary between chunks is not guaranteed to fall at the end of a PUTVAL line. The worst possible place this boundary can fall is in the value. In this case, the truncated line will pass putval_re and be loaded into Graphite with an incorrect value. --- collectd-graphite-proxy.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/collectd-graphite-proxy.js b/collectd-graphite-proxy.js index 72ac110..2aa24a7 100644 --- a/collectd-graphite-proxy.js +++ b/collectd-graphite-proxy.js @@ -58,8 +58,17 @@ graphite_connection.on("error", function() { var request_handler = function(request, response) { var putval_re = /^PUTVAL ([^ ]+)(?: ([^ ]+=[^ ]+)?) ([0-9.]+)(:.*)/; + var chunks = []; request.addListener("data", function(chunk) { - metrics = chunk.toString().split("\n"); + chunks.push(chunk.toString()); + }); + request.addListener("end", function(chunk) { + var body = chunks.join(""); + if (parseInt(request.headers["content-length"]) != body.length) { + console.log("Content-Length: %d != body.length: %d\n", + request.headers["content-length"], body.length); + } + var metrics = body.split("\n"); for (var i in metrics) { var m = putval_re.exec(metrics[i]); if (!m) {