2013-11-28 11:18:17 +04:00
|
|
|
// Serves a file with a given mime type and size at an optionally given rate.
|
2013-11-28 11:18:12 +04:00
|
|
|
|
|
|
|
function getQuery(request) {
|
|
|
|
var query = {};
|
|
|
|
request.queryString.split('&').forEach(function (val) {
|
|
|
|
var [name, value] = val.split('=');
|
|
|
|
query[name] = unescape(value);
|
|
|
|
});
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:18:17 +04:00
|
|
|
function handleResponse() {
|
|
|
|
// Is this a rate limited response?
|
|
|
|
if (this.state.rate > 0) {
|
|
|
|
// Calculate how many bytes we have left to send.
|
|
|
|
var bytesToWrite = this.state.totalBytes - this.state.sentBytes;
|
|
|
|
|
|
|
|
// Do we have any bytes left to send? If not we'll just fall thru and
|
|
|
|
// cancel our repeating timer and finalize the response.
|
|
|
|
if (bytesToWrite > 0) {
|
|
|
|
// Figure out how many bytes to send, based on the rate limit.
|
|
|
|
bytesToWrite =
|
|
|
|
(bytesToWrite > this.state.rate) ? this.state.rate : bytesToWrite;
|
|
|
|
|
|
|
|
for (let i = 0; i < bytesToWrite; i++) {
|
2014-01-24 23:53:05 +04:00
|
|
|
try {
|
|
|
|
this.response.bodyOutputStream.write("0", 1);
|
|
|
|
} catch (e) {
|
|
|
|
// Connection was closed by client.
|
|
|
|
if (e == Components.results.NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
// There's no harm in calling this multiple times.
|
|
|
|
this.response.finish();
|
|
|
|
|
|
|
|
// It's possible that our timer wasn't cancelled in time
|
|
|
|
// and we'll be called again.
|
|
|
|
if (this.timer) {
|
|
|
|
this.timer.cancel();
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-11-28 11:18:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the number of bytes we've sent to the client.
|
|
|
|
this.state.sentBytes += bytesToWrite;
|
|
|
|
|
|
|
|
// Wait until the next call to do anything else.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Not rate limited, write it all out.
|
|
|
|
for (let i = 0; i < this.state.totalBytes; i++) {
|
|
|
|
this.response.write("0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize the response.
|
|
|
|
this.response.finish();
|
|
|
|
|
|
|
|
// All done sending, go ahead and cancel our repeating timer.
|
2014-01-24 23:53:05 +04:00
|
|
|
this.timer.cancel();
|
|
|
|
|
|
|
|
// Clear the timer.
|
|
|
|
this.timer = null;
|
2013-11-28 11:18:17 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 11:18:12 +04:00
|
|
|
function handleRequest(request, response) {
|
|
|
|
var query = getQuery(request);
|
|
|
|
|
2014-01-24 23:53:05 +04:00
|
|
|
// sending at a specific rate requires our response to be asynchronous so
|
|
|
|
// we handle all requests asynchronously. See handleResponse().
|
|
|
|
response.processAsync();
|
|
|
|
|
2014-01-14 02:14:34 +04:00
|
|
|
// Default status when responding.
|
|
|
|
var version = "1.1";
|
|
|
|
var statusCode = 200;
|
|
|
|
var description = "OK";
|
|
|
|
|
2013-11-28 11:18:17 +04:00
|
|
|
// Default values for content type, size and rate.
|
2013-11-28 11:18:12 +04:00
|
|
|
var contentType = "text/plain";
|
2014-01-14 02:14:34 +04:00
|
|
|
var contentRange = null;
|
2013-11-28 11:18:12 +04:00
|
|
|
var size = 1024;
|
2013-11-28 11:18:17 +04:00
|
|
|
var rate = 0;
|
2013-11-28 11:18:12 +04:00
|
|
|
|
2013-11-28 11:18:17 +04:00
|
|
|
// optional content type to be used by our response.
|
2013-11-28 11:18:12 +04:00
|
|
|
if ("contentType" in query) {
|
|
|
|
contentType = query["contentType"];
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:18:17 +04:00
|
|
|
// optional size (in bytes) for generated file.
|
2013-11-28 11:18:12 +04:00
|
|
|
if ("size" in query) {
|
2013-11-28 11:18:17 +04:00
|
|
|
size = parseInt(query["size"]);
|
2013-11-28 11:18:12 +04:00
|
|
|
}
|
|
|
|
|
2014-01-14 02:14:34 +04:00
|
|
|
// optional range request check.
|
|
|
|
if (request.hasHeader("range")) {
|
|
|
|
version = "1.1";
|
|
|
|
statusCode = 206;
|
|
|
|
description = "Partial Content";
|
|
|
|
|
|
|
|
// We'll only support simple range byte style requests.
|
|
|
|
var [offset, total] = request.getHeader("range").slice("bytes=".length).split("-");
|
|
|
|
// Enforce valid Number values.
|
|
|
|
offset = parseInt(offset);
|
|
|
|
offset = isNaN(offset) ? 0 : offset;
|
|
|
|
// Same.
|
|
|
|
total = parseInt(total);
|
|
|
|
total = isNaN(total) ? 0 : total;
|
|
|
|
|
|
|
|
// We'll need to original total size as part of the Content-Range header
|
|
|
|
// value in our response.
|
|
|
|
var originalSize = size;
|
|
|
|
|
|
|
|
// If we have a total size requested, we must make sure to send that number
|
|
|
|
// of bytes only (minus the start offset).
|
|
|
|
if (total && total < size) {
|
|
|
|
size = total - offset;
|
|
|
|
} else if (offset) {
|
|
|
|
// Looks like we just have a byte offset to deal with.
|
|
|
|
size = size - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We specifically need to add a Content-Range header to all responses for
|
|
|
|
// requests that include a range request header.
|
|
|
|
contentRange = "bytes " + offset + "-" + (size - 1) + "/" + originalSize;
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:18:17 +04:00
|
|
|
// optional rate (in bytes/s) at which to send the file.
|
|
|
|
if ("rate" in query) {
|
|
|
|
rate = parseInt(query["rate"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The context for the responseHandler.
|
|
|
|
var context = {
|
|
|
|
response: response,
|
|
|
|
state: {
|
|
|
|
contentType: contentType,
|
|
|
|
totalBytes: size,
|
|
|
|
sentBytes: 0,
|
|
|
|
rate: rate
|
2014-01-24 23:53:05 +04:00
|
|
|
},
|
|
|
|
timer: null
|
2013-11-28 11:18:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// The notify implementation for the timer.
|
|
|
|
context.notify = handleResponse.bind(context);
|
|
|
|
|
2014-01-24 23:53:05 +04:00
|
|
|
context.timer =
|
2013-11-28 11:18:17 +04:00
|
|
|
Components.classes["@mozilla.org/timer;1"]
|
|
|
|
.createInstance(Components.interfaces.nsITimer);
|
|
|
|
|
|
|
|
// generate the content.
|
2014-01-14 02:14:34 +04:00
|
|
|
response.setStatusLine(version, statusCode, description);
|
2013-11-28 11:18:12 +04:00
|
|
|
response.setHeader("Content-Type", contentType, false);
|
2014-01-14 02:14:34 +04:00
|
|
|
if (contentRange) {
|
|
|
|
response.setHeader("Content-Range", contentRange, false);
|
|
|
|
}
|
2013-11-28 11:18:17 +04:00
|
|
|
response.setHeader("Content-Length", size.toString(), false);
|
|
|
|
|
|
|
|
// initialize the timer and start writing out the response.
|
2014-01-24 23:53:05 +04:00
|
|
|
context.timer.initWithCallback(
|
|
|
|
context,
|
|
|
|
1000,
|
|
|
|
Components.interfaces.nsITimer.TYPE_REPEATING_SLACK
|
|
|
|
);
|
2013-11-28 11:18:12 +04:00
|
|
|
|
|
|
|
}
|