Mercurial > spidermonkey-playground
diff memory_profiler_server.js @ 77:8cf72992387d default tip
add JSONP
author | Dion Almaer <dion@mozilla.com> |
---|---|
date | Fri, 26 Jun 2009 10:13:32 -0700 |
parents | b87b6ebb6e86 |
children |
line wrap: on
line diff
--- a/memory_profiler_server.js Thu Jun 25 23:02:29 2009 -0700 +++ b/memory_profiler_server.js Fri Jun 26 10:13:32 2009 -0700 @@ -23,6 +23,20 @@ print(lines.join('\n')); } +function debug(out) { + print("DEBUG: " + out); +} + +// Work out if the caller wants the output to be wrapped in a JSONP function wrapper +function wrapJSONP(path, json) { + var callbackName = path.match(/callback=(\w+)/); + if (callbackName) { + return callbackName[1] + "(" + json + ")"; + } else { + return json; + } +} + // This is just a test to exercise the code a bit. JSON.stringify(getObjectInfo(getGCRoots()[0])); @@ -69,10 +83,11 @@ if (requestHeaders == null) return true; var requestLines = requestHeaders.split("\r\n"); - print(requestLines[0]); + debug("Request: " + requestLines[0]); var reqParts = requestLines[0].split(" "); var method = reqParts[0]; var path = reqParts[1]; + debug("Path: " + path); var code = "200 OK"; var toSend; @@ -81,26 +96,28 @@ // but Firefox doesn't let us browse a webserver this way, which is // annoying, so we're just leaving it at text/plain for now. - if (path == "/") - toSend = HELP; - - if (path == "/gc-roots") - toSend = JSON.stringify(getGCRoots()); - - if (path == "/stop") - toSend = "Stopping server now!"; - - var objNum = path.match(/^\/objects\/(\d+)$/); - if (objNum) { - throw new Error('wut'); - objNum = objNum[1]; - var objInfo = getObjectInfo(objNum); - if (objInfo) { - toSend = JSON.stringify(objInfo); - } else { - code = "404 Not Found"; - toSend = "Object " + objNum + " does not exist."; - } + if (path == "/") { + toSend = HELP; + } else if (path.indexOf("/stop") == 0) { + toSend = "Stopping server now!"; + } else { + if (path.indexOf("/gc-roots") == 0) { + toSend = JSON.stringify(getGCRoots()); + } else { + var objNum = path.match(/^\/objects\/(\d+)/); + if (objNum) { + //throw new Error('wut'); + objNum = objNum[1]; + debug(objNum); + var objInfo = getObjectInfo(objNum); + if (objInfo) { + toSend = JSON.stringify(objInfo); + } else { + code = "404 Not Found"; + toSend = "Object " + objNum + " does not exist."; + } + } + } } if (!toSend) { @@ -108,6 +125,10 @@ toSend = "Not found, yo."; } + // maybe wrap the response in JSONP + toSend = wrapJSONP(path, toSend); + debug("toSend == " + toSend); + //print("headers: " + uneval(requestHeaders)); var headerLines = ["HTTP/1.0 " + code, "Content-Type: text/plain",