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

remove require('url') in favor of URL #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const http = require('http')
const https = require('https')
const once = require('once')
const querystring = require('querystring')
const url = require('url')

const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'

Expand All @@ -16,10 +15,10 @@ function simpleGet (opts, cb) {
cb = once(cb)

if (opts.url) {
const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
const { hostname, port, protocol, username, password, pathname, href } = new URL(opts.url)
if (username || password) opts.auth = `${username}:${password}`
delete opts.url
if (!hostname && !port && !protocol && !auth) opts.path = path // Relative redirect
else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
Object.assign(opts, { hostname, port, protocol, path: pathname, href })
}

const headers = { 'accept-encoding': 'gzip, deflate' }
Expand Down Expand Up @@ -48,13 +47,12 @@ function simpleGet (opts, cb) {
const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
const req = protocol.request(opts, res => {
if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
opts.url = res.headers.location // Follow 3xx redirects
opts.url = new URL(res.headers.location, opts.href) // Follow 3xx redirects
delete opts.headers.host // Discard `host` header on redirect (see #32)
res.resume() // Discard response

const redirectHost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
// If redirected host is different than original host, drop headers to prevent cookie leak (#73)
if (redirectHost !== null && redirectHost !== originalHost) {
if (opts.url.hostname !== originalHost) {
delete opts.headers.cookie
delete opts.headers.authorization
}
Expand Down
23 changes: 23 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,26 @@ test('rewrite POST redirects to GET', function (t) {
})
})
})

test('simple get hostname + url', function (t) {
t.plan(5)

const server = http.createServer(function (req, res) {
t.equal(req.url, '/path')
res.statusCode = 200
res.end('response')
})

server.listen(0, function () {
const port = server.address().port
get({ host: 'localhost', port, url: '/path' }, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
concat(res, function (err, data) {
t.error(err)
t.equal(data.toString(), 'response')
server.close()
})
})
})
})
32 changes: 32 additions & 0 deletions test/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,35 @@ test('redirect should clear explicitly specified `Host` (note uppercase) header'
})
})
})

test('follow redirects without "url" option', function (t) {
t.plan(15)

let num = 0
const server = http.createServer(function (req, res) {
t.equal(req.url, '/' + num, 'visited /' + num)

if (num < 10) {
num += 1
res.statusCode = 301
res.setHeader('Location', '/' + num)
res.end()
} else {
res.statusCode = 200
res.end('response')
}
})

server.listen(0, function () {
const port = server.address().port
get({ hostname: 'localhost', port, path: '/0' }, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
concat(res, function (err, data) {
t.error(err)
t.equal(data.toString(), 'response')
server.close()
})
})
})
})