Skip to content

Commit 698b01d

Browse files
committed
[fix] spdy should look like https when forwarding (until we get a client)
1 parent bf04bbd commit 698b01d

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

lib/node-http-proxy.js

+41-40
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ exports.createServer = function () {
7979
case 'number': port = arg; break;
8080
case 'object': options = arg || {}; break;
8181
case 'function': callback = arg; handlers.push(callback); break;
82-
}
82+
};
8383
});
84-
84+
8585
//
8686
// Helper function to create intelligent error message(s)
8787
// for the very liberal arguments parsing performed by
@@ -90,35 +90,36 @@ exports.createServer = function () {
9090
function validArguments() {
9191
var conditions = {
9292
'port and host': function () {
93-
return port && host;
93+
return port && host;
9494
},
9595
'options.target or options.router': function () {
96-
return options && (options.router ||
96+
return options && (options.router ||
9797
(options.target && options.target.host && options.target.port));
9898
},
9999
'or proxy handlers': function () {
100100
return handlers && handlers.length;
101101
}
102-
};
103-
102+
}
103+
104104
var missing = Object.keys(conditions).filter(function (name) {
105105
return !conditions[name]();
106106
});
107-
107+
108108
if (missing.length === 3) {
109109
message = 'Cannot proxy without ' + missing.join(', ');
110110
return false;
111111
}
112-
112+
113113
return true;
114-
}
115-
114+
}
115+
116116
if (!validArguments()) {
117117
//
118-
// If `host`, `port` and `options` are all not passed (with valid
118+
// If `host`, `port` and `options` are all not passed (with valid
119119
// options) then this server is improperly configured.
120120
//
121121
throw new Error(message);
122+
return;
122123
}
123124

