2009-01-13 22:53:43 -08:00
|
|
|
const CC = Components.Constructor;
|
|
|
|
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
|
|
|
|
"nsIBinaryInputStream",
|
|
|
|
"setInputStream");
|
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
function handleRequest(request, response)
|
|
|
|
{
|
|
|
|
var query = {};
|
|
|
|
request.queryString.split('&').forEach(function (val) {
|
2009-04-10 03:30:27 -07:00
|
|
|
var [name, value] = val.split('=');
|
2008-12-27 15:26:55 -08:00
|
|
|
query[name] = unescape(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
var isPreflight = request.method == "OPTIONS";
|
|
|
|
|
2009-01-13 22:53:43 -08:00
|
|
|
var bodyStream = new BinaryInputStream(request.bodyInputStream);
|
|
|
|
var bodyBytes = [];
|
|
|
|
while ((bodyAvail = bodyStream.available()) > 0)
|
|
|
|
Array.prototype.push.apply(bodyBytes, bodyStream.readByteArray(bodyAvail));
|
|
|
|
|
|
|
|
var body = decodeURIComponent(
|
|
|
|
escape(String.fromCharCode.apply(null, bodyBytes)));
|
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
// Check that request was correct
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2009-01-13 22:53:43 -08:00
|
|
|
if (!isPreflight && query.body && body != query.body) {
|
|
|
|
sendHttp500(response, "Wrong body. Expected " + query.body + " got " +
|
|
|
|
body);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if (!isPreflight && "headers" in query) {
|
|
|
|
headers = eval(query.headers);
|
|
|
|
for(headerName in headers) {
|
2009-01-13 22:53:43 -08:00
|
|
|
// Content-Type is changed if there was a body
|
|
|
|
if (!(headerName == "Content-Type" && body) &&
|
|
|
|
request.getHeader(headerName) != headers[headerName]) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Header " + headerName + " had wrong value. Expected " +
|
|
|
|
headers[headerName] + " got " + request.getHeader(headerName));
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if (isPreflight && "requestHeaders" in query &&
|
|
|
|
request.getHeader("Access-Control-Request-Headers") != query.requestHeaders) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Access-Control-Request-Headers had wrong value. Expected " +
|
|
|
|
query.requestHeaders + " got " +
|
|
|
|
request.getHeader("Access-Control-Request-Headers"));
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if (isPreflight && "requestMethod" in query &&
|
|
|
|
request.getHeader("Access-Control-Request-Method") != query.requestMethod) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Access-Control-Request-Method had wrong value. Expected " +
|
|
|
|
query.requestMethod + " got " +
|
|
|
|
request.getHeader("Access-Control-Request-Method"));
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if ("origin" in query && request.getHeader("Origin") != query.origin) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Origin had wrong value. Expected " + query.origin + " got " +
|
|
|
|
request.getHeader("Origin"));
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if ("cookie" in query) {
|
|
|
|
cookies = {};
|
|
|
|
request.getHeader("Cookie").split(/ *; */).forEach(function (val) {
|
2009-04-10 03:30:27 -07:00
|
|
|
var [name, value] = val.split('=');
|
2008-12-27 15:26:55 -08:00
|
|
|
cookies[name] = unescape(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
query.cookie.split(",").forEach(function (val) {
|
2009-04-10 03:30:27 -07:00
|
|
|
var [name, value] = val.split('=');
|
2008-12-27 15:26:55 -08:00
|
|
|
if (cookies[name] != value) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Cookie " + name + " had wrong value. Expected " + value +
|
|
|
|
" got " + cookies[name]);
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if ("noCookie" in query && request.hasHeader("Cookie")) {
|
2008-12-27 15:26:59 -08:00
|
|
|
sendHttp500(response,
|
|
|
|
"Got cookies when didn't expect to: " + request.getHeader("Cookie"));
|
|
|
|
return;
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send response
|
2009-01-21 14:50:28 -08:00
|
|
|
|
|
|
|
if (query.hop) {
|
|
|
|
query.hop = parseInt(query.hop, 10);
|
|
|
|
hops = eval(query.hops);
|
|
|
|
query.allowOrigin = hops[query.hop-1].allowOrigin;
|
2009-02-24 11:46:51 -08:00
|
|
|
query.allowHeaders = hops[query.hop-1].allowHeaders;
|
2009-01-21 14:50:28 -08:00
|
|
|
}
|
2010-10-04 17:41:07 -07:00
|
|
|
|
|
|
|
if (!isPreflight && query.status) {
|
|
|
|
response.setStatusLine(null, query.status, query.statusMessage);
|
|
|
|
}
|
|
|
|
if (isPreflight && query.preflightStatus) {
|
|
|
|
response.setStatusLine(null, query.preflightStatus, "preflight status");
|
|
|
|
}
|
2009-01-21 14:50:28 -08:00
|
|
|
|
2008-12-27 15:26:55 -08:00
|
|
|
if (query.allowOrigin && (!isPreflight || !query.noAllowPreflight))
|
|
|
|
response.setHeader("Access-Control-Allow-Origin", query.allowOrigin);
|
|
|
|
|
|
|
|
if (query.allowCred)
|
|
|
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
|
|
|
|
|
if (query.setCookie)
|
|
|
|
response.setHeader("Set-Cookie", query.setCookie + "; path=/");
|
|
|
|
|
|
|
|
if (isPreflight) {
|
|
|
|
if (query.allowHeaders)
|
|
|
|
response.setHeader("Access-Control-Allow-Headers", query.allowHeaders);
|
|
|
|
|
|
|
|
if (query.allowMethods)
|
|
|
|
response.setHeader("Access-Control-Allow-Methods", query.allowMethods);
|
2009-01-21 14:50:28 -08:00
|
|
|
}
|
2010-10-04 18:25:44 -07:00
|
|
|
else {
|
|
|
|
if (query.responseHeaders) {
|
|
|
|
let responseHeaders = eval(query.responseHeaders);
|
|
|
|
for (let responseHeader in responseHeaders) {
|
|
|
|
response.setHeader(responseHeader, responseHeaders[responseHeader]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.exposeHeaders)
|
|
|
|
response.setHeader("Access-Control-Expose-Headers", query.exposeHeaders);
|
|
|
|
}
|
2009-01-21 14:50:28 -08:00
|
|
|
|
|
|
|
if (query.hop && query.hop < hops.length) {
|
|
|
|
newURL = hops[query.hop].server +
|
|
|
|
"/tests/content/base/test/file_CrossSiteXHR_server.sjs?" +
|
|
|
|
"hop=" + (query.hop + 1) + "&hops=" + query.hops;
|
2009-02-24 11:46:51 -08:00
|
|
|
response.setStatusLine(null, 307, "redirect");
|
2009-01-21 14:50:28 -08:00
|
|
|
response.setHeader("Location", newURL);
|
2008-12-27 15:26:55 -08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-21 14:50:28 -08:00
|
|
|
// Send response body
|
|
|
|
if (!isPreflight && request.method != "HEAD") {
|
|
|
|
response.setHeader("Content-Type", "application/xml", false);
|
|
|
|
response.write("<res>hello pass</res>\n");
|
|
|
|
}
|
2011-04-11 18:28:28 -07:00
|
|
|
if (isPreflight && "preflightBody" in query) {
|
|
|
|
response.setHeader("Content-Type", "text/plain", false);
|
|
|
|
response.write(query.preflightBody);
|
|
|
|
}
|
2008-12-27 15:26:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendHttp500(response, text) {
|
|
|
|
response.setStatusLine(null, 500, text);
|
2008-12-27 15:26:55 -08:00
|
|
|
}
|