head_utilities.js (1791B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 var { 7 HTTP_400, 8 HTTP_401, 9 HTTP_402, 10 HTTP_403, 11 HTTP_404, 12 HTTP_405, 13 HTTP_406, 14 HTTP_407, 15 HTTP_408, 16 HTTP_409, 17 HTTP_410, 18 HTTP_411, 19 HTTP_412, 20 HTTP_413, 21 HTTP_414, 22 HTTP_415, 23 HTTP_417, 24 HTTP_500, 25 HTTP_501, 26 HTTP_502, 27 HTTP_503, 28 HTTP_504, 29 HTTP_505, 30 HttpError, 31 HttpServer, 32 } = ChromeUtils.importESModule("resource://testing-common/httpd.sys.mjs"); 33 var { NetUtil } = ChromeUtils.importESModule( 34 "resource://gre/modules/NetUtil.sys.mjs" 35 ); 36 37 const nsIDocumentEncoder = Ci.nsIDocumentEncoder; 38 const replacementChar = 39 Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER; 40 41 function loadContentFile(aFile, aCharset) { 42 // if(aAsIso == undefined) aAsIso = false; 43 if (aCharset == undefined) { 44 aCharset = "UTF-8"; 45 } 46 47 var file = do_get_file(aFile); 48 49 var chann = NetUtil.newChannel({ 50 uri: Services.io.newFileURI(file), 51 loadUsingSystemPrincipal: true, 52 }); 53 chann.contentCharset = aCharset; 54 55 /* var inputStream = Components.classes["@mozilla.org/scriptableinputstream;1"] 56 .createInstance(Components.interfaces.nsIScriptableInputStream); 57 inputStream.init(chann.open()); 58 return inputStream.read(file.fileSize); 59 */ 60 61 var inputStream = Cc[ 62 "@mozilla.org/intl/converter-input-stream;1" 63 ].createInstance(Ci.nsIConverterInputStream); 64 inputStream.init(chann.open(), aCharset, 1024, replacementChar); 65 var str = {}, 66 content = ""; 67 while (inputStream.readString(4096, str) != 0) { 68 content += str.value; 69 } 70 return content; 71 }