Mercurial > spidermonkey-playground
view memory_profiler_server.js @ 54:c451579bf83c
Built out more of the /objects/ method.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 24 Jun 2009 18:15:03 -0700 |
parents | 2b22291fa09a |
children | 4910bc49a182 |
line wrap: on
line source
var socket = new ServerSocket(); var PORT = 8080; socket.bind("127.0.0.1", PORT); socket.listen(); var NEWLINE = "\r\n"; var DOUBLE_NEWLINE = NEWLINE + NEWLINE; function getHeaders(conn) { var headers = ""; while (1) { var character = conn.recv(1); if (character == null) return null; headers += character; if (headers.indexOf(DOUBLE_NEWLINE) != -1) return headers; } } print("Waiting for requests on port " + PORT + "."); function processRequest(socket) { var conn = socket.accept(); try { var requestHeaders = getHeaders(conn); if (requestHeaders == null) return true; var requestLines = requestHeaders.split("\r\n"); print(requestLines[0]); var reqParts = requestLines[0].split(" "); var method = reqParts[0]; var path = reqParts[1]; var code = "200 OK"; var toSend; // TODO: We'd like to set the MIME type of JSON data to application/json, // 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 == "/gc-roots") toSend = JSON.stringify(getGCRoots()); if (path == "/stop") toSend = "Stopping server now!"; var objNum = path.match(/^\/objects\/(\d+)$/); if (objNum) { 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 (!toSend) { code = "404 Not Found"; toSend = "Not found, yo."; } //print("headers: " + uneval(requestHeaders)); var headerLines = ["HTTP/1.0 " + code, "Content-Type: text/plain", "Connection: close", "Content-Length: " + toSend.length]; var headers = headerLines.join("\r\n"); var response = headers + DOUBLE_NEWLINE + toSend; //print("response: " + uneval(response)); conn.send(response); conn.close(); //print("response sent."); } catch (e) { print(e.toString()); try { conn.close(); } catch (e) {} } if (path == "/stop") return false; return true; } while (processRequest(socket)) {} socket.close();