124125
//
@@ -129,7 +130,7 @@ exports.createServer = function () {
129130
options.target = options.target || {};
130131
options.target.port = options.target.port || port;
131132
options.target.host = options.target.host || host;
132-
133+
133134
if (options.target && options.target.host && options.target.port) {
134135
//
135136
// If an explicit `host` and `port` combination has been passed
@@ -147,31 +148,31 @@ exports.createServer = function () {
147148
// we have to assume that this is a "go-anywhere" Proxy (i.e. a `RoutingProxy`).
148149
//
149150
proxy = new RoutingProxy(options);
150-
151+
151152
if (options.router) {
152153
//
153-
// If a routing table has been supplied than we assume
154+
// If a routing table has been supplied than we assume
154155
// the user intends us to add the "proxy" middleware layer
155-
// for them
156+
// for them
156157
//
157158
handlers.push(function (req, res) {
158159
proxy.proxyRequest(req, res);
159160
});
160-
161+
161162
proxy.on('routes', function (routes) {
162163
server.emit('routes', routes);
163164
});
164-
}
165+
}
165166
}
166-
167+
167168
//
168169
// Create the `http[s].Server` instance which will use
169170
// an instance of `httpProxy.HttpProxy`.
170171
//
171-
handler = handlers.length > 1
172+
handler = handlers.length > 1
172173
? exports.stack(handlers, proxy)
173174
: function (req, res) { handlers[0](req, res, proxy) };
174-
175+
175176
server = options.https
176177
? https.createServer(options.https, handler)
177178
: http.createServer(handler);
@@ -183,8 +184,8 @@ exports.createServer = function () {
183184
if (!callback) {
184185
//
185186
// If an explicit callback has not been supplied then
186-
// automagically proxy the request using the `HttpProxy`
187-
// instance we have created.
187+
// automagically proxy the request using the `HttpProxy`
188+
// instance we have created.
188189
//
189190
server.on('upgrade', function (req, socket, head) {
190191
proxy.proxyWebSocketRequest(req, socket, head);
@@ -221,7 +222,7 @@ exports.createServer = function () {
221222
//
222223
exports.buffer = function (obj) {
223224
var events = [],
224-
onData,
225+
onData,
225226
onEnd;
226227

227228
obj.on('data', onData = function (data, encoding) {
@@ -239,11 +240,11 @@ exports.buffer = function (obj) {
239240
},
240241
destroy: function () {
241242
this.end();
242-
this.resume = function () {
243-
console.error("Cannot resume buffer after destroying it.");
244-
};
245-
246-
onData = onEnd = events = obj = null;
243+
this.resume = function () {
244+
console.error("Cannot resume buffer after destroying it.");
245+
};
246+
247+
onData = onEnd = events = obj = null;
247248
},
248249
resume: function () {
249250
this.end();
@@ -277,10 +278,10 @@ exports.setMaxSockets = function (value) {
277278
//
278279
// ### function stack (middlewares, proxy)
279280
// #### @middlewares {Array} Array of functions to stack.
280-
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
281+
// #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
281282
// Iteratively build up a single handler to the `http.Server`
282283
// `request` event (i.e. `function (req, res)`) by wrapping
283-
// each middleware `layer` into a `child` middleware which
284+
// each middleware `layer` into a `child` middleware which
284285
// is in invoked by the parent (i.e. predecessor in the Array).
285286
//
286287
// adapted from https://github.com/creationix/stack
@@ -294,17 +295,17 @@ exports.stack = function stack (middlewares, proxy) {
294295
if (err) {
295296
if (res._headerSent) {
296297
res.destroy();
297-
}
298+
}
298299
else {
299300
res.statusCode = 500;
300301
res.setHeader('Content-Type', 'text/plain');
301302
res.end('Internal Server Error');
302303
}
303-
304+
304305
console.error('Error in middleware(s): %s', err.stack);
305306
return;
306307
}
307-
308+
308309
if (child) {
309310
child(req, res);
310311
}
@@ -343,29 +344,29 @@ exports._getAgent = function _getAgent (options) {
343344
if (!options || !options.host) {
344345
throw new Error('`options.host` is required to create an Agent.');
345346
}
346-
347+
347348
if (!options.port) {
348349
options.port = options.https ? 443 : 80;
349350
}
350351

351352
var Agent = options.https ? https.Agent : http.Agent,
352353
agent;
353354

354-
agent = new Agent({
355-
host: options.host,
355+
agent = new Agent({
356+
host: options.host,
356357
port: options.port
357358
});
358359

359360
agent.maxSockets = options.maxSockets || maxSockets;
360361

361362
return agent;
362-
};
363+
}
363364

364365
//
365366
// ### function _getProtocol (options)
366367
// #### @options {Object} Options for the proxy target.
367-
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
368-
// based on the `options` supplied.
368+
// Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
369+
// based on the `options` supplied.
369370
//
370371
exports._getProtocol = function _getProtocol (options) {
371372
return options.https ? https : http;
@@ -381,7 +382,7 @@ exports._getProtocol = function _getProtocol (options) {
381382
//
382383
exports._getBase = function _getBase (options) {
383384
var result = function () {};
384-
385+
385386
if (options.https && typeof options.https === 'object') {
386387
['ca', 'cert', 'key'].forEach(function (key) {
387388
if (options.https[key]) {

lib/node-http-proxy/http-proxy.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ util.inherits(HttpProxy, events.EventEmitter);
115115
// #### @res {ServerResponse} Outgoing HTTP Request to write proxied data to.
116116
// #### @buffer {Object} Result from `httpProxy.buffer(req)`
117117
//
118+
function getProto(req) {
119+
return req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http');
120+
}
118121
HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
119122
var self = this,
120123
errState = false,
@@ -148,11 +151,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
148151
}
149152

150153
if (req.headers['x-forwarded-proto']){
151-
var protoToAppend = "," + (req.connection.pair ? 'https' : 'http');
154+
var protoToAppend = "," + getProto(req);
152155
req.headers['x-forwarded-proto'] += protoToAppend;
153156
}
154157
else {
155-
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
158+
req.headers['x-forwarded-proto'] = getProto(req);
156159
}
157160
}
158161

0 commit comments

Comments
 (0)