Skip to content

Commit c36da3b

Browse files
committed
Use 'http:' as a default protocol
If global.location.protocol isn't 'http:' or 'https:', use 'http:'. This makes behavior reasonable when the page is opened directly without using a server.
1 parent cbc0bd8 commit c36da3b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Diff for: index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ http.request = function (opts, cb) {
1111
else
1212
opts = extend(opts)
1313

14-
var protocol = opts.protocol || ''
14+
// Normally, the page is loaded from http or https, so not specifying a protocol
15+
// will result in a (valid) protocol-relative url. However, this won't work if
16+
// the protocol is something else, like 'file:'
17+
var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
18+
19+
var protocol = opts.protocol || defaultProtocol
1520
var host = opts.hostname || opts.host
1621
var port = opts.port
1722
var path = opts.path || '/'

Diff for: test/node/http-browserify.js

+19
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ test('Test alt protocol', function(t) {
6464
t.end()
6565
})
6666

67+
test('Test page with \'file:\' protocol', function (t) {
68+
var params = {
69+
hostname: 'localhost',
70+
port: 3000,
71+
path: '/bar'
72+
}
73+
74+
var fileLocation = 'file:///home/me/stuff/index.html'
75+
76+
var normalLocation = global.location
77+
global.location = url.parse(fileLocation) // Temporarily change the location
78+
var request = http.get(params, noop)
79+
global.location = normalLocation // Reset the location
80+
81+
var resolved = url.resolve(fileLocation, request._opts.url)
82+
t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
83+
t.end()
84+
})
85+
6786
test('Test string as parameters', function(t) {
6887
var testUrl = '/api/foo'
6988
var request = http.get(testUrl, noop)

0 commit comments

Comments
 (0)