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

Deprecate url.parse API in favor of URL constructor #11

Open
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use strict'

const parseUrl = require('url').parse
const PROTOCOL_PLACEHOLDER = 'protocol-placeholder:'
const { URL, parse } = require('url')
const parseForwarded = require('forwarded-parse')
const net = require('net')

module.exports = function (req) {
const raw = req.originalUrl || req.url
const url = parseUrl(raw || '')
const url = parsePartialURL(raw)
const secure = req.secure || (req.connection && req.connection.encrypted)
const result = { raw: raw }
let host
let host, forwarded

if (req.headers.forwarded) {
let forwarded = getFirstHeader(req, 'forwarded')
forwarded = getFirstHeader(req, 'forwarded')
try {
// Always choose the original (first) Forwarded pair in case the request
// passed through multiple proxies
Expand All @@ -23,7 +24,6 @@ module.exports = function (req) {
const port = conn[conn.length - 1].split(':')[1]
if (port) host.port = Number(port)
}
if (forwarded.proto) host.protocol = forwarded.proto + ':'
} catch (e) {}
} else if (req.headers['x-forwarded-host']) {
host = parsePartialURL(getFirstHeader(req, 'x-forwarded-host'))
Expand All @@ -38,13 +38,14 @@ module.exports = function (req) {
}

// protocol
if (url.protocol) result.protocol = url.protocol
if (url.protocol !== PROTOCOL_PLACEHOLDER) result.protocol = url.protocol
else if (req.headers['x-forwarded-proto']) result.protocol = getFirstHeader(req, 'x-forwarded-proto') + ':'
else if (req.headers['x-forwarded-protocol']) result.protocol = getFirstHeader(req, 'x-forwarded-protocol') + ':'
else if (req.headers['x-url-scheme']) result.protocol = getFirstHeader(req, 'x-url-scheme') + ':'
else if (req.headers['front-end-https']) result.protocol = getFirstHeader(req, 'front-end-https') === 'on' ? 'https:' : 'http:'
else if (req.headers['x-forwarded-ssl']) result.protocol = getFirstHeader(req, 'x-forwarded-ssl') === 'on' ? 'https:' : 'http:'
else if (host.protocol) result.protocol = host.protocol
else if (forwarded && forwarded.proto) result.protocol = forwarded.proto + ':'
else if (host.protocol && host.protocol !== PROTOCOL_PLACEHOLDER) result.protocol = host.protocol
else if (secure) result.protocol = 'https:'
else result.protocol = 'http:'

Expand Down Expand Up @@ -92,7 +93,10 @@ function getFirstHeader (req, header) {

function parsePartialURL (url) {
const containsProtocol = url.indexOf('://') !== -1
const result = parseUrl(containsProtocol ? url : 'invalid://' + url)
if (!containsProtocol) result.protocol = ''
return result
const urlToParse = containsProtocol ? url : PROTOCOL_PLACEHOLDER + '//' + url
try {
return new URL(urlToParse)
} catch (e) {
return parse(urlToParse)
}
}
65 changes: 65 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ test('http - root, no special http headers', function (t) {
})
})

test('http - double slash, no special http headers', function (t) {
t.plan(1)
http({path: '//some/path'}, function (result, port) {
t.deepEqual(result, {
protocol: 'http:',
hostname: 'localhost',
port: port,
pathname: '//some/path',
full: 'http://localhost:' + port + '//some/path',
raw: '//some/path'
})
})
})

test('http - auth like, no special http headers', function (t) {
t.plan(1)
http({path: '//user@pass'}, function (result, port) {
t.deepEqual(result, {
protocol: 'http:',
hostname: 'localhost',
port: port,
pathname: '//user@pass',
full: 'http://localhost:' + port + '//user@pass',
raw: '//user@pass'
})
})
})

test('http - path, no special http headers', function (t) {
t.plan(1)
http({path: '/some/path'}, function (result, port) {
Expand Down Expand Up @@ -115,6 +143,34 @@ test('https - path, no special http headers', function (t) {
})
})

test('http - double slash, no special http headers', function (t) {
t.plan(1)
https({path: '//some/path'}, function (result, port) {
t.deepEqual(result, {
protocol: 'https:',
hostname: 'localhost',
port: port,
pathname: '//some/path',
full: 'https://localhost:' + port + '//some/path',
raw: '//some/path'
})
})
})

test('http - auth like, no special http headers', function (t) {
t.plan(1)
https({path: '//user@pass'}, function (result, port) {
t.deepEqual(result, {
protocol: 'https:',
hostname: 'localhost',
port: port,
pathname: '//user@pass',
full: 'https://localhost:' + port + '//user@pass',
raw: '//user@pass'
})
})
})

test('https - path+query params, no special http headers', function (t) {
t.plan(1)
https({path: '/some/path?key=value'}, function (result, port) {
Expand Down Expand Up @@ -691,6 +747,15 @@ test('No Host header', function (t) {
t.end()
})

test('Weird auth string', function (t) {
const mockReq = {url: '@@', headers: {}, connection: {}}
t.deepEqual(originalUrl(mockReq), {
protocol: 'http:',
raw: '@@'
})
t.end()
})

/**
* Utils
*/
Expand Down