Bug 1031160 - Disable HTTP Keepalive for SafeBrowsing. r=rnewman
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Fri, 14 Nov 2014 12:55:35 -0800
changeset 240447 83d3b89ca16cc856262ee2a18d27b7361d6e58f5
parent 240446 06f612773cc0d1f489161c9ca118505b3dd54196
child 240448 96c7f2a79b41c3b3e47d76cf85109a7be1e36c34
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1031160
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1031160 - Disable HTTP Keepalive for SafeBrowsing. r=rnewman
toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js
toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp
toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
--- a/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js
+++ b/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js
@@ -171,17 +171,17 @@ HashCompleterRequest.prototype = {
                                          Ci.nsISupports]),
 
   // This is called by the HashCompleter to add a hash and callback to the
   // HashCompleterRequest. It must be called before calling |begin|.
   add: function HCR_add(aPartialHash, aCallback) {
     this._requests.push({
       partialHash: aPartialHash,
       callback: aCallback,
-      responses: [],
+      responses: []
     });
   },
 
   // This initiates the HTTP request. It can fail due to backoff timings and
   // will notify all callbacks as necessary. We notify the backoff object on
   // begin.
   begin: function HCR_begin() {
     if (!this._completer.canMakeRequest(this.gethashUrl)) {
@@ -218,16 +218,20 @@ HashCompleterRequest.prototype = {
   openChannel: function HCR_openChannel() {
     let loadFlags = Ci.nsIChannel.INHIBIT_CACHING |
                     Ci.nsIChannel.LOAD_BYPASS_CACHE;
 
     let uri = Services.io.newURI(this.gethashUrl, null, null);
     let channel = Services.io.newChannelFromURI(uri);
     channel.loadFlags = loadFlags;
 
+    // Disable keepalive.
+    let httpChannel = channel.QueryInterface(Ci.nsIHttpChannel);
+    httpChannel.setRequestHeader("Connection", "close", false);
+
     this._channel = channel;
 
     let body = this.buildRequest();
     this.addRequestBody(body);
 
     // Set a timer that cancels the channel after timeout_ms in case we
     // don't get a gethash response.
     this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
@@ -360,16 +364,17 @@ HashCompleterRequest.prototype = {
         let response = request.responses[j];
         request.callback.completion(response.completeHash, response.tableName,
                                     response.chunkId);
       }
 
       request.callback.completionFinished(Cr.NS_OK);
     }
   },
+
   notifyFailure: function HCR_notifyFailure(aStatus) {
     dump("hashcompleter: notifying failure\n");
     for (let i = 0; i < this._requests.length; i++) {
       let request = this._requests[i];
       request.callback.completionFinished(aStatus);
     }
   },
 
--- a/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp
@@ -115,27 +115,35 @@ nsUrlClassifierStreamUpdater::FetchUpdat
 
   // Set the appropriate content type for file/data URIs, for unit testing
   // purposes.
   // This is only used for testing and should be deleted.
   bool match;
   if ((NS_SUCCEEDED(aUpdateUrl->SchemeIs("file", &match)) && match) ||
       (NS_SUCCEEDED(aUpdateUrl->SchemeIs("data", &match)) && match)) {
     mChannel->SetContentType(NS_LITERAL_CSTRING("application/vnd.google.safebrowsing-update"));
+  } else {
+    // We assume everything else is an HTTP request.
+
+    // Disable keepalive.
+    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Connection"), NS_LITERAL_CSTRING("close"), false);
+    NS_ENSURE_SUCCESS(rv, rv);
   }
 
-   // Create a custom LoadContext for SafeBrowsing, so we can use callbacks on
-   // the channel to query the appId which allows separation of safebrowsing
-   // cookies in a separate jar.
+  // Create a custom LoadContext for SafeBrowsing, so we can use callbacks on
+  // the channel to query the appId which allows separation of safebrowsing
+  // cookies in a separate jar.
   nsCOMPtr<nsIInterfaceRequestor> sbContext =
     new mozilla::LoadContext(NECKO_SAFEBROWSING_APP_ID);
   rv = mChannel->SetNotificationCallbacks(sbContext);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  // Make the request
+  // Make the request.
   rv = mChannel->AsyncOpen(this, nullptr);
   NS_ENSURE_SUCCESS(rv, rv);
 
   mStreamTable = aStreamTable;
 
   return NS_OK;
 }
 
--- a/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
+++ b/toolkit/components/url-classifier/tests/unit/test_hashcompleter.js
@@ -215,18 +215,18 @@ function run_test() {
       }
     }
   }
   do_test_pending();
 
   server = new HttpServer();
   server.registerPathHandler(SERVER_PATH, hashCompleterServer);
 
-  const SERVER_PORT = 8080;
-  server.start(SERVER_PORT);
+  server.start(-1);
+  const SERVER_PORT = server.identity.primaryPort;
 
   gethashUrl = "http://localhost:" + SERVER_PORT + SERVER_PATH;
 
   runNextCompletion();
 }
 
 function runNextCompletion() {
   // The server relies on currentCompletionSet to send the correct response, so