56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
var ClientRequest = require('./lib/request')
|
|
var extend = require('xtend')
|
|
var statusCodes = require('builtin-status-codes')
|
|
var url = require('url')
|
|
|
|
var http = exports
|
|
|
|
http.request = function (opts, cb) {
|
|
if (typeof opts === 'string')
|
|
opts = url.parse(opts)
|
|
else
|
|
opts = extend(opts)
|
|
|
|
// Split opts.host into its components
|
|
var hostHostname = opts.host ? opts.host.split(':')[0] : null
|
|
var hostPort = opts.host ? parseInt(opts.host.split(':')[1], 10) : null
|
|
|
|
opts.method = opts.method || 'GET'
|
|
opts.headers = opts.headers || {}
|
|
opts.path = opts.path || '/'
|
|
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
|
|
|
|
if (opts.withCredentials === undefined)
|
|
opts.withCredentials = true
|
|
|
|
// Also valid opts.auth, opts.mode
|
|
|
|
var req = new ClientRequest(opts)
|
|
if (cb)
|
|
req.on('response', cb)
|
|
return req
|
|
}
|
|
|
|
http.get = function get (opts, cb) {
|
|
var req = http.request(opts, cb)
|
|
req.end()
|
|
return req
|
|
}
|
|
|
|
http.Agent = function () {}
|
|
http.Agent.defaultMaxSockets = 4
|
|
|
|
http.STATUS_CODES = statusCodes
|
|
|
|
http.METHODS = [
|
|
'GET',
|
|
'POST',
|
|
'PUT',
|
|
'DELETE' // TODO: include the methods from RFC 2616 and 2518?
|
|
]
|