2011-10-28 14:43:18 -07:00
|
|
|
const Cm = Components.manager;
|
|
|
|
|
2012-01-25 13:32:05 -08:00
|
|
|
const TEST_CLUSTER_URL = "http://localhost:8080/";
|
|
|
|
const TEST_SERVER_URL = "http://localhost:8080/";
|
|
|
|
|
2011-09-29 11:50:32 -07:00
|
|
|
// Shared logging for all HTTP server functions.
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2011-09-29 11:50:32 -07:00
|
|
|
const SYNC_HTTP_LOGGER = "Sync.Test.Server";
|
2011-09-29 11:51:27 -07:00
|
|
|
const SYNC_API_VERSION = "1.1";
|
2011-09-29 11:50:32 -07:00
|
|
|
|
2011-04-18 13:25:08 -07:00
|
|
|
// Use the same method that record.js does, which mirrors the server.
|
|
|
|
// The server returns timestamps with 1/100 sec granularity. Note that this is
|
|
|
|
// subject to change: see Bug 650435.
|
|
|
|
function new_timestamp() {
|
|
|
|
return Math.round(Date.now() / 10) / 100;
|
|
|
|
}
|
2011-08-26 15:31:19 -07:00
|
|
|
|
2011-10-27 22:25:00 -07:00
|
|
|
function return_timestamp(request, response, timestamp) {
|
|
|
|
if (!timestamp) {
|
|
|
|
timestamp = new_timestamp();
|
|
|
|
}
|
|
|
|
let body = "" + timestamp;
|
|
|
|
response.setHeader("X-Weave-Timestamp", body);
|
|
|
|
response.setStatusLine(request.httpVersion, 200, "OK");
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2011-07-06 17:20:36 -07:00
|
|
|
function basic_auth_header(user, password) {
|
|
|
|
return "Basic " + btoa(user + ":" + Utils.encodeUTF8(password));
|
|
|
|
}
|
|
|
|
|
|
|
|
function basic_auth_matches(req, user, password) {
|
2012-03-22 15:49:50 -07:00
|
|
|
if (!req.hasHeader("Authorization")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let expected = basic_auth_header(user, Utils.encodeUTF8(password));
|
|
|
|
return req.getHeader("Authorization") == expected;
|
2011-07-06 17:20:36 -07:00
|
|
|
}
|
|
|
|
|
2008-11-03 14:41:39 -08:00
|
|
|
function httpd_basic_auth_handler(body, metadata, response) {
|
2011-04-18 13:26:03 -07:00
|
|
|
if (basic_auth_matches(metadata, "guest", "guest")) {
|
2008-11-03 14:41:39 -08:00
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "OK, authorized");
|
|
|
|
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
|
|
|
|
} else {
|
|
|
|
body = "This path exists and is protected - failed";
|
|
|
|
response.setStatusLine(metadata.httpVersion, 401, "Unauthorized");
|
|
|
|
response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
|
|
|
|
}
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Represent a WBO on the server
|
|
|
|
*/
|
2011-10-01 12:36:51 -07:00
|
|
|
function ServerWBO(id, initialPayload, modified) {
|
2011-04-18 13:25:08 -07:00
|
|
|
if (!id) {
|
|
|
|
throw "No ID for ServerWBO!";
|
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
this.id = id;
|
|
|
|
if (!initialPayload) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof initialPayload == "object") {
|
|
|
|
initialPayload = JSON.stringify(initialPayload);
|
|
|
|
}
|
|
|
|
this.payload = initialPayload;
|
2011-10-01 12:36:51 -07:00
|
|
|
this.modified = modified || new_timestamp();
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
ServerWBO.prototype = {
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
return JSON.parse(this.payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
get: function() {
|
2011-04-18 13:25:08 -07:00
|
|
|
return JSON.stringify(this, ["id", "modified", "payload"]);
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
put: function(input) {
|
|
|
|
input = JSON.parse(input);
|
|
|
|
this.payload = input.payload;
|
2011-04-18 13:25:08 -07:00
|
|
|
this.modified = new_timestamp();
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
delete: function() {
|
|
|
|
delete this.payload;
|
|
|
|
delete this.modified;
|
|
|
|
},
|
|
|
|
|
2011-04-19 12:56:46 -07:00
|
|
|
// This handler sets `newModified` on the response body if the collection
|
|
|
|
// timestamp has changed. This allows wrapper handlers to extract information
|
|
|
|
// that otherwise would exist only in the body stream.
|
2010-06-01 15:07:50 -07:00
|
|
|
handler: function() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return function(request, response) {
|
|
|
|
var statusCode = 200;
|
|
|
|
var status = "OK";
|
|
|
|
var body;
|
|
|
|
|
|
|
|
switch(request.method) {
|
|
|
|
case "GET":
|
|
|
|
if (self.payload) {
|
|
|
|
body = self.get();
|
|
|
|
} else {
|
|
|
|
statusCode = 404;
|
|
|
|
status = "Not Found";
|
|
|
|
body = "Not Found";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
self.put(readBytesFromInputStream(request.bodyInputStream));
|
|
|
|
body = JSON.stringify(self.modified);
|
2011-09-29 11:50:28 -07:00
|
|
|
response.setHeader("Content-Type", "application/json");
|
2011-04-19 12:56:46 -07:00
|
|
|
response.newModified = self.modified;
|
2010-06-01 15:07:50 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
self.delete();
|
2011-04-19 12:56:46 -07:00
|
|
|
let ts = new_timestamp();
|
|
|
|
body = JSON.stringify(ts);
|
2011-09-29 11:50:28 -07:00
|
|
|
response.setHeader("Content-Type", "application/json");
|
2011-04-19 12:56:46 -07:00
|
|
|
response.newModified = ts;
|
2010-06-01 15:07:50 -07:00
|
|
|
break;
|
|
|
|
}
|
2011-04-18 13:25:08 -07:00
|
|
|
response.setHeader("X-Weave-Timestamp", "" + new_timestamp(), false);
|
2010-06-01 15:07:50 -07:00
|
|
|
response.setStatusLine(request.httpVersion, statusCode, status);
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-29 11:50:28 -07:00
|
|
|
/**
|
|
|
|
* Represent a collection on the server. The '_wbos' attribute is a
|
2010-06-01 15:07:50 -07:00
|
|
|
* mapping of id -> ServerWBO objects.
|
2011-09-29 11:50:28 -07:00
|
|
|
*
|
2010-06-01 15:07:50 -07:00
|
|
|
* Note that if you want these records to be accessible individually,
|
2011-09-29 11:50:28 -07:00
|
|
|
* you need to register their handlers with the server separately, or use a
|
|
|
|
* containing HTTP server that will do so on your behalf.
|
|
|
|
*
|
|
|
|
* @param wbos
|
|
|
|
* An object mapping WBO IDs to ServerWBOs.
|
|
|
|
* @param acceptNew
|
|
|
|
* If true, POSTs to this collection URI will result in new WBOs being
|
|
|
|
* created and wired in on the fly.
|
|
|
|
* @param timestamp
|
|
|
|
* An optional timestamp value to initialize the modified time of the
|
|
|
|
* collection. This should be in the format returned by new_timestamp().
|
|
|
|
*
|
|
|
|
* @return the new ServerCollection instance.
|
|
|
|
*
|
2010-06-01 15:07:50 -07:00
|
|
|
*/
|
2011-09-29 11:50:28 -07:00
|
|
|
function ServerCollection(wbos, acceptNew, timestamp) {
|
|
|
|
this._wbos = wbos || {};
|
2011-01-24 23:06:42 -08:00
|
|
|
this.acceptNew = acceptNew || false;
|
2011-09-29 11:50:28 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Track modified timestamp.
|
|
|
|
* We can't just use the timestamps of contained WBOs: an empty collection
|
|
|
|
* has a modified time.
|
|
|
|
*/
|
|
|
|
this.timestamp = timestamp || new_timestamp();
|
2011-09-29 11:50:32 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger(SYNC_HTTP_LOGGER);
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
ServerCollection.prototype = {
|
|
|
|
|
2011-09-29 11:50:28 -07:00
|
|
|
/**
|
|
|
|
* Convenience accessor for our WBO keys.
|
|
|
|
* Excludes deleted items, of course.
|
|
|
|
*
|
|
|
|
* @param filter
|
|
|
|
* A predicate function (applied to the ID and WBO) which dictates
|
|
|
|
* whether to include the WBO's ID in the output.
|
|
|
|
*
|
|
|
|
* @return an array of IDs.
|
|
|
|
*/
|
|
|
|
keys: function keys(filter) {
|
|
|
|
return [id for ([id, wbo] in Iterator(this._wbos))
|
|
|
|
if (wbo.payload &&
|
|
|
|
(!filter || filter(id, wbo)))];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to get an array of WBOs.
|
|
|
|
* Optionally provide a filter function.
|
|
|
|
*
|
|
|
|
* @param filter
|
|
|
|
* A predicate function, applied to the WBO, which dictates whether to
|
|
|
|
* include the WBO in the output.
|
|
|
|
*
|
|
|
|
* @return an array of ServerWBOs.
|
|
|
|
*/
|
|
|
|
wbos: function wbos(filter) {
|
|
|
|
let os = [wbo for ([id, wbo] in Iterator(this._wbos))
|
|
|
|
if (wbo.payload)];
|
|
|
|
if (filter) {
|
|
|
|
return os.filter(filter);
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to get an array of parsed ciphertexts.
|
|
|
|
*
|
|
|
|
* @return an array of the payloads of each stored WBO.
|
|
|
|
*/
|
|
|
|
payloads: function () {
|
|
|
|
return this.wbos().map(function (wbo) {
|
|
|
|
return JSON.parse(JSON.parse(wbo.payload).ciphertext);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Just for syntactic elegance.
|
|
|
|
wbo: function wbo(id) {
|
|
|
|
return this._wbos[id];
|
|
|
|
},
|
|
|
|
|
|
|
|
payload: function payload(id) {
|
|
|
|
return this.wbo(id).payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the provided WBO under its ID.
|
|
|
|
*
|
|
|
|
* @return the provided WBO.
|
|
|
|
*/
|
|
|
|
insertWBO: function insertWBO(wbo) {
|
|
|
|
return this._wbos[wbo.id] = wbo;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the provided payload as part of a new ServerWBO with the provided
|
|
|
|
* ID.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* The GUID for the WBO.
|
|
|
|
* @param payload
|
|
|
|
* The payload, as provided to the ServerWBO constructor.
|
2011-10-01 12:36:51 -07:00
|
|
|
* @param modified
|
|
|
|
* An optional modified time for the ServerWBO.
|
2011-09-29 11:50:28 -07:00
|
|
|
*
|
|
|
|
* @return the inserted WBO.
|
|
|
|
*/
|
2011-10-01 12:36:51 -07:00
|
|
|
insert: function insert(id, payload, modified) {
|
|
|
|
return this.insertWBO(new ServerWBO(id, payload, modified));
|
2011-09-29 11:50:28 -07:00
|
|
|
},
|
|
|
|
|
2012-03-08 13:50:09 -08:00
|
|
|
/**
|
|
|
|
* Removes an object entirely from the collection.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* (string) ID to remove.
|
|
|
|
*/
|
|
|
|
remove: function remove(id) {
|
|
|
|
delete this._wbos[id];
|
|
|
|
},
|
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
_inResultSet: function(wbo, options) {
|
2011-01-14 13:22:20 -08:00
|
|
|
return wbo.payload
|
|
|
|
&& (!options.ids || (options.ids.indexOf(wbo.id) != -1))
|
|
|
|
&& (!options.newer || (wbo.modified > options.newer));
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
2011-03-01 11:56:29 -08:00
|
|
|
count: function(options) {
|
|
|
|
options = options || {};
|
|
|
|
let c = 0;
|
2011-09-29 11:50:28 -07:00
|
|
|
for (let [id, wbo] in Iterator(this._wbos)) {
|
2011-03-01 11:56:29 -08:00
|
|
|
if (wbo.modified && this._inResultSet(wbo, options)) {
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
},
|
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
get: function(options) {
|
|
|
|
let result;
|
|
|
|
if (options.full) {
|
2011-09-29 11:50:28 -07:00
|
|
|
let data = [wbo.get() for ([id, wbo] in Iterator(this._wbos))
|
2011-01-24 23:06:42 -08:00
|
|
|
// Drop deleted.
|
|
|
|
if (wbo.modified &&
|
|
|
|
this._inResultSet(wbo, options))];
|
2010-06-01 15:07:50 -07:00
|
|
|
if (options.limit) {
|
|
|
|
data = data.slice(0, options.limit);
|
|
|
|
}
|
2011-09-29 11:53:28 -07:00
|
|
|
// Our implementation of application/newlines.
|
2010-06-01 15:07:50 -07:00
|
|
|
result = data.join("\n") + "\n";
|
2011-09-29 11:53:28 -07:00
|
|
|
|
|
|
|
// Use options as a backchannel to report count.
|
|
|
|
options.recordCount = data.length;
|
2010-06-01 15:07:50 -07:00
|
|
|
} else {
|
2011-09-29 11:50:28 -07:00
|
|
|
let data = [id for ([id, wbo] in Iterator(this._wbos))
|
2010-06-01 15:07:50 -07:00
|
|
|
if (this._inResultSet(wbo, options))];
|
|
|
|
if (options.limit) {
|
|
|
|
data = data.slice(0, options.limit);
|
|
|
|
}
|
|
|
|
result = JSON.stringify(data);
|
2011-09-29 11:53:28 -07:00
|
|
|
options.recordCount = data.length;
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
post: function(input) {
|
|
|
|
input = JSON.parse(input);
|
|
|
|
let success = [];
|
2010-07-31 04:28:00 -07:00
|
|
|
let failed = {};
|
2010-06-01 15:07:50 -07:00
|
|
|
|
|
|
|
// This will count records where we have an existing ServerWBO
|
|
|
|
// registered with us as successful and all other records as failed.
|
|
|
|
for each (let record in input) {
|
2011-09-29 11:50:28 -07:00
|
|
|
let wbo = this.wbo(record.id);
|
2011-01-24 23:06:42 -08:00
|
|
|
if (!wbo && this.acceptNew) {
|
2011-09-29 11:50:32 -07:00
|
|
|
this._log.debug("Creating WBO " + JSON.stringify(record.id) +
|
|
|
|
" on the fly.");
|
2011-01-24 23:06:42 -08:00
|
|
|
wbo = new ServerWBO(record.id);
|
2011-09-29 11:50:28 -07:00
|
|
|
this.insertWBO(wbo);
|
2011-01-24 23:06:42 -08:00
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
if (wbo) {
|
|
|
|
wbo.payload = record.payload;
|
2011-04-18 13:25:08 -07:00
|
|
|
wbo.modified = new_timestamp();
|
2010-06-01 15:07:50 -07:00
|
|
|
success.push(record.id);
|
|
|
|
} else {
|
2010-07-31 04:28:00 -07:00
|
|
|
failed[record.id] = "no wbo configured";
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
}
|
2011-04-18 13:25:08 -07:00
|
|
|
return {modified: new_timestamp(),
|
2010-07-31 04:28:00 -07:00
|
|
|
success: success,
|
2010-06-01 15:07:50 -07:00
|
|
|
failed: failed};
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: function(options) {
|
2011-09-29 11:50:32 -07:00
|
|
|
let deleted = [];
|
2011-09-29 11:50:28 -07:00
|
|
|
for (let [id, wbo] in Iterator(this._wbos)) {
|
2010-06-01 15:07:50 -07:00
|
|
|
if (this._inResultSet(wbo, options)) {
|
2011-09-29 11:50:32 -07:00
|
|
|
this._log.debug("Deleting " + JSON.stringify(wbo));
|
|
|
|
deleted.push(wbo.id);
|
2010-06-01 15:07:50 -07:00
|
|
|
wbo.delete();
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 11:50:32 -07:00
|
|
|
return deleted;
|
2010-06-01 15:07:50 -07:00
|
|
|
},
|
|
|
|
|
2011-04-19 12:56:46 -07:00
|
|
|
// This handler sets `newModified` on the response body if the collection
|
|
|
|
// timestamp has changed.
|
2010-06-01 15:07:50 -07:00
|
|
|
handler: function() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return function(request, response) {
|
|
|
|
var statusCode = 200;
|
|
|
|
var status = "OK";
|
|
|
|
var body;
|
|
|
|
|
|
|
|
// Parse queryString
|
|
|
|
let options = {};
|
2011-04-18 13:25:08 -07:00
|
|
|
for each (let chunk in request.queryString.split("&")) {
|
2010-06-01 15:07:50 -07:00
|
|
|
if (!chunk) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-18 13:25:08 -07:00
|
|
|
chunk = chunk.split("=");
|
2010-06-01 15:07:50 -07:00
|
|
|
if (chunk.length == 1) {
|
|
|
|
options[chunk[0]] = "";
|
|
|
|
} else {
|
|
|
|
options[chunk[0]] = chunk[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.ids) {
|
2011-04-18 13:25:08 -07:00
|
|
|
options.ids = options.ids.split(",");
|
2010-06-01 15:07:50 -07:00
|
|
|
}
|
|
|
|
if (options.newer) {
|
|
|
|
options.newer = parseFloat(options.newer);
|
|
|
|
}
|
|
|
|
if (options.limit) {
|
|
|
|
options.limit = parseInt(options.limit, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(request.method) {
|
|
|
|
case "GET":
|
|
|
|
body = self.get(options);
|
2011-09-29 11:53:28 -07:00
|
|
|
// "If supported by the db, this header will return the number of
|
|
|
|
// records total in the request body of any multiple-record GET
|
|
|
|
// request."
|
|
|
|
let records = options.recordCount;
|
|
|
|
self._log.info("Records: " + records);
|
|
|
|
if (records != null) {
|
|
|
|
response.setHeader("X-Weave-Records", "" + records);
|
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "POST":
|
|
|
|
let res = self.post(readBytesFromInputStream(request.bodyInputStream));
|
|
|
|
body = JSON.stringify(res);
|
2011-04-19 12:56:46 -07:00
|
|
|
response.newModified = res.modified;
|
2010-06-01 15:07:50 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "DELETE":
|
2011-09-29 11:50:32 -07:00
|
|
|
self._log.debug("Invoking ServerCollection.DELETE.");
|
|
|
|
let deleted = self.delete(options);
|
2011-04-19 12:56:46 -07:00
|
|
|
let ts = new_timestamp();
|
|
|
|
body = JSON.stringify(ts);
|
|
|
|
response.newModified = ts;
|
2011-09-29 11:50:32 -07:00
|
|
|
response.deleted = deleted;
|
2010-06-01 15:07:50 -07:00
|
|
|
break;
|
|
|
|
}
|
2011-04-18 13:25:08 -07:00
|
|
|
response.setHeader("X-Weave-Timestamp",
|
|
|
|
"" + new_timestamp(),
|
|
|
|
false);
|
2010-06-01 15:07:50 -07:00
|
|
|
response.setStatusLine(request.httpVersion, statusCode, status);
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
2011-09-29 11:50:28 -07:00
|
|
|
|
|
|
|
// Update the collection timestamp to the appropriate modified time.
|
|
|
|
// This is either a value set by the handler, or the current time.
|
|
|
|
if (request.method != "GET") {
|
|
|
|
this.timestamp = (response.newModified >= 0) ?
|
|
|
|
response.newModified :
|
|
|
|
new_timestamp();
|
|
|
|
}
|
2010-06-01 15:07:50 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2010-12-06 17:25:35 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test setup helpers.
|
|
|
|
*/
|
|
|
|
function sync_httpd_setup(handlers) {
|
2011-04-08 14:52:08 -07:00
|
|
|
handlers["/1.1/foo/storage/meta/global"]
|
2011-04-18 13:25:08 -07:00
|
|
|
= (new ServerWBO("global", {})).handler();
|
2010-12-06 17:25:35 -08:00
|
|
|
return httpd_setup(handlers);
|
|
|
|
}
|
2010-12-09 10:32:03 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Track collection modified times. Return closures.
|
|
|
|
*/
|
|
|
|
function track_collections_helper() {
|
2012-08-29 14:43:41 -07:00
|
|
|
|
2010-12-09 10:32:03 -08:00
|
|
|
/*
|
|
|
|
* Our tracking object.
|
|
|
|
*/
|
|
|
|
let collections = {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the timestamp of a collection.
|
|
|
|
*/
|
2011-04-19 12:56:46 -07:00
|
|
|
function update_collection(coll, ts) {
|
|
|
|
_("Updating collection " + coll + " to " + ts);
|
|
|
|
let timestamp = ts || new_timestamp();
|
2010-12-09 10:32:03 -08:00
|
|
|
collections[coll] = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invoke a handler, updating the collection's modified timestamp unless
|
|
|
|
* it's a GET request.
|
|
|
|
*/
|
|
|
|
function with_updated_collection(coll, f) {
|
|
|
|
return function(request, response) {
|
|
|
|
f.call(this, request, response);
|
2011-04-19 12:56:46 -07:00
|
|
|
|
|
|
|
// Update the collection timestamp to the appropriate modified time.
|
|
|
|
// This is either a value set by the handler, or the current time.
|
|
|
|
if (request.method != "GET") {
|
|
|
|
update_collection(coll, response.newModified)
|
|
|
|
}
|
2010-12-09 10:32:03 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the info/collections object.
|
|
|
|
*/
|
|
|
|
function info_collections(request, response) {
|
|
|
|
let body = "Error.";
|
|
|
|
switch(request.method) {
|
|
|
|
case "GET":
|
|
|
|
body = JSON.stringify(collections);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw "Non-GET on info_collections.";
|
|
|
|
}
|
2012-03-05 17:36:45 -08:00
|
|
|
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
2011-04-18 13:25:08 -07:00
|
|
|
response.setHeader("X-Weave-Timestamp",
|
|
|
|
"" + new_timestamp(),
|
|
|
|
false);
|
2010-12-09 10:32:03 -08:00
|
|
|
response.setStatusLine(request.httpVersion, 200, "OK");
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
2012-08-29 14:43:41 -07:00
|
|
|
|
2010-12-09 10:32:03 -08:00
|
|
|
return {"collections": collections,
|
|
|
|
"handler": info_collections,
|
|
|
|
"with_updated_collection": with_updated_collection,
|
|
|
|
"update_collection": update_collection};
|
|
|
|
}
|
2011-09-29 11:51:27 -07:00
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
// httpd.js-based Sync server. //
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In general, the preferred way of using SyncServer is to directly introspect
|
|
|
|
* it. Callbacks are available for operations which are hard to verify through
|
|
|
|
* introspection, such as deletions.
|
|
|
|
*
|
|
|
|
* One of the goals of this server is to provide enough hooks for test code to
|
|
|
|
* find out what it needs without monkeypatching. Use this object as your
|
|
|
|
* prototype, and override as appropriate.
|
|
|
|
*/
|
|
|
|
let SyncServerCallback = {
|
|
|
|
onCollectionDeleted: function onCollectionDeleted(user, collection) {},
|
2012-03-07 10:42:39 -08:00
|
|
|
onItemDeleted: function onItemDeleted(user, collection, wboID) {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called at the top of every request.
|
|
|
|
*
|
|
|
|
* Allows the test to inspect the request. Hooks should be careful not to
|
|
|
|
* modify or change state of the request or they may impact future processing.
|
|
|
|
*/
|
|
|
|
onRequest: function onRequest(request) {},
|
2011-09-29 11:51:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new test Sync server. Takes a callback object (e.g.,
|
|
|
|
* SyncServerCallback) as input.
|
|
|
|
*/
|
|
|
|
function SyncServer(callback) {
|
|
|
|
this.callback = callback || {__proto__: SyncServerCallback};
|
2012-06-29 13:28:17 -07:00
|
|
|
this.server = new HttpServer();
|
2011-09-29 11:51:27 -07:00
|
|
|
this.started = false;
|
|
|
|
this.users = {};
|
|
|
|
this._log = Log4Moz.repository.getLogger(SYNC_HTTP_LOGGER);
|
|
|
|
|
|
|
|
// Install our own default handler. This allows us to mess around with the
|
|
|
|
// whole URL space.
|
|
|
|
let handler = this.server._handler;
|
|
|
|
handler._handleDefault = this.handleDefault.bind(this, handler);
|
|
|
|
}
|
|
|
|
SyncServer.prototype = {
|
|
|
|
port: 8080,
|
2012-06-29 13:28:17 -07:00
|
|
|
server: null, // HttpServer.
|
2011-09-29 11:51:27 -07:00
|
|
|
users: null, // Map of username => {collections, password}.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the SyncServer's underlying HTTP server.
|
|
|
|
*
|
|
|
|
* @param port
|
|
|
|
* The numeric port on which to start. A falsy value implies the
|
|
|
|
* default (8080).
|
|
|
|
* @param cb
|
|
|
|
* A callback function (of no arguments) which is invoked after
|
|
|
|
* startup.
|
|
|
|
*/
|
|
|
|
start: function start(port, cb) {
|
|
|
|
if (this.started) {
|
|
|
|
this._log.warn("Warning: server already started on " + this.port);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (port) {
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.server.start(this.port);
|
|
|
|
this.started = true;
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
_("==========================================");
|
|
|
|
_("Got exception starting Sync HTTP server on port " + this.port);
|
|
|
|
_("Error: " + Utils.exceptionStr(ex));
|
|
|
|
_("Is there a process already listening on port " + this.port + "?");
|
|
|
|
_("==========================================");
|
|
|
|
do_throw(ex);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the SyncServer's HTTP server.
|
|
|
|
*
|
|
|
|
* @param cb
|
|
|
|
* A callback function. Invoked after the server has been stopped.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
stop: function stop(cb) {
|
|
|
|
if (!this.started) {
|
|
|
|
this._log.warn("SyncServer: Warning: server not running. Can't stop me now!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.server.stop(cb);
|
|
|
|
this.started = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a server timestamp for a record.
|
|
|
|
* The server returns timestamps with 1/100 sec granularity. Note that this is
|
|
|
|
* subject to change: see Bug 650435.
|
|
|
|
*/
|
|
|
|
timestamp: function timestamp() {
|
2011-10-27 22:25:00 -07:00
|
|
|
return new_timestamp();
|
2011-09-29 11:51:27 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new user, complete with an empty set of collections.
|
2011-10-01 12:36:51 -07:00
|
|
|
*
|
|
|
|
* @param username
|
|
|
|
* The username to use. An Error will be thrown if a user by that name
|
|
|
|
* already exists.
|
|
|
|
* @param password
|
|
|
|
* A password string.
|
|
|
|
*
|
|
|
|
* @return a user object, as would be returned by server.user(username).
|
2011-09-29 11:51:27 -07:00
|
|
|
*/
|
|
|
|
registerUser: function registerUser(username, password) {
|
|
|
|
if (username in this.users) {
|
|
|
|
throw new Error("User already exists.");
|
|
|
|
}
|
|
|
|
this.users[username] = {
|
|
|
|
password: password,
|
|
|
|
collections: {}
|
|
|
|
};
|
2011-10-01 12:36:51 -07:00
|
|
|
return this.user(username);
|
2011-09-29 11:51:27 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
userExists: function userExists(username) {
|
|
|
|
return username in this.users;
|
|
|
|
},
|
|
|
|
|
|
|
|
getCollection: function getCollection(username, collection) {
|
|
|
|
return this.users[username].collections[collection];
|
|
|
|
},
|
|
|
|
|
|
|
|
_insertCollection: function _insertCollection(collections, collection, wbos) {
|
|
|
|
let coll = new ServerCollection(wbos, true);
|
|
|
|
coll.collectionHandler = coll.handler();
|
|
|
|
collections[collection] = coll;
|
|
|
|
return coll;
|
|
|
|
},
|
|
|
|
|
|
|
|
createCollection: function createCollection(username, collection, wbos) {
|
|
|
|
if (!(username in this.users)) {
|
|
|
|
throw new Error("Unknown user.");
|
|
|
|
}
|
|
|
|
let collections = this.users[username].collections;
|
|
|
|
if (collection in collections) {
|
|
|
|
throw new Error("Collection already exists.");
|
|
|
|
}
|
|
|
|
return this._insertCollection(collections, collection, wbos);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept a map like the following:
|
|
|
|
* {
|
|
|
|
* meta: {global: {version: 1, ...}},
|
|
|
|
* crypto: {"keys": {}, foo: {bar: 2}},
|
|
|
|
* bookmarks: {}
|
|
|
|
* }
|
|
|
|
* to cause collections and WBOs to be created.
|
|
|
|
* If a collection already exists, no error is raised.
|
|
|
|
* If a WBO already exists, it will be updated to the new contents.
|
|
|
|
*/
|
|
|
|
createContents: function createContents(username, collections) {
|
|
|
|
if (!(username in this.users)) {
|
|
|
|
throw new Error("Unknown user.");
|
|
|
|
}
|
|
|
|
let userCollections = this.users[username].collections;
|
|
|
|
for (let [id, contents] in Iterator(collections)) {
|
|
|
|
let coll = userCollections[id] ||
|
|
|
|
this._insertCollection(userCollections, id);
|
|
|
|
for (let [wboID, payload] in Iterator(contents)) {
|
|
|
|
coll.insert(wboID, payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a WBO in an existing collection.
|
|
|
|
*/
|
|
|
|
insertWBO: function insertWBO(username, collection, wbo) {
|
|
|
|
if (!(username in this.users)) {
|
|
|
|
throw new Error("Unknown user.");
|
|
|
|
}
|
|
|
|
let userCollections = this.users[username].collections;
|
|
|
|
if (!(collection in userCollections)) {
|
|
|
|
throw new Error("Unknown collection.");
|
|
|
|
}
|
|
|
|
userCollections[collection].insertWBO(wbo);
|
|
|
|
return wbo;
|
|
|
|
},
|
|
|
|
|
2011-10-12 13:57:39 -07:00
|
|
|
/**
|
|
|
|
* Delete all of the collections for the named user.
|
|
|
|
*
|
|
|
|
* @param username
|
|
|
|
* The name of the affected user.
|
|
|
|
*
|
|
|
|
* @return a timestamp.
|
|
|
|
*/
|
|
|
|
deleteCollections: function deleteCollections(username) {
|
|
|
|
if (!(username in this.users)) {
|
|
|
|
throw new Error("Unknown user.");
|
|
|
|
}
|
|
|
|
let userCollections = this.users[username].collections;
|
|
|
|
for each (let [name, coll] in Iterator(userCollections)) {
|
|
|
|
this._log.trace("Bulk deleting " + name + " for " + username + "...");
|
|
|
|
coll.delete({});
|
|
|
|
}
|
|
|
|
this.users[username].collections = {};
|
|
|
|
return this.timestamp();
|
|
|
|
},
|
|
|
|
|
2011-09-29 11:51:27 -07:00
|
|
|
/**
|
|
|
|
* Simple accessor to allow collective binding and abbreviation of a bunch of
|
|
|
|
* methods. Yay!
|
|
|
|
* Use like this:
|
|
|
|
*
|
|
|
|
* let u = server.user("john");
|
|
|
|
* u.collection("bookmarks").wbo("abcdefg").payload; // Etc.
|
|
|
|
*
|
|
|
|
* @return a proxy for the user data stored in this server.
|
|
|
|
*/
|
|
|
|
user: function user(username) {
|
|
|
|
let collection = this.getCollection.bind(this, username);
|
|
|
|
let createCollection = this.createCollection.bind(this, username);
|
2011-10-04 09:03:18 -07:00
|
|
|
let createContents = this.createContents.bind(this, username);
|
|
|
|
let modified = function (collectionName) {
|
|
|
|
return collection(collectionName).timestamp;
|
|
|
|
}
|
2011-10-12 13:57:39 -07:00
|
|
|
let deleteCollections = this.deleteCollections.bind(this, username);
|
2011-09-29 11:51:27 -07:00
|
|
|
return {
|
2011-10-12 13:57:39 -07:00
|
|
|
collection: collection,
|
|
|
|
createCollection: createCollection,
|
|
|
|
createContents: createContents,
|
|
|
|
deleteCollections: deleteCollections,
|
|
|
|
modified: modified
|
2011-09-29 11:51:27 -07:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Regular expressions for splitting up Sync request paths.
|
|
|
|
* Sync URLs are of the form:
|
|
|
|
* /$apipath/$version/$user/$further
|
|
|
|
* where $further is usually:
|
|
|
|
* storage/$collection/$wbo
|
|
|
|
* or
|
|
|
|
* storage/$collection
|
|
|
|
* or
|
|
|
|
* info/$op
|
|
|
|
* We assume for the sake of simplicity that $apipath is empty.
|
|
|
|
*
|
|
|
|
* N.B., we don't follow any kind of username spec here, because as far as I
|
|
|
|
* can tell there isn't one. See Bug 689671. Instead we follow the Python
|
|
|
|
* server code.
|
|
|
|
*
|
|
|
|
* Path: [all, version, username, first, rest]
|
2011-10-12 13:57:39 -07:00
|
|
|
* Storage: [all, collection?, id?]
|
2011-09-29 11:51:27 -07:00
|
|
|
*/
|
2011-10-12 13:57:39 -07:00
|
|
|
pathRE: /^\/([0-9]+(?:\.[0-9]+)?)\/([-._a-zA-Z0-9]+)(?:\/([^\/]+)(?:\/(.+))?)?$/,
|
2011-09-29 11:51:27 -07:00
|
|
|
storageRE: /^([-_a-zA-Z0-9]+)(?:\/([-_a-zA-Z0-9]+)\/?)?$/,
|
|
|
|
|
|
|
|
defaultHeaders: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP response utility.
|
|
|
|
*/
|
|
|
|
respond: function respond(req, resp, code, status, body, headers) {
|
|
|
|
resp.setStatusLine(req.httpVersion, code, status);
|
|
|
|
for each (let [header, value] in Iterator(headers || this.defaultHeaders)) {
|
|
|
|
resp.setHeader(header, value);
|
|
|
|
}
|
|
|
|
resp.setHeader("X-Weave-Timestamp", "" + this.timestamp(), false);
|
|
|
|
resp.bodyOutputStream.write(body, body.length);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-06-29 13:28:17 -07:00
|
|
|
* This is invoked by the HttpServer. `this` is bound to the SyncServer;
|
|
|
|
* `handler` is the HttpServer's handler.
|
2011-09-29 11:51:27 -07:00
|
|
|
*
|
|
|
|
* TODO: need to use the correct Sync API response codes and errors here.
|
|
|
|
* TODO: Basic Auth.
|
2012-08-29 14:43:41 -07:00
|
|
|
* TODO: check username in path against username in BasicAuth.
|
2011-09-29 11:51:27 -07:00
|
|
|
*/
|
|
|
|
handleDefault: function handleDefault(handler, req, resp) {
|
2012-03-05 17:36:45 -08:00
|
|
|
try {
|
|
|
|
this._handleDefault(handler, req, resp);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof HttpError) {
|
|
|
|
this.respond(req, resp, e.code, e.description, "", {});
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleDefault: function _handleDefault(handler, req, resp) {
|
2011-09-29 11:51:27 -07:00
|
|
|
this._log.debug("SyncServer: Handling request: " + req.method + " " + req.path);
|
2012-03-07 10:42:39 -08:00
|
|
|
|
|
|
|
if (this.callback.onRequest) {
|
|
|
|
this.callback.onRequest(req);
|
|
|
|
}
|
|
|
|
|
2011-09-29 11:51:27 -07:00
|
|
|
let parts = this.pathRE.exec(req.path);
|
|
|
|
if (!parts) {
|
|
|
|
this._log.debug("SyncServer: Unexpected request: bad URL " + req.path);
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [all, version, username, first, rest] = parts;
|
|
|
|
if (version != SYNC_API_VERSION) {
|
|
|
|
this._log.debug("SyncServer: Unknown version.");
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.userExists(username)) {
|
|
|
|
this._log.debug("SyncServer: Unknown user.");
|
|
|
|
throw HTTP_401;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hand off to the appropriate handler for this path component.
|
|
|
|
if (first in this.toplevelHandlers) {
|
|
|
|
let handler = this.toplevelHandlers[first];
|
|
|
|
return handler.call(this, handler, req, resp, version, username, rest);
|
|
|
|
}
|
|
|
|
this._log.debug("SyncServer: Unknown top-level " + first);
|
|
|
|
throw HTTP_404;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the object that is returned for an info/collections request.
|
|
|
|
*/
|
|
|
|
infoCollections: function infoCollections(username) {
|
|
|
|
let responseObject = {};
|
|
|
|
let colls = this.users[username].collections;
|
|
|
|
for (let coll in colls) {
|
|
|
|
responseObject[coll] = colls[coll].timestamp;
|
|
|
|
}
|
|
|
|
this._log.trace("SyncServer: info/collections returning " +
|
|
|
|
JSON.stringify(responseObject));
|
|
|
|
return responseObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of the handler methods we use for top-level path components.
|
|
|
|
*/
|
|
|
|
toplevelHandlers: {
|
|
|
|
"storage": function handleStorage(handler, req, resp, version, username, rest) {
|
2011-10-12 13:57:39 -07:00
|
|
|
let respond = this.respond.bind(this, req, resp);
|
|
|
|
if (!rest || !rest.length) {
|
|
|
|
this._log.debug("SyncServer: top-level storage " +
|
|
|
|
req.method + " request.");
|
|
|
|
|
|
|
|
// TODO: verify if this is spec-compliant.
|
|
|
|
if (req.method != "DELETE") {
|
|
|
|
respond(405, "Method Not Allowed", "[]", {"Allow": "DELETE"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all collections and track the timestamp for the response.
|
|
|
|
let timestamp = this.user(username).deleteCollections();
|
|
|
|
|
|
|
|
// Return timestamp and OK for deletion.
|
|
|
|
respond(200, "OK", JSON.stringify(timestamp));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 11:51:27 -07:00
|
|
|
let match = this.storageRE.exec(rest);
|
|
|
|
if (!match) {
|
|
|
|
this._log.warn("SyncServer: Unknown storage operation " + rest);
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
let [all, collection, wboID] = match;
|
|
|
|
let coll = this.getCollection(username, collection);
|
|
|
|
switch (req.method) {
|
|
|
|
case "GET":
|
|
|
|
if (!coll) {
|
2011-10-15 15:29:11 -07:00
|
|
|
if (wboID) {
|
|
|
|
respond(404, "Not found", "Not found");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-29 11:51:27 -07:00
|
|
|
// *cries inside*: Bug 687299.
|
|
|
|
respond(200, "OK", "[]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!wboID) {
|
|
|
|
return coll.collectionHandler(req, resp);
|
|
|
|
}
|
|
|
|
let wbo = coll.wbo(wboID);
|
|
|
|
if (!wbo) {
|
|
|
|
respond(404, "Not found", "Not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wbo.handler()(req, resp);
|
|
|
|
|
|
|
|
// TODO: implement handling of X-If-Unmodified-Since for write verbs.
|
|
|
|
case "DELETE":
|
|
|
|
if (!coll) {
|
|
|
|
respond(200, "OK", "{}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (wboID) {
|
|
|
|
let wbo = coll.wbo(wboID);
|
|
|
|
if (wbo) {
|
|
|
|
wbo.delete();
|
2011-10-15 15:29:11 -07:00
|
|
|
this.callback.onItemDeleted(username, collection, wboID);
|
2011-09-29 11:51:27 -07:00
|
|
|
}
|
|
|
|
respond(200, "OK", "{}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
coll.collectionHandler(req, resp);
|
|
|
|
|
|
|
|
// Spot if this is a DELETE for some IDs, and don't blow away the
|
|
|
|
// whole collection!
|
|
|
|
//
|
|
|
|
// We already handled deleting the WBOs by invoking the deleted
|
2012-08-29 14:43:41 -07:00
|
|
|
// collection's handler. However, in the case of
|
2011-09-29 11:51:27 -07:00
|
|
|
//
|
|
|
|
// DELETE storage/foobar
|
|
|
|
//
|
|
|
|
// we also need to remove foobar from the collections map. This
|
|
|
|
// clause tries to differentiate the above request from
|
|
|
|
//
|
|
|
|
// DELETE storage/foobar?ids=foo,baz
|
|
|
|
//
|
|
|
|
// and do the right thing.
|
|
|
|
// TODO: less hacky method.
|
|
|
|
if (-1 == req.queryString.indexOf("ids=")) {
|
|
|
|
// When you delete the entire collection, we drop it.
|
|
|
|
this._log.debug("Deleting entire collection.");
|
|
|
|
delete this.users[username].collections[collection];
|
|
|
|
this.callback.onCollectionDeleted(username, collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify of item deletion.
|
|
|
|
let deleted = resp.deleted || [];
|
|
|
|
for (let i = 0; i < deleted.length; ++i) {
|
|
|
|
this.callback.onItemDeleted(username, collection, deleted[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case "POST":
|
|
|
|
case "PUT":
|
|
|
|
if (!coll) {
|
|
|
|
coll = this.createCollection(username, collection);
|
|
|
|
}
|
|
|
|
if (wboID) {
|
|
|
|
let wbo = coll.wbo(wboID);
|
|
|
|
if (!wbo) {
|
|
|
|
this._log.trace("SyncServer: creating WBO " + collection + "/" + wboID);
|
|
|
|
wbo = coll.insert(wboID);
|
|
|
|
}
|
|
|
|
// Rather than instantiate each WBO's handler function, do it once
|
|
|
|
// per request. They get hit far less often than do collections.
|
|
|
|
wbo.handler()(req, resp);
|
|
|
|
coll.timestamp = resp.newModified;
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
return coll.collectionHandler(req, resp);
|
|
|
|
default:
|
|
|
|
throw "Request method " + req.method + " not implemented.";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"info": function handleInfo(handler, req, resp, version, username, rest) {
|
|
|
|
switch (rest) {
|
|
|
|
case "collections":
|
|
|
|
let body = JSON.stringify(this.infoCollections(username));
|
|
|
|
this.respond(req, resp, 200, "OK", body, {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
case "collection_usage":
|
|
|
|
case "collection_counts":
|
|
|
|
case "quota":
|
|
|
|
// TODO: implement additional info methods.
|
|
|
|
this.respond(req, resp, 200, "OK", "TODO");
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// TODO
|
|
|
|
this._log.warn("SyncServer: Unknown info operation " + rest);
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test helper.
|
|
|
|
*/
|
|
|
|
function serverForUsers(users, contents, callback) {
|
|
|
|
let server = new SyncServer(callback);
|
|
|
|
for (let [user, pass] in Iterator(users)) {
|
|
|
|
server.registerUser(user, pass);
|
|
|
|
server.createContents(user, contents);
|
|
|
|
}
|
|
|
|
server.start();
|
|
|
|
return server;
|
|
|
|
}
|