-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var common = exports; | ||
|
||
/** | ||
* Copies the right headers from `options` and `req` to | ||
* `outgoing` which is then used to fire the proxied | ||
* request. | ||
* | ||
* Examples: | ||
* | ||
* common.setupOutgoing(outgoing, options, req) | ||
* // => { host: ..., hostname: ...} | ||
* | ||
* @param {Object} Outgoing Base object to be filled with required properties | ||
* @param {Object} Options Config object passed to the proxy | ||
* @param {ClientRequest} Req Request Object | ||
* | ||
* @return {Object} Outgoing Object with all required properties set | ||
* | ||
* @api private | ||
*/ | ||
|
||
common.setupOutgoing = function(outgoing, options, req) { | ||
['host', 'hostname', 'port', 'socketPath', 'agent'].forEach( | ||
function(e) { outgoing[e] = options[e]; } | ||
); | ||
|
||
['method', 'path', 'headers'].forEach( | ||
function(e) { outgoing[e] = req[e]; } | ||
); | ||
|
||
return outgoing; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,79 @@ | ||
var Writable = require('stream').Writable, | ||
common = require('../common'), | ||
http = require('http'), | ||
https = require('https'); | ||
|
||
module.exports = ForwardStream; | ||
|
||
/** | ||
* Forwards the request to the external target specified in options | ||
* | ||
* Examples: | ||
* | ||
* new ForwardStream(options) | ||
* // => { ... } | ||
* | ||
* @param {Object} Options Config object passed to the proxy | ||
* | ||
* @return {ForwardStream} Stream A clone of ForwardStream | ||
* | ||
* @api private | ||
*/ | ||
|
||
function ForwardStream() { | ||
|
||
} | ||
Writable.call(this); | ||
|
||
this.once('pipe', this.onPipe); | ||
this.once('finish', this.onFinish); | ||
} | ||
|
||
/** | ||
* Fires up the request to the external target | ||
* | ||
* Examples: | ||
* | ||
* (new ForwardStream(options)).onPipe(req) | ||
* // => undefined | ||
* | ||
* @param {HttpRequest} Req Request object | ||
* | ||
* @api private | ||
*/ | ||
|
||
ForwardStream.prototype.onPipe = function(request) { | ||
this.forwardReq = (options.ssl ? https : http).request( | ||
common.setupOutgoing(options.ssl || {}, options, request); | ||
); | ||
}; | ||
|
||
/** | ||
* Closes forwarded request when `pipe` is finished | ||
* | ||
* Examples: | ||
* | ||
* (new ForwardStream(options)).onFinish() | ||
* // => undefined | ||
* | ||
* @api private | ||
*/ | ||
|
||
ForwardStream.prototype.onFinish = function() { | ||
this.forwardReq.end(); | ||
}; | ||
|
||
/** | ||
* Implements `stream.Writable`, writes to the forwarded request | ||
* | ||
* Examples: | ||
* | ||
* (new ForwardStream(options))._write(chunk, encoding, clb) | ||
* // => undefined | ||
* | ||
* @api private | ||
*/ | ||
|
||
ForwardStream.prototype._write = function(chunk, encoding, clb) { | ||
this.forwardReq.write(chunk, encoding, clb); | ||
}; | ||
|
||
require('util').inherits(ForwardStream, Writable); |