Skip to content

Commit ae1a0fe

Browse files
authored
Use HTTPS for protocol-less URLs (#405)
Previously it used HTTP. I think we should have safe defaults.
1 parent b9bf2e6 commit ae1a0fe

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"pify": "^3.0.0",
6767
"safe-buffer": "^5.0.1",
6868
"timed-out": "^4.0.0",
69-
"url-parse-lax": "^1.0.0",
69+
"url-parse-lax": "^3.0.0",
7070
"url-to-options": "^1.0.1"
7171
},
7272
"devDependencies": {

readme.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $ npm install got
4444
const fs = require('fs');
4545
const got = require('got');
4646

47-
got('todomvc.com')
47+
got('sindresorhus.com')
4848
.then(response => {
4949
console.log(response.body);
5050
//=> '<!doctype html> ...'
@@ -55,10 +55,10 @@ got('todomvc.com')
5555
});
5656

5757
// Streams
58-
got.stream('todomvc.com').pipe(fs.createWriteStream('index.html'));
58+
got.stream('sindresorhus.com').pipe(fs.createWriteStream('index.html'));
5959

6060
// For POST, PUT and PATCH methods got.stream returns a WritableStream
61-
fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com'));
61+
fs.createReadStream('index.html').pipe(got.stream.post('sindresorhus.com'));
6262
```
6363

6464

@@ -82,6 +82,8 @@ The URL to request as simple string, a [`http.request` options](https://nodejs.o
8282

8383
Properties from `options` will override properties in the parsed `url`.
8484

85+
If no protocol is specified, it will default to `https`.
86+
8587
##### options
8688

8789
Type: `Object`
@@ -233,7 +235,7 @@ If it's not possible to retrieve the body size (can happen when streaming), `tot
233235
**Note**: Progress events can also be used with promises.
234236

235237
```js
236-
got('todomvc.com')
238+
got('sindresorhus.com')
237239
.on('downloadProgress', progress => {
238240
// Report download progress
239241
})
@@ -343,11 +345,11 @@ const got = require('got');
343345
const map = new Map();
344346

345347
(async () => {
346-
let response = await got('todomvc.com', {cache: map});
348+
let response = await got('sindresorhus.com', {cache: map});
347349
console.log(response.fromCache);
348350
//=> false
349351

350-
response = await got('todomvc.com', {cache: map});
352+
response = await got('sindresorhus.com', {cache: map});
351353
console.log(response.fromCache);
352354
//=> true
353355
})();
@@ -365,7 +367,7 @@ const KeyvRedis = require('@keyv/redis');
365367

366368
const redis = new KeyvRedis('redis://user:pass@localhost:6379');
367369

368-
got('todomvc.com', {cache: redis});
370+
got('sindresorhus.com', {cache: redis});
369371
```
370372

371373
Got supports anything that follows the Map API, so it's easy to write your own storage adapter or use a third-party solution.
@@ -380,7 +382,7 @@ const storageAdapter = require('./my-storage-adapter');
380382
const QuickLRU = require('quick-lru');
381383
const storageAdapter = new QuickLRU({maxSize: 1000});
382384

383-
got('todomvc.com', {cache: storageAdapter});
385+
got('sindresorhus.com', {cache: storageAdapter});
384386
```
385387

386388
View the [Keyv docs](https://github.com/lukechilds/keyv) for more information on how to use storage adapters.
@@ -394,7 +396,7 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with t
394396
const got = require('got');
395397
const tunnel = require('tunnel');
396398

397-
got('todomvc.com', {
399+
got('sindresorhus.com', {
398400
agent: tunnel.httpOverHttp({
399401
proxy: {
400402
host: 'localhost'
@@ -550,7 +552,7 @@ It's a good idea to set the `'user-agent'` header so the provider can more easil
550552
const got = require('got');
551553
const pkg = require('./package.json');
552554

553-
got('todomvc.com', {
555+
got('sindresorhus.com', {
554556
headers: {
555557
'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
556558
}

test/http.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ test('simple request', async t => {
3636
t.is((await got(s.url)).body, 'ok');
3737
});
3838

39-
test('protocol-less URLs', async t => {
40-
t.is((await got(s.url.replace(/^http:\/\//, ''))).body, 'ok');
41-
});
42-
4339
test('empty response', async t => {
4440
t.is((await got(`${s.url}/empty`)).body, '');
4541
});

test/https.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ test('make request to https server with ca', async t => {
5353
t.is(body, 'ok');
5454
});
5555

56+
test('protocol-less URLs default to HTTPS', async t => {
57+
const {body, requestUrl} = await got(s.url.replace(/^https:\/\//, ''), {
58+
ca: caRootCert,
59+
headers: {host: 'sindresorhus.com'}
60+
});
61+
t.is(body, 'ok');
62+
t.true(requestUrl.startsWith('https://'));
63+
});
64+
5665
test.after('cleanup', async () => {
5766
await s.close();
5867
});

0 commit comments

Comments
 (0)