/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+ * Writes the error page for the given HTTP error code over the given
+ * connection.
+ *
+ * @param errorCode : uint
+ * the HTTP error code to be used
+ * @param connection : Connection
+ * the connection on which the error occurred
+ */
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+ * Handles a request which generates the given error code, using the
+ * user-defined error handler if one has been set, gracefully falling back to
+ * the x00 status code if the code has no handler, and failing to status code
+ * 500 if all else fails.
+ *
+ * @param errorCode : uint
+ * the HTTP error which is to be returned
+ * @param metadata : Request
+ * metadata for the request, which will often be incomplete since this is an
+ * error
+ * @param response : Response
+ * an uninitialized Response should be initialized when this method
+ * completes with information which represents the desired error code in the
+ * ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+ * fallback for 505, per HTTP specs)
+ */
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+ * This object contains the default handlers for the various HTTP error codes.
+ */
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain;charset=utf-8", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ 403 Forbidden\
+ \
+ 403 Forbidden
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ 404 Not Found\
+ \
+ 404 Not Found
\
+ \
+ " +
+ htmlEscape(metadata.path) +
+ " was not found.\
+
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ \
+ 416 Requested Range Not Satisfiable\
+ \
+ 416 Requested Range Not Satisfiable
\
+ The byte range was not valid for the\
+ requested resource.\
+
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ 500 Internal Server Error\
+ \
+ 500 Internal Server Error
\
+ Something's broken in this server and\
+ needs to be fixed.
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ 501 Not Implemented\
+ \
+ 501 Not Implemented
\
+ This server is not (yet) Apache.
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ 505 HTTP Version Not Supported\
+ \
+ 505 HTTP Version Not Supported
\
+ This server only supports HTTP/1.0 and HTTP/1.1\
+ connections.
\
+ \
+ ";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+ * Contains handlers for the default set of URIs contained in this server.
+ */
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html;charset=utf-8", false);
+
+ var body = "\
+ httpd.js\
+ \
+ httpd.js
\
+ If you're seeing this page, httpd.js is up and\
+ serving requests! Now set a base path and serve some\
+ files!
\
+ \
+ ";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain;charset=utf-8", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+ * Maps absolute paths to files on the local file system (as nsILocalFiles).
+ */
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+ * Maps key to a clone of the nsILocalFile value if value is non-null;
+ * otherwise, removes any extant mapping for key.
+ *
+ * @param key : string
+ * string to which a clone of value is mapped
+ * @param value : nsILocalFile
+ * the file to map to key, or null to remove a mapping
+ */
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+ * Returns a clone of the nsILocalFile mapped to key, or null if no such
+ * mapping exists.
+ *
+ * @param key : string
+ * key to which the returned file maps
+ * @returns nsILocalFile
+ * a clone of the mapped file, or null if no mapping exists
+ */
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *
+// CHAR =
+// CTL =
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+ * Determines whether the given character code is a CTL.
+ *
+ * @param code : uint
+ * the character code
+ * @returns boolean
+ * true if code is a CTL, false otherwise
+ */
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+ * Represents a response to an HTTP request, encapsulating all details of that
+ * response. This includes all headers, the HTTP version, status code and
+ * explanation, and the entity itself.
+ *
+ * @param connection : Connection
+ * the connection over which this response is to be written
+ */
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+ * The HTTP version of this response; defaults to 1.1 if not set by the
+ * handler.
+ */
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+ * The HTTP code of this response; defaults to 200.
+ */
+ this._httpCode = 200;
+
+ /**
+ * The description of the HTTP code in this response; defaults to "OK".
+ */
+ this._httpDescription = "OK";
+
+ /**
+ * An nsIHttpHeaders object in which the headers in this response should be
+ * stored. This property is null after the status line and headers have been
+ * written to the network, and it may be modified up until it is cleared,
+ * except if this._finished is set first (in which case headers are written
+ * asynchronously in response to a finish() call not preceded by
+ * flushHeaders()).
+ */
+ this._headers = new nsHttpHeaders();
+
+ /**
+ * Set to true when this response is ended (completely constructed if possible
+ * and the connection closed); further actions on this will then fail.
+ */
+ this._ended = false;
+
+ /**
+ * A stream used to hold data written to the body of this response.
+ */
+ this._bodyOutputStream = null;
+
+ /**
+ * A stream containing all data that has been written to the body of this
+ * response so far. (Async handlers make the data contained in this
+ * unreliable as a way of determining content length in general, but auxiliary
+ * saved information can sometimes be used to guarantee reliability.)
+ */
+ this._bodyInputStream = null;
+
+ /**
+ * A stream copier which copies data to the network. It is initially null
+ * until replaced with a copier for response headers; when headers have been
+ * fully sent it is replaced with a copier for the response body, remaining
+ * so for the duration of response processing.
+ */
+ this._asyncCopier = null;
+
+ /**
+ * True if this response has been designated as being processed
+ * asynchronously rather than for the duration of a single call to
+ * nsIHttpRequestHandler.handle.
+ */
+ this._processAsync = false;
+
+ /**
+ * True iff finish() has been called on this, signaling that no more changes
+ * to this may be made.
+ */
+ this._finished = false;
+
+ /**
+ * True iff powerSeized() has been called on this, signaling that this
+ * response is to be handled manually by the response handler (which may then
+ * send arbitrary data in response, even non-HTTP responses).
+ */
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *
+ // TEXT =
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+ * Either the bodyOutputStream getter or this method is responsible for
+ * starting the asynchronous processor and catching writes of data to the
+ * response body of async responses as they happen, for the purpose of
+ * forwarding those writes to the actual connection's output stream.
+ * If bodyOutputStream is accessed first, calling this method will create
+ * the processor (when it first is clear that body data is to be written
+ * immediately, not buffered). If this method is called first, accessing
+ * bodyOutputStream will create the processor. If only this method is
+ * called, we'll write nothing, neither headers nor the nonexistent body,
+ * until finish() is called. Since that delay is easily avoided by simply
+ * getting bodyOutputStream or calling write(""), we don't worry about it.
+ */
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+ * The HTTP version number of this, as a string (e.g. "1.1").
+ */
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+ * The HTTP status code of this response, as a string of three characters per
+ * RFC 2616.
+ */
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+ * The description of the HTTP status code of this response, or "" if none is
+ * set.
+ */
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+ * The headers in this response, as an nsHttpHeaders object.
+ */
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+ * Determines whether this response may be abandoned in favor of a newly
+ * constructed response. A response may be abandoned only if it is not being
+ * sent asynchronously and if raw control over it has not been taken from the
+ * server.
+ *
+ * @returns boolean
+ * true iff no data has been written to the network
+ */
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+ * If necessary, kicks off the remaining request processing needed to be done
+ * after a request handler performs its initial work upon this response.
+ */
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+ * Abruptly ends processing of this response, usually due to an error in an
+ * incoming request but potentially due to a bad error handler. Since we
+ * cannot handle the error in the usual way (giving an HTTP error page in
+ * response) because data may already have been sent (or because the response
+ * might be expected to have been generated asynchronously or completely from
+ * scratch by the handler), we stop processing this response and abruptly
+ * close the connection.
+ *
+ * @param e : Error
+ * the exception which precipitated this abort, or null if no such exception
+ * was generated
+ */
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+ * Closes this response's network connection, marks the response as finished,
+ * and notifies the server handler that the request is done being processed.
+ */
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+ * Sends the status line and headers of this response if they haven't been
+ * sent and initiates the process of copying data written to this response's
+ * body to the network.
+ */
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+ * Signals that all modifications to the response status line and headers are
+ * complete and then sends that data over the network to the client. Once
+ * this method completes, a different response to the request that resulted
+ * in this response cannot be sent -- the only possible action in case of
+ * error is to abort the response and close the connection.
+ */
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!Components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+ * Asynchronously writes the body of the response (or the entire response, if
+ * seizePower() has been called) to the network.
+ */
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!Components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+ * Size of the segments in the buffer used in storing response data and writing
+ * it to the socket.
+ */
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+ * Copies data from source to sink as it becomes available, when that data can
+ * be written to sink without blocking.
+ *
+ * @param source : nsIAsyncInputStream
+ * the stream from which data is to be read
+ * @param sink : nsIAsyncOutputStream
+ * the stream to which data is to be copied
+ * @param observer : nsIRequestObserver
+ * an observer which will be notified when the copy starts and finishes
+ * @param context : nsISupports
+ * context passed to observer when notified of start/stop
+ * @throws NS_ERROR_NULL_POINTER
+ * if source, sink, or observer are null
+ */
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+ * True iff this is currently being canceled (cancel has been called, the
+ * callback may not yet have been made).
+ */
+ this._canceled = false;
+
+ /**
+ * False until all data has been read from input and written to output, at
+ * which point this copy is completed and cancel() is asynchronously called.
+ */
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+ * Receives a more-data-in-input notification and writes the corresponding
+ * data to the output.
+ *
+ * @param input : nsIAsyncInputStream
+ * the input stream on whose data we have been waiting
+ */
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+ * Callback when data may be written to the output stream without blocking, or
+ * when the output stream has been closed.
+ *
+ * @param output : nsIAsyncOutputStream
+ * the output stream on whose writability we've been waiting, also known as
+ * this._sink
+ */
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+ * If we might, then wait for the output stream to be closed. (We wait
+ * only for closure because we have no data to write -- and if we waited
+ * for a specific amount of data, we would get repeatedly notified for no
+ * reason if over time the output stream permitted more and more data to
+ * be written to it without blocking.)
+ */
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+ * On the other hand, if we can't have more data because the input
+ * stream's gone away, then it's time to notify of copy completion.
+ * Victory!
+ */
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+ * Cancels data reading from input, asynchronously writes out any pending
+ * data, and causes the observer to be notified with the given error code when
+ * all writing has finished.
+ *
+ * @param status : nsresult
+ * the status to pass to the observer when data copying has been canceled
+ */
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+ * Stop reading input if we haven't already done so, passing e as the status
+ * when closing the stream, and kick off a copy-completion notice if no more
+ * data remains to be written.
+ *
+ * @param e : nsresult
+ * the status to be used when closing the input stream
+ */
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+ * Stop writing output if we haven't already done so, discard any data that
+ * remained to be sent, close off input if it wasn't already closed, and kick
+ * off a copy-completion notice.
+ *
+ * @param e : nsresult
+ * the status to be used when closing input if it wasn't already closed
+ */
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+ * Completes processing of this copy: either by canceling the copy if it
+ * hasn't already been canceled using the provided status, or by dispatching
+ * the cancel callback event (with the originally provided status, of course)
+ * if it already has been canceled.
+ *
+ * @param status : nsresult
+ * the status code to use to cancel this, if this hasn't already been
+ * canceled
+ */
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+ * Kicks off another wait for more data to be available from the input stream.
+ */
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+ * Kicks off another wait until data can be written to the output stream.
+ */
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+ * Kicks off a wait for the sink to which data is being copied to be closed.
+ * We wait for stream closure when we don't have any data to be copied, rather
+ * than waiting to write a specific amount of data. We can't wait to write
+ * data because the sink might be infinitely writable, and if no data appears
+ * in the source for a long time we might have to spin quite a bit waiting to
+ * write, waiting to write again, &c. Waiting on stream closure instead means
+ * we'll get just one notification if the sink dies. Note that when data
+ * starts arriving from the sink we'll resume waiting for data to be written,
+ * dropping this closure-only callback entirely.
+ */
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+ * Closes input with the given status, if it hasn't already been closed;
+ * otherwise a no-op.
+ *
+ * @param status : nsresult
+ * status code use to close the source stream if necessary
+ */
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+ * A container for utility functions used with HTTP headers.
+ */
+const headerUtils =
+{
+ /**
+ * Normalizes fieldName (by converting it to lowercase) and ensures it is a
+ * valid header field name (although not necessarily one specified in RFC
+ * 2616).
+ *
+ * @throws NS_ERROR_INVALID_ARG
+ * if fieldName does not match the field-name production in RFC 2616
+ * @returns string
+ * fieldName converted to lowercase if it is a valid header, for characters
+ * where case conversion is possible
+ */
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ {
+ dumpn("*** Empty fieldName");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+ * Ensures that fieldValue is a valid header field value (although not
+ * necessarily as specified in RFC 2616 if the corresponding field name is
+ * part of the HTTP protocol), normalizes the value if it is, and
+ * returns the normalized value.
+ *
+ * @param fieldValue : string
+ * a value to be normalized as an HTTP header field value
+ * @throws NS_ERROR_INVALID_ARG
+ * if fieldValue does not match the field-value production in RFC 2616
+ * @returns string
+ * fieldValue as a normalized HTTP header field value
+ */
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content =
+ // TEXT =
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = >
+ // quoted-pair = "\" CHAR
+ // CHAR =
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ dumpn("*** Normalized value: '" + val + "'");
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ {
+ dump("*** Char " + i + " has charcode " + val.charCodeAt(i));
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+ * Converts the given string into a string which is safe for use in an HTML
+ * context.
+ *
+ * @param str : string
+ * the string to make HTML-safe
+ * @returns string
+ * an HTML-safe version of str
+ */
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+ * Constructs an object representing an HTTP version (see section 3.1).
+ *
+ * @param versionString
+ * a string of the form "#.#", where # is an non-negative decimal integer with
+ * or without leading zeros
+ * @throws
+ * if versionString does not specify a valid HTTP version number
+ */
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+ * Returns the standard string representation of the HTTP version represented
+ * by this (e.g., "1.1").
+ */
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+ * Returns true if this represents the same HTTP version as otherVersion,
+ * false otherwise.
+ *
+ * @param otherVersion : nsHttpVersion
+ * the version to compare against this
+ */
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+ * An object which stores HTTP headers for a request or response.
+ *
+ * Note that since headers are case-insensitive, this object converts headers to
+ * lowercase before storing them. This allows the getHeader and hasHeader
+ * methods to work correctly for any case of a header, but it means that the
+ * values returned by .enumerator may not be equal case-sensitively to the
+ * values passed to setHeader when adding headers to this.
+ */
+function nsHttpHeaders()
+{
+ /**
+ * A hash of headers, with header field names as the keys and header field
+ * values as the values. Header field names are case-insensitive, but upon
+ * insertion here they are converted to lowercase. Header field values are
+ * normalized upon insertion to contain no leading or trailing whitespace.
+ *
+ * Note also that per RFC 2616, section 4.2, two headers with the same name in
+ * a message may be treated as one header with the same field name and a field
+ * value consisting of the separate field values joined together with a "," in
+ * their original order. This hash stores multiple headers with the same name
+ * in this manner.
+ */
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+ * Sets the header represented by name and value in this.
+ *
+ * @param name : string
+ * the header name
+ * @param value : string
+ * the header value
+ * @throws NS_ERROR_INVALID_ARG
+ * if name or value is not a valid header component
+ */
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+ * Returns the value for the header specified by this.
+ *
+ * @throws NS_ERROR_INVALID_ARG
+ * if fieldName does not constitute a valid header field name
+ * @throws NS_ERROR_NOT_AVAILABLE
+ * if the given header does not exist in this
+ * @returns string
+ * the field value for the given header, possibly with non-semantic changes
+ * (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+ * with spaces, etc.) at the option of the implementation; multiple
+ * instances of the header will be combined with a comma, except for
+ * the three headers noted in the description of getHeaderValues
+ */
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+ * Returns the value for the header specified by fieldName as an array.
+ *
+ * @throws NS_ERROR_INVALID_ARG
+ * if fieldName does not constitute a valid header field name
+ * @throws NS_ERROR_NOT_AVAILABLE
+ * if the given header does not exist in this
+ * @returns [string]
+ * an array of all the header values in this for the given
+ * header name. Header values will generally be collapsed
+ * into a single header by joining all header values together
+ * with commas, but certain headers (Proxy-Authenticate,
+ * WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+ * and cannot be collapsed in this manner. For these headers
+ * only, the returned array may contain multiple elements if
+ * that header has been added more than once.
+ */
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+ * Returns true if a header with the given field name exists in this, false
+ * otherwise.
+ *
+ * @param fieldName : string
+ * the field name whose existence is to be determined in this
+ * @throws NS_ERROR_INVALID_ARG
+ * if fieldName does not constitute a valid header field name
+ * @returns boolean
+ * true if the header's present, false otherwise
+ */
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+ * Returns a new enumerator over the field names of the headers in this, as
+ * nsISupportsStrings. The names returned will be in lowercase, regardless of
+ * how they were input using setHeader (header names are case-insensitive per
+ * RFC 2616).
+ */
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+ * Constructs an nsISimpleEnumerator for the given array of items.
+ *
+ * @param items : Array
+ * the items, which must all implement nsISupports
+ */
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+ * A representation of the data in an HTTP request.
+ *
+ * @param port : uint
+ * the port on which the server receiving this request runs
+ */
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+ * The headers in this request.
+ */
+ this._headers = new nsHttpHeaders();
+
+ /**
+ * For the addition of ad-hoc properties and new functionality without having
+ * to change nsIHttpRequest every time; currently lazily created, as its only
+ * use is in directory listings.
+ */
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+
+/**
+ * Creates a new HTTP server listening for loopback traffic on the given port,
+ * starts it, and runs the server until the server processes a shutdown request,
+ * spinning an event loop so that events posted by the server's socket are
+ * processed.
+ *
+ * This method is primarily intended for use in running this script from within
+ * xpcshell and running a functional HTTP server without having to deal with
+ * non-essential details.
+ *
+ * Note that running multiple servers using variants of this method probably
+ * doesn't work, simply due to how the internal event loop is spun and stopped.
+ *
+ * @note
+ * This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+ * you should use this server as a component in Mozilla 1.8.
+ * @param port
+ * the port on which the server will run, or -1 if there exists no preference
+ * for a specific port; note that attempting to use some values for this
+ * parameter (particularly those below 1024) may cause this method to throw or
+ * may result in the server being prematurely shut down
+ * @param basePath
+ * a local directory from which requests will be served (i.e., if this is
+ * "/home/jwalden/" then a request to /index.html will load
+ * /home/jwalden/index.html); if this is omitted, only the default URLs in
+ * this server implementation will be functional
+ */
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.identity.setPrimary("http", "localhost", port);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
diff --git a/test/tests/data/snapshot/img.gif b/test/tests/data/snapshot/img.gif
new file mode 100644
index 0000000000..f191b280ce
Binary files /dev/null and b/test/tests/data/snapshot/img.gif differ
diff --git a/test/tests/data/test.html b/test/tests/data/test.html
new file mode 100644
index 0000000000..2835ff2838
--- /dev/null
+++ b/test/tests/data/test.html
@@ -0,0 +1,8 @@
+
+
+
+
+
+ This is a test.
+
+
diff --git a/test/tests/data/test.txt b/test/tests/data/test.txt
new file mode 100644
index 0000000000..6de7b8c69d
--- /dev/null
+++ b/test/tests/data/test.txt
@@ -0,0 +1 @@
+This is a test file.
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
index 486677187b..0981e93a07 100644
--- a/test/tests/itemTest.js
+++ b/test/tests/itemTest.js
@@ -537,6 +537,10 @@ describe("Zotero.Item", function () {
file.append(filename);
assert.equal(item.getFilePath(), file.path);
});
+
+ it.skip("should get and set a filename for a base-dir-relative file", function* () {
+
+ })
})
describe("#attachmentPath", function () {
@@ -608,11 +612,13 @@ describe("Zotero.Item", function () {
assert.equal(OS.Path.basename(path), newName)
yield OS.File.exists(path);
+ // File should be flagged for upload
+ // DEBUG: Is this necessary?
assert.equal(
- (yield Zotero.Sync.Storage.getSyncState(item.id)),
+ (yield Zotero.Sync.Storage.Local.getSyncState(item.id)),
Zotero.Sync.Storage.SYNC_STATE_TO_UPLOAD
);
- assert.isNull(yield Zotero.Sync.Storage.getSyncedHash(item.id));
+ assert.isNull(yield Zotero.Sync.Storage.Local.getSyncedHash(item.id));
})
})
diff --git a/test/tests/storageEngineTest.js b/test/tests/storageEngineTest.js
new file mode 100644
index 0000000000..1efc1b4761
--- /dev/null
+++ b/test/tests/storageEngineTest.js
@@ -0,0 +1,822 @@
+"use strict";
+
+describe("Zotero.Sync.Storage.Engine", function () {
+ Components.utils.import("resource://zotero-unit/httpd.js");
+
+ var win;
+ var apiKey = Zotero.Utilities.randomString(24);
+ var port = 16213;
+ var baseURL = `http://localhost:${port}/`;
+ var server;
+
+ var responses = {};
+
+ var setup = Zotero.Promise.coroutine(function* (options = {}) {
+ server = sinon.fakeServer.create();
+ server.autoRespond = true;
+
+ Components.utils.import("resource://zotero/concurrentCaller.js");
+ var caller = new ConcurrentCaller(1);
+ caller.setLogger(msg => Zotero.debug(msg));
+ caller.stopOnError = true;
+
+ Components.utils.import("resource://zotero/config.js");
+ var client = new Zotero.Sync.APIClient({
+ baseURL,
+ apiVersion: options.apiVersion || ZOTERO_CONFIG.API_VERSION,
+ apiKey,
+ caller,
+ background: options.background || true
+ });
+
+ var engine = new Zotero.Sync.Storage.Engine({
+ apiClient: client,
+ libraryID: options.libraryID || Zotero.Libraries.userLibraryID,
+ stopOnError: true
+ });
+
+ return { engine, client, caller };
+ });
+
+ function setResponse(response) {
+ setHTTPResponse(server, baseURL, response, responses);
+ }
+
+ function parseQueryString(str) {
+ var queryStringParams = str.split('&');
+ var params = {};
+ for (let param of queryStringParams) {
+ let [ key, val ] = param.split('=');
+ params[key] = decodeURIComponent(val);
+ }
+ return params;
+ }
+
+ function assertAPIKey(request) {
+ assert.equal(request.requestHeaders["Zotero-API-Key"], apiKey);
+ }
+
+ //
+ // Tests
+ //
+ before(function* () {
+ })
+ beforeEach(function* () {
+ Zotero.debug("BEFORE HERE");
+ yield resetDB({
+ thisArg: this,
+ skipBundledFiles: true
+ });
+ Zotero.debug("DONE RESET");
+ win = yield loadZoteroPane();
+
+ Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
+
+ this.httpd = new HttpServer();
+ this.httpd.start(port);
+
+ yield Zotero.Users.setCurrentUserID(1);
+ yield Zotero.Users.setCurrentUsername("testuser");
+
+ // Set download-on-sync by default
+ Zotero.Sync.Storage.Local.downloadOnSync(
+ Zotero.Libraries.userLibraryID, true
+ );
+ Zotero.debug("DONE BEFORE");
+ })
+ afterEach(function* () {
+ var defer = new Zotero.Promise.defer();
+ this.httpd.stop(() => defer.resolve());
+ yield defer.promise;
+ win.close();
+ })
+ after(function* () {
+ this.timeout(60000);
+ //yield resetDB();
+ win.close();
+ })
+
+
+ describe("ZFS", function () {
+ describe("Syncing", function () {
+ it("should skip downloads if no last storage sync time", function* () {
+ var { engine, client, caller } = yield setup();
+
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 404
+ });
+ var result = yield engine.start();
+
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check last sync time
+ assert.isFalse(Zotero.Libraries.userLibrary.lastStorageSync);
+ })
+
+ it("should skip downloads if unchanged last storage sync time", function* () {
+ var { engine, client, caller } = yield setup();
+
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ var library = Zotero.Libraries.userLibrary;
+ library.lastStorageSync = newStorageSyncTime;
+ yield library.saveTx();
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+ var result = yield engine.start();
+
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check last sync time
+ assert.equal(library.lastStorageSync, newStorageSyncTime);
+ })
+
+ it("should ignore a remotely missing file", function* () {
+ var { engine, client, caller } = yield setup();
+
+ var item = new Zotero.Item("attachment");
+ item.attachmentLinkMode = 'imported_file';
+ item.attachmentPath = 'storage:test.txt';
+ yield item.saveTx();
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+ this.httpd.registerPathHandler(
+ `/users/1/items/${item.key}/file`,
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 404, null);
+ }
+ }
+ );
+ var result = yield engine.start();
+
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check last sync time
+ assert.equal(Zotero.Libraries.userLibrary.lastStorageSync, newStorageSyncTime);
+ })
+
+ it("should handle a remotely failing file", function* () {
+ var { engine, client, caller } = yield setup();
+
+ var item = new Zotero.Item("attachment");
+ item.attachmentLinkMode = 'imported_file';
+ item.attachmentPath = 'storage:test.txt';
+ yield item.saveTx();
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+ this.httpd.registerPathHandler(
+ `/users/1/items/${item.key}/file`,
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 500, null);
+ }
+ }
+ );
+ // TODO: In stopOnError mode, this the promise is rejected.
+ // This should probably test with stopOnError mode turned off instead.
+ var e = yield getPromiseError(engine.start());
+ assert.equal(e.message, Zotero.Sync.Storage.defaultError);
+ })
+
+ it("should download a missing file", function* () {
+ var { engine, client, caller } = yield setup();
+
+ var item = new Zotero.Item("attachment");
+ item.attachmentLinkMode = 'imported_file';
+ item.attachmentPath = 'storage:test.txt';
+ // TODO: Test binary data
+ var text = Zotero.Utilities.randomString();
+ yield item.saveTx();
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+
+ var mtime = "1441252524905";
+ var md5 = Zotero.Utilities.Internal.md5(text)
+
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+ var s3Path = `pretend-s3/${item.key}`;
+ this.httpd.registerPathHandler(
+ `/users/1/items/${item.key}/file`,
+ {
+ handle: function (request, response) {
+ if (!request.hasHeader('Zotero-API-Key')) {
+ response.setStatusLine(null, 403, "Forbidden");
+ return;
+ }
+ var key = request.getHeader('Zotero-API-Key');
+ if (key != apiKey) {
+ response.setStatusLine(null, 403, "Invalid key");
+ return;
+ }
+ response.setStatusLine(null, 302, "Found");
+ response.setHeader("Zotero-File-Modification-Time", mtime, false);
+ response.setHeader("Zotero-File-MD5", md5, false);
+ response.setHeader("Zotero-File-Compressed", "No", false);
+ response.setHeader("Location", baseURL + s3Path, false);
+ }
+ }
+ );
+ this.httpd.registerPathHandler(
+ "/" + s3Path,
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 200, "OK");
+ response.write(text);
+ }
+ }
+ );
+ var result = yield engine.start();
+
+ assert.isTrue(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check last sync time
+ assert.equal(Zotero.Libraries.userLibrary.lastStorageSync, newStorageSyncTime);
+ var contents = yield Zotero.File.getContentsAsync(yield item.getFilePathAsync());
+ assert.equal(contents, text);
+ })
+
+ it("should upload new files", function* () {
+ var { engine, client, caller } = yield setup();
+
+ // Single file
+ var file1 = getTestDataDirectory();
+ file1.append('test.png');
+ var item1 = yield Zotero.Attachments.importFromFile({ file: file1 });
+ var mtime1 = yield item1.attachmentModificationTime;
+ var hash1 = yield item1.attachmentHash;
+ var path1 = item1.getFilePath();
+ var filename1 = 'test.png';
+ var size1 = (yield OS.File.stat(path1)).size;
+ var contentType1 = 'image/png';
+ var prefix1 = Zotero.Utilities.randomString();
+ var suffix1 = Zotero.Utilities.randomString();
+ var uploadKey1 = Zotero.Utilities.randomString(32, 'abcdef0123456789');
+
+ // HTML file with auxiliary image
+ var file2 = OS.Path.join(getTestDataDirectory().path, 'snapshot', 'index.html');
+ var parentItem = yield createDataObject('item');
+ var item2 = yield Zotero.Attachments.importSnapshotFromFile({
+ file: file2,
+ url: 'http://example.com/',
+ parentItemID: parentItem.id,
+ title: 'Test',
+ contentType: 'text/html',
+ charset: 'utf-8'
+ });
+ var mtime2 = yield item2.attachmentModificationTime;
+ var hash2 = yield item2.attachmentHash;
+ var path2 = item2.getFilePath();
+ var filename2 = 'index.html';
+ var size2 = (yield OS.File.stat(path2)).size;
+ var contentType2 = 'text/html';
+ var charset2 = 'utf-8';
+ var prefix2 = Zotero.Utilities.randomString();
+ var suffix2 = Zotero.Utilities.randomString();
+ var uploadKey2 = Zotero.Utilities.randomString(32, 'abcdef0123456789');
+
+ var deferreds = [];
+
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 404
+ });
+ // https://github.com/cjohansen/Sinon.JS/issues/607
+ let fixSinonBug = ";charset=utf-8";
+ server.respond(function (req) {
+ // Get upload authorization for single file
+ if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item1.key}/file`
+ && req.requestBody.indexOf('upload=') == -1) {
+ assertAPIKey(req);
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/x-www-form-urlencoded" + fixSinonBug
+ );
+
+ let parts = req.requestBody.split('&');
+ let params = {};
+ for (let part of parts) {
+ let [key, val] = part.split('=');
+ params[key] = decodeURIComponent(val);
+ }
+ assert.equal(params.md5, hash1);
+ assert.equal(params.mtime, mtime1);
+ assert.equal(params.filename, filename1);
+ assert.equal(params.filesize, size1);
+ assert.equal(params.contentType, contentType1);
+
+ req.respond(
+ 200,
+ {
+ "Content-Type": "application/json"
+ },
+ JSON.stringify({
+ url: baseURL + "pretend-s3/1",
+ contentType: contentType1,
+ prefix: prefix1,
+ suffix: suffix1,
+ uploadKey: uploadKey1
+ })
+ );
+ }
+ // Get upload authorization for multi-file zip
+ else if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item2.key}/file`
+ && req.requestBody.indexOf('upload=') == -1) {
+ assertAPIKey(req);
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/x-www-form-urlencoded" + fixSinonBug
+ );
+
+ // Verify ZIP hash
+ let tmpZipPath = OS.Path.join(
+ Zotero.getTempDirectory().path,
+ item2.key + '.zip'
+ );
+ deferreds.push({
+ promise: Zotero.Utilities.Internal.md5Async(tmpZipPath)
+ .then(function (md5) {
+ assert.equal(params.zipMD5, md5);
+ })
+ });
+
+ let parts = req.requestBody.split('&');
+ let params = {};
+ for (let part of parts) {
+ let [key, val] = part.split('=');
+ params[key] = decodeURIComponent(val);
+ }
+ Zotero.debug(params);
+ assert.equal(params.md5, hash2);
+ assert.notEqual(params.zipMD5, hash2);
+ assert.equal(params.mtime, mtime2);
+ assert.equal(params.filename, filename2);
+ assert.equal(params.zipFilename, item2.key + ".zip");
+ assert.isTrue(parseInt(params.filesize) == params.filesize);
+ assert.equal(params.contentType, contentType2);
+ assert.equal(params.charset, charset2);
+
+ req.respond(
+ 200,
+ {
+ "Content-Type": "application/json"
+ },
+ JSON.stringify({
+ url: baseURL + "pretend-s3/2",
+ contentType: 'application/zip',
+ prefix: prefix2,
+ suffix: suffix2,
+ uploadKey: uploadKey2
+ })
+ );
+ }
+ // Upload single file to S3
+ else if (req.method == "POST" && req.url == baseURL + "pretend-s3/1") {
+ assert.equal(req.requestHeaders["Content-Type"], contentType1 + fixSinonBug);
+ assert.equal(req.requestBody.size, (new Blob([prefix1, File(file1), suffix1]).size));
+ req.respond(201, {}, "");
+ }
+ // Upload multi-file ZIP to S3
+ else if (req.method == "POST" && req.url == baseURL + "pretend-s3/2") {
+ assert.equal(req.requestHeaders["Content-Type"], "application/zip" + fixSinonBug);
+
+ // Verify uploaded ZIP file
+ let tmpZipPath = OS.Path.join(
+ Zotero.getTempDirectory().path,
+ Zotero.Utilities.randomString() + '.zip'
+ );
+
+ let deferred = Zotero.Promise.defer();
+ deferreds.push(deferred);
+ var reader = new FileReader();
+ reader.addEventListener("loadend", Zotero.Promise.coroutine(function* () {
+ try {
+
+ let file = yield OS.File.open(tmpZipPath, {
+ create: true
+ });
+
+ var contents = new Uint8Array(reader.result);
+ contents = contents.slice(prefix2.length, suffix2.length * -1);
+ yield file.write(contents);
+ yield file.close();
+
+ var zr = Components.classes["@mozilla.org/libjar/zip-reader;1"]
+ .createInstance(Components.interfaces.nsIZipReader);
+ zr.open(Zotero.File.pathToFile(tmpZipPath));
+ zr.test(null);
+ var entries = zr.findEntries('*');
+ var entryNames = [];
+ while (entries.hasMore()) {
+ entryNames.push(entries.getNext());
+ }
+ assert.equal(entryNames.length, 2);
+ assert.sameMembers(entryNames, ['index.html', 'img.gif']);
+ assert.equal(zr.getEntry('index.html').realSize, size2);
+ assert.equal(zr.getEntry('img.gif').realSize, 42);
+
+ deferred.resolve();
+ }
+ catch (e) {
+ deferred.reject(e);
+ }
+ }));
+ reader.readAsArrayBuffer(req.requestBody);
+
+ req.respond(201, {}, "");
+ }
+ // Register single-file upload
+ else if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item1.key}/file`
+ && req.requestBody.indexOf('upload=') != -1) {
+ assertAPIKey(req);
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/x-www-form-urlencoded" + fixSinonBug
+ );
+
+ let parts = req.requestBody.split('&');
+ let params = {};
+ for (let part of parts) {
+ let [key, val] = part.split('=');
+ params[key] = decodeURIComponent(val);
+ }
+ assert.equal(params.upload, uploadKey1);
+
+ req.respond(
+ 204,
+ {
+ "Last-Modified-Version": 10
+ },
+ ""
+ );
+ }
+ // Register multi-file upload
+ else if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item2.key}/file`
+ && req.requestBody.indexOf('upload=') != -1) {
+ assertAPIKey(req);
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/x-www-form-urlencoded" + fixSinonBug
+ );
+
+ let parts = req.requestBody.split('&');
+ let params = {};
+ for (let part of parts) {
+ let [key, val] = part.split('=');
+ params[key] = decodeURIComponent(val);
+ }
+ assert.equal(params.upload, uploadKey2);
+
+ req.respond(
+ 204,
+ {
+ "Last-Modified-Version": 15
+ },
+ ""
+ );
+ }
+ })
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "POST",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+
+ // TODO: One-step uploads
+ /*// https://github.com/cjohansen/Sinon.JS/issues/607
+ let fixSinonBug = ";charset=utf-8";
+ server.respond(function (req) {
+ if (req.method == "POST" && req.url == `${baseURL}users/1/items/${item.key}/file`) {
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/json" + fixSinonBug
+ );
+
+ let params = JSON.parse(req.requestBody);
+ assert.equal(params.md5, hash);
+ assert.equal(params.mtime, mtime);
+ assert.equal(params.filename, filename);
+ assert.equal(params.size, size);
+ assert.equal(params.contentType, contentType);
+
+ req.respond(
+ 200,
+ {
+ "Content-Type": "application/json"
+ },
+ JSON.stringify({
+ url: baseURL + "pretend-s3",
+ headers: {
+ "Content-Type": contentType,
+ "Content-MD5": hash,
+ //"Content-Length": params.size, process but don't return
+ //"x-amz-meta-"
+ },
+ uploadKey
+ })
+ );
+ }
+ else if (req.method == "PUT" && req.url == baseURL + "pretend-s3") {
+ assert.equal(req.requestHeaders["Content-Type"], contentType + fixSinonBug);
+ assert.instanceOf(req.requestBody, File);
+ req.respond(201, {}, "");
+ }
+ })*/
+ var result = yield engine.start();
+
+ yield Zotero.Promise.all(deferreds.map(d => d.promise));
+
+ assert.isTrue(result.localChanges);
+ assert.isTrue(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check local objects
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedModificationTime(item1.id)), mtime1);
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedHash(item1.id)), hash1);
+ assert.equal(item1.version, 10);
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedModificationTime(item2.id)), mtime2);
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedHash(item2.id)), hash2);
+ assert.equal(item2.version, 15);
+
+ // Check last sync time
+ assert.equal(Zotero.Libraries.userLibrary.lastStorageSync, newStorageSyncTime);
+ })
+
+ it("should update local info for file that already exists on the server", function* () {
+ var { engine, client, caller } = yield setup();
+
+ var file = getTestDataDirectory();
+ file.append('test.png');
+ var item = yield Zotero.Attachments.importFromFile({ file: file });
+ item.version = 5;
+ yield item.saveTx();
+ var json = yield item.toJSON();
+ yield Zotero.Sync.Data.Local.saveCacheObject('item', item.libraryID, json);
+
+ var mtime = yield item.attachmentModificationTime;
+ var hash = yield item.attachmentHash;
+ var path = item.getFilePath();
+ var filename = 'test.png';
+ var size = (yield OS.File.stat(path)).size;
+ var contentType = 'image/png';
+
+ var newVersion = 10;
+ setResponse({
+ method: "POST",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + (Math.round(new Date().getTime() / 1000) - 50000)
+ });
+ // https://github.com/cjohansen/Sinon.JS/issues/607
+ let fixSinonBug = ";charset=utf-8";
+ server.respond(function (req) {
+ // Get upload authorization for single file
+ if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item.key}/file`
+ && req.requestBody.indexOf('upload=') == -1) {
+ assertAPIKey(req);
+ assert.equal(req.requestHeaders["If-None-Match"], "*");
+ assert.equal(
+ req.requestHeaders["Content-Type"],
+ "application/x-www-form-urlencoded" + fixSinonBug
+ );
+
+ req.respond(
+ 200,
+ {
+ "Content-Type": "application/json",
+ "Last-Modified-Version": newVersion
+ },
+ JSON.stringify({
+ exists: 1,
+ })
+ );
+ }
+ })
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "POST",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+
+ // TODO: One-step uploads
+ var result = yield engine.start();
+
+ assert.isTrue(result.localChanges);
+ assert.isTrue(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+
+ // Check local objects
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedModificationTime(item.id)), mtime);
+ assert.equal((yield Zotero.Sync.Storage.Local.getSyncedHash(item.id)), hash);
+ assert.equal(item.version, newVersion);
+
+ // Check last sync time
+ assert.equal(Zotero.Libraries.userLibrary.lastStorageSync, newStorageSyncTime);
+ })
+ })
+
+ describe("#_processUploadFile()", function () {
+ it("should handle 412 with matching version and hash matching local file", function* () {
+ var { engine, client, caller } = yield setup();
+ var zfs = new Zotero.Sync.Storage.ZFS_Module({
+ apiClient: client
+ })
+
+ var filePath = OS.Path.join(getTestDataDirectory().path, 'test.png');
+ var item = yield Zotero.Attachments.importFromFile({ file: filePath });
+ item.version = 5;
+ item.synced = true;
+ yield item.saveTx();
+
+ var itemJSON = yield item.toResponseJSON();
+
+ // Set saved hash to a different value, which should be overwritten
+ //
+ // We're also testing cases where a hash isn't set for a file (e.g., if the
+ // storage directory was transferred, the mtime doesn't match, but the file was
+ // never downloaded), but there's no difference in behavior
+ var dbHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedHash(item.id, dbHash)
+ });
+
+ server.respond(function (req) {
+ if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item.key}/file`
+ && req.requestBody.indexOf('upload=') == -1
+ && req.requestHeaders["If-Match"] == dbHash) {
+ req.respond(
+ 412,
+ {
+ "Content-Type": "application/json",
+ "Last-Modified-Version": 5
+ },
+ "ETag does not match current version of file"
+ );
+ }
+ })
+ setResponse({
+ method: "GET",
+ url: `users/1/items?format=json&itemKey=${item.key}&includeTrashed=1`,
+ status: 200,
+ text: JSON.stringify([itemJSON])
+ });
+
+ var result = yield zfs._processUploadFile({
+ name: item.libraryKey
+ });
+ yield assert.eventually.equal(
+ Zotero.Sync.Storage.Local.getSyncedHash(item.id), itemJSON.data.md5
+ );
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+ assert.isFalse(result.fileSyncRequired);
+ })
+
+ it("should handle 412 with matching version and hash not matching local file", function* () {
+ var { engine, client, caller } = yield setup();
+ var zfs = new Zotero.Sync.Storage.ZFS_Module({
+ apiClient: client
+ })
+
+ var filePath = OS.Path.join(getTestDataDirectory().path, 'test.png');
+ var item = yield Zotero.Attachments.importFromFile({ file: filePath });
+ item.version = 5;
+ item.synced = true;
+ yield item.saveTx();
+
+ var fileHash = yield item.attachmentHash;
+ var itemJSON = yield item.toResponseJSON();
+ itemJSON.data.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaa'
+
+ server.respond(function (req) {
+ if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item.key}/file`
+ && req.requestBody.indexOf('upload=') == -1
+ && req.requestHeaders["If-None-Match"] == "*") {
+ req.respond(
+ 412,
+ {
+ "Content-Type": "application/json",
+ "Last-Modified-Version": 5
+ },
+ "If-None-Match: * set but file exists"
+ );
+ }
+ })
+ setResponse({
+ method: "GET",
+ url: `users/1/items?format=json&itemKey=${item.key}&includeTrashed=1`,
+ status: 200,
+ text: JSON.stringify([itemJSON])
+ });
+
+ var result = yield zfs._processUploadFile({
+ name: item.libraryKey
+ });
+ yield assert.eventually.isNull(Zotero.Sync.Storage.Local.getSyncedHash(item.id));
+ yield assert.eventually.equal(
+ Zotero.Sync.Storage.Local.getSyncState(item.id),
+ Zotero.Sync.Storage.SYNC_STATE_IN_CONFLICT
+ );
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isFalse(result.syncRequired);
+ assert.isTrue(result.fileSyncRequired);
+ })
+
+ it("should handle 412 with greater version", function* () {
+ var { engine, client, caller } = yield setup();
+ var zfs = new Zotero.Sync.Storage.ZFS_Module({
+ apiClient: client
+ })
+
+ var file = getTestDataDirectory();
+ file.append('test.png');
+ var item = yield Zotero.Attachments.importFromFile({ file });
+ item.version = 5;
+ item.synced = true;
+ yield item.saveTx();
+
+ server.respond(function (req) {
+ if (req.method == "POST"
+ && req.url == `${baseURL}users/1/items/${item.key}/file`
+ && req.requestBody.indexOf('upload=') == -1
+ && req.requestHeaders["If-None-Match"] == "*") {
+ req.respond(
+ 412,
+ {
+ "Content-Type": "application/json",
+ "Last-Modified-Version": 10
+ },
+ "If-None-Match: * set but file exists"
+ );
+ }
+ })
+
+ var result = yield zfs._processUploadFile({
+ name: item.libraryKey
+ });
+ assert.equal(item.version, 5);
+ assert.equal(item.synced, true);
+ assert.isFalse(result.localChanges);
+ assert.isFalse(result.remoteChanges);
+ assert.isTrue(result.syncRequired);
+ })
+ })
+ })
+})
diff --git a/test/tests/storageLocalTest.js b/test/tests/storageLocalTest.js
new file mode 100644
index 0000000000..31694feed5
--- /dev/null
+++ b/test/tests/storageLocalTest.js
@@ -0,0 +1,329 @@
+"use strict";
+
+describe("Zotero.Sync.Storage.Local", function () {
+ var win;
+
+ before(function* () {
+ win = yield loadBrowserWindow();
+ });
+ beforeEach(function* () {
+ yield resetDB({
+ thisArg: this
+ })
+ })
+ after(function () {
+ if (win) {
+ win.close();
+ }
+ });
+
+ describe("#checkForUpdatedFiles()", function () {
+ it("should flag modified file for upload and return it", function* () {
+ // Create attachment
+ let item = yield importFileAttachment('test.txt')
+ var hash = yield item.attachmentHash;
+ // Set file mtime to the past (without milliseconds, which aren't used on OS X)
+ var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) - 1000;
+ yield OS.File.setDates((yield item.getFilePathAsync()), null, mtime);
+
+ // Mark as synced, so it will be checked
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedHash(item.id, hash);
+ yield Zotero.Sync.Storage.Local.setSyncedModificationTime(item.id, mtime);
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC
+ );
+ });
+
+ // Update mtime and contents
+ var path = yield item.getFilePathAsync();
+ yield OS.File.setDates(path);
+ yield Zotero.File.putContentsAsync(path, Zotero.Utilities.randomString());
+
+ // File should be returned
+ var libraryID = Zotero.Libraries.userLibraryID;
+ var changed = yield Zotero.Sync.Storage.Local.checkForUpdatedFiles(libraryID);
+
+ yield item.eraseTx();
+
+ assert.equal(changed, true);
+ assert.equal(
+ (yield Zotero.Sync.Storage.Local.getSyncState(item.id)),
+ Zotero.Sync.Storage.SYNC_STATE_TO_UPLOAD
+ );
+ })
+
+ it("should skip a file if mod time hasn't changed", function* () {
+ // Create attachment
+ let item = yield importFileAttachment('test.txt')
+ var hash = yield item.attachmentHash;
+ var mtime = yield item.attachmentModificationTime;
+
+ // Mark as synced, so it will be checked
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedHash(item.id, hash);
+ yield Zotero.Sync.Storage.Local.setSyncedModificationTime(item.id, mtime);
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC
+ );
+ });
+
+ var libraryID = Zotero.Libraries.userLibraryID;
+ var changed = yield Zotero.Sync.Storage.Local.checkForUpdatedFiles(libraryID);
+ var syncState = yield Zotero.Sync.Storage.Local.getSyncState(item.id);
+
+ yield item.eraseTx();
+
+ assert.isFalse(changed);
+ assert.equal(syncState, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC);
+ })
+
+ it("should skip a file if mod time has changed but contents haven't", function* () {
+ // Create attachment
+ let item = yield importFileAttachment('test.txt')
+ var hash = yield item.attachmentHash;
+ // Set file mtime to the past (without milliseconds, which aren't used on OS X)
+ var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) - 1000;
+ yield OS.File.setDates((yield item.getFilePathAsync()), null, mtime);
+
+ // Mark as synced, so it will be checked
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedHash(item.id, hash);
+ yield Zotero.Sync.Storage.Local.setSyncedModificationTime(item.id, mtime);
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC
+ );
+ });
+
+ // Update mtime, but not contents
+ var path = yield item.getFilePathAsync();
+ yield OS.File.setDates(path);
+
+ var libraryID = Zotero.Libraries.userLibraryID;
+ var changed = yield Zotero.Sync.Storage.Local.checkForUpdatedFiles(libraryID);
+ var syncState = yield Zotero.Sync.Storage.Local.getSyncState(item.id);
+ var syncedModTime = yield Zotero.Sync.Storage.Local.getSyncedModificationTime(item.id);
+ var newModTime = yield item.attachmentModificationTime;
+
+ yield item.eraseTx();
+
+ assert.isFalse(changed);
+ assert.equal(syncState, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC);
+ assert.equal(syncedModTime, newModTime);
+ })
+ })
+
+ describe("#processDownload()", function () {
+ var file1Name = 'index.html';
+ var file1Contents = 'Test';
+ var file2Name = 'test.txt';
+ var file2Contents = 'Test';
+
+ var createZIP = Zotero.Promise.coroutine(function* (zipFile) {
+ var tmpDir = Zotero.getTempDirectory().path;
+ var zipDir = OS.Path.join(tmpDir, Zotero.Utilities.randomString());
+ yield OS.File.makeDir(zipDir);
+
+ yield Zotero.File.putContentsAsync(OS.Path.join(zipDir, file1Name), file1Contents);
+ yield Zotero.File.putContentsAsync(OS.Path.join(zipDir, file2Name), file2Contents);
+
+ yield Zotero.File.zipDirectory(zipDir, zipFile);
+ yield OS.File.removeDir(zipDir);
+ });
+
+ it("should download and extract a ZIP file into the attachment directory", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+ var parentItem = yield createDataObject('item');
+ var key = Zotero.DataObjectUtilities.generateKey();
+
+ var tmpDir = Zotero.getTempDirectory().path;
+ var zipFile = OS.Path.join(tmpDir, key + '.tmp');
+ yield createZIP(zipFile);
+
+ var md5 = Zotero.Utilities.Internal.md5(Zotero.File.pathToFile(zipFile));
+ var mtime = 1445667239000;
+
+ var json = {
+ key,
+ version: 10,
+ itemType: 'attachment',
+ linkMode: 'imported_url',
+ url: 'https://example.com',
+ filename: file1Name,
+ contentType: 'text/html',
+ charset: 'utf-8',
+ md5,
+ mtime
+ };
+ yield Zotero.Sync.Data.Local.saveCacheObjects(
+ 'item', libraryID, [json]
+ );
+ yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
+ libraryID, 'item', { stopOnError: true }
+ );
+ var item = yield Zotero.Items.getByLibraryAndKeyAsync(libraryID, key);
+ yield Zotero.Sync.Storage.Local.processDownload({
+ item,
+ md5,
+ mtime,
+ compressed: true
+ });
+ yield OS.File.remove(zipFile);
+
+ yield assert.eventually.equal(
+ item.attachmentHash, Zotero.Utilities.Internal.md5(file1Contents)
+ );
+ yield assert.eventually.equal(item.attachmentModificationTime, mtime);
+ })
+ })
+
+ describe("#_deleteExistingAttachmentFiles()", function () {
+ it("should delete all files", function* () {
+ var item = yield importFileAttachment('test.html');
+ var path = OS.Path.dirname(item.getFilePath());
+ var files = ['a', 'b', 'c', 'd'];
+ for (let file of files) {
+ yield Zotero.File.putContentsAsync(OS.Path.join(path, file), file);
+ }
+ yield Zotero.Sync.Storage.Local._deleteExistingAttachmentFiles(item);
+ for (let file of files) {
+ assert.isFalse(
+ (yield OS.File.exists(OS.Path.join(path, file))),
+ `File '${file}' doesn't exist`
+ );
+ }
+ })
+ })
+
+ describe("#getConflicts()", function () {
+ it("should return an array of objects for attachments in conflict", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+
+ var item1 = yield importFileAttachment('test.png');
+ item1.version = 10;
+ yield item1.saveTx();
+ var item2 = yield importFileAttachment('test.txt');
+ var item3 = yield importFileAttachment('test.html');
+ item3.version = 11;
+ yield item3.saveTx();
+
+ var json1 = yield item1.toJSON();
+ var json3 = yield item3.toJSON();
+ // Change remote mtimes
+ // Round to nearest second because OS X doesn't support ms resolution
+ var now = Math.round(new Date().getTime() / 1000) * 1000;
+ json1.mtime = now - 10000;
+ json3.mtime = now - 20000;
+ yield Zotero.Sync.Data.Local.saveCacheObjects('item', libraryID, [json1, json3]);
+
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item1.id, Zotero.Sync.Storage.SYNC_STATE_IN_CONFLICT
+ );
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item3.id, Zotero.Sync.Storage.SYNC_STATE_IN_CONFLICT
+ );
+
+ var conflicts = yield Zotero.Sync.Storage.Local.getConflicts(libraryID);
+ assert.lengthOf(conflicts, 2);
+
+ var item1Conflict = conflicts.find(x => x.left.key == item1.key);
+ assert.equal(
+ item1Conflict.left.dateModified,
+ Zotero.Date.dateToISO(new Date(yield item1.attachmentModificationTime))
+ );
+ assert.equal(
+ item1Conflict.right.dateModified,
+ Zotero.Date.dateToISO(new Date(json1.mtime))
+ );
+
+ var item3Conflict = conflicts.find(x => x.left.key == item3.key);
+ assert.equal(
+ item3Conflict.left.dateModified,
+ Zotero.Date.dateToISO(new Date(yield item3.attachmentModificationTime))
+ );
+ assert.equal(
+ item3Conflict.right.dateModified,
+ Zotero.Date.dateToISO(new Date(json3.mtime))
+ );
+ })
+ })
+
+ describe("#resolveConflicts()", function () {
+ it("should show the conflict resolution window on attachment conflicts", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+
+ var item1 = yield importFileAttachment('test.png');
+ item1.version = 10;
+ yield item1.saveTx();
+ var item2 = yield importFileAttachment('test.txt');
+ var item3 = yield importFileAttachment('test.html');
+ item3.version = 11;
+ yield item3.saveTx();
+
+ var json1 = yield item1.toJSON();
+ var json3 = yield item3.toJSON();
+ // Change remote mtimes
+ json1.mtime = new Date().getTime() + 10000;
+ json3.mtime = new Date().getTime() - 10000;
+ yield Zotero.Sync.Data.Local.saveCacheObjects('item', libraryID, [json1, json3]);
+
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item1.id, Zotero.Sync.Storage.SYNC_STATE_IN_CONFLICT
+ );
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item3.id, Zotero.Sync.Storage.SYNC_STATE_IN_CONFLICT
+ );
+
+ var promise = waitForWindow('chrome://zotero/content/merge.xul', function (dialog) {
+ var doc = dialog.document;
+ var wizard = doc.documentElement;
+ var mergeGroup = wizard.getElementsByTagName('zoteromergegroup')[0];
+
+ // 1 (remote)
+ // Later remote version should be selected
+ assert.equal(mergeGroup.rightpane.getAttribute('selected'), 'true');
+
+ // Check checkbox text
+ assert.equal(
+ doc.getElementById('resolve-all').label,
+ Zotero.getString('sync.conflict.resolveAllRemote')
+ );
+
+ // Select local object
+ mergeGroup.leftpane.click();
+ assert.equal(mergeGroup.leftpane.getAttribute('selected'), 'true');
+
+ wizard.getButton('next').click();
+
+ // 2 (local)
+ // Later local version should be selected
+ assert.equal(mergeGroup.leftpane.getAttribute('selected'), 'true');
+ // Select remote object
+ mergeGroup.rightpane.click();
+ assert.equal(mergeGroup.rightpane.getAttribute('selected'), 'true');
+
+ if (Zotero.isMac) {
+ assert.isTrue(wizard.getButton('next').hidden);
+ assert.isFalse(wizard.getButton('finish').hidden);
+ }
+ else {
+ // TODO
+ }
+ wizard.getButton('finish').click();
+ })
+ yield Zotero.Sync.Storage.Local.resolveConflicts(libraryID);
+ yield promise;
+
+ yield assert.eventually.equal(
+ Zotero.Sync.Storage.Local.getSyncState(item1.id),
+ Zotero.Sync.Storage.SYNC_STATE_FORCE_UPLOAD
+ );
+ yield assert.eventually.equal(
+ Zotero.Sync.Storage.Local.getSyncState(item3.id),
+ Zotero.Sync.Storage.SYNC_STATE_FORCE_DOWNLOAD
+ );
+ })
+ })
+
+
+})
diff --git a/test/tests/storageRequestTest.js b/test/tests/storageRequestTest.js
new file mode 100644
index 0000000000..9ab0b0c2dc
--- /dev/null
+++ b/test/tests/storageRequestTest.js
@@ -0,0 +1,22 @@
+"use strict";
+
+describe("Zotero.Sync.Storage.Request", function () {
+ describe("#run()", function () {
+ it("should run a request and wait for it to complete", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+ var count = 0;
+ var request = new Zotero.Sync.Storage.Request({
+ type: 'download',
+ libraryID,
+ name: "1/AAAAAAAA",
+ onStart: Zotero.Promise.coroutine(function* () {
+ yield Zotero.Promise.delay(25);
+ count++;
+ return new Zotero.Sync.Storage.Result;
+ })
+ });
+ var results = yield request.start();
+ assert.equal(count, 1);
+ })
+ })
+})
diff --git a/test/tests/syncEngineTest.js b/test/tests/syncEngineTest.js
index 09d602aedd..80ac189700 100644
--- a/test/tests/syncEngineTest.js
+++ b/test/tests/syncEngineTest.js
@@ -19,28 +19,20 @@ describe("Zotero.Sync.Data.Engine", function () {
var caller = new ConcurrentCaller(1);
caller.setLogger(msg => Zotero.debug(msg));
caller.stopOnError = true;
- caller.onError = function (e) {
- Zotero.logError(e);
- if (options.onError) {
- options.onError(e);
- }
- if (e.fatal) {
- caller.stop();
- throw e;
- }
- };
+ Components.utils.import("resource://zotero/config.js");
var client = new Zotero.Sync.APIClient({
- baseURL: baseURL,
+ baseURL,
apiVersion: options.apiVersion || ZOTERO_CONFIG.API_VERSION,
- apiKey: apiKey,
- concurrentCaller: caller,
+ apiKey,
+ caller,
background: options.background || true
});
var engine = new Zotero.Sync.Data.Engine({
apiClient: client,
- libraryID: options.libraryID || Zotero.Libraries.userLibraryID
+ libraryID: options.libraryID || Zotero.Libraries.userLibraryID,
+ stopOnError: true
});
return { engine, client, caller };
diff --git a/test/tests/syncLocalTest.js b/test/tests/syncLocalTest.js
index 22fec6806d..cecee39aab 100644
--- a/test/tests/syncLocalTest.js
+++ b/test/tests/syncLocalTest.js
@@ -4,7 +4,7 @@ describe("Zotero.Sync.Data.Local", function() {
describe("#processSyncCacheForObjectType()", function () {
var types = Zotero.DataObjectUtilities.getTypes();
- it("should update local version number if remote version is identical", function* () {
+ it("should update local version number and mark as synced if remote version is identical", function* () {
var libraryID = Zotero.Libraries.userLibraryID;
for (let type of types) {
@@ -24,11 +24,167 @@ describe("Zotero.Sync.Data.Local", function() {
yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
libraryID, type, { stopOnError: true }
);
- assert.equal(
- objectsClass.getByLibraryAndKey(libraryID, obj.key).version, 10
- );
+ let localObj = objectsClass.getByLibraryAndKey(libraryID, obj.key);
+ assert.equal(localObj.version, 10);
+ assert.isTrue(localObj.synced);
}
})
+
+ it("should keep local item changes while applying non-conflicting remote changes", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+
+ var type = 'item';
+ let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
+ let obj = yield createDataObject(type, { version: 5 });
+ let data = yield obj.toJSON();
+ yield Zotero.Sync.Data.Local.saveCacheObjects(
+ type, libraryID, [data]
+ );
+
+ // Change local title
+ yield modifyDataObject(obj)
+ var changedTitle = obj.getField('title');
+
+ // Save remote version to cache without title but with changed place
+ data.key = obj.key;
+ data.version = 10;
+ var changedPlace = data.place = 'New York';
+ let json = {
+ key: obj.key,
+ version: 10,
+ data: data
+ };
+ yield Zotero.Sync.Data.Local.saveCacheObjects(
+ type, libraryID, [json]
+ );
+
+ yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
+ libraryID, type, { stopOnError: true }
+ );
+ assert.equal(obj.version, 10);
+ assert.equal(obj.getField('title'), changedTitle);
+ assert.equal(obj.getField('place'), changedPlace);
+ })
+
+ it("should mark new attachment items for download", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+ Zotero.Sync.Storage.Local.setModeForLibrary(libraryID, 'zfs');
+
+ var key = Zotero.DataObjectUtilities.generateKey();
+ var version = 10;
+ var json = {
+ key,
+ version,
+ data: {
+ key,
+ version,
+ itemType: 'attachment',
+ linkMode: 'imported_file',
+ md5: '57f8a4fda823187b91e1191487b87fe6',
+ mtime: 1442261130615
+ }
+ };
+
+ yield Zotero.Sync.Data.Local.saveCacheObjects(
+ 'item', Zotero.Libraries.userLibraryID, [json]
+ );
+ yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
+ libraryID, 'item', { stopOnError: true }
+ );
+ var id = Zotero.Items.getIDFromLibraryAndKey(libraryID, key);
+ assert.equal(
+ (yield Zotero.Sync.Storage.Local.getSyncState(id)),
+ Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+ })
+
+ it("should mark updated attachment items for download", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+ Zotero.Sync.Storage.Local.setModeForLibrary(libraryID, 'zfs');
+
+ var item = yield importFileAttachment('test.png');
+ item.version = 5;
+ item.synced = true;
+ yield item.saveTx();
+
+ // Set file as synced
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedModificationTime(
+ item.id, (yield item.attachmentModificationTime)
+ );
+ yield Zotero.Sync.Storage.Local.setSyncedHash(
+ item.id, (yield item.attachmentHash)
+ );
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC
+ );
+ });
+
+ // Simulate download of version with updated attachment
+ var json = yield item.toResponseJSON();
+ json.version = 10;
+ json.data.version = 10;
+ json.data.md5 = '57f8a4fda823187b91e1191487b87fe6';
+ json.data.mtime = new Date().getTime() + 10000;
+ yield Zotero.Sync.Data.Local.saveCacheObjects(
+ 'item', Zotero.Libraries.userLibraryID, [json]
+ );
+
+ yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
+ libraryID, 'item', { stopOnError: true }
+ );
+
+ assert.equal(
+ (yield Zotero.Sync.Storage.Local.getSyncState(item.id)),
+ Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+ })
+
+ it("should ignore attachment metadata when resolving metadata conflict", function* () {
+ var libraryID = Zotero.Libraries.userLibraryID;
+ Zotero.Sync.Storage.Local.setModeForLibrary(libraryID, 'zfs');
+
+ var item = yield importFileAttachment('test.png');
+ item.version = 5;
+ yield item.saveTx();
+ var json = yield item.toResponseJSON();
+ yield Zotero.Sync.Data.Local.saveCacheObjects('item', libraryID, [json]);
+
+ // Set file as synced
+ yield Zotero.DB.executeTransaction(function* () {
+ yield Zotero.Sync.Storage.Local.setSyncedModificationTime(
+ item.id, (yield item.attachmentModificationTime)
+ );
+ yield Zotero.Sync.Storage.Local.setSyncedHash(
+ item.id, (yield item.attachmentHash)
+ );
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_IN_SYNC
+ );
+ });
+
+ // Modify title locally, leaving item unsynced
+ var newTitle = Zotero.Utilities.randomString();
+ item.setField('title', newTitle);
+ yield item.saveTx();
+
+ // Simulate download of version with original title but updated attachment
+ json.version = 10;
+ json.data.version = 10;
+ json.data.md5 = '57f8a4fda823187b91e1191487b87fe6';
+ json.data.mtime = new Date().getTime() + 10000;
+ yield Zotero.Sync.Data.Local.saveCacheObjects('item', libraryID, [json]);
+
+ yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
+ libraryID, 'item', { stopOnError: true }
+ );
+
+ assert.equal(item.getField('title'), newTitle);
+ assert.equal(
+ (yield Zotero.Sync.Storage.Local.getSyncState(item.id)),
+ Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+ })
})
describe("Conflict Resolution", function () {
@@ -232,7 +388,10 @@ describe("Zotero.Sync.Data.Local", function() {
jsonData.title = Zotero.Utilities.randomString();
yield Zotero.Sync.Data.Local.saveCacheObjects(type, libraryID, [json]);
+ var windowOpened = false;
waitForWindow('chrome://zotero/content/merge.xul', function (dialog) {
+ windowOpened = true;
+
var doc = dialog.document;
var wizard = doc.documentElement;
var mergeGroup = wizard.getElementsByTagName('zoteromergegroup')[0];
@@ -240,12 +399,14 @@ describe("Zotero.Sync.Data.Local", function() {
// Remote version should be selected by default
assert.equal(mergeGroup.rightpane.getAttribute('selected'), 'true');
assert.ok(mergeGroup.leftpane.pane.onclick);
+ // Select local deleted version
mergeGroup.leftpane.pane.click();
wizard.getButton('finish').click();
})
yield Zotero.Sync.Data.Local.processSyncCacheForObjectType(
libraryID, type, { stopOnError: true }
);
+ assert.isTrue(windowOpened);
obj = objectsClass.getByLibraryAndKey(libraryID, key);
assert.isFalse(obj);
@@ -825,15 +986,28 @@ describe("Zotero.Sync.Data.Local", function() {
assert.sameDeepMembers(
result.conflicts,
[
- {
- field: "place",
- op: "delete"
- },
- {
- field: "date",
- op: "add",
- value: "2015-05-15"
- }
+ [
+ {
+ field: "place",
+ op: "add",
+ value: "Place"
+ },
+ {
+ field: "place",
+ op: "delete"
+ }
+ ],
+ [
+ {
+ field: "date",
+ op: "delete"
+ },
+ {
+ field: "date",
+ op: "add",
+ value: "2015-05-15"
+ }
+ ]
]
);
})
@@ -1296,4 +1470,68 @@ describe("Zotero.Sync.Data.Local", function() {
})
})
})
+
+
+ describe("#reconcileChangesWithoutCache()", function () {
+ it("should return conflict for conflicting fields", function () {
+ var json1 = {
+ key: "AAAAAAAA",
+ version: 1234,
+ title: "Title 1",
+ pages: 10,
+ dateModified: "2015-05-14 14:12:34"
+ };
+ var json2 = {
+ key: "AAAAAAAA",
+ version: 1235,
+ title: "Title 2",
+ place: "New York",
+ dateModified: "2015-05-14 13:45:12"
+ };
+ var ignoreFields = ['dateAdded', 'dateModified'];
+ var result = Zotero.Sync.Data.Local._reconcileChangesWithoutCache(
+ 'item', json1, json2, ignoreFields
+ );
+ assert.lengthOf(result.changes, 0);
+ assert.sameDeepMembers(
+ result.conflicts,
+ [
+ [
+ {
+ field: "title",
+ op: "add",
+ value: "Title 1"
+ },
+ {
+ field: "title",
+ op: "add",
+ value: "Title 2"
+ }
+ ],
+ [
+ {
+ field: "pages",
+ op: "add",
+ value: 10
+ },
+ {
+ field: "pages",
+ op: "delete"
+ }
+ ],
+ [
+ {
+ field: "place",
+ op: "delete"
+ },
+ {
+ field: "place",
+ op: "add",
+ value: "New York"
+ }
+ ]
+ ]
+ );
+ })
+ })
})
diff --git a/test/tests/syncRunnerTest.js b/test/tests/syncRunnerTest.js
index 6f505afad2..4e725f6180 100644
--- a/test/tests/syncRunnerTest.js
+++ b/test/tests/syncRunnerTest.js
@@ -5,7 +5,7 @@ describe("Zotero.Sync.Runner", function () {
var apiKey = Zotero.Utilities.randomString(24);
var baseURL = "http://local.zotero/";
- var userLibraryID, publicationsLibraryID, runner, caller, server, client, stub, spy;
+ var userLibraryID, publicationsLibraryID, runner, caller, server, stub, spy;
var responses = {
keyInfo: {
@@ -129,15 +129,7 @@ describe("Zotero.Sync.Runner", function () {
}
};
- var client = new Zotero.Sync.APIClient({
- baseURL: baseURL,
- apiVersion: options.apiVersion || ZOTERO_CONFIG.API_VERSION,
- apiKey: apiKey,
- concurrentCaller: caller,
- background: options.background || true
- });
-
- return { runner, caller, client };
+ return { runner, caller };
})
function setResponse(response) {
@@ -160,7 +152,7 @@ describe("Zotero.Sync.Runner", function () {
server = sinon.fakeServer.create();
server.autoRespond = true;
- ({ runner, caller, client } = yield setup());
+ ({ runner, caller } = yield setup());
yield Zotero.Users.setCurrentUserID(1);
yield Zotero.Users.setCurrentUsername("A");
@@ -180,7 +172,7 @@ describe("Zotero.Sync.Runner", function () {
it("should check key access", function* () {
spy = sinon.spy(runner, "checkUser");
setResponse('keyInfo.fullAccess');
- var json = yield runner.checkAccess(client);
+ var json = yield runner.checkAccess(runner.getAPIClient());
sinon.assert.calledWith(spy, 1, "Username");
var compare = {};
Object.assign(compare, responses.keyInfo.fullAccess.json);
@@ -216,7 +208,7 @@ describe("Zotero.Sync.Runner", function () {
setResponse('userGroups.groupVersions');
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 4);
assert.sameMembers(
@@ -240,19 +232,25 @@ describe("Zotero.Sync.Runner", function () {
setResponse('userGroups.groupVersions');
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json, [userLibraryID]
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json, [userLibraryID]
);
assert.lengthOf(libraries, 1);
assert.sameMembers(libraries, [userLibraryID]);
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json, [userLibraryID, publicationsLibraryID]
+ runner.getAPIClient(),
+ false,
+ responses.keyInfo.fullAccess.json,
+ [userLibraryID, publicationsLibraryID]
);
assert.lengthOf(libraries, 2);
assert.sameMembers(libraries, [userLibraryID, publicationsLibraryID]);
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json, [group1.libraryID]
+ runner.getAPIClient(),
+ false,
+ responses.keyInfo.fullAccess.json,
+ [group1.libraryID]
);
assert.lengthOf(libraries, 1);
assert.sameMembers(libraries, [group1.libraryID]);
@@ -277,7 +275,7 @@ describe("Zotero.Sync.Runner", function () {
setResponse('groups.ownerGroup');
setResponse('groups.memberGroup');
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 4);
assert.sameMembers(
@@ -318,7 +316,7 @@ describe("Zotero.Sync.Runner", function () {
setResponse('groups.ownerGroup');
setResponse('groups.memberGroup');
var libraries = yield runner.checkLibraries(
- client,
+ runner.getAPIClient(),
false,
responses.keyInfo.fullAccess.json,
[group1.libraryID, group2.libraryID]
@@ -339,7 +337,7 @@ describe("Zotero.Sync.Runner", function () {
setResponse('groups.ownerGroup');
setResponse('groups.memberGroup');
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 4);
var groupData1 = responses.groups.ownerGroup;
@@ -370,7 +368,7 @@ describe("Zotero.Sync.Runner", function () {
assert.include(text, group1.name);
});
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 3);
assert.sameMembers(libraries, [userLibraryID, publicationsLibraryID, group2.libraryID]);
@@ -388,7 +386,7 @@ describe("Zotero.Sync.Runner", function () {
assert.include(text, group.name);
}, "extra1");
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 3);
assert.sameMembers(libraries, [userLibraryID, publicationsLibraryID, group.libraryID]);
@@ -405,7 +403,7 @@ describe("Zotero.Sync.Runner", function () {
assert.include(text, group.name);
}, "cancel");
var libraries = yield runner.checkLibraries(
- client, false, responses.keyInfo.fullAccess.json
+ runner.getAPIClient(), false, responses.keyInfo.fullAccess.json
);
assert.lengthOf(libraries, 0);
assert.isTrue(Zotero.Groups.exists(groupData.json.id));
@@ -656,6 +654,11 @@ describe("Zotero.Sync.Runner", function () {
Zotero.Libraries.getVersion(Zotero.Groups.getLibraryIDFromGroupID(2694172)),
20
);
+
+ // Last sync time should be within the last second
+ var lastSyncTime = Zotero.Sync.Data.Local.getLastSyncTime();
+ assert.isAbove(lastSyncTime, new Date().getTime() - 1000);
+ assert.isBelow(lastSyncTime, new Date().getTime());
})
})
})
diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js
index 8d93b4e8d9..26968d0e4c 100644
--- a/test/tests/zoteroPaneTest.js
+++ b/test/tests/zoteroPaneTest.js
@@ -1,3 +1,5 @@
+"use strict";
+
describe("ZoteroPane", function() {
var win, doc, zp;
@@ -90,4 +92,96 @@ describe("ZoteroPane", function() {
);
})
})
+
+ describe("#viewAttachment", function () {
+ Components.utils.import("resource://zotero-unit/httpd.js");
+ var apiKey = Zotero.Utilities.randomString(24);
+ var port = 16213;
+ var baseURL = `http://localhost:${port}/`;
+ var server;
+ var responses = {};
+
+ var setup = Zotero.Promise.coroutine(function* (options = {}) {
+ server = sinon.fakeServer.create();
+ server.autoRespond = true;
+ });
+
+ function setResponse(response) {
+ setHTTPResponse(server, baseURL, response, responses);
+ }
+
+ before(function () {
+ Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
+
+ Zotero.Sync.Runner.apiKey = apiKey;
+ Zotero.Sync.Runner.baseURL = baseURL;
+ })
+ beforeEach(function* () {
+ this.httpd = new HttpServer();
+ this.httpd.start(port);
+
+ yield Zotero.Users.setCurrentUserID(1);
+ yield Zotero.Users.setCurrentUsername("testuser");
+ })
+ afterEach(function* () {
+ var defer = new Zotero.Promise.defer();
+ this.httpd.stop(() => defer.resolve());
+ yield defer.promise;
+ })
+
+ it("should download an attachment on-demand", function* () {
+ yield setup();
+ Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
+
+ var item = new Zotero.Item("attachment");
+ item.attachmentLinkMode = 'imported_file';
+ item.attachmentPath = 'storage:test.txt';
+ // TODO: Test binary data
+ var text = Zotero.Utilities.randomString();
+ yield item.saveTx();
+ yield Zotero.Sync.Storage.Local.setSyncState(
+ item.id, Zotero.Sync.Storage.SYNC_STATE_TO_DOWNLOAD
+ );
+
+ var mtime = "1441252524000";
+ var md5 = Zotero.Utilities.Internal.md5(text)
+
+ var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
+ setResponse({
+ method: "GET",
+ url: "users/1/laststoragesync",
+ status: 200,
+ text: "" + newStorageSyncTime
+ });
+ var s3Path = `pretend-s3/${item.key}`;
+ this.httpd.registerPathHandler(
+ `/users/1/items/${item.key}/file`,
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 302, "Found");
+ response.setHeader("Zotero-File-Modification-Time", mtime, false);
+ response.setHeader("Zotero-File-MD5", md5, false);
+ response.setHeader("Zotero-File-Compressed", "No", false);
+ response.setHeader("Location", baseURL + s3Path, false);
+ }
+ }
+ );
+ this.httpd.registerPathHandler(
+ "/" + s3Path,
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 200, "OK");
+ response.write(text);
+ }
+ }
+ );
+
+ yield zp.viewAttachment(item.id);
+
+ assert.equal((yield item.attachmentHash), md5);
+ assert.equal((yield item.attachmentModificationTime), mtime);
+ var path = yield item.getFilePathAsync();
+ assert.equal((yield Zotero.File.getContentsAsync(path)), text);
+ })
+ })
})