Refactor ServerSession.generateResponse() to group all logic around setting each parameter -- both the internal setting and the response value -- together, and from here work around an issue in Firefox that means the client will fail if the server sends an unsolicited server_max_window_bits parameter.

This commit is contained in:
James Coglan
2015-10-11 13:21:04 +01:00
parent dca0a77b0d
commit 9cb62ecd49
4 changed files with 34 additions and 30 deletions
+3 -3
View File
@@ -64,12 +64,12 @@ ClientSession.prototype.activate = function(params) {
this._ownContextTakeover = !(this._acceptNoContextTakeover || params.client_no_context_takeover);
this._ownWindowBits = Math.min(
this._acceptMaxWindowBits || common.DEFAULT_MAX_WINDOW_BITS,
params.client_max_window_bits || common.DEFAULT_MAX_WINDOW_BITS
this._acceptMaxWindowBits || common.MAX_WINDOW_BITS,
params.client_max_window_bits || common.MAX_WINDOW_BITS
);
this._peerContextTakeover = !params.server_no_context_takeover;
this._peerWindowBits = params.server_max_window_bits || common.DEFAULT_MAX_WINDOW_BITS;
this._peerWindowBits = params.server_max_window_bits || common.MAX_WINDOW_BITS;
return true;
};
+1 -1
View File
@@ -8,7 +8,7 @@ var common = {
'client_max_window_bits'
],
DEFAULT_MAX_WINDOW_BITS: 15,
MAX_WINDOW_BITS: 15,
VALID_WINDOW_BITS: [8, 9, 10, 11, 12, 13, 14, 15],
concat: function(buffers, length) {
+27 -23
View File
@@ -21,42 +21,46 @@ ServerSession.validParams = function(params) {
};
ServerSession.prototype.generateResponse = function() {
var params = {};
var response = {};
// https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.1.1
if (this._acceptNoContextTakeover || this._params.server_no_context_takeover)
params.server_no_context_takeover = true;
this._ownContextTakeover = !this._acceptNoContextTakeover &&
!this._params.server_no_context_takeover;
if (!this._ownContextTakeover) response.server_no_context_takeover = true;
// https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.1.2
if (this._requestNoContextTakeover || this._params.client_no_context_takeover)
params.client_no_context_takeover = true;
this._peerContextTakeover = !this._requestNoContextTakeover &&
!this._params.client_no_context_takeover;
if (!this._peerContextTakeover) response.client_no_context_takeover = true;
// https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.2.1
var acceptMax, serverMax;
if (this._acceptMaxWindowBits || this._params.server_max_window_bits) {
acceptMax = this._acceptMaxWindowBits || common.DEFAULT_MAX_WINDOW_BITS;
serverMax = this._params.server_max_window_bits || common.DEFAULT_MAX_WINDOW_BITS;
params.server_max_window_bits = Math.min(acceptMax, serverMax);
}
this._ownWindowBits = Math.min(this._acceptMaxWindowBits || common.MAX_WINDOW_BITS,
this._params.server_max_window_bits || common.MAX_WINDOW_BITS);
// In violation of the spec, Firefox closes the connection if it does not
// send server_max_window_bits but the server includes this in its response
if (this._ownWindowBits < common.MAX_WINDOW_BITS && this._params.server_max_window_bits)
response.server_max_window_bits = this._ownWindowBits;
// https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression#section-8.1.2.2
var clientMax = this._params.client_max_window_bits, requestMax;
if (clientMax) {
if (clientMax === true) {
if (this._requestMaxWindowBits) params.client_max_window_bits = this._requestMaxWindowBits;
} else {
requestMax = this._requestMaxWindowBits || common.DEFAULT_MAX_WINDOW_BITS;
params.client_max_window_bits = Math.min(requestMax, clientMax);
}
if (clientMax === true) clientMax = common.MAX_WINDOW_BITS;
this._peerWindowBits = Math.min(this._requestMaxWindowBits || common.MAX_WINDOW_BITS, clientMax);
} else {
this._peerWindowBits = common.MAX_WINDOW_BITS;
}
this._ownContextTakeover = !params.server_no_context_takeover;
this._ownWindowBits = params.server_max_window_bits || common.DEFAULT_MAX_WINDOW_BITS;
if (this._peerWindowBits < common.MAX_WINDOW_BITS)
response.client_max_window_bits = this._peerWindowBits;
this._peerContextTakeover = !params.client_no_context_takeover;
this._peerWindowBits = params.client_max_window_bits || common.DEFAULT_MAX_WINDOW_BITS;
return params;
return response;
};
module.exports = ServerSession;
+3 -3
View File
@@ -2,7 +2,7 @@ var PermessageDeflate = require('../lib/permessage_deflate'),
zlib = require('zlib'),
test = require('jstest').Test
test.describe("ClientSession", function() { with(this) {
test.describe("ServerSession", function() { with(this) {
before(function() { with(this) {
this.ext = PermessageDeflate.configure(options)
this.session = ext.createServerSession([offer])
@@ -183,8 +183,8 @@ test.describe("ClientSession", function() { with(this) {
define("options", {maxWindowBits: 12})
describe("with an empty offer", function() { with(this) {
it("includes server_max_window_bits in the response", function() { with(this) {
assertEqual( {server_max_window_bits: 12}, response() )
it("does not include server_max_window_bits in the response", function() { with(this) {
assertEqual( {}, response() )
}})
it("uses context takeover and 12 window bits for deflating outgoing messages", function() { with(this) {