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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Cu.importGlobalProperties(["TextEncoder"]);
// Simple server which can handle several response types and states.
// Trimmed down from devtools/client/netmonitor/test/sjs_content-type-test-server.sjs
// Additional features can be ported if needed.
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 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;
}
response.setHeader("Expires", Date(Date.now() + cacheExpire * 1000), 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);
setCacheHeaders();
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
);
setCacheHeaders();
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 "font": {
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "font/woff", false);
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 "ws": {
response.setStatusLine(
request.httpVersion,
101,
"Switching Protocols"
);
response.setHeader("Connection", "upgrade", false);
response.setHeader("Upgrade", "websocket", false);
setCacheHeaders();
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.
}
|