2008-11-03 14:36:29 -08:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
2008-12-18 15:42:12 -08:00
|
|
|
* Anant Narayanan <anant@kix.in>
|
2010-10-29 10:20:27 -07:00
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2011-04-01 15:27:19 -07:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
2008-11-03 14:36:29 -08:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-01-18 16:23:38 -08:00
|
|
|
const EXPORTED_SYMBOLS = ["Resource", "AsyncResource",
|
|
|
|
"Auth", "BrokenBasicAuthenticator",
|
|
|
|
"BasicAuthenticator", "NoOpAuthenticator"];
|
2008-11-03 14:36:29 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2011-05-30 18:52:23 -07:00
|
|
|
Cu.import("resource://services-sync/async.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/ext/Observers.js");
|
|
|
|
Cu.import("resource://services-sync/ext/Preferences.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "Auth", function () {
|
|
|
|
return new AuthMgr();
|
|
|
|
});
|
2011-01-18 16:23:38 -08:00
|
|
|
|
|
|
|
// XXX: the authenticator api will probably need to be changed to support
|
|
|
|
// other methods (digest, oauth, etc)
|
|
|
|
|
|
|
|
function NoOpAuthenticator() {}
|
|
|
|
NoOpAuthenticator.prototype = {
|
|
|
|
onRequest: function NoOpAuth_onRequest(headers) {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Warning: This will drop the high unicode bytes from passwords.
|
|
|
|
// Use BasicAuthenticator to send non-ASCII passwords UTF8-encoded.
|
|
|
|
function BrokenBasicAuthenticator(identity) {
|
|
|
|
this._id = identity;
|
|
|
|
}
|
|
|
|
BrokenBasicAuthenticator.prototype = {
|
|
|
|
onRequest: function BasicAuth_onRequest(headers) {
|
2011-04-06 16:12:21 -07:00
|
|
|
headers['authorization'] = 'Basic ' +
|
2011-01-18 16:23:38 -08:00
|
|
|
btoa(this._id.username + ':' + this._id.password);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function BasicAuthenticator(identity) {
|
|
|
|
this._id = identity;
|
|
|
|
}
|
|
|
|
BasicAuthenticator.prototype = {
|
|
|
|
onRequest: function onRequest(headers) {
|
2011-04-06 16:12:21 -07:00
|
|
|
headers['authorization'] = 'Basic ' +
|
2011-01-18 16:23:38 -08:00
|
|
|
btoa(this._id.username + ':' + this._id.passwordUTF8);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function AuthMgr() {
|
|
|
|
this._authenticators = {};
|
|
|
|
this.defaultAuthenticator = new NoOpAuthenticator();
|
|
|
|
}
|
|
|
|
AuthMgr.prototype = {
|
|
|
|
defaultAuthenticator: null,
|
|
|
|
|
|
|
|
registerAuthenticator: function AuthMgr_register(match, authenticator) {
|
|
|
|
this._authenticators[match] = authenticator;
|
|
|
|
},
|
|
|
|
|
|
|
|
lookupAuthenticator: function AuthMgr_lookup(uri) {
|
|
|
|
for (let match in this._authenticators) {
|
|
|
|
if (uri.match(match))
|
|
|
|
return this._authenticators[match];
|
|
|
|
}
|
|
|
|
return this.defaultAuthenticator;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
/*
|
|
|
|
* AsyncResource represents a remote network resource, identified by a URI.
|
|
|
|
* Create an instance like so:
|
|
|
|
*
|
|
|
|
* let resource = new AsyncResource("http://foobar.com/path/to/resource");
|
|
|
|
*
|
|
|
|
* The 'resource' object has the following methods to issue HTTP requests
|
|
|
|
* of the corresponding HTTP methods:
|
|
|
|
*
|
|
|
|
* get(callback)
|
|
|
|
* put(data, callback)
|
|
|
|
* post(data, callback)
|
|
|
|
* delete(callback)
|
|
|
|
*
|
|
|
|
* 'callback' is a function with the following signature:
|
|
|
|
*
|
|
|
|
* function callback(error, result) {...}
|
|
|
|
*
|
|
|
|
* 'error' will be null on successful requests. Likewise, result will not be
|
2011-04-01 15:27:19 -07:00
|
|
|
* passed (=undefined) when an error occurs. Note that this is independent of
|
2010-10-29 10:20:27 -07:00
|
|
|
* the status of the HTTP response.
|
|
|
|
*/
|
|
|
|
function AsyncResource(uri) {
|
2010-02-11 15:25:31 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger(this._logName);
|
|
|
|
this._log.level =
|
2011-05-19 18:09:02 -07:00
|
|
|
Log4Moz.Level[Svc.Prefs.get("log.logger.network.resources")];
|
2010-02-11 15:25:31 -08:00
|
|
|
this.uri = uri;
|
2010-06-01 15:15:53 -07:00
|
|
|
this._headers = {};
|
2010-10-29 10:20:27 -07:00
|
|
|
this._onComplete = Utils.bind2(this, this._onComplete);
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
2010-10-29 10:20:27 -07:00
|
|
|
AsyncResource.prototype = {
|
2008-11-03 14:36:29 -08:00
|
|
|
_logName: "Net.Resource",
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.serverTime }}} **
|
|
|
|
//
|
|
|
|
// Caches the latest server timestamp (X-Weave-Timestamp header).
|
|
|
|
serverTime: null,
|
|
|
|
|
2011-04-07 13:55:02 -07:00
|
|
|
// The string to use as the base User-Agent in Sync requests.
|
2011-04-08 13:43:38 -07:00
|
|
|
// These strings will look something like
|
|
|
|
//
|
|
|
|
// Firefox/4.0 FxSync/1.8.0.20100101.mobile
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
|
|
|
|
//
|
|
|
|
_userAgent:
|
2011-05-19 18:08:07 -07:00
|
|
|
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
|
|
|
|
" FxSync/" + WEAVE_VERSION + "." + // Sync.
|
|
|
|
Services.appinfo.appBuildID + ".", // Build.
|
2011-04-07 13:55:02 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Wait 5 minutes before killing a request.
|
2011-01-31 20:55:48 -08:00
|
|
|
ABORT_TIMEOUT: 300000,
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.authenticator }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// Getter and setter for the authenticator module
|
|
|
|
// responsible for this particular resource. The authenticator
|
|
|
|
// module may modify the headers to perform authentication
|
|
|
|
// while performing a request for the resource, for example.
|
2008-11-03 14:36:29 -08:00
|
|
|
get authenticator() {
|
|
|
|
if (this._authenticator)
|
|
|
|
return this._authenticator;
|
|
|
|
else
|
|
|
|
return Auth.lookupAuthenticator(this.spec);
|
|
|
|
},
|
|
|
|
set authenticator(value) {
|
|
|
|
this._authenticator = value;
|
|
|
|
},
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.headers }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
2010-06-01 15:15:53 -07: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-03 14:36:29 -08:00
|
|
|
get headers() {
|
|
|
|
return this.authenticator.onRequest(this._headers);
|
|
|
|
},
|
|
|
|
set headers(value) {
|
|
|
|
this._headers = value;
|
|
|
|
},
|
2011-03-30 18:28:56 -07:00
|
|
|
setHeader: function Res_setHeader(header, value) {
|
|
|
|
this._headers[header.toLowerCase()] = value;
|
2009-03-25 19:30:16 -07:00
|
|
|
},
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.uri }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// URI representing this resource.
|
2008-11-03 14:36:29 -08:00
|
|
|
get uri() {
|
|
|
|
return this._uri;
|
|
|
|
},
|
|
|
|
set uri(value) {
|
2008-11-08 02:00:33 -08:00
|
|
|
if (typeof value == 'string')
|
|
|
|
this._uri = Utils.makeURI(value);
|
|
|
|
else
|
|
|
|
this._uri = value;
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.spec }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// Get the string representation of the URI.
|
2008-11-03 14:36:29 -08:00
|
|
|
get spec() {
|
2008-11-08 02:00:33 -08:00
|
|
|
if (this._uri)
|
|
|
|
return this._uri.spec;
|
|
|
|
return null;
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource.data }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// Get and set the data encapulated in the resource.
|
2008-11-06 23:23:35 -08:00
|
|
|
_data: null,
|
|
|
|
get data() this._data,
|
2008-11-03 14:36:29 -08:00
|
|
|
set data(value) {
|
|
|
|
this._data = value;
|
|
|
|
},
|
|
|
|
|
2011-04-25 13:17:26 -07:00
|
|
|
// ** {{{ AsyncResource._createRequest }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// This method returns a new IO Channel for requests to be made
|
2010-10-29 10:20:27 -07:00
|
|
|
// through. It is never called directly, only {{{_doRequest}}} uses it
|
2009-06-25 16:13:52 -07:00
|
|
|
// to obtain a request channel.
|
|
|
|
//
|
2008-12-18 15:42:12 -08:00
|
|
|
_createRequest: function Res__createRequest() {
|
2011-05-19 18:08:07 -07:00
|
|
|
let channel = Services.io.newChannel(this.spec, null, null)
|
|
|
|
.QueryInterface(Ci.nsIRequest)
|
|
|
|
.QueryInterface(Ci.nsIHttpChannel);
|
2009-06-25 16:13:52 -07:00
|
|
|
|
2009-02-27 18:28:26 -08:00
|
|
|
// Always validate the cache:
|
2009-08-26 15:32:46 -07:00
|
|
|
channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
|
|
|
|
channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// Setup a callback to handle bad HTTPS certificates.
|
2010-06-01 15:15:53 -07:00
|
|
|
channel.notificationCallbacks = new BadCertListener();
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2011-04-07 13:55:02 -07: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);
|
|
|
|
}
|
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Avoid calling the authorizer more than once.
|
2009-11-20 14:34:20 -08:00
|
|
|
let headers = this.headers;
|
2008-11-03 14:36:29 -08:00
|
|
|
for (let key in headers) {
|
2011-04-06 16:12:21 -07:00
|
|
|
if (key == 'authorization')
|
2008-11-03 14:36:29 -08:00
|
|
|
this._log.trace("HTTP Header " + key + ": ***** (suppressed)");
|
|
|
|
else
|
|
|
|
this._log.trace("HTTP Header " + key + ": " + headers[key]);
|
2009-08-26 15:32:46 -07:00
|
|
|
channel.setRequestHeader(key, headers[key], false);
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
2009-08-26 15:32:46 -07:00
|
|
|
return channel;
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2009-07-22 16:38:34 -07:00
|
|
|
_onProgress: function Res__onProgress(channel) {},
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
_doRequest: function _doRequest(action, data, callback) {
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("In _doRequest.");
|
2010-10-29 10:20:27 -07:00
|
|
|
this._callback = callback;
|
|
|
|
let channel = this._channel = this._createRequest();
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2008-11-19 16:19:51 -08:00
|
|
|
if ("undefined" != typeof(data))
|
|
|
|
this._data = data;
|
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// PUT and POST are treated differently because they have payload data.
|
2008-11-19 16:19:51 -08:00
|
|
|
if ("PUT" == action || "POST" == action) {
|
2009-09-10 11:05:13 -07:00
|
|
|
// Convert non-string bodies into JSON
|
|
|
|
if (this._data.constructor.toString() != String)
|
|
|
|
this._data = JSON.stringify(this._data);
|
|
|
|
|
2009-07-14 13:59:46 -07:00
|
|
|
this._log.debug(action + " Length: " + this._data.length);
|
2009-07-09 17:44:41 -07:00
|
|
|
this._log.trace(action + " Body: " + this._data);
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2010-06-01 15:15:53 -07:00
|
|
|
let type = ('content-type' in this._headers) ?
|
|
|
|
this._headers['content-type'] : 'text/plain';
|
2009-03-25 19:30:16 -07:00
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
let stream = Cc["@mozilla.org/io/string-input-stream;1"].
|
2008-12-18 15:42:12 -08:00
|
|
|
createInstance(Ci.nsIStringInputStream);
|
2009-01-27 13:35:10 -08:00
|
|
|
stream.setData(this._data, this._data.length);
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
channel.QueryInterface(Ci.nsIUploadChannel);
|
2009-03-25 19:30:16 -07:00
|
|
|
channel.setUploadStream(stream, type, this._data.length);
|
2008-12-18 15:42:12 -08:00
|
|
|
}
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// Setup a channel listener so that the actual network operation
|
|
|
|
// is performed asynchronously.
|
2010-10-29 10:20:27 -07:00
|
|
|
let listener = new ChannelListener(this._onComplete, this._onProgress,
|
2011-01-31 20:55:48 -08:00
|
|
|
this._log, this.ABORT_TIMEOUT);
|
2008-12-18 15:42:12 -08:00
|
|
|
channel.requestMethod = action;
|
2010-10-29 10:20:27 -07:00
|
|
|
channel.asyncOpen(listener, null);
|
|
|
|
},
|
2009-08-12 20:28:46 -07:00
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
_onComplete: function _onComplete(error, data) {
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("In _onComplete. Error is " + error + ".");
|
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
if (error) {
|
|
|
|
this._callback(error);
|
|
|
|
return;
|
2009-08-12 20:28:46 -07:00
|
|
|
}
|
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
this._data = data;
|
|
|
|
let channel = this._channel;
|
|
|
|
let action = channel.requestMethod;
|
2008-12-18 15:42:12 -08:00
|
|
|
|
2011-04-01 15:27:19 -07: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-26 15:32:46 -07:00
|
|
|
let status = 0;
|
2010-12-20 12:47:51 -08:00
|
|
|
let success = false;
|
2011-04-01 15:27:19 -07:00
|
|
|
|
2009-08-26 15:32:46 -07:00
|
|
|
try {
|
2011-04-01 15:27:19 -07:00
|
|
|
status = channel.responseStatus;
|
|
|
|
success = channel.requestSucceeded; // HTTP status.
|
|
|
|
|
|
|
|
this._log.trace("Status: " + status);
|
|
|
|
this._log.trace("Success: " + success);
|
2009-08-26 15:32:46 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Log the status of the request.
|
2009-12-17 18:51:55 -08:00
|
|
|
let mesg = [action, success ? "success" : "fail", status,
|
2010-10-29 10:20:27 -07:00
|
|
|
channel.URI.spec].join(" ");
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.debug("mesg: " + mesg);
|
|
|
|
|
2009-12-17 18:51:55 -08:00
|
|
|
if (mesg.length > 200)
|
|
|
|
mesg = mesg.substr(0, 200) + "…";
|
|
|
|
this._log.debug(mesg);
|
2011-04-01 15:27:19 -07:00
|
|
|
|
|
|
|
// Additionally give the full response body when Trace logging.
|
2009-12-17 18:51:55 -08:00
|
|
|
if (this._log.level <= Log4Moz.Level.Trace)
|
2010-10-29 10:20:27 -07:00
|
|
|
this._log.trace(action + " body: " + data);
|
2009-09-15 18:38:52 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
} catch(ex) {
|
|
|
|
// Got a response, but an exception occurred during processing.
|
|
|
|
// This shouldn't occur.
|
|
|
|
this._log.warn("Caught unexpected exception " + Utils.exceptionStr(ex) +
|
|
|
|
" in _onComplete.");
|
|
|
|
this._log.debug(Utils.stackTrace(ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:39:21 -07:00
|
|
|
// This is a server-side safety valve to allow slowing down
|
|
|
|
// clients without hurting performance.
|
2010-06-01 15:15:53 -07:00
|
|
|
if (headers["x-weave-backoff"])
|
2010-09-11 09:39:21 -07:00
|
|
|
Observers.notify("weave:service:backoff:interval",
|
|
|
|
parseInt(headers["x-weave-backoff"], 10));
|
|
|
|
|
|
|
|
if (success && headers["x-weave-quota-remaining"])
|
|
|
|
Observers.notify("weave:service:quota:remaining",
|
|
|
|
parseInt(headers["x-weave-quota-remaining"], 10));
|
2011-04-01 15:27:19 -07:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Caught exception " + Utils.exceptionStr(ex) +
|
|
|
|
" visiting headers in _onComplete.");
|
|
|
|
this._log.debug(Utils.stackTrace(ex));
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
let ret = new String(data);
|
|
|
|
ret.status = status;
|
2009-08-26 15:32:46 -07:00
|
|
|
ret.success = success;
|
2011-04-01 15:27:19 -07:00
|
|
|
ret.headers = headers;
|
2009-09-10 11:04:36 -07:00
|
|
|
|
2011-04-01 15:27:19 -07: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-05-19 18:08:35 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(ret, "obj", function() JSON.parse(ret));
|
2009-09-10 11:04:36 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Notify if we get a 401 to maybe try again with a new URI.
|
|
|
|
// TODO: more retry logic.
|
2010-06-03 18:37:07 -07:00
|
|
|
if (status == 401) {
|
2011-04-01 15:27:19 -07:00
|
|
|
// Create an object to allow observers to decide if we should try again.
|
2010-06-03 18:37:07 -07:00
|
|
|
let subject = {
|
|
|
|
newUri: "",
|
|
|
|
resource: this,
|
|
|
|
response: ret
|
|
|
|
}
|
|
|
|
Observers.notify("weave:resource:status:401", subject);
|
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Do the same type of request but with the new URI.
|
2010-06-03 18:37:07 -07:00
|
|
|
if (subject.newUri != "") {
|
|
|
|
this.uri = subject.newUri;
|
2010-10-29 10:20:27 -07:00
|
|
|
this._doRequest(action, this._data, this._callback);
|
|
|
|
return;
|
2010-06-03 18:37:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 10:20:27 -07: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);
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: function delete(callback) {
|
|
|
|
this._doRequest("DELETE", undefined, callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represent a remote network resource, identified by a URI, with a
|
|
|
|
* synchronous API.
|
|
|
|
*
|
|
|
|
* 'Resource' is not recommended for new code. Use the asynchronous API of
|
|
|
|
* 'AsyncResource' instead.
|
|
|
|
*/
|
|
|
|
function Resource(uri) {
|
|
|
|
AsyncResource.call(this, uri);
|
|
|
|
}
|
|
|
|
Resource.prototype = {
|
|
|
|
|
|
|
|
__proto__: AsyncResource.prototype,
|
|
|
|
|
|
|
|
// ** {{{ 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-30 18:52:23 -07:00
|
|
|
let cb = Async.makeSyncCallback();
|
2010-10-29 10:20:27 -07:00
|
|
|
function callback(error, ret) {
|
|
|
|
if (error)
|
|
|
|
cb.throw(error);
|
|
|
|
cb(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The channel listener might get a failure code
|
|
|
|
try {
|
2011-02-25 14:43:18 -08:00
|
|
|
this._doRequest(action, data, callback);
|
2011-05-30 18:52:23 -07:00
|
|
|
return Async.waitForSyncCallback(cb);
|
2010-10-29 10:20:27 -07:00
|
|
|
} catch(ex) {
|
2011-01-31 20:55:48 -08:00
|
|
|
// Combine the channel stack with this request stack. Need to create
|
|
|
|
// a new error object for that.
|
2010-10-29 10:20:27 -07:00
|
|
|
let error = Error(ex.message);
|
2011-01-31 20:55:48 -08:00
|
|
|
error.result = ex.result;
|
2010-10-29 10:20:27 -07: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-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// ** {{{ Resource.get }}} **
|
|
|
|
//
|
|
|
|
// Perform an asynchronous HTTP GET for this resource.
|
2009-06-04 10:16:07 -07:00
|
|
|
get: function Res_get() {
|
2011-04-22 12:13:17 -07:00
|
|
|
return this._request("GET");
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2010-10-29 10:20:27 -07:00
|
|
|
// ** {{{ Resource.put }}} **
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// Perform a HTTP PUT for this resource.
|
2009-06-04 10:16:07 -07:00
|
|
|
put: function Res_put(data) {
|
|
|
|
return this._request("PUT", data);
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// ** {{{ Resource.post }}} **
|
|
|
|
//
|
|
|
|
// Perform a HTTP POST for this resource.
|
2009-06-04 10:16:07 -07:00
|
|
|
post: function Res_post(data) {
|
|
|
|
return this._request("POST", data);
|
2008-11-19 16:19:51 -08:00
|
|
|
},
|
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// ** {{{ Resource.delete }}} **
|
|
|
|
//
|
|
|
|
// Perform a HTTP DELETE for this resource.
|
2009-06-04 10:16:07 -07:00
|
|
|
delete: function Res_delete() {
|
|
|
|
return this._request("DELETE");
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-25 16:13:52 -07:00
|
|
|
// = ChannelListener =
|
2009-11-20 14:34:20 -08:00
|
|
|
//
|
2009-06-25 16:13:52 -07:00
|
|
|
// This object implements the {{{nsIStreamListener}}} interface
|
|
|
|
// and is called as the network operation proceeds.
|
2011-01-31 20:55:48 -08:00
|
|
|
function ChannelListener(onComplete, onProgress, logger, timeout) {
|
2008-12-18 15:42:12 -08:00
|
|
|
this._onComplete = onComplete;
|
|
|
|
this._onProgress = onProgress;
|
|
|
|
this._log = logger;
|
2011-01-31 20:55:48 -08:00
|
|
|
this._timeout = timeout;
|
2010-04-21 11:10:21 -07:00
|
|
|
this.delayAbort();
|
2008-12-18 15:42:12 -08:00
|
|
|
}
|
|
|
|
ChannelListener.prototype = {
|
2010-04-01 15:25:47 -07:00
|
|
|
|
2008-12-18 15:42:12 -08:00
|
|
|
onStartRequest: function Channel_onStartRequest(channel) {
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("onStartRequest called for channel " + channel + ".");
|
2009-03-10 04:30:30 -07:00
|
|
|
channel.QueryInterface(Ci.nsIHttpChannel);
|
2010-04-01 15:54:53 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
// Save the latest server timestamp when possible.
|
2010-04-01 15:54:53 -07:00
|
|
|
try {
|
2011-04-25 13:17:26 -07:00
|
|
|
AsyncResource.serverTime = channel.getResponseHeader("X-Weave-Timestamp") - 0;
|
2010-04-01 15:54:53 -07:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("onStartRequest: " + channel.requestMethod + " " +
|
|
|
|
channel.URI.spec);
|
2008-12-18 15:42:12 -08:00
|
|
|
this._data = '';
|
2010-04-21 11:10:21 -07:00
|
|
|
this.delayAbort();
|
2008-12-18 15:42:12 -08:00
|
|
|
},
|
|
|
|
|
2009-08-12 20:28:46 -07:00
|
|
|
onStopRequest: function Channel_onStopRequest(channel, context, status) {
|
2011-04-01 15:27:19 -07:00
|
|
|
// Clear the abort timer now that the channel is done.
|
2010-04-01 15:25:47 -07:00
|
|
|
this.abortTimer.clear();
|
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
let success = Components.isSuccessCode(status);
|
2011-05-30 14:21:03 -07:00
|
|
|
let uri = channel && channel.URI && channel.URI.spec || "<unknown>";
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("Channel for " + channel.requestMethod + " " +
|
2011-05-30 14:21:03 -07:00
|
|
|
uri + ": isSuccessCode(" + status + ")? " +
|
2011-04-01 15:27:19 -07:00
|
|
|
success);
|
|
|
|
|
2008-12-18 15:42:12 -08:00
|
|
|
if (this._data == '')
|
|
|
|
this._data = null;
|
|
|
|
|
2011-01-31 20:55:48 -08:00
|
|
|
// Throw the failure code and stop execution. Use Components.Exception()
|
|
|
|
// instead of Error() so the exception is QI-able and can be passed across
|
|
|
|
// XPCOM borders while preserving the status code.
|
2011-04-01 15:27:19 -07:00
|
|
|
if (!success) {
|
2011-01-31 20:55:48 -08:00
|
|
|
let message = Components.Exception("", status).name;
|
|
|
|
let error = Components.Exception(message, status);
|
|
|
|
this._onComplete(error);
|
2010-10-29 10:20:27 -07:00
|
|
|
return;
|
|
|
|
}
|
2009-08-12 20:28:46 -07:00
|
|
|
|
2011-04-01 15:27:19 -07:00
|
|
|
this._log.trace("Channel: flags = " + channel.loadFlags +
|
2011-05-30 14:21:03 -07:00
|
|
|
", URI = " + uri +
|
2011-04-01 15:27:19 -07:00
|
|
|
", HTTP success? " + channel.requestSucceeded);
|
2010-10-29 10:20:27 -07:00
|
|
|
this._onComplete(null, this._data);
|
2008-12-18 15:42:12 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function Channel_onDataAvail(req, cb, stream, off, count) {
|
|
|
|
let siStream = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
siStream.init(stream);
|
2011-01-04 16:27:00 -08:00
|
|
|
try {
|
|
|
|
this._data += siStream.read(count);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Exception thrown reading " + count +
|
|
|
|
" bytes from " + siStream + ".");
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._onProgress();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception calling onProgress handler during fetch of "
|
|
|
|
+ req.URI.spec);
|
2011-01-07 14:30:16 -08:00
|
|
|
this._log.debug(Utils.exceptionStr(ex));
|
2011-01-04 16:27:00 -08:00
|
|
|
this._log.trace("Rethrowing; expect a failure code from the HTTP channel.");
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
2010-04-21 11:10:21 -07:00
|
|
|
this.delayAbort();
|
|
|
|
},
|
2010-04-01 15:25:47 -07:00
|
|
|
|
2010-04-21 11:10:21 -07:00
|
|
|
/**
|
|
|
|
* Create or push back the abort timer that kills this request
|
|
|
|
*/
|
|
|
|
delayAbort: function delayAbort() {
|
2011-06-06 12:27:36 -07:00
|
|
|
Utils.namedTimer(this.abortRequest, this._timeout, this, "abortTimer");
|
2010-04-01 15:25:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
abortRequest: function abortRequest() {
|
|
|
|
// Ignore any callbacks if we happen to get any now
|
|
|
|
this.onStopRequest = function() {};
|
2011-01-31 20:55:48 -08:00
|
|
|
let error = Components.Exception("Aborting due to channel inactivity.",
|
|
|
|
Cr.NS_ERROR_NET_TIMEOUT);
|
|
|
|
this._onComplete(error);
|
2009-01-07 21:29:55 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-01 15:15:53 -07:00
|
|
|
// = BadCertListener =
|
2009-06-25 16:13:52 -07:00
|
|
|
//
|
|
|
|
// We use this listener to ignore bad HTTPS
|
|
|
|
// certificates and continue a request on a network
|
|
|
|
// channel. Probably not a very smart thing to do,
|
|
|
|
// but greatly simplifies debugging and is just very
|
|
|
|
// convenient.
|
2010-06-01 15:15:53 -07:00
|
|
|
function BadCertListener() {
|
2009-02-11 09:11:18 -08:00
|
|
|
}
|
2010-06-01 15:15:53 -07:00
|
|
|
BadCertListener.prototype = {
|
2009-02-11 09:11:18 -08:00
|
|
|
getInterface: function(aIID) {
|
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
2009-02-20 01:50:26 -08:00
|
|
|
|
2009-02-11 09:11:18 -08:00
|
|
|
QueryInterface: function(aIID) {
|
|
|
|
if (aIID.equals(Components.interfaces.nsIBadCertListener2) ||
|
|
|
|
aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
|
|
|
|
aIID.equals(Components.interfaces.nsISupports))
|
|
|
|
return this;
|
2009-02-20 01:50:26 -08:00
|
|
|
|
2009-02-11 09:11:18 -08:00
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
2009-02-20 01:50:26 -08:00
|
|
|
|
2009-02-11 09:11:18 -08:00
|
|
|
notifyCertProblem: function certProblem(socketInfo, sslStatus, targetHost) {
|
|
|
|
// Silently ignore?
|
|
|
|
let log = Log4Moz.repository.getLogger("Service.CertListener");
|
|
|
|
log.level =
|
2011-05-19 18:09:02 -07:00
|
|
|
Log4Moz.Level[Svc.Prefs.get("log.logger.network.resources")];
|
2009-02-11 09:11:18 -08:00
|
|
|
log.debug("Invalid HTTPS certificate encountered, ignoring!");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|