Skip to content

Commit 77d536c

Browse files
authored
Merge pull request #14 from launchdarkly/eb/sc-136154/url-properties
clean up HTTP request options logic to avoid including extra URL properties (and fix user/password)
2 parents fbe0086 + 63a95fe commit 77d536c

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

lib/eventsource.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ function EventSource (url, eventSourceInitDict) {
6868
config.jitterRatio ? retryDelay.defaultJitter(config.jitterRatio) : null
6969
)
7070

71-
function makeRequestOptions () {
72-
var options = parse(url)
73-
if (config.skipDefaultHeaders) {
74-
options.headers = {}
75-
} else {
76-
options.headers = { 'Cache-Control': 'no-cache', 'Accept': 'text/event-stream' }
71+
function makeRequestUrlAndOptions () {
72+
// Returns { url, options }; url is null/undefined if the URL properties are in options
73+
var actualUrl = url
74+
var options = { headers: {} }
75+
if (!config.skipDefaultHeaders) {
76+
options.headers['Cache-Control'] = 'no-cache'
77+
options.headers['Accept'] = 'text/event-stream'
7778
}
7879
if (lastEventId) options.headers['Last-Event-ID'] = lastEventId
7980
if (config.headers) {
80-
for (var i in config.headers) {
81-
var header = config.headers[i]
82-
if (header) {
83-
options.headers[i] = header
81+
for (var key in config.headers) {
82+
if (config.headers.hasOwnProperty(key)) {
83+
options.headers[key] = config.headers[key]
8484
}
8585
}
8686
}
@@ -91,15 +91,19 @@ function EventSource (url, eventSourceInitDict) {
9191

9292
// If specify http proxy, make the request to sent to the proxy server,
9393
// and include the original url in path and Host headers
94-
var useProxy = config.proxy
95-
if (useProxy) {
94+
if (config.proxy) {
95+
actualUrl = null
96+
var parsedUrl = parse(url)
9697
var proxy = parse(config.proxy)
9798
options.protocol = proxy.protocol === 'https:' ? 'https:' : 'http:'
9899
options.path = url
99-
options.headers.Host = options.host
100+
options.headers.Host = parsedUrl.host
100101
options.hostname = proxy.hostname
101102
options.host = proxy.host
102103
options.port = proxy.port
104+
if (proxy.username) {
105+
options.auth = proxy.username + ':' + proxy.password
106+
}
103107
}
104108

105109
// When running in Node, proxies can also be specified as an agent
@@ -130,7 +134,7 @@ function EventSource (url, eventSourceInitDict) {
130134
options.method = config.method
131135
}
132136

133-
return options
137+
return { url: actualUrl, options: options }
134138
}
135139

136140
function defaultErrorFilter (error) {
@@ -179,10 +183,11 @@ function EventSource (url, eventSourceInitDict) {
179183
}
180184

181185
function connect () {
182-
var options = makeRequestOptions()
183-
var isSecure = options.protocol === 'https:'
186+
var urlAndOptions = makeRequestUrlAndOptions()
187+
var isSecure = urlAndOptions.options.protocol === 'https:' ||
188+
(urlAndOptions.url && urlAndOptions.url.startsWith('https:'))
184189

185-
req = (isSecure ? https : http).request(options, function (res) {
190+
var callback = function (res) {
186191
// Handle HTTP redirects
187192
if (res.statusCode === 301 || res.statusCode === 307) {
188193
if (!res.headers.location) {
@@ -279,7 +284,11 @@ function EventSource (url, eventSourceInitDict) {
279284
buf = buf.slice(pos)
280285
}
281286
})
282-
})
287+
}
288+
var api = isSecure ? https : http
289+
req = urlAndOptions.url ?
290+
api.request(urlAndOptions.url, urlAndOptions.options, callback) :
291+
api.request(urlAndOptions.options, callback)
283292

284293
if (config.readTimeoutMillis) {
285294
req.setTimeout(config.readTimeoutMillis)

0 commit comments

Comments
 (0)