2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2012-03-23 02:49:50 +04:00
|
|
|
"AsyncResource",
|
|
|
|
"Resource"
|
|
|
|
];
|
2008-11-04 01:36:29 +03:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2013-04-15 23:45:37 +04:00
|
|
|
Cu.import("resource://gre/modules/Preferences.jsm");
|
2012-04-06 10:26:06 +04:00
|
|
|
Cu.import("resource://services-common/async.js");
|
2013-08-26 22:55:58 +04:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2012-04-06 10:26:06 +04:00
|
|
|
Cu.import("resource://services-common/observers.js");
|
2012-11-06 05:31:12 +04:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2012-10-10 22:48:16 +04:00
|
|
|
const DEFAULT_LOAD_FLAGS =
|
|
|
|
// Always validate the cache:
|
|
|
|
Ci.nsIRequest.LOAD_BYPASS_CACHE |
|
|
|
|
Ci.nsIRequest.INHIBIT_CACHING |
|
|
|
|
// Don't send user cookies over the wire (Bug 644734).
|
|
|
|
Ci.nsIRequest.LOAD_ANONYMOUS;
|
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
/*
|
|
|
|
* AsyncResource represents a remote network resource, identified by a URI.
|
|
|
|
* Create an instance like so:
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* let resource = new AsyncResource("http://foobar.com/path/to/resource");
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* The 'resource' object has the following methods to issue HTTP requests
|
|
|
|
* of the corresponding HTTP methods:
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* get(callback)
|
|
|
|
* put(data, callback)
|
|
|
|
* post(data, callback)
|
|
|
|
* delete(callback)
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* 'callback' is a function with the following signature:
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* function callback(error, result) {...}
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* 'error' will be null on successful requests. Likewise, result will not be
|
2011-04-02 02:27:19 +04:00
|
|
|
* passed (=undefined) when an error occurs. Note that this is independent of
|
2010-10-29 21:20:27 +04:00
|
|
|
* the status of the HTTP response.
|
|
|
|
*/
|
2012-10-31 20:13:28 +04:00
|
|
|
this.AsyncResource = function AsyncResource(uri) {
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log = Log.repository.getLogger(this._logName);
|
2010-02-12 02:25:31 +03:00
|
|
|
this._log.level =
|
2013-08-26 22:55:58 +04:00
|
|
|
Log.Level[Svc.Prefs.get("log.logger.network.resources")];
|
2010-02-12 02:25:31 +03:00
|
|
|
this.uri = uri;
|
2010-06-02 02:15:53 +04:00
|
|
|
this._headers = {};
|
2010-10-29 21:20:27 +04:00
|
|
|
this._onComplete = Utils.bind2(this, this._onComplete);
|
2008-11-04 01:36:29 +03:00
|
|
|
}
|
2010-10-29 21:20:27 +04:00
|
|
|
AsyncResource.prototype = {
|
2011-06-13 22:42:18 +04:00
|
|
|
_logName: "Sync.AsyncResource",
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource.serverTime }}} **
|
|
|
|
//
|
|
|
|
// Caches the latest server timestamp (X-Weave-Timestamp header).
|
|
|
|
serverTime: null,
|
|
|
|
|
2012-03-23 02:49:50 +04:00
|
|
|
/**
|
|
|
|
* Callback to be invoked at request time to add authentication details.
|
|
|
|
*
|
|
|
|
* By default, a global authenticator is provided. If this is set, it will
|
|
|
|
* be used instead of the global one.
|
|
|
|
*/
|
|
|
|
authenticator: null,
|
|
|
|
|
2011-04-08 00:55:02 +04:00
|
|
|
// The string to use as the base User-Agent in Sync requests.
|
2011-04-09 00:43:38 +04:00
|
|
|
// These strings will look something like
|
2012-09-15 03:02:32 +04:00
|
|
|
//
|
2011-04-09 00:43:38 +04:00
|
|
|
// Firefox/4.0 FxSync/1.8.0.20100101.mobile
|
2012-09-15 03:02:32 +04:00
|
|
|
//
|
2011-04-09 00:43:38 +04:00
|
|
|
// or
|
2012-09-15 03:02:32 +04:00
|
|
|
//
|
2011-04-09 00:43:38 +04:00
|
|
|
// Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
|
|
|
|
//
|
|
|
|
_userAgent:
|
2011-05-20 05:08:07 +04:00
|
|
|
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
|
|
|
|
" FxSync/" + WEAVE_VERSION + "." + // Sync.
|
|
|
|
Services.appinfo.appBuildID + ".", // Build.
|
2011-04-08 00:55:02 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
// Wait 5 minutes before killing a request.
|
2011-02-01 07:55:48 +03:00
|
|
|
ABORT_TIMEOUT: 300000,
|
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource.headers }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
2010-06-02 02:15:53 +04:00
|
|
|
// Headers to be included when making a request for the resource.
|
|
|
|
// Note: Header names should be all lower case, there's no explicit
|
|
|
|
// check for duplicates due to case!
|
2008-11-04 01:36:29 +03:00
|
|
|
get headers() {
|
2012-03-23 02:49:50 +04:00
|
|
|
return this._headers;
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
set headers(value) {
|
|
|
|
this._headers = value;
|
|
|
|
},
|
2011-03-31 05:28:56 +04:00
|
|
|
setHeader: function Res_setHeader(header, value) {
|
|
|
|
this._headers[header.toLowerCase()] = value;
|
2009-03-26 05:30:16 +03:00
|
|
|
},
|
2012-10-10 22:48:16 +04:00
|
|
|
get headerNames() {
|
|
|
|
return Object.keys(this.headers);
|
|
|
|
},
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource.uri }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
|
|
|
// URI representing this resource.
|
2008-11-04 01:36:29 +03:00
|
|
|
get uri() {
|
|
|
|
return this._uri;
|
|
|
|
},
|
|
|
|
set uri(value) {
|
2008-11-08 13:00:33 +03:00
|
|
|
if (typeof value == 'string')
|
2012-11-06 05:31:12 +04:00
|
|
|
this._uri = CommonUtils.makeURI(value);
|
2008-11-08 13:00:33 +03:00
|
|
|
else
|
|
|
|
this._uri = value;
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource.spec }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
|
|
|
// Get the string representation of the URI.
|
2008-11-04 01:36:29 +03:00
|
|
|
get spec() {
|
2008-11-08 13:00:33 +03:00
|
|
|
if (this._uri)
|
|
|
|
return this._uri.spec;
|
|
|
|
return null;
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource.data }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
|
|
|
// Get and set the data encapulated in the resource.
|
2008-11-07 10:23:35 +03:00
|
|
|
_data: null,
|
|
|
|
get data() this._data,
|
2008-11-04 01:36:29 +03:00
|
|
|
set data(value) {
|
|
|
|
this._data = value;
|
|
|
|
},
|
|
|
|
|
2011-04-26 00:17:26 +04:00
|
|
|
// ** {{{ AsyncResource._createRequest }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
|
|
|
// This method returns a new IO Channel for requests to be made
|
2010-10-29 21:20:27 +04:00
|
|
|
// through. It is never called directly, only {{{_doRequest}}} uses it
|
2009-06-26 03:13:52 +04:00
|
|
|
// to obtain a request channel.
|
|
|
|
//
|
2012-03-23 02:49:50 +04:00
|
|
|
_createRequest: function Res__createRequest(method) {
|
2011-05-20 05:08:07 +04:00
|
|
|
let channel = Services.io.newChannel(this.spec, null, null)
|
|
|
|
.QueryInterface(Ci.nsIRequest)
|
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
2009-06-26 03:13:52 +04:00
|
|
|
|
2012-10-10 22:48:16 +04:00
|
|
|
channel.loadFlags |= DEFAULT_LOAD_FLAGS;
|
2009-11-21 01:34:20 +03:00
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
// Setup a callback to handle channel notifications.
|
2012-10-10 22:48:16 +04:00
|
|
|
let listener = new ChannelNotificationListener(this.headerNames);
|
|
|
|
channel.notificationCallbacks = listener;
|
2009-11-21 01:34:20 +03:00
|
|
|
|
2011-04-08 00:55:02 +04:00
|
|
|
// Compose a UA string fragment from the various available identifiers.
|
|
|
|
if (Svc.Prefs.get("sendVersionInfo", true)) {
|
|
|
|
let ua = this._userAgent + Svc.Prefs.get("client.type", "desktop");
|
|
|
|
channel.setRequestHeader("user-agent", ua, false);
|
|
|
|
}
|
2012-01-17 23:51:45 +04:00
|
|
|
|
2009-11-21 01:34:20 +03:00
|
|
|
let headers = this.headers;
|
2012-03-23 02:49:50 +04:00
|
|
|
|
2012-09-15 03:02:32 +04:00
|
|
|
if (this.authenticator) {
|
|
|
|
let result = this.authenticator(this, method);
|
2012-03-23 02:49:50 +04:00
|
|
|
if (result && result.headers) {
|
|
|
|
for (let [k, v] in Iterator(result.headers)) {
|
|
|
|
headers[k.toLowerCase()] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._log.debug("No authenticator found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let [key, value] in Iterator(headers)) {
|
2011-04-07 03:12:21 +04:00
|
|
|
if (key == 'authorization')
|
2008-11-04 01:36:29 +03:00
|
|
|
this._log.trace("HTTP Header " + key + ": ***** (suppressed)");
|
|
|
|
else
|
|
|
|
this._log.trace("HTTP Header " + key + ": " + headers[key]);
|
2009-08-27 02:32:46 +04:00
|
|
|
channel.setRequestHeader(key, headers[key], false);
|
2008-11-04 01:36:29 +03:00
|
|
|
}
|
2009-08-27 02:32:46 +04:00
|
|
|
return channel;
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2009-07-23 03:38:34 +04:00
|
|
|
_onProgress: function Res__onProgress(channel) {},
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
_doRequest: function _doRequest(action, data, callback) {
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("In _doRequest.");
|
2010-10-29 21:20:27 +04:00
|
|
|
this._callback = callback;
|
2012-03-23 02:49:50 +04:00
|
|
|
let channel = this._createRequest(action);
|
2009-01-22 22:48:36 +03:00
|
|
|
|
2008-11-20 03:19:51 +03:00
|
|
|
if ("undefined" != typeof(data))
|
|
|
|
this._data = data;
|
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
// PUT and POST are treated differently because they have payload data.
|
2008-11-20 03:19:51 +03:00
|
|
|
if ("PUT" == action || "POST" == action) {
|
2009-09-10 22:05:13 +04:00
|
|
|
// Convert non-string bodies into JSON
|
|
|
|
if (this._data.constructor.toString() != String)
|
|
|
|
this._data = JSON.stringify(this._data);
|
|
|
|
|
2009-07-15 00:59:46 +04:00
|
|
|
this._log.debug(action + " Length: " + this._data.length);
|
2009-07-10 04:44:41 +04:00
|
|
|
this._log.trace(action + " Body: " + this._data);
|
2009-01-22 22:48:36 +03:00
|
|
|
|
2010-06-02 02:15:53 +04:00
|
|
|
let type = ('content-type' in this._headers) ?
|
|
|
|
this._headers['content-type'] : 'text/plain';
|
2009-03-26 05:30:16 +03:00
|
|
|
|
2009-01-28 00:35:10 +03:00
|
|
|
let stream = Cc["@mozilla.org/io/string-input-stream;1"].
|
2008-12-19 02:42:12 +03:00
|
|
|
createInstance(Ci.nsIStringInputStream);
|
2009-01-28 00:35:10 +03:00
|
|
|
stream.setData(this._data, this._data.length);
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2009-01-28 00:35:10 +03:00
|
|
|
channel.QueryInterface(Ci.nsIUploadChannel);
|
2009-03-26 05:30:16 +03:00
|
|
|
channel.setUploadStream(stream, type, this._data.length);
|
2008-12-19 02:42:12 +03:00
|
|
|
}
|
2008-11-04 01:36:29 +03:00
|
|
|
|
2009-06-26 03:13:52 +04:00
|
|
|
// Setup a channel listener so that the actual network operation
|
|
|
|
// is performed asynchronously.
|
2010-10-29 21:20:27 +04:00
|
|
|
let listener = new ChannelListener(this._onComplete, this._onProgress,
|
2011-02-01 07:55:48 +03:00
|
|
|
this._log, this.ABORT_TIMEOUT);
|
2008-12-19 02:42:12 +03:00
|
|
|
channel.requestMethod = action;
|
2013-01-03 03:40:03 +04:00
|
|
|
try {
|
|
|
|
channel.asyncOpen(listener, null);
|
|
|
|
} catch (ex) {
|
|
|
|
// asyncOpen can throw in a bunch of cases -- e.g., a forbidden port.
|
|
|
|
this._log.warn("Caught an error in asyncOpen: " + CommonUtils.exceptionStr(ex));
|
|
|
|
CommonUtils.nextTick(callback.bind(this, ex));
|
|
|
|
}
|
2010-10-29 21:20:27 +04:00
|
|
|
},
|
2009-08-13 07:28:46 +04:00
|
|
|
|
2011-10-29 01:43:18 +04:00
|
|
|
_onComplete: function _onComplete(error, data, channel) {
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("In _onComplete. Error is " + error + ".");
|
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
if (error) {
|
|
|
|
this._callback(error);
|
|
|
|
return;
|
2009-08-13 07:28:46 +04:00
|
|
|
}
|
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
this._data = data;
|
|
|
|
let action = channel.requestMethod;
|
2008-12-19 02:42:12 +03:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("Channel: " + channel);
|
|
|
|
this._log.trace("Action: " + action);
|
|
|
|
|
|
|
|
// Process status and success first. This way a problem with headers
|
|
|
|
// doesn't fail to include accurate status information.
|
2009-08-27 02:32:46 +04:00
|
|
|
let status = 0;
|
2010-12-20 23:47:51 +03:00
|
|
|
let success = false;
|
2011-04-02 02:27:19 +04:00
|
|
|
|
2009-08-27 02:32:46 +04:00
|
|
|
try {
|
2011-04-02 02:27:19 +04:00
|
|
|
status = channel.responseStatus;
|
|
|
|
success = channel.requestSucceeded; // HTTP status.
|
|
|
|
|
|
|
|
this._log.trace("Status: " + status);
|
|
|
|
this._log.trace("Success: " + success);
|
2009-08-27 02:32:46 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
// Log the status of the request.
|
2009-12-18 05:51:55 +03:00
|
|
|
let mesg = [action, success ? "success" : "fail", status,
|
2010-10-29 21:20:27 +04:00
|
|
|
channel.URI.spec].join(" ");
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.debug("mesg: " + mesg);
|
|
|
|
|
2009-12-18 05:51:55 +03:00
|
|
|
if (mesg.length > 200)
|
|
|
|
mesg = mesg.substr(0, 200) + "…";
|
|
|
|
this._log.debug(mesg);
|
2011-04-02 02:27:19 +04:00
|
|
|
|
|
|
|
// Additionally give the full response body when Trace logging.
|
2013-08-26 22:55:58 +04:00
|
|
|
if (this._log.level <= Log.Level.Trace)
|
2010-10-29 21:20:27 +04:00
|
|
|
this._log.trace(action + " body: " + data);
|
2009-09-16 05:38:52 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
} catch(ex) {
|
|
|
|
// Got a response, but an exception occurred during processing.
|
|
|
|
// This shouldn't occur.
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.warn("Caught unexpected exception " + CommonUtils.exceptionStr(ex) +
|
2011-04-02 02:27:19 +04:00
|
|
|
" in _onComplete.");
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.debug(CommonUtils.stackTrace(ex));
|
2011-04-02 02:27:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process headers. They can be empty, or the call can otherwise fail, so
|
|
|
|
// put this in its own try block.
|
|
|
|
let headers = {};
|
|
|
|
try {
|
|
|
|
this._log.trace("Processing response headers.");
|
|
|
|
|
|
|
|
// Read out the response headers if available.
|
|
|
|
channel.visitResponseHeaders({
|
|
|
|
visitHeader: function visitHeader(header, value) {
|
|
|
|
headers[header.toLowerCase()] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-09-11 20:39:21 +04:00
|
|
|
// This is a server-side safety valve to allow slowing down
|
|
|
|
// clients without hurting performance.
|
2012-12-22 23:43:56 +04:00
|
|
|
if (headers["x-weave-backoff"]) {
|
|
|
|
let backoff = headers["x-weave-backoff"];
|
|
|
|
this._log.debug("Got X-Weave-Backoff: " + backoff);
|
2010-09-11 20:39:21 +04:00
|
|
|
Observers.notify("weave:service:backoff:interval",
|
2012-12-22 23:43:56 +04:00
|
|
|
parseInt(backoff, 10));
|
|
|
|
}
|
2010-09-11 20:39:21 +04:00
|
|
|
|
2012-12-22 23:43:56 +04:00
|
|
|
if (success && headers["x-weave-quota-remaining"]) {
|
2010-09-11 20:39:21 +04:00
|
|
|
Observers.notify("weave:service:quota:remaining",
|
|
|
|
parseInt(headers["x-weave-quota-remaining"], 10));
|
2012-12-22 23:43:56 +04:00
|
|
|
}
|
2011-04-02 02:27:19 +04:00
|
|
|
} catch (ex) {
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.debug("Caught exception " + CommonUtils.exceptionStr(ex) +
|
2011-04-02 02:27:19 +04:00
|
|
|
" visiting headers in _onComplete.");
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.debug(CommonUtils.stackTrace(ex));
|
2008-11-04 01:36:29 +03:00
|
|
|
}
|
2009-01-22 22:48:36 +03:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
let ret = new String(data);
|
|
|
|
ret.status = status;
|
2009-08-27 02:32:46 +04:00
|
|
|
ret.success = success;
|
2011-04-02 02:27:19 +04:00
|
|
|
ret.headers = headers;
|
2009-09-10 22:04:36 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
// Make a lazy getter to convert the json response into an object.
|
|
|
|
// Note that this can cause a parse error to be thrown far away from the
|
|
|
|
// actual fetch, so be warned!
|
2011-10-06 12:47:02 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(ret, "obj", function() {
|
|
|
|
try {
|
|
|
|
return JSON.parse(ret);
|
|
|
|
} catch (ex) {
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.warn("Got exception parsing response body: \"" + CommonUtils.exceptionStr(ex));
|
2011-10-06 12:47:02 +04:00
|
|
|
// Stringify to avoid possibly printing non-printable characters.
|
|
|
|
this._log.debug("Parse fail: Response body starts: \"" +
|
|
|
|
JSON.stringify((ret + "").slice(0, 100)) +
|
|
|
|
"\".");
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2009-09-10 22:04:36 +04:00
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
this._callback(null, ret);
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function get(callback) {
|
|
|
|
this._doRequest("GET", undefined, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
put: function put(data, callback) {
|
|
|
|
if (typeof data == "function")
|
|
|
|
[data, callback] = [undefined, data];
|
|
|
|
this._doRequest("PUT", data, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
post: function post(data, callback) {
|
|
|
|
if (typeof data == "function")
|
|
|
|
[data, callback] = [undefined, data];
|
|
|
|
this._doRequest("POST", data, callback);
|
|
|
|
},
|
|
|
|
|
2011-08-27 03:39:04 +04:00
|
|
|
delete: function delete_(callback) {
|
2010-10-29 21:20:27 +04:00
|
|
|
this._doRequest("DELETE", undefined, callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represent a remote network resource, identified by a URI, with a
|
|
|
|
* synchronous API.
|
2012-09-15 03:02:32 +04:00
|
|
|
*
|
2010-10-29 21:20:27 +04:00
|
|
|
* 'Resource' is not recommended for new code. Use the asynchronous API of
|
|
|
|
* 'AsyncResource' instead.
|
|
|
|
*/
|
2012-10-31 20:13:28 +04:00
|
|
|
this.Resource = function Resource(uri) {
|
2010-10-29 21:20:27 +04:00
|
|
|
AsyncResource.call(this, uri);
|
|
|
|
}
|
|
|
|
Resource.prototype = {
|
|
|
|
|
|
|
|
__proto__: AsyncResource.prototype,
|
|
|
|
|
2011-06-13 22:42:18 +04:00
|
|
|
_logName: "Sync.Resource",
|
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
// ** {{{ Resource._request }}} **
|
|
|
|
//
|
|
|
|
// Perform a particular HTTP request on the resource. This method
|
|
|
|
// is never called directly, but is used by the high-level
|
|
|
|
// {{{get}}}, {{{put}}}, {{{post}}} and {{delete}} methods.
|
|
|
|
_request: function Res__request(action, data) {
|
2011-05-31 05:52:23 +04:00
|
|
|
let cb = Async.makeSyncCallback();
|
2010-10-29 21:20:27 +04:00
|
|
|
function callback(error, ret) {
|
|
|
|
if (error)
|
|
|
|
cb.throw(error);
|
|
|
|
cb(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel listener might get a failure code
|
|
|
|
try {
|
2011-02-26 01:43:18 +03:00
|
|
|
this._doRequest(action, data, callback);
|
2011-05-31 05:52:23 +04:00
|
|
|
return Async.waitForSyncCallback(cb);
|
2010-10-29 21:20:27 +04:00
|
|
|
} catch(ex) {
|
2011-02-01 07:55:48 +03:00
|
|
|
// Combine the channel stack with this request stack. Need to create
|
|
|
|
// a new error object for that.
|
2010-10-29 21:20:27 +04:00
|
|
|
let error = Error(ex.message);
|
2011-02-01 07:55:48 +03:00
|
|
|
error.result = ex.result;
|
2010-10-29 21:20:27 +04:00
|
|
|
let chanStack = [];
|
|
|
|
if (ex.stack)
|
|
|
|
chanStack = ex.stack.trim().split(/\n/).slice(1);
|
|
|
|
let requestStack = error.stack.split(/\n/).slice(1);
|
|
|
|
|
|
|
|
// Strip out the args for the last 2 frames because they're usually HUGE!
|
|
|
|
for (let i = 0; i <= 1; i++)
|
|
|
|
requestStack[i] = requestStack[i].replace(/\(".*"\)@/, "(...)@");
|
|
|
|
|
|
|
|
error.stack = chanStack.concat(requestStack).join("\n");
|
|
|
|
throw error;
|
|
|
|
}
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2009-06-26 03:13:52 +04:00
|
|
|
// ** {{{ Resource.get }}} **
|
|
|
|
//
|
|
|
|
// Perform an asynchronous HTTP GET for this resource.
|
2009-06-04 21:16:07 +04:00
|
|
|
get: function Res_get() {
|
2011-04-22 23:13:17 +04:00
|
|
|
return this._request("GET");
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2010-10-29 21:20:27 +04:00
|
|
|
// ** {{{ Resource.put }}} **
|
2009-06-26 03:13:52 +04:00
|
|
|
//
|
|
|
|
// Perform a HTTP PUT for this resource.
|
2009-06-04 21:16:07 +04:00
|
|
|
put: function Res_put(data) {
|
|
|
|
return this._request("PUT", data);
|
2008-11-04 01:36:29 +03:00
|
|
|
},
|
|
|
|
|
2009-06-26 03:13:52 +04:00
|
|
|
// ** {{{ Resource.post }}} **
|
|
|
|
//
|
|
|
|
// Perform a HTTP POST for this resource.
|
2009-06-04 21:16:07 +04:00
|
|
|
post: function Res_post(data) {
|
|
|
|
return this._request("POST", data);
|
2008-11-20 03:19:51 +03:00
|
|
|
},
|
|
|
|
|
2009-06-26 03:13:52 +04:00
|
|
|
// ** {{{ Resource.delete }}} **
|
|
|
|
//
|
|
|
|
// Perform a HTTP DELETE for this resource.
|
2009-06-04 21:16:07 +04:00
|
|
|
delete: function Res_delete() {
|
|
|
|
return this._request("DELETE");
|
2008-11-04 01:36:29 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-26 03:13:52 +04:00
|
|
|
// = ChannelListener =
|
2009-11-21 01:34:20 +03:00
|
|
|
//
|
2009-06-26 03:13:52 +04:00
|
|
|
// This object implements the {{{nsIStreamListener}}} interface
|
|
|
|
// and is called as the network operation proceeds.
|
2011-02-01 07:55:48 +03:00
|
|
|
function ChannelListener(onComplete, onProgress, logger, timeout) {
|
2008-12-19 02:42:12 +03:00
|
|
|
this._onComplete = onComplete;
|
|
|
|
this._onProgress = onProgress;
|
|
|
|
this._log = logger;
|
2011-02-01 07:55:48 +03:00
|
|
|
this._timeout = timeout;
|
2010-04-21 22:10:21 +04:00
|
|
|
this.delayAbort();
|
2008-12-19 02:42:12 +03:00
|
|
|
}
|
|
|
|
ChannelListener.prototype = {
|
2010-04-02 02:25:47 +04:00
|
|
|
|
2008-12-19 02:42:12 +03:00
|
|
|
onStartRequest: function Channel_onStartRequest(channel) {
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("onStartRequest called for channel " + channel + ".");
|
2012-01-17 23:51:45 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
channel.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("Unexpected error: channel is not a nsIHttpChannel!");
|
|
|
|
channel.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
return;
|
|
|
|
}
|
2010-04-02 02:54:53 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
// Save the latest server timestamp when possible.
|
2010-04-02 02:54:53 +04:00
|
|
|
try {
|
2011-04-26 00:17:26 +04:00
|
|
|
AsyncResource.serverTime = channel.getResponseHeader("X-Weave-Timestamp") - 0;
|
2010-04-02 02:54:53 +04:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("onStartRequest: " + channel.requestMethod + " " +
|
|
|
|
channel.URI.spec);
|
2008-12-19 02:42:12 +03:00
|
|
|
this._data = '';
|
2010-04-21 22:10:21 +04:00
|
|
|
this.delayAbort();
|
2008-12-19 02:42:12 +03:00
|
|
|
},
|
|
|
|
|
2009-08-13 07:28:46 +04:00
|
|
|
onStopRequest: function Channel_onStopRequest(channel, context, status) {
|
2011-04-02 02:27:19 +04:00
|
|
|
// Clear the abort timer now that the channel is done.
|
2010-04-02 02:25:47 +04:00
|
|
|
this.abortTimer.clear();
|
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
if (!this._onComplete) {
|
|
|
|
this._log.error("Unexpected error: _onComplete not defined in onStopRequest.");
|
|
|
|
this._onProgress = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
channel.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("Unexpected error: channel is not a nsIHttpChannel!");
|
|
|
|
|
|
|
|
this._onComplete(ex, this._data, channel);
|
|
|
|
this._onComplete = this._onProgress = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-22 21:14:13 +04:00
|
|
|
let statusSuccess = Components.isSuccessCode(status);
|
2011-05-31 01:21:03 +04:00
|
|
|
let uri = channel && channel.URI && channel.URI.spec || "<unknown>";
|
2011-09-22 21:14:13 +04:00
|
|
|
this._log.trace("Channel for " + channel.requestMethod + " " + uri + ": " +
|
2011-10-29 01:43:18 +04:00
|
|
|
"isSuccessCode(" + status + ")? " + statusSuccess);
|
2011-04-02 02:27:19 +04:00
|
|
|
|
2011-09-22 21:14:13 +04:00
|
|
|
if (this._data == '') {
|
2008-12-19 02:42:12 +03:00
|
|
|
this._data = null;
|
2011-09-22 21:14:13 +04:00
|
|
|
}
|
2008-12-19 02:42:12 +03:00
|
|
|
|
2011-09-22 21:14:13 +04:00
|
|
|
// Pass back the failure code and stop execution. Use Components.Exception()
|
2011-02-01 07:55:48 +03:00
|
|
|
// instead of Error() so the exception is QI-able and can be passed across
|
|
|
|
// XPCOM borders while preserving the status code.
|
2011-10-29 01:43:18 +04:00
|
|
|
if (!statusSuccess) {
|
|
|
|
let message = Components.Exception("", status).name;
|
|
|
|
let error = Components.Exception(message, status);
|
2012-01-17 23:51:45 +04:00
|
|
|
|
2011-10-29 01:43:18 +04:00
|
|
|
this._onComplete(error, undefined, channel);
|
2012-01-17 23:51:45 +04:00
|
|
|
this._onComplete = this._onProgress = null;
|
2010-10-29 21:20:27 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-08-13 07:28:46 +04:00
|
|
|
|
2011-04-02 02:27:19 +04:00
|
|
|
this._log.trace("Channel: flags = " + channel.loadFlags +
|
2011-05-31 01:21:03 +04:00
|
|
|
", URI = " + uri +
|
2011-04-02 02:27:19 +04:00
|
|
|
", HTTP success? " + channel.requestSucceeded);
|
2011-10-29 01:43:18 +04:00
|
|
|
this._onComplete(null, this._data, channel);
|
2012-01-17 23:51:45 +04:00
|
|
|
this._onComplete = this._onProgress = null;
|
2008-12-19 02:42:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function Channel_onDataAvail(req, cb, stream, off, count) {
|
2012-11-06 05:31:12 +04:00
|
|
|
let siStream;
|
|
|
|
try {
|
|
|
|
siStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
siStream.init(stream);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Exception creating nsIScriptableInputStream." + CommonUtils.exceptionStr(ex));
|
|
|
|
this._log.debug("Parameters: " + req.URI.spec + ", " + stream + ", " + off + ", " + count);
|
|
|
|
// Cannot proceed, so rethrow and allow the channel to cancel itself.
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2011-01-05 03:27:00 +03:00
|
|
|
try {
|
|
|
|
this._data += siStream.read(count);
|
|
|
|
} catch (ex) {
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.warn("Exception thrown reading " + count + " bytes from " + siStream + ".");
|
2011-01-05 03:27:00 +03:00
|
|
|
throw ex;
|
|
|
|
}
|
2012-09-15 03:02:32 +04:00
|
|
|
|
2011-01-05 03:27:00 +03:00
|
|
|
try {
|
|
|
|
this._onProgress();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception calling onProgress handler during fetch of "
|
|
|
|
+ req.URI.spec);
|
2012-11-06 05:31:12 +04:00
|
|
|
this._log.debug(CommonUtils.exceptionStr(ex));
|
2011-01-05 03:27:00 +03:00
|
|
|
this._log.trace("Rethrowing; expect a failure code from the HTTP channel.");
|
|
|
|
throw ex;
|
|
|
|
}
|
2012-09-15 03:02:32 +04:00
|
|
|
|
2010-04-21 22:10:21 +04:00
|
|
|
this.delayAbort();
|
|
|
|
},
|
2010-04-02 02:25:47 +04:00
|
|
|
|
2010-04-21 22:10:21 +04:00
|
|
|
/**
|
2012-11-06 05:31:12 +04:00
|
|
|
* Create or push back the abort timer that kills this request.
|
2010-04-21 22:10:21 +04:00
|
|
|
*/
|
|
|
|
delayAbort: function delayAbort() {
|
2012-11-06 05:31:12 +04:00
|
|
|
try {
|
|
|
|
CommonUtils.namedTimer(this.abortRequest, this._timeout, this, "abortTimer");
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception extending abort timer: " + CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
2010-04-02 02:25:47 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
abortRequest: function abortRequest() {
|
|
|
|
// Ignore any callbacks if we happen to get any now
|
|
|
|
this.onStopRequest = function() {};
|
2011-02-01 07:55:48 +03:00
|
|
|
let error = Components.Exception("Aborting due to channel inactivity.",
|
|
|
|
Cr.NS_ERROR_NET_TIMEOUT);
|
2012-01-17 23:51:45 +04:00
|
|
|
if (!this._onComplete) {
|
|
|
|
this._log.error("Unexpected error: _onComplete not defined in " +
|
|
|
|
"abortRequest.");
|
|
|
|
return;
|
|
|
|
}
|
2011-02-01 07:55:48 +03:00
|
|
|
this._onComplete(error);
|
2009-01-08 08:29:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
/**
|
|
|
|
* This class handles channel notification events.
|
|
|
|
*
|
|
|
|
* An instance of this class is bound to each created channel.
|
2012-10-10 22:48:16 +04:00
|
|
|
*
|
|
|
|
* Optionally pass an array of header names. Each header named
|
|
|
|
* in this array will be copied between the channels in the
|
|
|
|
* event of a redirect.
|
2012-01-17 23:51:45 +04:00
|
|
|
*/
|
2012-10-10 22:48:16 +04:00
|
|
|
function ChannelNotificationListener(headersToCopy) {
|
|
|
|
this._headersToCopy = headersToCopy;
|
|
|
|
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log = Log.repository.getLogger(this._logName);
|
|
|
|
this._log.level = Log.Level[Svc.Prefs.get("log.logger.network.resources")];
|
2009-02-11 20:11:18 +03:00
|
|
|
}
|
2012-01-17 23:51:45 +04:00
|
|
|
ChannelNotificationListener.prototype = {
|
2012-10-10 22:48:16 +04:00
|
|
|
_logName: "Sync.Resource",
|
|
|
|
|
2009-02-11 20:11:18 +03:00
|
|
|
getInterface: function(aIID) {
|
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
2009-02-20 12:50:26 +03:00
|
|
|
|
2009-02-11 20:11:18 +03:00
|
|
|
QueryInterface: function(aIID) {
|
2012-01-17 23:51:45 +04:00
|
|
|
if (aIID.equals(Ci.nsIBadCertListener2) ||
|
|
|
|
aIID.equals(Ci.nsIInterfaceRequestor) ||
|
|
|
|
aIID.equals(Ci.nsISupports) ||
|
|
|
|
aIID.equals(Ci.nsIChannelEventSink))
|
2009-02-11 20:11:18 +03:00
|
|
|
return this;
|
2009-02-20 12:50:26 +03:00
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
2009-02-11 20:11:18 +03:00
|
|
|
},
|
2009-02-20 12:50:26 +03:00
|
|
|
|
2009-02-11 20:11:18 +03:00
|
|
|
notifyCertProblem: function certProblem(socketInfo, sslStatus, targetHost) {
|
2013-08-26 22:55:58 +04:00
|
|
|
let log = Log.repository.getLogger("Sync.CertListener");
|
2012-01-17 23:51:45 +04:00
|
|
|
log.warn("Invalid HTTPS certificate encountered!");
|
2009-02-11 20:11:18 +03:00
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
// This suppresses the UI warning only. The request is still cancelled.
|
2009-02-11 20:11:18 +03:00
|
|
|
return true;
|
2012-01-17 23:51:45 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
asyncOnChannelRedirect:
|
|
|
|
function asyncOnChannelRedirect(oldChannel, newChannel, flags, callback) {
|
|
|
|
|
2012-11-06 05:31:12 +04:00
|
|
|
let oldSpec = (oldChannel && oldChannel.URI) ? oldChannel.URI.spec : "<undefined>";
|
|
|
|
let newSpec = (newChannel && newChannel.URI) ? newChannel.URI.spec : "<undefined>";
|
|
|
|
this._log.debug("Channel redirect: " + oldSpec + ", " + newSpec + ", " + flags);
|
2012-10-10 22:48:16 +04:00
|
|
|
|
2012-12-18 23:43:58 +04:00
|
|
|
this._log.debug("Ensuring load flags are set.");
|
|
|
|
newChannel.loadFlags |= DEFAULT_LOAD_FLAGS;
|
|
|
|
|
2012-10-10 22:48:16 +04:00
|
|
|
// For internal redirects, copy the headers that our caller set.
|
|
|
|
try {
|
|
|
|
if ((flags & Ci.nsIChannelEventSink.REDIRECT_INTERNAL) &&
|
|
|
|
newChannel.URI.equals(oldChannel.URI)) {
|
2012-12-18 23:43:58 +04:00
|
|
|
this._log.debug("Copying headers for safe internal redirect.");
|
2013-01-05 10:23:50 +04:00
|
|
|
|
|
|
|
// QI the channel so we can set headers on it.
|
|
|
|
try {
|
|
|
|
newChannel.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("Unexpected error: channel is not a nsIHttpChannel!");
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:48:16 +04:00
|
|
|
for (let header of this._headersToCopy) {
|
|
|
|
let value = oldChannel.getRequestHeader(header);
|
|
|
|
if (value) {
|
2012-12-18 23:43:58 +04:00
|
|
|
let printed = (header == "authorization") ? "****" : value;
|
|
|
|
this._log.debug("Header: " + header + " = " + printed);
|
2013-01-05 10:23:50 +04:00
|
|
|
newChannel.setRequestHeader(header, value, false);
|
2012-12-18 23:43:58 +04:00
|
|
|
} else {
|
|
|
|
this._log.warn("No value for header " + header);
|
2012-10-10 22:48:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("Error copying headers: " + CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
|
2012-01-17 23:51:45 +04:00
|
|
|
// We let all redirects proceed.
|
2012-11-06 05:31:12 +04:00
|
|
|
try {
|
|
|
|
callback.onRedirectVerifyCallback(Cr.NS_OK);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.error("onRedirectVerifyCallback threw!" + CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
2009-02-11 20:11:18 +03:00
|
|
|
}
|
|
|
|
};
|