Created
March 13, 2012 01:46
-
-
Save anonymous/2026105 to your computer and use it in GitHub Desktop.
a guest on Mar 12th, 2012 - pastebin.com/QAzuZUDr
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
coffee: | |
response_handler = (response)=> | |
buf = null | |
ptr = 0 | |
response.on "data", (chunk) => | |
if not buf | |
if response.headers['content-length'] | |
buf = new Buffer(parseInt(response.headers['content-length'],10)+2) | |
else | |
buf = '' | |
if typeof buf is 'string' | |
buf += chunk.toString('utf8') | |
else | |
chunk.copy buf, ptr | |
ptr += chunk.length | |
response.on "end", => | |
cookies.update response.headers["set-cookie"] | |
error = null | |
switch response.headers['content-encoding'] | |
when 'gzip', 'deflate' | |
zlib.unzip buf, (err, data) => | |
if err | |
error = new Error("Error decompressing data") | |
else | |
body = data.toString('utf8') | |
when 'sdch' | |
error = new Error("No SDCH support") | |
else | |
body = buf | |
console.log body | |
# Turn body from string into a String, so we can add property getters. | |
resource.response = new HTTPResponse(url, response.statusCode, response.headers, body) | |
converted javascript: | |
var response_handler; | |
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
response_handler = __bind(function(response) { | |
var buf, ptr; | |
buf = null; | |
ptr = 0; | |
response.on("data", __bind(function(chunk) { | |
if (!buf) { | |
if (response.headers['content-length']) { | |
buf = new Buffer(parseInt(response.headers['content-length'], 10) + 2); | |
} else { | |
buf = ''; | |
} | |
} | |
if (typeof buf === 'string') { | |
return buf += chunk.toString('utf8'); | |
} else { | |
chunk.copy(buf, ptr); | |
return ptr += chunk.length; | |
} | |
}, this)); | |
return response.on("end", __bind(function() { | |
var body, error; | |
cookies.update(response.headers["set-cookie"]); | |
error = null; | |
switch (response.headers['content-encoding']) { | |
case 'gzip': | |
case 'deflate': | |
zlib.unzip(buf, __bind(function(err, data) { | |
var body; | |
if (err) { | |
return error = new Error("Error decompressing data"); | |
} else { | |
return body = data.toString('utf8'); | |
} | |
}, this)); | |
break; | |
case 'sdch': | |
error = new Error("No SDCH support"); | |
break; | |
default: | |
body = buf; | |
} | |
console.log(body); | |
return resource.response = new HTTPResponse(url, response.statusCode, response.headers, body); | |
}, this)); | |
}, this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment