Browse Source

support protocol-less URLs

http2
Sindre Sorhus 10 years ago
parent
commit
0b6d7da87a
  1. 3
      index.js
  2. 1
      package.json
  3. 8
      readme.md
  4. 8
      test/test-http.js

3
index.js

@ -10,6 +10,7 @@ var read = require('read-all-stream');
var timeout = require('timed-out'); var timeout = require('timed-out');
var urlLib = require('url'); var urlLib = require('url');
var zlib = require('zlib'); var zlib = require('zlib');
var prependHttp = require('prepend-http');
function got(url, opts, cb) { function got(url, opts, cb) {
if (typeof opts === 'function') { if (typeof opts === 'function') {
@ -53,7 +54,7 @@ function got(url, opts, cb) {
var redirectCount = 0; var redirectCount = 0;
var get = function (url, opts, cb) { 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 fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts); var arg = assign({}, parsedUrl, opts);

1
package.json

@ -42,6 +42,7 @@
"infinity-agent": "^0.1.0", "infinity-agent": "^0.1.0",
"isstream": "^0.1.1", "isstream": "^0.1.1",
"object-assign": "^2.0.0", "object-assign": "^2.0.0",
"prepend-http": "^1.0.0",
"read-all-stream": "^0.1.0", "read-all-stream": "^0.1.0",
"timed-out": "^2.0.0" "timed-out": "^2.0.0"
}, },

8
readme.md

@ -22,17 +22,17 @@ $ npm install --save got
var got = require('got'); var got = require('got');
// Callback mode // Callback mode
got('http://todomvc.com', function (err, data, res) { got('todomvc.com', function (err, data, res) {
console.log(data); console.log(data);
//=> <!doctype html> ... //=> <!doctype html> ...
}); });
// Stream mode // 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 // 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 ### API
@ -117,7 +117,7 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with t
var got = require('got'); var got = require('got');
var tunnel = require('tunnel'); var tunnel = require('tunnel');
got('http://todomvc.com', { got('todomvc.com', {
agent: tunnel.httpsOverHttp({ agent: tunnel.httpsOverHttp({
proxy: { proxy: {
host: 'localhost' host: 'localhost'

8
test/test-http.js

@ -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) { tape('empty response', function (t) {
got(s.url + '/empty', function (err, data) { got(s.url + '/empty', function (err, data) {
t.error(err); t.error(err);

Loading…
Cancel
Save