Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for h2 make sure it follows http messaging from spec #56

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/spdy-transport/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ Connection.prototype._onVersion = function _onVersion (version) {
})

// Update session window
if (state.version >= 3.1 || (state.isServer && state.autoSpdy31)) { this._onSessionWindowDrain() }
if (state.version >= 3.1 ||
(state.isServer && state.autoSpdy31 && state.version >= 3.1)) {
this._onSessionWindowDrain()
}

this.emit('version', version)
}
Expand Down Expand Up @@ -469,7 +472,8 @@ Connection.prototype._handleHeaders = function _handleHeaders (frame) {

Connection.prototype._onSessionWindowDrain = function _onSessionWindowDrain () {
var state = this._spdyState
if (state.version < 3.1 && !(state.isServer && state.autoSpdy31)) {
if (state.version < 3.1 &&
!(state.isServer && state.autoSpdy31 && state.version >= 3.1)) {
return
}

Expand Down
3 changes: 2 additions & 1 deletion lib/spdy-transport/protocol/spdy/zlib-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function createDeflate (version, compression) {
function createInflate (version) {
var inflate = zlib.createInflate({
dictionary: transport.protocol.spdy.dictionary[version],
flush: zlib.Z_SYNC_FLUSH
flush: zlib.Z_SYNC_FLUSH,
windowBits: 15
})

// For node.js v0.8
Expand Down
49 changes: 34 additions & 15 deletions lib/spdy-transport/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function Stream (connection, options) {
this.path = options.path
this.host = options.host
this.headers = options.headers || {}
this.headersSent = false
this.trailers = null
this.connection = connection
this.parent = options.parent || null

Expand Down Expand Up @@ -354,13 +356,20 @@ Stream.prototype._onFinish = function _onFinish () {
})
return
}

state.framer.dataFrame({
id: this.id,
priority: state.priority.getPriority(),
fin: true,
data: new Buffer(0)
})
if (this.trailers) {
state.framer.headersFrame({
id: this.id,
headers: this.trailers,
fin: true
})
} else {
state.framer.dataFrame({
id: this.id,
priority: state.priority.getPriority(),
fin: true,
data: Buffer.alloc(0)
})
}
}

this._maybeClose()
Expand Down Expand Up @@ -510,6 +519,8 @@ Stream.prototype.send = function send (callback) {
this._writableState.finished = true
}

this.headersSent = true

// TODO(indunty): ideally it should just take a stream object as an input
var self = this
this._hardCork()
Expand Down Expand Up @@ -546,6 +557,9 @@ Stream.prototype.respond = function respond (status, headers, callback) {
status: status,
headers: headers
}

this.headersSent = true

this._hardCork()
state.framer.responseFrame(frame, function (err) {
self._hardUncork()
Expand Down Expand Up @@ -597,14 +611,19 @@ Stream.prototype.sendHeaders = function sendHeaders (headers, callback) {
return
}

this._hardCork()
state.framer.headersFrame({
id: this.id,
headers: headers
}, function (err) {
self._hardUncork()
if (callback) { callback(err) }
})
if (state.protocol.name !== 'h2' || this.headersSent === false) {
this.headersSent = true
this._hardCork()
state.framer.headersFrame({
id: this.id,
headers: headers
}, function (err) {
self._hardUncork()
if (callback) { callback(err) }
})
} else {
this.trailers = headers
}
}

Stream.prototype.destroy = function destroy () {
Expand Down
16 changes: 15 additions & 1 deletion test/both/transport/stream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ describe('Transport/Stream', function () {
stream.sendHeaders({ a: 'b' })
stream.end()
})

server.on('stream', function (stream) {
var gotHeaders = false
stream.on('headers', function (headers) {
Expand Down Expand Up @@ -350,6 +349,7 @@ describe('Transport/Stream', function () {
})

it('should emit trailing headers', function (done) {
var dataReceived = false
client.request({
method: 'POST',
path: '/hello-split'
Expand All @@ -364,11 +364,25 @@ describe('Transport/Stream', function () {
})
})

if (name === 'h2') {
server.on('frame', function (frame) {
if (frame.type === 'HEADERS' && dataReceived) {
assert.ok(frame.fin)
}
})
}

server.on('stream', function (stream) {
stream.respond(200, {})

stream.resume()
stream.on('data', function (data) {
dataReceived = true
})
stream.on('headers', function (headers) {
if (name === 'h2') {
assert.ok(dataReceived)
}
assert.equal(headers.trailer, 'yes')
done()
})
Expand Down