Skip to content

Commit

Permalink
support protocol-less URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 5, 2015
1 parent ed2c26c commit 0b6d7da
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var read = require('read-all-stream');
var timeout = require('timed-out');
var urlLib = require('url');
var zlib = require('zlib');
var prependHttp = require('prepend-http');

function got(url, opts, cb) {
if (typeof opts === 'function') {
Expand Down Expand Up @@ -53,7 +54,7 @@ function got(url, opts, cb) {
var redirectCount = 0;

var get = function (url, opts, cb) {
var parsedUrl = urlLib.parse(url);
var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"infinity-agent": "^0.1.0",
"isstream": "^0.1.1",
"object-assign": "^2.0.0",
"prepend-http": "^1.0.0",
"read-all-stream": "^0.1.0",
"timed-out": "^2.0.0"
},
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ $ npm install --save got
var got = require('got');

// Callback mode
got('http://todomvc.com', function (err, data, res) {
got('todomvc.com', function (err, data, res) {
console.log(data);
//=> <!doctype html> ...
});


// Stream mode
got('http://todomvc.com').pipe(fs.createWriteStream('index.html'));
got('todomvc.com').pipe(fs.createWriteStream('index.html'));

// For POST, PUT and PATCH methods got returns a WritableStream
fs.createReadStream('index.html').pipe(got.post('http://todomvc.com'));
fs.createReadStream('index.html').pipe(got.post('todomvc.com'));
```

### API
Expand Down Expand Up @@ -117,7 +117,7 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with t
var got = require('got');
var tunnel = require('tunnel');

got('http://todomvc.com', {
got('todomvc.com', {
agent: tunnel.httpsOverHttp({
proxy: {
host: 'localhost'
Expand Down
8 changes: 8 additions & 0 deletions test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ tape('callback mode', function (t) {
});
});

tape('protocol-less URLs', function (t) {
got(s.url.replace(/^http:\/\//, ''), function (err, data) {
t.error(err);
t.equal(data, 'ok');
t.end();
});
});

tape('empty response', function (t) {
got(s.url + '/empty', function (err, data) {
t.error(err);
Expand Down

0 comments on commit 0b6d7da

Please sign in to comment.