| 6785 | } |
| 6786 | |
| 6787 | function request(opts, cb) { |
| 6788 | if (typeof opts === 'string') |
| 6789 | opts = urlParse(opts); |
| 6790 | |
| 6791 | |
| 6792 | // Normally, the page is loaded from http or https, so not specifying a protocol |
| 6793 | // will result in a (valid) protocol-relative url. However, this won't work if |
| 6794 | // the protocol is something else, like 'file:' |
| 6795 | var defaultProtocol = global$1.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''; |
| 6796 | |
| 6797 | var protocol = opts.protocol || defaultProtocol; |
| 6798 | var host = opts.hostname || opts.host; |
| 6799 | var port = opts.port; |
| 6800 | var path = opts.path || '/'; |
| 6801 | |
| 6802 | // Necessary for IPv6 addresses |
| 6803 | if (host && host.indexOf(':') !== -1) |
| 6804 | host = '[' + host + ']'; |
| 6805 | |
| 6806 | // This may be a relative url. The browser should always be able to interpret it correctly. |
| 6807 | opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path; |
| 6808 | opts.method = (opts.method || 'GET').toUpperCase(); |
| 6809 | opts.headers = opts.headers || {}; |
| 6810 | |
| 6811 | // Also valid opts.auth, opts.mode |
| 6812 | |
| 6813 | var req = new ClientRequest(opts); |
| 6814 | if (cb) |
| 6815 | req.on('response', cb); |
| 6816 | return req |
| 6817 | } |
| 6818 | |
| 6819 | function get(opts, cb) { |
| 6820 | var req = request(opts, cb); |