Skip to content

Commit

Permalink
Merge pull request #3 from ntoshev/master
Browse files Browse the repository at this point in the history
Using c-ares-backed DNS resolution
  • Loading branch information
andris9 committed Mar 7, 2012
2 parents c779ff4 + f92aa47 commit 7990923
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Possible option values
* **outputEncoding** valid for `fetchUrl`
* **disableDecoding** valid for `fetchUrl`, set to true to disable automatic charset decoding to utf-8
* **overrideCharset** valid for `fetchUrl`, set input encoding
* **asyncDnsLoookup** use high performance asyncronous DNS resolution based on c-ares instead of a thread pool calling getaddrinfo(3)
* **timeout** set a timeout in ms
* **agent** pass-through http.request agent parameter


## Meta object

Expand Down
32 changes: 32 additions & 0 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var http = require("http"),
urllib = require("url"),
utillib = require("util"),
zlib = require('zlib'),
dns = require('dns'),
Stream = require("stream").Stream,
CookieJar = require("./cookiejar").CookieJar,
Iconv = require("iconv").Iconv;
Expand All @@ -16,6 +17,9 @@ maxResponseLength : Infinity
method: GET
payload: str
disableGzip: false
asyncDnsLoookup: false
timeout: null
agent: undefined
cookies: ['name=val']
Expand Down Expand Up @@ -239,6 +243,30 @@ FetchStream.prototype.runStream = function(url){
url_data.urloptions.headers['content-length'] = this.options.payloadSize;
}

url_data.urloptions.agent = this.options.agent;

if (this.options.asyncDnsLoookup) {
var dnsCallback = (function (err, adresses){
if (err) {
this.emit("error", err);
return;
}

url_data.urloptions.headers['host'] = url_data.urloptions.hostname+url_data.urloptions.port;
url_data.urloptions.hostname = adresses[0];
url_data.urloptions.host=url_data.urloptions.hostname + (url_data.urloptions.port? ':' + url_data.urloptions.port: '');

this._runStream(url_data, url);
}).bind(this);

dns.resolve4(url_data.urloptions.host, dnsCallback);
} else {
this._runStream(url_data, url);
}
}

FetchStream.prototype._runStream = function(url_data, url){

var req = url_data.transport.request(url_data.urloptions, (function(res) {

// catch new cookies before potential redirect
Expand Down Expand Up @@ -329,6 +357,10 @@ FetchStream.prototype.runStream = function(url){
this.emit("error", e);
}).bind(this));

if (this.options.timeout) {
req.setTimeout(this.options.timeout, req.abort.bind(req));
}

if(this.options.payload){
req.end(this.options.payload);
}else if(this.options.payloadStream){
Expand Down

0 comments on commit 7990923

Please sign in to comment.