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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
function gzipCompressString(string, obs) {
const scs = Cc["@mozilla.org/streamConverters;1"].getService(
Ci.nsIStreamConverterService
);
const listener = Cc["@mozilla.org/network/stream-loader;1"].createInstance(
Ci.nsIStreamLoader
);
listener.init(obs);
const converter = scs.asyncConvertData(
"uncompressed",
"gzip",
listener,
null
);
const stringStream = Cc[
"@mozilla.org/io/string-input-stream;1"
].createInstance(Ci.nsIStringInputStream);
stringStream.data = string;
converter.onStartRequest(null);
converter.onDataAvailable(null, stringStream, 0, string.length);
converter.onStopRequest(null, null);
}
function doubleGzipCompressString(string, observer) {
const observer2 = {
onStreamComplete(loader, context, status, length, result) {
const buffer = String.fromCharCode.apply(this, result);
gzipCompressString(buffer, observer);
},
};
gzipCompressString(string, observer2);
}
function handleRequest(request, response) {
response.processAsync();
const params = request.queryString.split("&");
const format = (params.filter(s => s.includes("fmt="))[0] || "").split(
"="
)[1];
const status =
(params.filter(s => s.includes("sts="))[0] || "").split("=")[1] || 200;
const cookies =
(params.filter(s => s.includes("cookies="))[0] || "").split("=")[1] || 0;
const cors = request.queryString.includes("cors=1");
let cachedCount = 0;
const cacheExpire = 60; // seconds
function setCacheHeaders() {
if (status != 304) {
response.setHeader(
"Cache-Control",
"no-cache, no-store, must-revalidate"
);
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
return;
}
// Spice things up a little!
if (cachedCount % 2) {
response.setHeader("Cache-Control", "max-age=" + cacheExpire, false);
} else {
response.setHeader(
"Expires",
Date(Date.now() + cacheExpire * 1000),
false
);
}
cachedCount++;
}
function setCookieHeaders() {
if (cookies) {
response.setHeader(
"Set-Cookie",
"name1=value1; Domain=.foo.example.com",
true
);
response.setHeader(
"Set-Cookie",
"name2=value2; Domain=.example.com",
true
);
}
}
function setAllowOriginHeaders() {
if (cors) {
response.setHeader("Access-Control-Allow-Origin", "*", false);
}
}
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(
// eslint-disable-next-line complexity
() => {
// to avoid garbage collection
timer = null;
switch (format) {
case "txt": {
response.setStatusLine(request.httpVersion, status, "DA DA DA");
response.setHeader("Content-Type", "text/plain", false);
setCacheHeaders();
function convertToUtf8(str) {
return String.fromCharCode(...new TextEncoder().encode(str));
}
// This script must be evaluated as UTF-8 for this to write out the
// bytes of the string in UTF-8. If it's evaluated as Latin-1, the
// written bytes will be the result of UTF-8-encoding this string
// *twice*.
const data = "Братан, ты вообще качаешься?";
const stringOfUtf8Bytes = convertToUtf8(data);
response.write(stringOfUtf8Bytes);
response.finish();
break;
}
case "xml": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/xml; charset=utf-8", false);
setCacheHeaders();
response.write("<label value='greeting'>Hello XML!</label>");
response.finish();
break;
}
case "html": {
const content = (
params.filter(s => s.includes("res="))[0] || ""
).split("=")[1];
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
setAllowOriginHeaders();
setCacheHeaders();
setCookieHeaders();
response.write(content || "<p>Hello HTML!</p>");
response.finish();
break;
}
case "xhtml": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"application/xhtml+xml; charset=utf-8",
false
);
setAllowOriginHeaders();
setCacheHeaders();
setCookieHeaders();
response.write("<p>Hello XHTML!</p>");
response.finish();
break;
}
case "html-long": {
const str = new Array(102400 /* 100 KB in bytes */).join(".");
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
setCacheHeaders();
response.write("<p>" + str + "</p>");
response.finish();
break;
}
case "css": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/css; charset=utf-8", false);
setCacheHeaders();
response.write("body:pre { content: 'Hello CSS!' }");
response.finish();
break;
}
case "js": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"application/javascript; charset=utf-8",
false
);
setCacheHeaders();
response.write("function() { return 'Hello JS!'; }");
response.finish();
break;
}
case "json": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"application/json; charset=utf-8",
false
);
setCacheHeaders();
response.write('{ "greeting": "Hello JSON!" }');
response.finish();
break;
}
case "jsonp": {
const fun = (params.filter(s => s.includes("jsonp="))[0] || "").split(
"="
)[1];
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write(fun + '({ "greeting": "Hello JSONP!" })');
response.finish();
break;
}
case "jsonp2": {
const fun = (params.filter(s => s.includes("jsonp="))[0] || "").split(
"="
)[1];
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write(
" " + fun + ' ( { "greeting": "Hello weird JSONP!" } ) ; '
);
response.finish();
break;
}
case "json-b64": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write(btoa('{ "greeting": "This is a base 64 string." }'));
response.finish();
break;
}
case "json-long": {
const str = '{ "greeting": "Hello long string JSON!" },';
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write("[" + new Array(2048).join(str).slice(0, -1) + "]");
response.finish();
break;
}
case "json-malformed": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write('{ "greeting": "Hello malformed JSON!" },');
response.finish();
break;
}
case "json-text-mime": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"text/plain; charset=utf-8",
false
);
setCacheHeaders();
response.write('{ "greeting": "Hello third-party JSON!" }');
response.finish();
break;
}
case "json-custom-mime": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"text/x-bigcorp-json; charset=utf-8",
false
);
setCacheHeaders();
response.write('{ "greeting": "Hello oddly-named JSON!" }');
response.finish();
break;
}
case "json-valid-xssi-protection": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
setCacheHeaders();
response.write(')]}\'\n{"greeting": "Hello good XSSI protection"}');
response.finish();
break;
}
case "font": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "font/woff", false);
setAllowOriginHeaders();
setCacheHeaders();
response.finish();
break;
}
case "image": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "image/png", false);
setCacheHeaders();
response.finish();
break;
}
case "application-ogg": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "application/ogg", false);
setCacheHeaders();
response.finish();
break;
}
case "audio": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "audio/ogg", false);
setCacheHeaders();
response.finish();
break;
}
case "video": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "video/webm", false);
setCacheHeaders();
response.finish();
break;
}
case "flash": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"application/x-shockwave-flash",
false
);
setCacheHeaders();
response.finish();
break;
}
case "ws": {
response.setStatusLine(
request.httpVersion,
101,
"Switching Protocols"
);
response.setHeader("Connection", "upgrade", false);
response.setHeader("Upgrade", "websocket", false);
setCacheHeaders();
response.finish();
break;
}
case "gzip": {
// Note: we're doing a double gzip encoding to test multiple
// converters in network monitor.
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Content-Encoding", "gzip\t ,gzip", false);
setCacheHeaders();
const observer = {
onStreamComplete(loader, context, statusl, length, result) {
const buffer = String.fromCharCode.apply(this, result);
response.setHeader("Content-Length", "" + buffer.length, false);
response.write(buffer);
response.finish();
},
};
const data = new Array(1000).join("Hello gzip!");
doubleGzipCompressString(data, observer);
break;
}
case "br": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json", false);
response.setHeader("Content-Encoding", "br", false);
setCacheHeaders();
response.setHeader("Content-Length", "10", false);
// Use static data since we cannot encode brotli.
response.write("\x1b\x3f\x00\x00\x24\xb0\xe2\x99\x80\x12");
response.finish();
break;
}
case "hls-m3u8": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "application/x-mpegurl", false);
setCacheHeaders();
response.write("#EXTM3U\n");
response.finish();
break;
}
case "hls-m3u8-alt-mime-type": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader(
"Content-Type",
"application/vnd.apple.mpegurl",
false
);
setCacheHeaders();
response.write("#EXTM3U\n");
response.finish();
break;
}
case "mpeg-dash": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "video/vnd.mpeg.dash.mpd", false);
setCacheHeaders();
response.write('<?xml version="1.0" encoding="UTF-8"?>\n');
response.write(
'<MPD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></MPD>\n'
);
response.finish();
break;
}
default: {
response.setStatusLine(request.httpVersion, 404, "Not Found");
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
setCacheHeaders();
response.write("<blink>Not Found</blink>");
response.finish();
break;
}
}
},
10,
Ci.nsITimer.TYPE_ONE_SHOT
); // Make sure this request takes a few ms.
}
|