Skip to content

Commit

Permalink
chunked-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Jan 22, 2011
1 parent d0c172a commit f5a87f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
11 changes: 6 additions & 5 deletions examples/yahoo-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ var conn = require('net').createConnection(80, 'www.yahoo.com');

conn.on('connect', function() {

console.log('connected!');
// Create our first "stack", the HttpRequestStack instance. This class is
// responsible for writing an HTTP request to the provided 'conn', and then
// parsing the response into a 'response' event and clean 'data' events.
var req = new HttpRequestStack(conn);

req.get("/", [
"Host: www.yahoo.com",
"Connection: close",
"Accept-Encoding: gzip"
"Host: www.yahoo.com"
//"Connection: close",
//"Accept-Encoding: gzip"
]);
req.end();
//req.end();

// 'response' is fired after the final HTTP header has been parsed. 'res'
// is a ReadStream, that also contains 'rawHeaders', 'headers' properties.
Expand All @@ -28,7 +29,7 @@ conn.on('connect', function() {
});

res.on('end', function() {
//console.error('received FIN packet from "conn"');
console.error('received FIN packet from "conn"');
});

});
Expand Down
26 changes: 20 additions & 6 deletions lib/http-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ var STATUS_CODES = require('http').STATUS_CODES;
* encoding parsing.
*/
function HttpBaseStack(stream) {
StreamStack.call(this, stream);

this._bindedOnData = this._onData.bind(this);
this.stream.on('data', this._bindedOnData);
StreamStack.call(this, stream, {
data: this._onData
});
}
inherits(HttpBaseStack, StreamStack);
exports.HttpBaseStack = HttpBaseStack;
Expand All @@ -29,8 +28,8 @@ HttpBaseStack.prototype.parsingHeader = true;

// Calling 'write()' transparently adds the HTTP "Transfer-Encoding".
HttpBaseStack.prototype.write = function(chunk, enc) {
if (this.chunkedEncoding) {

if (this.chunkedOutgoing) {
return this._writeChunk(chunk, enc);
} else {
return this.stream.write(chunk, enc);
}
Expand All @@ -43,6 +42,21 @@ HttpBaseStack.prototype.end = function(chunk, enc) {
if (!this.shouldKeepAlive) this.stream.end();
}

HttpBaseStack.prototype._writeChunk = function(chunk, enc) {
var len = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(chunk, enc);
var lenHex = len.toString(16);
var buf = new Buffer(len + CRLF.length + lenHex.length + CRLF.length);
var pos = buf.write(lenHex, 0);
pos += buf.write(CRLF, pos);
if (Buffer.isBuffer(chunk)) {
pos += chunk.copy(buf, pos, 0);
} else {
pos += buf.write(chunk, enc, pos);
}
pos += buf.write(CRLF, pos);
return this.stream.write(buf);
}

HttpBaseStack.prototype._writeHeader = function(firstLine, headers) {
// Allow a regular Object to be passed, and coerce it into an Array before processing
if (!Array.isArray(headers)) {
Expand Down

0 comments on commit f5a87f2

Please sign in to comment.