Bug 967393 - Make the jsonp regex a bit more relaxed, r=rcampbell

This commit is contained in:
Victor Porof 2014-02-04 16:40:50 +02:00
parent c9376461c4
commit 7d6fb433f1
4 changed files with 53 additions and 22 deletions

View File

@ -2140,26 +2140,27 @@ NetworkDetailsView.prototype = {
}
if (jsonMimeType || jsonObject) {
// Extract the actual json substring in case this might be a "JSONP".
let jsonpRegex = /^[a-zA-Z0-9_$]+\(|\)$/g;
let sanitizedJSON = aString.replace(jsonpRegex, "");
let callbackPadding = aString.match(jsonpRegex);
// This regex basically parses a function call and captures the
// function name and arguments in two separate groups.
let jsonpRegex = /^\s*([\w$]+)\s*\(\s*([^]*)\s*\)\s*;?\s*$/;
let [_, callbackPadding, jsonpString] = aString.match(jsonpRegex) || [];
// Make sure this is a valid JSON object first. If so, nicely display
// the parsing results in a variables view. Otherwise, simply show
// the contents as plain text.
if (sanitizedJSON != aString) {
if (callbackPadding && jsonpString) {
try {
jsonObject = JSON.parse(sanitizedJSON);
jsonObject = JSON.parse(jsonpString);
} catch (e) {
jsonObjectParseError = e;
}
}
// Valid JSON.
// Valid JSON or JSONP.
if (jsonObject) {
$("#response-content-json-box").hidden = false;
let jsonScopeName = callbackPadding
? L10N.getFormatStr("jsonpScopeName", callbackPadding[0].slice(0, -1))
? L10N.getFormatStr("jsonpScopeName", callbackPadding)
: L10N.getStr("jsonScopeName");
return this._json.controller.setSingleVariable({

View File

@ -10,11 +10,12 @@ function test() {
info("Starting test... ");
let { document, L10N, NetMonitorView } = aMonitor.panelWin;
let { RequestsMenu } = NetMonitorView;
let { RequestsMenu, NetworkDetails } = NetMonitorView;
RequestsMenu.lazyUpdate = false;
NetworkDetails._json.lazyEmpty = false;
waitForNetworkEvents(aMonitor, 1).then(() => {
waitForNetworkEvents(aMonitor, 2).then(() => {
verifyRequestItemTarget(RequestsMenu.getItemAtIndex(0),
"GET", CONTENT_TYPE_SJS + "?fmt=jsonp&jsonp=$_0123Fun", {
status: 200,
@ -24,19 +25,37 @@ function test() {
size: L10N.getFormatStrWithNumbers("networkMenu.sizeKB", 0.04),
time: true
});
verifyRequestItemTarget(RequestsMenu.getItemAtIndex(1),
"GET", CONTENT_TYPE_SJS + "?fmt=jsonp2&jsonp=$_4567Sad", {
status: 200,
statusText: "OK",
type: "json",
fullMimeType: "text/json; charset=utf-8",
size: L10N.getFormatStrWithNumbers("networkMenu.sizeKB", 0.05),
time: true
});
EventUtils.sendMouseEvent({ type: "mousedown" },
document.getElementById("details-pane-toggle"));
EventUtils.sendMouseEvent({ type: "mousedown" },
document.querySelectorAll("#details-pane tab")[3]);
Task.spawn(function() {
let RESPONSE_BODY_DISPLAYED = aMonitor.panelWin.EVENTS.RESPONSE_BODY_DISPLAYED;
let RESPONSE_BODY_DISPLAYED = aMonitor.panelWin.EVENTS.RESPONSE_BODY_DISPLAYED;
waitFor(aMonitor.panelWin, RESPONSE_BODY_DISPLAYED)
.then(testResponseTab)
.then(() => teardown(aMonitor))
.then(finish);
EventUtils.sendMouseEvent({ type: "mousedown" },
document.getElementById("details-pane-toggle"));
EventUtils.sendMouseEvent({ type: "mousedown" },
document.querySelectorAll("#details-pane tab")[3]);
function testResponseTab() {
yield waitFor(aMonitor.panelWin, RESPONSE_BODY_DISPLAYED);
testResponseTab("$_0123Fun", "\"Hello JSONP!\"");
RequestsMenu.selectedIndex = 1;
yield waitFor(aMonitor.panelWin, RESPONSE_BODY_DISPLAYED);
testResponseTab("$_4567Sad", "\"Hello weird JSONP!\"");
yield teardown(aMonitor);
finish();
});
function testResponseTab(aFunction, aGreeting) {
let tab = document.querySelectorAll("#details-pane tab")[3];
let tabpanel = document.querySelectorAll("#details-pane tabpanel")[3];
@ -66,13 +85,13 @@ function test() {
let jsonScope = tabpanel.querySelectorAll(".variables-view-scope")[0];
is(jsonScope.querySelector(".name").getAttribute("value"),
L10N.getFormatStr("jsonpScopeName", "$_0123Fun"),
L10N.getFormatStr("jsonpScopeName", aFunction),
"The json scope doesn't have the correct title.");
is(jsonScope.querySelectorAll(".variables-view-property .name")[0].getAttribute("value"),
"greeting", "The first json property name was incorrect.");
is(jsonScope.querySelectorAll(".variables-view-property .value")[0].getAttribute("value"),
"\"Hello JSONP!\"", "The first json property value was incorrect.");
aGreeting, "The first json property value was incorrect.");
is(jsonScope.querySelectorAll(".variables-view-property .name")[1].getAttribute("value"),
"__proto__", "The second json property name was incorrect.");

View File

@ -26,7 +26,9 @@
function performRequests() {
get("sjs_content-type-test-server.sjs?fmt=jsonp&jsonp=$_0123Fun", function() {
// Done.
get("sjs_content-type-test-server.sjs?fmt=jsonp2&jsonp=$_4567Sad", function() {
// Done.
});
});
}
</script>

View File

@ -95,6 +95,15 @@ function handleRequest(request, response) {
response.finish();
break;
}
case "jsonp2": {
let fun = params.filter((s) => s.contains("jsonp="))[0].split("=")[1];
response.setStatusLine(request.httpVersion, status, "OK");
response.setHeader("Content-Type", "text/json; charset=utf-8", false);
maybeMakeCached();
response.write(" " + fun + " ( { \"greeting\": \"Hello weird JSONP!\" } ) ; ");
response.finish();
break;
}
case "json-long": {
let str = "{ \"greeting\": \"Hello long string JSON!\" },";
response.setStatusLine(request.httpVersion, status, "OK");