Bug 1185293 - HAR builder: Avoid an exception when the response is not available; r=jryans
authorJan Odvarko <odvarko@gmail.com>
Wed, 05 Aug 2015 17:47:04 +0200
changeset 288236 877a69dd3bfe8ea15d5317937a18977727f3058c
parent 288235 bb90b2703de614d158dbc3750bea80407b162937
child 288237 e00e8b9e3b356947b7aa48b4e791b62116fbbc14
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1185293
milestone42.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 1185293 - HAR builder: Avoid an exception when the response is not available; r=jryans
browser/devtools/netmonitor/har/har-builder.js
--- a/browser/devtools/netmonitor/har/har-builder.js
+++ b/browser/devtools/netmonitor/har/har-builder.js
@@ -162,17 +162,17 @@ HarBuilder.prototype = {
 
   buildRequest: function(file) {
     let request = {
       bodySize: 0
     };
 
     request.method = file.method;
     request.url = file.url;
-    request.httpVersion = file.httpVersion;
+    request.httpVersion = file.httpVersion || "";
 
     request.headers = this.buildHeaders(file.requestHeaders);
     request.cookies = this.buildCookies(file.requestCookies);
 
     request.queryString = NetworkHelper.parseQueryString(
       NetworkHelper.nsIURL(file.url).query) || [];
 
     request.postData = this.buildPostData(file);
@@ -280,48 +280,59 @@ HarBuilder.prototype = {
       status: 0
     };
 
     // Arbitrary value if it's aborted to make sure status has a number
     if (file.status) {
       response.status = parseInt(file.status);
     }
 
+    let responseHeaders = file.responseHeaders;
+
     response.statusText = file.statusText || "";
-    response.httpVersion = file.httpVersion;
+    response.httpVersion = file.httpVersion || "";
 
-    response.headers = this.buildHeaders(file.responseHeaders);
+    response.headers = this.buildHeaders(responseHeaders);
     response.cookies = this.buildCookies(file.responseCookies);
+    response.content = this.buildContent(file);
 
-    response.content = this.buildContent(file);
-    response.redirectURL = findValue(file.responseHeaders.headers, "Location");
-    response.headersSize = file.responseHeaders.headersSize;
+    let headers = responseHeaders ? responseHeaders.headers : null;
+    let headersSize = responseHeaders ? responseHeaders.headersSize : -1;
+
+    response.redirectURL = findValue(headers, "Location");
+    response.headersSize = headersSize;
     response.bodySize = file.transferredSize || -1;
 
     return response;
   },
 
   buildContent: function(file) {
     let content = {
       mimeType: file.mimeType,
       size: -1
     };
 
-    if (file.responseContent && file.responseContent.content) {
-      content.size = file.responseContent.content.size;
+    let responseContent = file.responseContent;
+    if (responseContent && responseContent.content) {
+      content.size = responseContent.content.size;
     }
 
-    if (!this._options.includeResponseBodies ||
-        file.responseContent.contentDiscarded) {
+    let includeBodies = this._options.includeResponseBodies;
+    let contentDiscarded = responseContent ?
+      responseContent.contentDiscarded : false;
+
+    // The comment is appended only if the response content
+    // is explicitly discarded.
+    if (!includeBodies || contentDiscarded) {
       content.comment = L10N.getStr("har.responseBodyNotIncluded");
       return content;
     }
 
-    if (file.responseContent) {
-      let text = file.responseContent.content.text;
+    if (responseContent) {
+      let text = responseContent.content.text;
       let promise = this.fetchData(text).then(value => {
         content.text = value;
       });
     }
 
     return content;
   },
 
@@ -410,16 +421,20 @@ function isURLEncodedFile(file, text) {
   return false;
 }
 
 /**
  * Find specified value within an array of name-value pairs
  * (used for headers, cookies and cache entries)
  */
 function findValue(arr, name) {
+  if (!arr) {
+    return "";
+  }
+
   name = name.toLowerCase();
   let result = arr.find(entry => entry.name.toLowerCase() == name);
   return result ? result.value : "";
 }
 
 /**
  * Generate HAR representation of a date.
  * (YYYY-MM-DDThh:mm:ss.sTZD, e.g. 2009-07-24T19:20:30.45+01:00)