summaryrefslogtreecommitdiffstats
path: root/dom/downloads/tests/serve_file.sjs
blob: d0171d7ca09d3da81101a255c3e7df09dc8251c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Serves a file with a given mime type and size at an optionally given rate.

function getQuery(request) {
  var query = {};
  request.queryString.split('&').forEach(function (val) {
    var [name, value] = val.split('=');
    query[name] = unescape(value);
  });
  return query;
}

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++) {
        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;
          }
        }
      }

      // 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.
  this.timer.cancel();

  // Clear the timer.
  this.timer = null;
}

function handleRequest(request, response) {
  var query = getQuery(request);

  // sending at a specific rate requires our response to be asynchronous so
  // we handle all requests asynchronously. See handleResponse().
  response.processAsync();

  // Default status when responding.
  var version = "1.1";
  var statusCode = 200;
  var description = "OK";

  // Default values for content type, size and rate.
  var contentType = "text/plain";
  var contentRange = null;
  var size = 1024;
  var rate = 0;

  // optional content type to be used by our response.
  if ("contentType" in query) {
    contentType = query["contentType"];
  }

  // optional size (in bytes) for generated file.
  if ("size" in query) {
    size = parseInt(query["size"]);
  }

  // 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;
  }

  // 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
    },
    timer: null
  };

  // The notify implementation for the timer.
  context.notify = handleResponse.bind(context);

  context.timer =
    Components.classes["@mozilla.org/timer;1"]
              .createInstance(Components.interfaces.nsITimer);

  // generate the content.
  response.setStatusLine(version, statusCode, description);
  response.setHeader("Content-Type", contentType, false);
  if (contentRange) {
    response.setHeader("Content-Range", contentRange, false);
  }
  response.setHeader("Content-Length", size.toString(), false);

  // initialize the timer and start writing out the response.
  context.timer.initWithCallback(
    context,
    1000,
    Components.interfaces.nsITimer.TYPE_REPEATING_SLACK
  );

}