Skip to content

Instantly share code, notes, and snippets.

@sidorares
Created February 27, 2014 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidorares/9245111 to your computer and use it in GitHub Desktop.
Save sidorares/9245111 to your computer and use it in GitHub Desktop.
function encode(data) {
var chunk = data.toString();
return chunk.length.toString(16) + "\r\n" + chunk + "\r\n";
}
var server = new HTTP.Server("localhost", 8000);
server.onRequest = function (request) {
log( "path: " + request.path );
log( "query string: " + request.query_string );
// onBody sends null on the last chunk.
request.onBody = function (chunk) {
if(chunk) {
this.respond(encode(chunk));
} else {
this.respond(encode("\n"));
this.respond("0\r\n\r\n");
this.respond(null); // signals end-of-request
}
}
request.respond("HTTP/1.0 200 OK\r\n");
request.respond("Content-Type: text/plain\r\n");
request.respond("Transfer-Encoding: chunked\r\n");
request.respond("\r\n");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment