[Pkg-javascript-commits] [node-stream-http] 25/208: Rework url logic

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:24 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 0e1bfedfa1e06fb72d3495691211dc9d8495bac5
Author: John Hiesey <john at hiesey.com>
Date:   Tue Jul 7 13:15:26 2015 -0700

    Rework url logic
---
 index.js       | 15 +++++++--------
 lib/request.js | 11 +++--------
 2 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/index.js b/index.js
index bc3deb5..ff212dc 100644
--- a/index.js
+++ b/index.js
@@ -17,15 +17,14 @@ http.request = function (opts, cb) {
 	opts.method = opts.method || 'GET'
 	opts.headers = opts.headers || {}
 	opts.path = opts.path || '/'
-	// If the hostname is specified, use that. If it isn't, just let the browser's
-	// logic figure it out
-	opts.hostname = opts.hostname || hostHostname
-	// If port is specified, use that. If it isn't, just let the browser's logic
-	// figure it out; i.e. use port 80/443 for absolute urls and window.location.port
-	// for relative urls
-	opts.port = opts.port || hostPort
+	opts.protocol = opts.protocol || window.location.protocol
+	// If the hostname is provided, use the default port for the protocol. If
+	// the url is instead relative, use window.location.port
+	var defaultPort = (opts.hostname || hostHostname) ? (opts.protocol === 'https:' ? 443 : 80) : window.location.port
+	opts.hostname = opts.hostname || hostHostname || window.location.hostname
+	opts.port = opts.port || hostPort || defaultPort
 
-	// Also valid: opts.protocol, opts.auth, opts.credentials
+	// Also valid opts.auth, opts.credentials, opts.mode
 
 	var req = new ClientRequest(opts)
 	if (cb)
diff --git a/lib/request.js b/lib/request.js
index af03097..28a874d 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -30,6 +30,7 @@ var ClientRequest = module.exports = function (opts) {
 	stream.Writable.call(self)
 
 	self._opts = opts
+	self._url = opts.protocol + '//' + opts.hostname + ':' + opts.port + opts.path
 	self._body = []
 	self._headers = {}
 	foreach(keys(opts.headers), function (name) {
@@ -80,12 +81,6 @@ ClientRequest.prototype._onFinish = function () {
 	var self = this
 
 	var opts = self._opts
-	var url = ''
-	// If hostname is provided, include protocol, hostname, port
-	if (opts.hostname)
-		url = (opts.protocol || '') + '//' + opts.hostname + (opts.port ? (':' + opots.port) : '')
-
-	url += opts.path
 
 	var user, pass
 	if (opts.auth) {
@@ -114,7 +109,7 @@ ClientRequest.prototype._onFinish = function () {
 			return [headersObj[name].name, headersObj[name].value]
 		})
 
-		window.fetch(url, {
+		window.fetch(self._url, {
 			method: self._opts.method,
 			headers: headers,
 			body: body,
@@ -126,7 +121,7 @@ ClientRequest.prototype._onFinish = function () {
 		})
 	} else {
 		var xhr = self._xhr = new window.XMLHttpRequest()
-		xhr.open(self._opts.method, url, true, user, pass)
+		xhr.open(self._opts.method, self._url, true, user, pass)
 
 		// Can't set responseType on really old browsers
 		if ('responseType' in xhr)

-- 
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