From 0157a5ecb4290bab696ce127500030aa2d6c65ca Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Wed, 25 Jan 2012 07:23:03 -0800 Subject: [PATCH] Bug 717061 - compress telemetry pings; r=taras --- toolkit/components/telemetry/TelemetryPing.js | 34 +++++++++++++++++-- .../tests/unit/test_TelemetryPing.js | 27 +++++++++++++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/toolkit/components/telemetry/TelemetryPing.js b/toolkit/components/telemetry/TelemetryPing.js index 3a9efdb61562..d336e5195d3c 100644 --- a/toolkit/components/telemetry/TelemetryPing.js +++ b/toolkit/components/telemetry/TelemetryPing.js @@ -468,7 +468,7 @@ TelemetryPing.prototype = { request.mozBackgroundRequest = true; request.open("POST", url, true); request.overrideMimeType("text/plain"); - request.setRequestHeader("Content-Type", "application/json"); + request.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); let startTime = new Date(); @@ -486,9 +486,37 @@ TelemetryPing.prototype = { request.addEventListener("error", function(aEvent) finishRequest(request.channel), false); request.addEventListener("load", function(aEvent) finishRequest(request.channel), false); - request.send(data.payload); + request.setRequestHeader("Content-Encoding", "gzip"); + let payloadStream = Cc["@mozilla.org/io/string-input-stream;1"] + .createInstance(Ci.nsIStringInputStream); + payloadStream.data = this.gzipCompressString(data.payload); + request.send(payloadStream); }, - + + gzipCompressString: function gzipCompressString(string) { + let observer = { + buffer: "", + onStreamComplete: function(loader, context, status, length, result) { + this.buffer = String.fromCharCode.apply(this, result); + } + }; + + let scs = Cc["@mozilla.org/streamConverters;1"] + .getService(Ci.nsIStreamConverterService); + let listener = Cc["@mozilla.org/network/stream-loader;1"] + .createInstance(Ci.nsIStreamLoader); + listener.init(observer); + let converter = scs.asyncConvertData("uncompressed", "gzip", + listener, null); + let stringStream = Cc["@mozilla.org/io/string-input-stream;1"] + .createInstance(Ci.nsIStringInputStream); + stringStream.data = string; + converter.onStartRequest(null, null); + converter.onDataAvailable(null, null, stringStream, 0, string.length); + converter.onStopRequest(null, null, null); + return observer.buffer; + }, + attachObservers: function attachObservers() { if (!this._initialized) return; diff --git a/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js b/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js index f85a86099966..5e2c5f6d055b 100644 --- a/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js +++ b/toolkit/components/telemetry/tests/unit/test_TelemetryPing.js @@ -57,8 +57,31 @@ function checkHistograms(request, response) { // do not need the http server anymore httpserver.stop(do_test_finished); let s = request.bodyInputStream; - let payload = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON) - .decodeFromStream(s, s.available()); + let payload = null; + let decoder = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON) + + if (request.getHeader("content-encoding") == "gzip") { + let observer = { + buffer: "", + onStreamComplete: function(loader, context, status, length, result) { + this.buffer = String.fromCharCode.apply(this, result); + } + }; + + let scs = Cc["@mozilla.org/streamConverters;1"] + .getService(Ci.nsIStreamConverterService); + let listener = Cc["@mozilla.org/network/stream-loader;1"] + .createInstance(Ci.nsIStreamLoader); + listener.init(observer); + let converter = scs.asyncConvertData("gzip", "uncompressed", + listener, null); + converter.onStartRequest(null, null); + converter.onDataAvailable(null, null, s, 0, s.available()); + converter.onStopRequest(null, null, null); + payload = decoder.decode(observer.buffer); + } else { + payload = decoder.decodeFromStream(s, s.available()); + } do_check_eq(request.getHeader("content-type"), "application/json; charset=UTF-8"); do_check_true(payload.simpleMeasurements.uptime >= 0);