зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1817084 - Remove now unused Http.sys.mjs; r=nordzilla
The old translations code was the last consumer of this code. Depends on D181700 Differential Revision: https://phabricator.services.mozilla.com/D181786
This commit is contained in:
Родитель
c18ddeaa00
Коммит
2a21e5ee23
|
@ -1,103 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* 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/. */
|
||||
|
||||
// Strictly follow RFC 3986 when encoding URI components.
|
||||
// Accepts a unescaped string and returns the URI encoded string for use in
|
||||
// an HTTP request.
|
||||
export function percentEncode(aString) {
|
||||
return encodeURIComponent(aString)
|
||||
.replace(/[!'()]/g, escape)
|
||||
.replace(/\*/g, "%2A");
|
||||
}
|
||||
|
||||
/*
|
||||
* aOptions can have a variety of fields:
|
||||
* headers, an array of headers
|
||||
* postData, this can be:
|
||||
* a string: send it as is
|
||||
* an array of parameters: encode as form values
|
||||
* null/undefined: no POST data.
|
||||
* method, GET, POST or PUT (this is set automatically if postData exists).
|
||||
* onLoad, a function handle to call when the load is complete, it takes two
|
||||
* parameters: the responseText and the XHR object.
|
||||
* onError, a function handle to call when an error occcurs, it takes three
|
||||
* parameters: the error, the responseText and the XHR object.
|
||||
* logger, an object that implements the debug and log methods (e.g. Log.sys.mjs).
|
||||
*
|
||||
* Headers or post data are given as an array of arrays, for each each inner
|
||||
* array the first value is the key and the second is the value, e.g.
|
||||
* [["key1", "value1"], ["key2", "value2"]].
|
||||
*/
|
||||
export function httpRequest(aUrl, aOptions) {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.mozBackgroundRequest = true; // no error dialogs
|
||||
xhr.open(aOptions.method || (aOptions.postData ? "POST" : "GET"), aUrl);
|
||||
xhr.channel.loadFlags =
|
||||
Ci.nsIChannel.LOAD_ANONYMOUS | // don't send cookies
|
||||
Ci.nsIChannel.LOAD_BYPASS_CACHE |
|
||||
Ci.nsIChannel.INHIBIT_CACHING;
|
||||
xhr.onerror = function (aProgressEvent) {
|
||||
if (aOptions.onError) {
|
||||
// adapted from toolkit/mozapps/extensions/nsBlocklistService.js
|
||||
let request = aProgressEvent.target;
|
||||
let status;
|
||||
try {
|
||||
// may throw (local file or timeout)
|
||||
status = request.status;
|
||||
} catch (e) {
|
||||
request = request.channel.QueryInterface(Ci.nsIRequest);
|
||||
status = request.status;
|
||||
}
|
||||
// When status is 0 we don't have a valid channel.
|
||||
let statusText = status ? request.statusText : "offline";
|
||||
aOptions.onError(statusText, null, this);
|
||||
}
|
||||
};
|
||||
xhr.onload = function (aRequest) {
|
||||
try {
|
||||
let target = aRequest.target;
|
||||
if (aOptions.logger) {
|
||||
aOptions.logger.debug("Received response: " + target.responseText);
|
||||
}
|
||||
if (target.status < 200 || target.status >= 300) {
|
||||
let errorText = target.responseText;
|
||||
if (!errorText || /<(ht|\?x)ml\b/i.test(errorText)) {
|
||||
errorText = target.statusText;
|
||||
}
|
||||
throw new Error(target.status + " - " + errorText);
|
||||
}
|
||||
if (aOptions.onLoad) {
|
||||
aOptions.onLoad(target.responseText, this);
|
||||
}
|
||||
} catch (e) {
|
||||
if (aOptions.onError) {
|
||||
aOptions.onError(e, aRequest.target.responseText, this);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (aOptions.headers) {
|
||||
aOptions.headers.forEach(function (header) {
|
||||
xhr.setRequestHeader(header[0], header[1]);
|
||||
});
|
||||
}
|
||||
|
||||
// Handle adding postData as defined above.
|
||||
let POSTData = aOptions.postData || null;
|
||||
if (POSTData && Array.isArray(POSTData)) {
|
||||
xhr.setRequestHeader(
|
||||
"Content-Type",
|
||||
"application/x-www-form-urlencoded; charset=utf-8"
|
||||
);
|
||||
POSTData = POSTData.map(p => p[0] + "=" + percentEncode(p[1])).join("&");
|
||||
}
|
||||
|
||||
if (aOptions.logger) {
|
||||
aOptions.logger.log(
|
||||
"sending request to " + aUrl + " (POSTData = " + POSTData + ")"
|
||||
);
|
||||
}
|
||||
xhr.send(POSTData);
|
||||
return xhr;
|
||||
}
|
|
@ -176,7 +176,6 @@ EXTRA_JS_MODULES += [
|
|||
"FormLikeFactory.sys.mjs",
|
||||
"Geometry.sys.mjs",
|
||||
"HiddenFrame.sys.mjs",
|
||||
"Http.sys.mjs",
|
||||
"IgnoreLists.sys.mjs",
|
||||
"IndexedDB.sys.mjs",
|
||||
"InlineSpellChecker.sys.mjs",
|
||||
|
|
|
@ -1,275 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
const { httpRequest } = ChromeUtils.importESModule(
|
||||
"resource://gre/modules/Http.sys.mjs"
|
||||
);
|
||||
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
|
||||
|
||||
const BinaryInputStream = Components.Constructor(
|
||||
"@mozilla.org/binaryinputstream;1",
|
||||
"nsIBinaryInputStream",
|
||||
"setInputStream"
|
||||
);
|
||||
|
||||
var server;
|
||||
|
||||
const kDefaultServerPort = 9000;
|
||||
const kSuccessPath = "/success";
|
||||
const kBaseUrl = "http://localhost:" + kDefaultServerPort;
|
||||
const kSuccessUrl = kBaseUrl + kSuccessPath;
|
||||
|
||||
const kPostPath = "/post";
|
||||
const kPostUrl = kBaseUrl + kPostPath;
|
||||
const kPostDataSent = [
|
||||
["foo", "bar"],
|
||||
["complex", "!*()@"],
|
||||
];
|
||||
const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40";
|
||||
const kPostMimeTypeReceived =
|
||||
"application/x-www-form-urlencoded; charset=utf-8";
|
||||
|
||||
const kJsonPostPath = "/json_post";
|
||||
const kJsonPostUrl = kBaseUrl + kJsonPostPath;
|
||||
const kJsonPostData = JSON.stringify(kPostDataSent);
|
||||
const kJsonPostMimeType = "application/json";
|
||||
|
||||
const kPutPath = "/put";
|
||||
const kPutUrl = kBaseUrl + kPutPath;
|
||||
const kPutDataSent = [["P", "NP"]];
|
||||
const kPutDataReceived = "P=NP";
|
||||
|
||||
const kGetPath = "/get";
|
||||
const kGetUrl = kBaseUrl + kGetPath;
|
||||
|
||||
function successResult(aRequest, aResponse) {
|
||||
aResponse.setStatusLine(null, 200, "OK");
|
||||
aResponse.setHeader("Content-Type", "application/json");
|
||||
aResponse.write("Success!");
|
||||
}
|
||||
|
||||
function getDataChecker(
|
||||
aExpectedMethod,
|
||||
aExpectedData,
|
||||
aExpectedMimeType = null
|
||||
) {
|
||||
return function (aRequest, aResponse) {
|
||||
let body = new BinaryInputStream(aRequest.bodyInputStream);
|
||||
let bytes = [];
|
||||
let avail;
|
||||
while ((avail = body.available()) > 0) {
|
||||
Array.prototype.push.apply(bytes, body.readByteArray(avail));
|
||||
}
|
||||
|
||||
Assert.equal(aRequest.method, aExpectedMethod);
|
||||
|
||||
// Checking if the Content-Type is as expected.
|
||||
if (aExpectedMimeType) {
|
||||
let contentType = aRequest.getHeader("Content-Type");
|
||||
Assert.equal(contentType, aExpectedMimeType);
|
||||
}
|
||||
|
||||
var data = String.fromCharCode.apply(null, bytes);
|
||||
|
||||
Assert.equal(data, aExpectedData);
|
||||
|
||||
aResponse.setStatusLine(null, 200, "OK");
|
||||
aResponse.setHeader("Content-Type", "application/json");
|
||||
aResponse.write("Success!");
|
||||
};
|
||||
}
|
||||
|
||||
add_test(function test_successCallback() {
|
||||
do_test_pending();
|
||||
let options = {
|
||||
onLoad(aResponse) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
};
|
||||
httpRequest(kSuccessUrl, options);
|
||||
});
|
||||
|
||||
add_test(function test_errorCallback() {
|
||||
do_test_pending();
|
||||
let options = {
|
||||
onSuccess(aResponse) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e, aResponse) {
|
||||
Assert.equal(e.message, "404 - Not Found");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
};
|
||||
httpRequest(kBaseUrl + "/failure", options);
|
||||
});
|
||||
|
||||
add_test(function test_PostData() {
|
||||
do_test_pending();
|
||||
let options = {
|
||||
onLoad(aResponse) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
postData: kPostDataSent,
|
||||
};
|
||||
httpRequest(kPostUrl, options);
|
||||
});
|
||||
|
||||
add_test(function test_PutData() {
|
||||
do_test_pending();
|
||||
let options = {
|
||||
method: "PUT",
|
||||
onLoad(aResponse) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
postData: kPutDataSent,
|
||||
};
|
||||
httpRequest(kPutUrl, options);
|
||||
});
|
||||
|
||||
add_test(function test_GetData() {
|
||||
do_test_pending();
|
||||
let options = {
|
||||
onLoad(aResponse) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
postData: null,
|
||||
};
|
||||
httpRequest(kGetUrl, options);
|
||||
});
|
||||
|
||||
add_test(function test_OptionalParameters() {
|
||||
let options = {
|
||||
onLoad: null,
|
||||
onError: null,
|
||||
logger: null,
|
||||
};
|
||||
// Just make sure that nothing throws when doing this (i.e. httpRequest
|
||||
// doesn't try to access null options).
|
||||
httpRequest(kGetUrl, options);
|
||||
run_next_test();
|
||||
});
|
||||
|
||||
/**
|
||||
* Makes sure that httpRequest API allows setting a custom Content-Type header
|
||||
* for POST requests when data is a string.
|
||||
*/
|
||||
add_test(function test_CustomContentTypeOnPost() {
|
||||
do_test_pending();
|
||||
|
||||
// Preparing the request parameters.
|
||||
let options = {
|
||||
onLoad(aResponse) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
postData: kJsonPostData,
|
||||
// Setting a custom Content-Type header.
|
||||
headers: [["Content-Type", "application/json"]],
|
||||
};
|
||||
|
||||
// Firing the request.
|
||||
httpRequest(kJsonPostUrl, options);
|
||||
});
|
||||
|
||||
/**
|
||||
* Ensures that the httpRequest API provides a way to override the response
|
||||
* MIME type.
|
||||
*/
|
||||
add_test(function test_OverrideMimeType() {
|
||||
do_test_pending();
|
||||
|
||||
// Preparing the request parameters.
|
||||
const kMimeType = "text/xml; charset=UTF-8";
|
||||
let options = {
|
||||
onLoad(aResponse, xhr) {
|
||||
Assert.equal(aResponse, "Success!");
|
||||
|
||||
// Set the expected MIME-type.
|
||||
let reportedMimeType = xhr.getResponseHeader("Content-Type");
|
||||
Assert.notEqual(reportedMimeType, kMimeType);
|
||||
|
||||
// responseXML should not be not null if overriding mime type succeeded.
|
||||
Assert.ok(xhr.responseXML != null);
|
||||
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
onError(e) {
|
||||
Assert.ok(false);
|
||||
do_test_finished();
|
||||
run_next_test();
|
||||
},
|
||||
};
|
||||
|
||||
// Firing the request.
|
||||
let xhr = httpRequest(kGetUrl, options);
|
||||
|
||||
// Override the response MIME type.
|
||||
xhr.overrideMimeType(kMimeType);
|
||||
});
|
||||
|
||||
function run_test() {
|
||||
const PREF = "dom.xhr.standard_content_type_normalization";
|
||||
Services.prefs.setBoolPref(PREF, true);
|
||||
|
||||
// Set up a mock HTTP server to serve a success page.
|
||||
server = new HttpServer();
|
||||
server.registerPathHandler(kSuccessPath, successResult);
|
||||
server.registerPathHandler(
|
||||
kPostPath,
|
||||
getDataChecker("POST", kPostDataReceived, kPostMimeTypeReceived)
|
||||
);
|
||||
server.registerPathHandler(kPutPath, getDataChecker("PUT", kPutDataReceived));
|
||||
server.registerPathHandler(kGetPath, getDataChecker("GET", ""));
|
||||
server.registerPathHandler(
|
||||
kJsonPostPath,
|
||||
getDataChecker("POST", kJsonPostData, kJsonPostMimeType)
|
||||
);
|
||||
|
||||
server.start(kDefaultServerPort);
|
||||
|
||||
run_next_test();
|
||||
|
||||
// Teardown.
|
||||
registerCleanupFunction(function () {
|
||||
Services.prefs.clearUserPref(PREF);
|
||||
server.stop(function () {});
|
||||
});
|
||||
}
|
|
@ -32,7 +32,6 @@ support-files =
|
|||
../../../mozapps/extensions/test/xpcshell/data/productaddons/content_signing_int.pem
|
||||
../../../mozapps/extensions/test/xpcshell/data/productaddons/bad.xml
|
||||
../../../mozapps/extensions/test/xpcshell/data/productaddons/good.xml
|
||||
[test_Http.js]
|
||||
[test_IgnoreList.js]
|
||||
tags = remote-settings
|
||||
[test_Integration.js]
|
||||
|
|
Загрузка…
Ссылка в новой задаче