Bug 1297082 - Part 3: Fix eslint issues in curl.js r=Honza

MozReview-Commit-ID: EeUbPoJ9DJ9

--HG--
extra : rebase_source : 8bfbb76618c4ac4ccbb46c23ce73b8db69d17d7c
This commit is contained in:
Jarda Snajdr 2016-08-22 16:22:54 +02:00
Родитель 75e31da493
Коммит 13b0a3df43
1 изменённых файлов: 62 добавлений и 63 удалений

Просмотреть файл

@ -45,19 +45,19 @@ const Curl = {
/** /**
* Generates a cURL command string which can be used from the command line etc. * Generates a cURL command string which can be used from the command line etc.
* *
* @param object aData * @param object data
* Datasource to create the command from. * Datasource to create the command from.
* The object must contain the following properties: * The object must contain the following properties:
* - url:string, the URL of the request. * - url:string, the URL of the request.
* - method:string, the request method upper cased. HEAD / GET / POST etc. * - method:string, the request method upper cased. HEAD / GET / POST etc.
* - headers:array, an array of request headers {name:x, value:x} tuples. * - headers:array, an array of request headers {name:x, value:x} tuples.
* - httpVersion:string, http protocol version rfc2616 formatted. Eg. "HTTP/1.1" * - httpVersion:string, http protocol version rfc2616 formatted. Eg. "HTTP/1.1"
* - postDataText:string, optional - the request payload. * - postDataText:string, optional - the request payload.
* *
* @return string * @return string
* A cURL command. * A cURL command.
*/ */
generateCommand: function (aData) { generateCommand: function (data) {
let utils = CurlUtils; let utils = CurlUtils;
let command = ["curl"]; let command = ["curl"];
@ -69,49 +69,49 @@ const Curl = {
utils.escapeStringWin : utils.escapeStringPosix; utils.escapeStringWin : utils.escapeStringPosix;
// Add URL. // Add URL.
command.push(escapeString(aData.url)); command.push(escapeString(data.url));
let postDataText = null; let postDataText = null;
let multipartRequest = utils.isMultipartRequest(aData); let multipartRequest = utils.isMultipartRequest(data);
// Create post data. // Create post data.
let data = []; let postData = [];
if (utils.isUrlEncodedRequest(aData) || aData.method == "PUT") { if (utils.isUrlEncodedRequest(data) || data.method == "PUT") {
postDataText = aData.postDataText; postDataText = data.postDataText;
data.push("--data"); postData.push("--data");
data.push(escapeString(utils.writePostDataTextParams(postDataText))); postData.push(escapeString(utils.writePostDataTextParams(postDataText)));
ignoredHeaders.add("Content-Length"); ignoredHeaders.add("Content-Length");
} else if (multipartRequest) { } else if (multipartRequest) {
postDataText = aData.postDataText; postDataText = data.postDataText;
data.push("--data-binary"); postData.push("--data-binary");
let boundary = utils.getMultipartBoundary(aData); let boundary = utils.getMultipartBoundary(data);
let text = utils.removeBinaryDataFromMultipartText(postDataText, boundary); let text = utils.removeBinaryDataFromMultipartText(postDataText, boundary);
data.push(escapeString(text)); postData.push(escapeString(text));
ignoredHeaders.add("Content-Length"); ignoredHeaders.add("Content-Length");
} }
// Add method. // Add method.
// For GET and POST requests this is not necessary as GET is the // For GET and POST requests this is not necessary as GET is the
// default. If --data or --binary is added POST is the default. // default. If --data or --binary is added POST is the default.
if (!(aData.method == "GET" || aData.method == "POST")) { if (!(data.method == "GET" || data.method == "POST")) {
command.push("-X"); command.push("-X");
command.push(aData.method); command.push(data.method);
} }
// Add -I (HEAD) // Add -I (HEAD)
// For servers that supports HEAD. // For servers that supports HEAD.
// This will fetch the header of a document only. // This will fetch the header of a document only.
if (aData.method == "HEAD") { if (data.method == "HEAD") {
command.push("-I"); command.push("-I");
} }
// Add http version. // Add http version.
if (aData.httpVersion && aData.httpVersion != DEFAULT_HTTP_VERSION) { if (data.httpVersion && data.httpVersion != DEFAULT_HTTP_VERSION) {
command.push("--" + aData.httpVersion.split("/")[1]); command.push("--" + data.httpVersion.split("/")[1]);
} }
// Add request headers. // Add request headers.
let headers = aData.headers; let headers = data.headers;
if (multipartRequest) { if (multipartRequest) {
let multipartHeaders = utils.getHeadersFromMultipartText(postDataText); let multipartHeaders = utils.getHeadersFromMultipartText(postDataText);
headers = headers.concat(multipartHeaders); headers = headers.concat(multipartHeaders);
@ -130,7 +130,7 @@ const Curl = {
} }
// Add post data. // Add post data.
command = command.concat(data); command = command.concat(postData);
return command.join(" "); return command.join(" ");
} }
@ -145,13 +145,13 @@ const CurlUtils = {
/** /**
* Check if the request is an URL encoded request. * Check if the request is an URL encoded request.
* *
* @param object aData * @param object data
* The data source. See the description in the Curl object. * The data source. See the description in the Curl object.
* @return boolean * @return boolean
* True if the request is URL encoded, false otherwise. * True if the request is URL encoded, false otherwise.
*/ */
isUrlEncodedRequest: function (aData) { isUrlEncodedRequest: function (data) {
let postDataText = aData.postDataText; let postDataText = data.postDataText;
if (!postDataText) { if (!postDataText) {
return false; return false;
} }
@ -161,7 +161,7 @@ const CurlUtils = {
return true; return true;
} }
let contentType = this.findHeader(aData.headers, "content-type"); let contentType = this.findHeader(data.headers, "content-type");
return (contentType && return (contentType &&
contentType.toLowerCase().includes("application/x-www-form-urlencoded")); contentType.toLowerCase().includes("application/x-www-form-urlencoded"));
@ -170,13 +170,13 @@ const CurlUtils = {
/** /**
* Check if the request is a multipart request. * Check if the request is a multipart request.
* *
* @param object aData * @param object data
* The data source. * The data source.
* @return boolean * @return boolean
* True if the request is multipart reqeust, false otherwise. * True if the request is multipart reqeust, false otherwise.
*/ */
isMultipartRequest: function (aData) { isMultipartRequest: function (data) {
let postDataText = aData.postDataText; let postDataText = data.postDataText;
if (!postDataText) { if (!postDataText) {
return false; return false;
} }
@ -186,7 +186,7 @@ const CurlUtils = {
return true; return true;
} }
let contentType = this.findHeader(aData.headers, "content-type"); let contentType = this.findHeader(data.headers, "content-type");
return (contentType && return (contentType &&
contentType.toLowerCase().includes("multipart/form-data;")); contentType.toLowerCase().includes("multipart/form-data;"));
@ -195,33 +195,33 @@ const CurlUtils = {
/** /**
* Write out paramters from post data text. * Write out paramters from post data text.
* *
* @param object aPostDataText * @param object postDataText
* Post data text. * Post data text.
* @return string * @return string
* Post data parameters. * Post data parameters.
*/ */
writePostDataTextParams: function (aPostDataText) { writePostDataTextParams: function (postDataText) {
let lines = aPostDataText.split("\r\n"); let lines = postDataText.split("\r\n");
return lines[lines.length - 1]; return lines[lines.length - 1];
}, },
/** /**
* Finds the header with the given name in the headers array. * Finds the header with the given name in the headers array.
* *
* @param array aHeaders * @param array headers
* Array of headers info {name:x, value:x}. * Array of headers info {name:x, value:x}.
* @param string aName * @param string name
* The header name to find. * The header name to find.
* @return string * @return string
* The found header value or null if not found. * The found header value or null if not found.
*/ */
findHeader: function (aHeaders, aName) { findHeader: function (headers, name) {
if (!aHeaders) { if (!headers) {
return null; return null;
} }
let name = aName.toLowerCase(); name = name.toLowerCase();
for (let header of aHeaders) { for (let header of headers) {
if (name == header.name.toLowerCase()) { if (name == header.name.toLowerCase()) {
return header.value; return header.value;
} }
@ -233,23 +233,23 @@ const CurlUtils = {
/** /**
* Returns the boundary string for a multipart request. * Returns the boundary string for a multipart request.
* *
* @param string aData * @param string data
* The data source. See the description in the Curl object. * The data source. See the description in the Curl object.
* @return string * @return string
* The boundary string for the request. * The boundary string for the request.
*/ */
getMultipartBoundary: function (aData) { getMultipartBoundary: function (data) {
let boundaryRe = /\bboundary=(-{3,}\w+)/i; let boundaryRe = /\bboundary=(-{3,}\w+)/i;
// Get the boundary string from the Content-Type request header. // Get the boundary string from the Content-Type request header.
let contentType = this.findHeader(aData.headers, "Content-Type"); let contentType = this.findHeader(data.headers, "Content-Type");
if (boundaryRe.test(contentType)) { if (boundaryRe.test(contentType)) {
return contentType.match(boundaryRe)[1]; return contentType.match(boundaryRe)[1];
} }
// Temporary workaround. As of 2014-03-11 the requestHeaders array does not // Temporary workaround. As of 2014-03-11 the requestHeaders array does not
// always contain the Content-Type header for mulitpart requests. See bug 978144. // always contain the Content-Type header for mulitpart requests. See bug 978144.
// Find the header from the request payload. // Find the header from the request payload.
let boundaryString = aData.postDataText.match(boundaryRe)[1]; let boundaryString = data.postDataText.match(boundaryRe)[1];
if (boundaryString) { if (boundaryString) {
return boundaryString; return boundaryString;
} }
@ -258,19 +258,19 @@ const CurlUtils = {
}, },
/** /**
* Removes the binary data from mulitpart text. * Removes the binary data from multipart text.
* *
* @param string aMultipartText * @param string multipartText
* Multipart form data text. * Multipart form data text.
* @param string aBoundary * @param string boundary
* The boundary string. * The boundary string.
* @return string * @return string
* The mulitpart text without the binary data. * The multipart text without the binary data.
*/ */
removeBinaryDataFromMultipartText: function (aMultipartText, aBoundary) { removeBinaryDataFromMultipartText: function (multipartText, boundary) {
let result = ""; let result = "";
let boundary = "--" + aBoundary; boundary = "--" + boundary;
let parts = aMultipartText.split(boundary); let parts = multipartText.split(boundary);
for (let part of parts) { for (let part of parts) {
// Each part is expected to have a content disposition line. // Each part is expected to have a content disposition line.
let contentDispositionLine = part.trimLeft().split("\r\n")[0]; let contentDispositionLine = part.trimLeft().split("\r\n")[0];
@ -284,8 +284,7 @@ const CurlUtils = {
// Add only the headers to the result. // Add only the headers to the result.
let headers = part.split("\r\n\r\n")[0]; let headers = part.split("\r\n\r\n")[0];
result += boundary + "\r\n" + headers + "\r\n\r\n"; result += boundary + "\r\n" + headers + "\r\n\r\n";
} } else {
else {
result += boundary + "\r\n" + part; result += boundary + "\r\n" + part;
} }
} }
@ -298,25 +297,25 @@ const CurlUtils = {
/** /**
* Get the headers from a multipart post data text. * Get the headers from a multipart post data text.
* *
* @param string aMultipartText * @param string multipartText
* Multipart post text. * Multipart post text.
* @return array * @return array
* An array of header objects {name:x, value:x} * An array of header objects {name:x, value:x}
*/ */
getHeadersFromMultipartText: function (aMultipartText) { getHeadersFromMultipartText: function (multipartText) {
let headers = []; let headers = [];
if (!aMultipartText || aMultipartText.startsWith("---")) { if (!multipartText || multipartText.startsWith("---")) {
return headers; return headers;
} }
// Get the header section. // Get the header section.
let index = aMultipartText.indexOf("\r\n\r\n"); let index = multipartText.indexOf("\r\n\r\n");
if (index == -1) { if (index == -1) {
return headers; return headers;
} }
// Parse the header lines. // Parse the header lines.
let headersText = aMultipartText.substring(0, index); let headersText = multipartText.substring(0, index);
let headerLines = headersText.split("\r\n"); let headerLines = headersText.split("\r\n");
let lastHeaderName = null; let lastHeaderName = null;
@ -367,10 +366,10 @@ const CurlUtils = {
.replace(/\n/g, "\\n") .replace(/\n/g, "\\n")
.replace(/\r/g, "\\r") .replace(/\r/g, "\\r")
.replace(/[^\x20-\x7E]/g, escapeCharacter) + "'"; .replace(/[^\x20-\x7E]/g, escapeCharacter) + "'";
} else {
// Use single quote syntax.
return "'" + str + "'";
} }
// Use single quote syntax.
return "'" + str + "'";
}, },
/** /**