addon-sdk/source/lib/sdk/net/url.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "experimental"
michael@0 9 };
michael@0 10
michael@0 11 const { Cu, components } = require("chrome");
michael@0 12 const { defer } = require("../core/promise");
michael@0 13 const { merge } = require("../util/object");
michael@0 14
michael@0 15 const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
michael@0 16
michael@0 17 /**
michael@0 18 * Reads a URI and returns a promise.
michael@0 19 *
michael@0 20 * @param uri {string} The URI to read
michael@0 21 * @param [options] {object} This parameter can have any or all of the following
michael@0 22 * fields: `charset`. By default the `charset` is set to 'UTF-8'.
michael@0 23 *
michael@0 24 * @returns {promise} The promise that will be resolved with the content of the
michael@0 25 * URL given.
michael@0 26 *
michael@0 27 * @example
michael@0 28 * let promise = readURI('resource://gre/modules/NetUtil.jsm', {
michael@0 29 * charset: 'US-ASCII'
michael@0 30 * });
michael@0 31 */
michael@0 32 function readURI(uri, options) {
michael@0 33 options = options || {};
michael@0 34 let charset = options.charset || 'UTF-8';
michael@0 35
michael@0 36 let channel = NetUtil.newChannel(uri, charset, null);
michael@0 37
michael@0 38 let { promise, resolve, reject } = defer();
michael@0 39
michael@0 40 try {
michael@0 41 NetUtil.asyncFetch(channel, function (stream, result) {
michael@0 42 if (components.isSuccessCode(result)) {
michael@0 43 let count = stream.available();
michael@0 44 let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
michael@0 45
michael@0 46 resolve(data);
michael@0 47 } else {
michael@0 48 reject("Failed to read: '" + uri + "' (Error Code: " + result + ")");
michael@0 49 }
michael@0 50 });
michael@0 51 }
michael@0 52 catch (e) {
michael@0 53 reject("Failed to read: '" + uri + "' (Error: " + e.message + ")");
michael@0 54 }
michael@0 55
michael@0 56 return promise;
michael@0 57 }
michael@0 58
michael@0 59 exports.readURI = readURI;
michael@0 60
michael@0 61 /**
michael@0 62 * Reads a URI synchronously.
michael@0 63 * This function is intentionally undocumented to favorites the `readURI` usage.
michael@0 64 *
michael@0 65 * @param uri {string} The URI to read
michael@0 66 * @param [charset] {string} The character set to use when read the content of
michael@0 67 * the `uri` given. By default is set to 'UTF-8'.
michael@0 68 *
michael@0 69 * @returns {string} The content of the URI given.
michael@0 70 *
michael@0 71 * @example
michael@0 72 * let data = readURISync('resource://gre/modules/NetUtil.jsm');
michael@0 73 */
michael@0 74 function readURISync(uri, charset) {
michael@0 75 charset = typeof charset === "string" ? charset : "UTF-8";
michael@0 76
michael@0 77 let channel = NetUtil.newChannel(uri, charset, null);
michael@0 78 let stream = channel.open();
michael@0 79
michael@0 80 let count = stream.available();
michael@0 81 let data = NetUtil.readInputStreamToString(stream, count, { charset : charset });
michael@0 82
michael@0 83 stream.close();
michael@0 84
michael@0 85 return data;
michael@0 86 }
michael@0 87
michael@0 88 exports.readURISync = readURISync;

mercurial