Bug 1297082 - Part 2: Use curl.js utility functions in HAR builder r?Honza draft
authorJarda Snajdr <jsnajdr@gmail.com>
Mon, 22 Aug 2016 16:22:14 +0200
changeset 404350 360989a998568152e9871e4f71f2cdd0ef39f5be
parent 404349 f88fedc9edd56e6172fb0731b2eac3c7d322782d
child 404351 8f8082bb283716d375246cdd4da3434a760f37e5
push id27190
push userbmo:jsnajdr@gmail.com
push dateTue, 23 Aug 2016 09:42:05 +0000
reviewersHonza
bugs1297082
milestone51.0a1
Bug 1297082 - Part 2: Use curl.js utility functions in HAR builder r?Honza MozReview-Commit-ID: HffHwyLhoO6
devtools/client/netmonitor/har/har-builder.js
--- a/devtools/client/netmonitor/har/har-builder.js
+++ b/devtools/client/netmonitor/har/har-builder.js
@@ -2,16 +2,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const { defer, all } = require("promise");
 const { LocalizationHelper } = require("devtools/client/shared/l10n");
 const Services = require("Services");
 const appInfo = Services.appinfo;
+const { CurlUtils } = require("devtools/client/shared/curl");
 
 loader.lazyRequireGetter(this, "NetworkHelper", "devtools/shared/webconsole/network-helper");
 
 loader.lazyGetter(this, "L10N", () => {
   return new LocalizationHelper("chrome://devtools/locale/har.properties");
 });
 
 const HAR_VERSION = "1.1";
@@ -201,31 +202,19 @@ HarBuilder.prototype = {
   },
 
   appendHeadersPostData: function (input = [], file) {
     if (!file.requestPostData) {
       return input;
     }
 
     this.fetchData(file.requestPostData.postData.text).then(value => {
-      let contentType = value.match(/Content-Type: ([^;\s]+)/);
-      let contentLength = value.match(/Content-Length: (.+)/);
-
-      if (contentType && contentType.length > 1) {
-        input.push({
-          name: "Content-Type",
-          value: contentType[1]
-        });
-      }
-
-      if (contentLength && contentLength.length > 1) {
-        input.push({
-          name: "Content-Length",
-          value: contentLength[1]
-        });
+      let multipartHeaders = CurlUtils.getHeadersFromMultipartText(value);
+      for (let header of multipartHeaders) {
+        input.push(header);
       }
     });
 
     return input;
   },
 
   buildCookies: function (input) {
     if (!input) {
@@ -269,21 +258,22 @@ HarBuilder.prototype = {
     }
 
     if (file.requestPostData.postDataDiscarded) {
       postData.comment = L10N.getStr("har.requestBodyNotIncluded");
       return postData;
     }
 
     // Load request body from the backend.
-    this.fetchData(file.requestPostData.postData.text).then(value => {
-      postData.text = value;
+    this.fetchData(file.requestPostData.postData.text).then(postDataText => {
+      postData.text = postDataText;
 
       // If we are dealing with URL encoded body, parse parameters.
-      if (isURLEncodedFile(file, value)) {
+      let { headers } = file.requestHeaders;
+      if (CurlUtils.isUrlEncodedRequest({ headers, postDataText })) {
         postData.mimeType = "application/x-www-form-urlencoded";
 
         // Extract form parameters and produce nice HAR array.
         this._options.view._getFormDataSections(file.requestHeaders,
           file.requestHeadersFromUploadStream,
           file.requestPostData).then(formDataSections => {
             formDataSections.forEach(section => {
               let paramsArray = NetworkHelper.parseQueryString(section);
@@ -428,37 +418,16 @@ HarBuilder.prototype = {
 
     return promise;
   }
 };
 
 // Helpers
 
 /**
- * Returns true if specified request body is URL encoded.
- */
-function isURLEncodedFile(file, text) {
-  let contentType = "content-type: application/x-www-form-urlencoded";
-  if (text && text.toLowerCase().indexOf(contentType) != -1) {
-    return true;
-  }
-
-  // The header value doesn't have to be always exactly
-  // "application/x-www-form-urlencoded",
-  // there can be even charset specified. So, use indexOf rather than just
-  // "==".
-  let value = findValue(file.requestHeaders.headers, "content-type");
-  if (value && value.indexOf("application/x-www-form-urlencoded") == 0) {
-    return true;
-  }
-
-  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 "";
   }