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>
|
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 ***** */
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ['Resource', 'JsonFilter'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-01-22 11:48:36 -08:00
|
|
|
Cu.import("resource://weave/ext/Observers.js");
|
|
|
|
Cu.import("resource://weave/ext/Preferences.js");
|
2008-11-03 14:36:29 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Cu.import("resource://weave/auth.js");
|
|
|
|
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
|
|
|
function RequestException(resource, action, request) {
|
|
|
|
this._resource = resource;
|
|
|
|
this._action = action;
|
|
|
|
this._request = request;
|
|
|
|
this.location = Components.stack.caller;
|
|
|
|
}
|
|
|
|
RequestException.prototype = {
|
|
|
|
get resource() { return this._resource; },
|
|
|
|
get action() { return this._action; },
|
|
|
|
get request() { return this._request; },
|
|
|
|
get status() { return this._request.status; },
|
|
|
|
toString: function ReqEx_toString() {
|
2008-11-19 16:19:51 -08:00
|
|
|
return "Could not " + this._action + " resource " + this._resource.spec +
|
2008-12-18 15:42:12 -08:00
|
|
|
" (" + this._request.responseStatus + ")";
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function Resource(uri) {
|
|
|
|
this._init(uri);
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
Resource.prototype = {
|
|
|
|
_logName: "Net.Resource",
|
|
|
|
|
|
|
|
get authenticator() {
|
|
|
|
if (this._authenticator)
|
|
|
|
return this._authenticator;
|
|
|
|
else
|
|
|
|
return Auth.lookupAuthenticator(this.spec);
|
|
|
|
},
|
|
|
|
set authenticator(value) {
|
|
|
|
this._authenticator = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get headers() {
|
|
|
|
return this.authenticator.onRequest(this._headers);
|
|
|
|
},
|
|
|
|
set headers(value) {
|
|
|
|
this._headers = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get uri() {
|
|
|
|
return this._uri;
|
|
|
|
},
|
|
|
|
set uri(value) {
|
|
|
|
this._dirty = true;
|
|
|
|
this._downloaded = false;
|
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
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
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._dirty = true;
|
|
|
|
this._data = value;
|
|
|
|
},
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
_lastChannel: null,
|
2008-11-06 23:23:35 -08:00
|
|
|
_downloaded: false,
|
|
|
|
_dirty: false,
|
2009-01-27 13:35:10 -08:00
|
|
|
get lastChannel() this._lastChannel,
|
2008-11-06 23:23:35 -08:00
|
|
|
get downloaded() this._downloaded,
|
|
|
|
get dirty() this._dirty,
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2008-11-06 23:23:35 -08:00
|
|
|
_filters: null,
|
2008-11-03 14:36:29 -08:00
|
|
|
pushFilter: function Res_pushFilter(filter) {
|
|
|
|
this._filters.push(filter);
|
|
|
|
},
|
|
|
|
popFilter: function Res_popFilter() {
|
|
|
|
return this._filters.pop();
|
|
|
|
},
|
|
|
|
clearFilters: function Res_clearFilters() {
|
|
|
|
this._filters = [];
|
|
|
|
},
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
_init: function Res__init(uri) {
|
2008-11-06 23:23:35 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger(this._logName);
|
2008-11-19 16:19:51 -08:00
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.network.resources")];
|
2008-11-08 02:00:33 -08:00
|
|
|
this.uri = uri;
|
2008-11-03 14:36:29 -08:00
|
|
|
this._headers = {'Content-type': 'text/plain'};
|
2008-11-08 02:00:33 -08:00
|
|
|
this._filters = [];
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
2008-12-18 15:42:12 -08:00
|
|
|
_createRequest: function Res__createRequest() {
|
2009-03-10 04:30:30 -07:00
|
|
|
this._lastChannel = Svc.IO.newChannel(this.spec, null, null).
|
2009-02-27 18:28:26 -08:00
|
|
|
QueryInterface(Ci.nsIRequest);
|
|
|
|
// Always validate the cache:
|
|
|
|
let loadFlags = this._lastChannel.loadFlags;
|
2009-03-12 00:35:54 -07:00
|
|
|
loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
|
|
|
|
loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
|
2009-02-27 18:28:26 -08:00
|
|
|
this._lastChannel.loadFlags = loadFlags;
|
|
|
|
this._lastChannel = this._lastChannel.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
|
2009-02-11 09:11:18 -08:00
|
|
|
this._lastChannel.notificationCallbacks = new badCertListener();
|
2009-02-20 01:50:26 -08:00
|
|
|
|
2008-11-03 14:36:29 -08:00
|
|
|
let headers = this.headers; // avoid calling the authorizer more than once
|
|
|
|
for (let key in headers) {
|
|
|
|
if (key == 'Authorization')
|
|
|
|
this._log.trace("HTTP Header " + key + ": ***** (suppressed)");
|
|
|
|
else
|
|
|
|
this._log.trace("HTTP Header " + key + ": " + headers[key]);
|
2009-01-27 13:35:10 -08:00
|
|
|
this._lastChannel.setRequestHeader(key, headers[key], true);
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
2009-01-27 13:35:10 -08:00
|
|
|
return this._lastChannel;
|
2008-11-03 14:36:29 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_onProgress: function Res__onProgress(event) {
|
|
|
|
this._lastProgress = Date.now();
|
|
|
|
},
|
|
|
|
|
2008-11-19 16:19:51 -08:00
|
|
|
filterUpload: function Res_filterUpload(onComplete) {
|
|
|
|
let fn = function() {
|
|
|
|
let self = yield;
|
|
|
|
for each (let filter in this._filters) {
|
|
|
|
this._data = yield filter.beforePUT.async(filter, self.cb, this._data, this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fn.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
filterDownload: function Res_filterUpload(onComplete) {
|
|
|
|
let fn = function() {
|
|
|
|
let self = yield;
|
|
|
|
let filters = this._filters.slice(); // reverse() mutates, so we copy
|
|
|
|
for each (let filter in filters.reverse()) {
|
|
|
|
this._data = yield filter.afterGET.async(filter, self.cb, this._data, this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fn.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2008-11-03 14:36:29 -08:00
|
|
|
_request: function Res__request(action, data) {
|
|
|
|
let self = yield;
|
|
|
|
let iter = 0;
|
2008-12-18 15:42:12 -08:00
|
|
|
let 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;
|
|
|
|
|
|
|
|
if ("PUT" == action || "POST" == action) {
|
|
|
|
yield this.filterUpload(self.cb);
|
|
|
|
this._log.trace(action + " Body:\n" + this._data);
|
2009-01-22 11:48:36 -08: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);
|
|
|
|
channel.setUploadStream(stream, 'text/plain', this._data.length);
|
2008-12-18 15:42:12 -08:00
|
|
|
}
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2008-12-19 15:24:37 -08:00
|
|
|
let listener = new ChannelListener(self.cb, this._onProgress, this._log);
|
2008-12-18 15:42:12 -08:00
|
|
|
channel.requestMethod = action;
|
|
|
|
this._data = yield channel.asyncOpen(listener, null);
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
if (!channel.requestSucceeded) {
|
2008-12-19 15:24:37 -08:00
|
|
|
this._log.debug(action + " request failed (" + channel.responseStatus + ")");
|
2008-12-18 15:42:12 -08:00
|
|
|
if (this._data)
|
|
|
|
this._log.debug("Error response: \n" + this._data);
|
|
|
|
throw new RequestException(this, action, channel);
|
2009-01-27 13:35:10 -08:00
|
|
|
|
2008-12-18 15:42:12 -08:00
|
|
|
} else {
|
2009-01-27 13:35:10 -08:00
|
|
|
this._log.debug(action + " request successful (" + channel.responseStatus + ")");
|
2008-12-18 15:42:12 -08:00
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case "DELETE":
|
|
|
|
if (Utils.checkStatus(channel.responseStatus, null, [[200,300],404])){
|
|
|
|
this._dirty = false;
|
|
|
|
this._data = null;
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-12-18 15:42:12 -08:00
|
|
|
case "GET":
|
|
|
|
case "POST":
|
|
|
|
this._log.trace(action + " Body:\n" + this._data);
|
|
|
|
yield this.filterDownload(self.cb);
|
|
|
|
break;
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
}
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2008-11-03 14:36:29 -08:00
|
|
|
self.done(this._data);
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function Res_get(onComplete) {
|
|
|
|
this._request.async(this, onComplete, "GET");
|
|
|
|
},
|
|
|
|
|
|
|
|
put: function Res_put(onComplete, data) {
|
|
|
|
this._request.async(this, onComplete, "PUT", data);
|
|
|
|
},
|
|
|
|
|
2008-11-19 16:19:51 -08:00
|
|
|
post: function Res_post(onComplete, data) {
|
|
|
|
this._request.async(this, onComplete, "POST", data);
|
|
|
|
},
|
|
|
|
|
2008-11-03 14:36:29 -08:00
|
|
|
delete: function Res_delete(onComplete) {
|
|
|
|
this._request.async(this, onComplete, "DELETE");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-18 15:42:12 -08:00
|
|
|
function ChannelListener(onComplete, onProgress, logger) {
|
|
|
|
this._onComplete = onComplete;
|
|
|
|
this._onProgress = onProgress;
|
|
|
|
this._log = logger;
|
|
|
|
}
|
|
|
|
ChannelListener.prototype = {
|
|
|
|
onStartRequest: function Channel_onStartRequest(channel) {
|
2009-03-10 04:30:30 -07:00
|
|
|
// XXX Bug 482179 Some reason xpconnect makes |channel| only nsIRequest
|
|
|
|
channel.QueryInterface(Ci.nsIHttpChannel);
|
2008-12-18 15:42:12 -08:00
|
|
|
this._log.debug(channel.requestMethod + " request for " +
|
|
|
|
channel.URI.spec);
|
|
|
|
this._data = '';
|
|
|
|
},
|
|
|
|
|
|
|
|
onStopRequest: function Channel_onStopRequest(channel, ctx, time) {
|
|
|
|
if (this._data == '')
|
|
|
|
this._data = null;
|
|
|
|
|
|
|
|
this._onComplete(this._data);
|
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function Channel_onDataAvail(req, cb, stream, off, count) {
|
|
|
|
this._onProgress();
|
|
|
|
|
|
|
|
let siStream = Cc["@mozilla.org/scriptableinputstream;1"].
|
|
|
|
createInstance(Ci.nsIScriptableInputStream);
|
|
|
|
siStream.init(stream);
|
|
|
|
|
|
|
|
this._data += siStream.read(count);
|
|
|
|
}
|
|
|
|
};
|
2008-11-03 14:36:29 -08:00
|
|
|
|
2009-01-07 21:29:55 -08:00
|
|
|
/* Parses out single WBOs from a full dump */
|
|
|
|
function RecordParser(data) {
|
|
|
|
this._data = data;
|
|
|
|
}
|
|
|
|
RecordParser.prototype = {
|
2009-01-07 21:39:39 -08:00
|
|
|
getNextRecord: function RecordParser_getNextRecord() {
|
2009-01-07 21:29:55 -08:00
|
|
|
let start;
|
|
|
|
let bCount = 0;
|
|
|
|
let done = false;
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2009-01-07 21:29:55 -08:00
|
|
|
for (let i = 1; i < this._data.length; i++) {
|
2009-01-07 21:39:39 -08:00
|
|
|
if (this._data[i] == '{') {
|
2009-01-07 21:29:55 -08:00
|
|
|
if (bCount == 0)
|
|
|
|
start = i;
|
|
|
|
bCount++;
|
2009-01-07 21:39:39 -08:00
|
|
|
} else if (this._data[i] == '}') {
|
2009-01-07 21:29:55 -08:00
|
|
|
bCount--;
|
|
|
|
if (bCount == 0)
|
|
|
|
done = true;
|
|
|
|
}
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2009-01-07 21:39:39 -08:00
|
|
|
if (done) {
|
|
|
|
let ret = this._data.substring(start, i + 1);
|
|
|
|
this._data = this._data.substring(i + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-07 21:29:55 -08:00
|
|
|
}
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2009-01-07 21:29:55 -08:00
|
|
|
return false;
|
|
|
|
},
|
2009-01-22 11:48:36 -08:00
|
|
|
|
2009-01-07 21:39:39 -08:00
|
|
|
append: function RecordParser_append(data) {
|
|
|
|
this._data += data;
|
2009-01-07 21:29:55 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-03 14:36:29 -08:00
|
|
|
function JsonFilter() {
|
2009-01-06 13:54:18 -08:00
|
|
|
let level = "Debug";
|
|
|
|
try { level = Utils.prefs.getCharPref("log.logger.network.jsonFilter"); }
|
|
|
|
catch (e) { /* ignore unset prefs */ }
|
2008-11-06 23:23:35 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Net.JsonFilter");
|
2009-01-06 13:54:18 -08:00
|
|
|
this._log.level = Log4Moz.Level[level];
|
2008-11-03 14:36:29 -08:00
|
|
|
}
|
|
|
|
JsonFilter.prototype = {
|
|
|
|
get _json() {
|
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
this.__defineGetter__("_json", function() json);
|
|
|
|
return this._json;
|
|
|
|
},
|
|
|
|
|
|
|
|
beforePUT: function JsonFilter_beforePUT(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.trace("Encoding data as JSON");
|
|
|
|
self.done(this._json.encode(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
afterGET: function JsonFilter_afterGET(data) {
|
|
|
|
let self = yield;
|
|
|
|
this._log.trace("Decoding JSON data");
|
|
|
|
self.done(this._json.decode(data));
|
|
|
|
}
|
|
|
|
};
|
2009-02-11 09:11:18 -08:00
|
|
|
|
|
|
|
function badCertListener() {
|
|
|
|
}
|
|
|
|
badCertListener.prototype = {
|
|
|
|
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 =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.network.resources")];
|
|
|
|
log.debug("Invalid HTTPS certificate encountered, ignoring!");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|