Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using c-ares-backed DNS resolution #3

Merged
merged 6 commits into from
Mar 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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