This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
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
Andris Reinman
committed
Apr 16, 2012
1 parent
79c21d6
commit 4c0d4a3
Showing
5 changed files
with
141 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var http = require("http"), | ||
https = require("https"), | ||
urllib = require("url"), | ||
Stream = require('stream'); | ||
|
||
/** | ||
* @namespace URLFetch | ||
* @name urlfetch | ||
*/ | ||
module.exports = openUrlStream; | ||
|
||
/** | ||
* <p>Open a stream to a specified URL</p> | ||
* | ||
* @memberOf urlfetch | ||
* @param {String} url URL to open | ||
* @param {Object} [options] Optional options object | ||
* @param {String} [options.userAgent="mailcomposer"] User Agent for the request | ||
* @return {Stream} Stream for the URL contents | ||
*/ | ||
function openUrlStream(url, options){ | ||
options = options || {}; | ||
var urlparts = urllib.parse(url), | ||
urloptions = { | ||
host: urlparts.hostname, | ||
port: urlparts.port || (urlparts.protocol=="https:"?443:80), | ||
path: urlparts.path, | ||
method: "GET", | ||
headers: { | ||
"User-Agent": options.userAgent || "mailcomposer" | ||
} | ||
}, | ||
client = (urlparts.protocol=="https:"?https:http), | ||
stream = new Stream(), | ||
request; | ||
|
||
stream.resume = function(){}; | ||
|
||
if(urlparts.auth){ | ||
urloptions.auth = urlparts.auth; | ||
} | ||
|
||
request = client.request(urloptions, function(response) { | ||
if((response.statusCode || 0).toString().charAt(0) != "2"){ | ||
stream.emit("error", "Invalid status code " + (response.statusCode || 0)); | ||
return; | ||
} | ||
|
||
response.on('error', function(err) { | ||
stream.emit("error", err); | ||
}); | ||
|
||
response.on('data', function(chunk) { | ||
stream.emit("data", chunk); | ||
}); | ||
|
||
response.on('end', function(chunk) { | ||
if(chunk){ | ||
stream.emit("data", chunk); | ||
} | ||
stream.emit("end"); | ||
}); | ||
}); | ||
request.end(); | ||
|
||
request.on('error', function(err) { | ||
stream.emit("error", err); | ||
}); | ||
|
||
return stream; | ||
} |
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