Created
April 1, 2010 16:10
-
-
Save mranney/352004 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require("sys"), | |
http = require("http"), | |
port = 12345, | |
spawn = require("child_process").spawn; | |
function gzipStr (str, callback) { | |
var child = spawn("gzip", ["-c", "-f", "-n"]), | |
stdout = "", stderr = ""; | |
child.stdout.addListener("data", function (chunk) { | |
stdout += chunk; | |
}); | |
child.stderr.addListener("data", function (chunk) { | |
stderr+= chunk; | |
}); | |
child.addListener("exit", function (code) { | |
sys.puts("input: \""+ str+ "\"\n-->"+ hexdump(stdout)); | |
callback(code ? "" : stdout); | |
}); | |
child.stdout.setEncoding('binary'); | |
child.stdin.write(str, "utf8"); | |
child.stdin.close(); | |
}; | |
function hexdump (input, r, i) { | |
r= ""; | |
if (typeof input === "string") { | |
i= 0; | |
while (i < input.length) { | |
r+= hexdump(input.charCodeAt(i++)); | |
} | |
} else if (typeof input === "number") { | |
i= input.toString(16); | |
i= (i.length < 2) ? "0"+i : i; | |
r= i+ "."; | |
} | |
return r; | |
} | |
http.createServer(function (request, response) { | |
if (request.url.indexOf("favicon") >= 0) { | |
response.writeHeader(404, {}); | |
response.write(""); | |
return response.close(); | |
} | |
gzipStr("this is a test", function (str) { | |
response.writeHeader(200, { | |
"Content-Type": "text/plain", | |
"server":"Node.js", | |
"Content-Encoding":"gzip", | |
"Connection":"close", | |
"Content-Length":str.length | |
}); | |
response.write(str, "binary"); | |
response.close(); | |
}); | |
}).listen(port); | |
sys.puts("Server running at http://localhost:"+ port+ "/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment