-
-
Save w3fs/f20e9cdb3283b84c66ed9ee8f790f2bb to your computer and use it in GitHub Desktop.
Node.js `http.Agent` class implementations...
This file contains hidden or 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
| /** | |
| * Module dependencies. | |
| */ | |
| var net = require('net'); | |
| var inherits = require('util').inherits; | |
| var EventEmitter = require('events').EventEmitter; | |
| /** | |
| * Module exports. | |
| */ | |
| module.exports = Agent; | |
| /** | |
| * Base HTTP "Agent" class. Emulates the node-core `http.Agent` class, but | |
| * implemented in a way that can be easily extended for additional functionality. | |
| * | |
| * This base implementation does no socket pooling, and opens | |
| * a new connection for every HTTP request. | |
| * | |
| * It behaves more-or-less like `agent: false`. | |
| * | |
| * @api public | |
| */ | |
| function Agent () { | |
| if (!(this instanceof Agent)) return new Agent(); | |
| EventEmitter.call(this); | |
| } | |
| inherits(Agent, EventEmitter); | |
| /** | |
| * Called by node-core's "_http_client.js" module when creating a new HTTP request | |
| * with this Agent instance. | |
| * | |
| * @api public | |
| */ | |
| Agent.prototype.addRequest = function (req, host, port, localAddress) { | |
| // hint to use "Connection: close" | |
| req.shouldKeepAlive = false; | |
| // create the `net.Socket` instance | |
| var opts = {}; | |
| opts.port = port; | |
| opts.host = host; | |
| if (localAddress) opts.localAddress = localAddress; | |
| this.createConnection(opts, function (err, socket) { | |
| if (err) { | |
| req.emit('error', err); | |
| } else { | |
| req.onSocket(socket); | |
| } | |
| }); | |
| }; | |
| /** | |
| * Creates and returns a `net.Socket` instance to use for an HTTP request. | |
| * | |
| * @api public | |
| */ | |
| Agent.prototype.createConnection = function (opts, fn) { | |
| var socket = net.connect(opts); | |
| fn(null, socket); | |
| return socket; | |
| }; |
This file contains hidden or 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
| /** | |
| * Module dependencies. | |
| */ | |
| var net = require('net'); | |
| var url = require('url'); | |
| var Agent = require('./agent'); | |
| var inherits = require('util').inherits; | |
| /** | |
| * Module exports. | |
| */ | |
| module.exports = HttpProxyAgent; | |
| /** | |
| * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the | |
| * specified "HTTP proxy server" in order to proxy HTTP requests. | |
| * | |
| * @api public | |
| */ | |
| function HttpProxyAgent (opts) { | |
| if (!(this instanceof HttpProxyAgent)) return new HttpProxyAgent(opts); | |
| if ('string' == typeof opts) opts = url.parse(opts); | |
| Agent.call(this); | |
| this.proxy = opts; | |
| } | |
| inherits(HttpProxyAgent, Agent); | |
| /** | |
| * Called when the node-core HTTP client library is creating a new HTTP request. | |
| * | |
| * @api public | |
| */ | |
| HttpProxyAgent.prototype.addRequest = function (req, host, port, localAddress) { | |
| // change the `http.ClientRequest` instance's "path" field | |
| // to the absolute path of the URL that will be requested | |
| var absolute = url.format({ | |
| protocol: 'http:', | |
| hostname: host, | |
| port: port, | |
| pathname: req.path | |
| }); | |
| req.path = absolute; | |
| Agent.prototype.addRequest.apply(this, arguments); | |
| }; | |
| /** | |
| * Initiates a TCP connection to the specified HTTP proxy server. | |
| * | |
| * @api public | |
| */ | |
| HttpProxyAgent.prototype.createConnection = function (opts) { | |
| return net.connect({ | |
| host: this.proxy.hostname, | |
| port: this.proxy.port | |
| }); | |
| }; |
This file contains hidden or 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
| /** | |
| * Usage example of the `Agent` base class. | |
| */ | |
| var url = require('url'); | |
| var http = require('http'); | |
| var Agent = require('./agent'); | |
| var endpoint = process.argv[2] || 'http://nodejs.org/api/'; | |
| var opts = url.parse(endpoint); | |
| opts.agent = new Agent(); | |
| var req = http.get(opts, function (res) { | |
| console.log('"response" event!', res.headers); | |
| res.pipe(process.stdout); | |
| }); |
This file contains hidden or 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
| /** | |
| * Usage example of the `HttpProxyAgent` class proxying an HTTP endpoint. | |
| */ | |
| var url = require('url'); | |
| var http = require('http'); | |
| var HttpProxyAgent = require('./http-proxy-agent'); | |
| // HTTP proxy to connect to | |
| var proxy = process.env.http_proxy || 'http://168.63.76.32:3128'; | |
| proxy = url.parse(proxy); | |
| // HTTP endpoint for the proxy to connect to | |
| var endpoint = process.argv[2] || 'http://nodejs.org/api/'; | |
| var opts = url.parse(endpoint); | |
| // create an instance of the `HttpProxyAgent` class with the proxy server information | |
| var agent = new HttpProxyAgent(proxy); | |
| opts.agent = agent; | |
| http.get(opts, function (res) { | |
| console.log('"response" event!', res.headers); | |
| res.pipe(process.stdout); | |
| }); |
This file contains hidden or 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
| /** | |
| * Usage example of the `HttpsProxyAgent` class proxying an | |
| * HTTPS (SSL) endpoint over an HTTP (unencrypted) proxy. | |
| * | |
| * WARNING: This is a VERY DANGEROUS thing to do! | |
| */ | |
| var url = require('url'); | |
| var https = require('https'); | |
| var HttpsProxyAgent = require('./https-proxy-agent'); | |
| // HTTP proxy to connect to | |
| var proxy = process.env.http_proxy || 'http://168.63.76.32:3128'; | |
| proxy = url.parse(proxy); | |
| // HTTPS endpoint for the proxy to connect to | |
| var endpoint = process.argv[2] || 'https://gist.github.com/TooTallNate/5952254/raw/07972db49b5b70a212c39dfee56ed3ab82f8179c/agent.js'; | |
| var opts = url.parse(endpoint); | |
| // create an instance of the `HttpsProxyAgent` class with the proxy server information | |
| opts.agent = new HttpsProxyAgent(proxy); | |
| https.get(opts, function (res) { | |
| console.log('"response" event!', res.headers); | |
| res.pipe(process.stdout); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment