-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.js
100 lines (87 loc) · 2.5 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var middleware = require('./middleware')
var http = require('http')
var https = require('https')
var proxyForUrl = require('proxy-from-env').getProxyForUrl
var URL = require('url').URL
var merge = require('../merge')
var HttpsProxyAgent = require('https-proxy-agent')
var obfuscateUrlPassword = require('../obfuscateUrlPassword')
function nodeRequest (request, options, protocol, withResponse) {
if (protocol === 'https:') {
return https.request(merge(request, options.https), withResponse)
} else {
return http.request(merge(request, options.http), withResponse)
}
}
function proxyUrl (request, proxy) {
var url = new URL(request.url)
var proxyUrl = new URL(proxy)
request.headers.host = url.hostname
if (url.protocol === 'https:') {
url.agent = new HttpsProxyAgent(proxy)
return url
} else {
if (proxyUrl.auth) {
request.headers['proxy-authorization'] = 'Basic ' + Buffer.from(proxyUrl.auth).toString('base64')
}
var split = request.url.split('?')
var pathname = split[0]
var search = split[1] || ''
return {
hostname: proxyUrl.hostname,
port: proxyUrl.port,
protocol: proxyUrl.protocol,
pathname: pathname,
search: search
}
}
}
function parseUrl (request) {
var proxy = proxyForUrl(request.url) || request.options.proxy
if (proxy) {
return proxyUrl(request, proxy)
} else {
return new URL(request.url)
}
}
module.exports = middleware('http', function (request) {
return new Promise(function (resolve, reject) {
var url = parseUrl(request)
var req = nodeRequest(
{
hostname: url.hostname,
port: url.port,
method: request.method,
path: url.pathname + url.search,
headers: request.headers,
agent: url.agent
},
request.options,
url.protocol,
function (res) {
return resolve({
statusCode: res.statusCode,
statusText: http.STATUS_CODES[res.statusCode],
url: request.url,
headers: res.headers,
body: res
})
}
)
if (request.options.timeout) {
req.setTimeout(request.options.timeout)
req.on('timeout', function () {
var msg = request.method.toUpperCase() + ' ' + obfuscateUrlPassword(request.url) + ' => timeout (' + request.options.timeout + 'ms)'
reject(new Error(msg))
})
}
req.on('error', function (e) {
reject(e)
})
if (request.body) {
request.body.pipe(req)
} else {
req.end()
}
})
})