Skip to content

Commit

Permalink
[framer] more abstraction, .streamFrame()
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jan 14, 2012
1 parent 9ae4b6c commit ee31db6
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions lib/spdy/framer.js
Expand Up @@ -179,6 +179,33 @@ function headersToDict(headers, preprocess) {
return result;
};

Framer.prototype._synFrame = function _synFrame(type, id, assoc, dict,
callback) {
// Compress headers
spdy.utils.zstream(this.deflate, dict, function (err, chunks, size) {
if (err) return callback(err);

var offset = type === 'SYN_STREAM' ? 18 : 14,
frame = new Buffer(offset + size);;

frame.writeUInt16BE(0x8002, 0, true); // Control + Version
frame.writeUInt16BE(0x0002, type === 'SYN_STREAM' ? 1 : 2, true); // type
frame.writeUInt32BE((size + 6) & 0x00ffffff, 4, true); // No flag support
frame.writeUInt32BE(id & 0x7fffffff, 8, true); // Stream-ID

if (type === 'SYN_STREAM') {
frame.writeUInt32BE(assoc & 0x7fffffff, 12, true); // Stream-ID
}

for (var i = 0; i < chunks.length; i++) {
chunks[i].copy(frame, offset);
offset += chunks[i].length;
}

callback(null, frame);
});
};

//
// ### function replyFrame (id, code, reason, headers, callback)
// #### @id {Number} Stream ID
Expand All @@ -190,46 +217,34 @@ function headersToDict(headers, preprocess) {
//
Framer.prototype.replyFrame = function replyFrame(id, code, reason, headers,
callback) {
var self = this,
dict = headersToDict(headers, function(headers) {
var dict = headersToDict(headers, function(headers) {
headers.status = code + ' ' + reason;
headers.version = 'HTTP/1.1';
});

// Compress headers
spdy.utils.zstream(self.deflate, dict, function (err, chunks, size) {
if (err) return callback(err);

var frame = new Buffer(14 + size);
frame.writeUInt16BE(0x8002, 0, true); // Control + Version
frame.writeUInt16BE(0x0002, 2, true); // SYN_REPLY
frame.writeUInt32BE((size + 6) & 0x00ffffff, 4, true); // No flag support

// Write body start
frame.writeUInt32BE(id & 0x7fffffff, 8, true); // Stream-ID
frame.writeUInt16BE(0, 12, true); // Unused

var offset = 14;
for (var i = 0; i < chunks.length; i++) {
chunks[i].copy(frame, offset);
offset += chunks[i].length;
}

callback(null, frame);
});
this._synFrame('SYN_REPLY', id, null, dict, callback);
};

//
// ### function streamFrame (id, assoc, headers, callback)
// #### @id {Number} stream id
// #### @assoc {Number} associated stream id
// #### @meta {Object} meta headers ( method, scheme, url, version )
// #### @headers {Object} stream headers
// #### @callback {Function} continuation callback
// Create SYN_STREAM frame
// (needed for server push and testing)
//
Framer.prototype.streamFrame = function streamFrame(id, assoc, headers,
Framer.prototype.streamFrame = function streamFrame(id, assoc, meta, headers,
callback) {
var dict = headersToDict(headers, function(headers) {
headers.method = meta.method || 'GET';
headers.url = meta.url;
headers.scheme = meta.scheme || 'https';
headers.version = meta.version || 'HTTP/1.1';
});

this._synFrame('SYN_STREAM', id, assoc, dict, callback);
};

//
Expand Down

0 comments on commit ee31db6

Please sign in to comment.