[Pkg-javascript-commits] [node-stream-http] 112/208: Use 'http:' as a default protocol

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:34 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stream-http.

commit c36da3bade632e46b4db25e440f0d295257431cf
Author: John Hiesey <john at hiesey.com>
Date:   Sat Jan 9 03:50:54 2016 +0100

    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.
---
 index.js                     |  7 ++++++-
 test/node/http-browserify.js | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/index.js b/index.js
index eedc173..829290c 100644
--- a/index.js
+++ b/index.js
@@ -11,7 +11,12 @@ http.request = function (opts, cb) {
 	else
 		opts = extend(opts)
 
-	var protocol = opts.protocol || ''
+	// Normally, the page is loaded from http or https, so not specifying a protocol
+	// will result in a (valid) protocol-relative url. However, this won't work if
+	// the protocol is something else, like 'file:'
+	var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
+
+	var protocol = opts.protocol || defaultProtocol
 	var host = opts.hostname || opts.host
 	var port = opts.port
 	var path = opts.path || '/'
diff --git a/test/node/http-browserify.js b/test/node/http-browserify.js
index 3ca7d39..fda8512 100644
--- a/test/node/http-browserify.js
+++ b/test/node/http-browserify.js
@@ -64,6 +64,25 @@ test('Test alt protocol', function(t) {
 	t.end()
 })
 
+test('Test page with \'file:\' protocol', function (t) {
+	var params = {
+		hostname: 'localhost',
+		port: 3000,
+		path: '/bar'
+	}
+
+	var fileLocation = 'file:///home/me/stuff/index.html'
+
+	var normalLocation = global.location
+	global.location = url.parse(fileLocation) // Temporarily change the location
+	var request = http.get(params, noop)
+	global.location = normalLocation // Reset the location
+
+	var resolved = url.resolve(fileLocation, request._opts.url)
+	t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
+	t.end()
+})
+
 test('Test string as parameters', function(t) {
 	var testUrl = '/api/foo'
 	var request = http.get(testUrl, noop)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git



More information about the Pkg-javascript-commits mailing list