1
|
var ClientRequest = require('./lib/request')
|
2
|
var response = require('./lib/response')
|
3
|
var extend = require('xtend')
|
4
|
var statusCodes = require('builtin-status-codes')
|
5
|
var url = require('url')
|
6
|
|
7
|
var http = exports
|
8
|
|
9
|
http.request = function (opts, cb) {
|
10
|
if (typeof opts === 'string')
|
11
|
opts = url.parse(opts)
|
12
|
else
|
13
|
opts = extend(opts)
|
14
|
|
15
|
// Normally, the page is loaded from http or https, so not specifying a protocol
|
16
|
// will result in a (valid) protocol-relative url. However, this won't work if
|
17
|
// the protocol is something else, like 'file:'
|
18
|
var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
|
19
|
|
20
|
var protocol = opts.protocol || defaultProtocol
|
21
|
var host = opts.hostname || opts.host
|
22
|
var port = opts.port
|
23
|
var path = opts.path || '/'
|
24
|
|
25
|
// Necessary for IPv6 addresses
|
26
|
if (host && host.indexOf(':') !== -1)
|
27
|
host = '[' + host + ']'
|
28
|
|
29
|
// This may be a relative url. The browser should always be able to interpret it correctly.
|
30
|
opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
|
31
|
opts.method = (opts.method || 'GET').toUpperCase()
|
32
|
opts.headers = opts.headers || {}
|
33
|
|
34
|
// Also valid opts.auth, opts.mode
|
35
|
|
36
|
var req = new ClientRequest(opts)
|
37
|
if (cb)
|
38
|
req.on('response', cb)
|
39
|
return req
|
40
|
}
|
41
|
|
42
|
http.get = function get (opts, cb) {
|
43
|
var req = http.request(opts, cb)
|
44
|
req.end()
|
45
|
return req
|
46
|
}
|
47
|
|
48
|
http.ClientRequest = ClientRequest
|
49
|
http.IncomingMessage = response.IncomingMessage
|
50
|
|
51
|
http.Agent = function () {}
|
52
|
http.Agent.defaultMaxSockets = 4
|
53
|
|
54
|
http.globalAgent = new http.Agent()
|
55
|
|
56
|
http.STATUS_CODES = statusCodes
|
57
|
|
58
|
http.METHODS = [
|
59
|
'CHECKOUT',
|
60
|
'CONNECT',
|
61
|
'COPY',
|
62
|
'DELETE',
|
63
|
'GET',
|
64
|
'HEAD',
|
65
|
'LOCK',
|
66
|
'M-SEARCH',
|
67
|
'MERGE',
|
68
|
'MKACTIVITY',
|
69
|
'MKCOL',
|
70
|
'MOVE',
|
71
|
'NOTIFY',
|
72
|
'OPTIONS',
|
73
|
'PATCH',
|
74
|
'POST',
|
75
|
'PROPFIND',
|
76
|
'PROPPATCH',
|
77
|
'PURGE',
|
78
|
'PUT',
|
79
|
'REPORT',
|
80
|
'SEARCH',
|
81
|
'SUBSCRIBE',
|
82
|
'TRACE',
|
83
|
'UNLOCK',
|
84
|
'UNSUBSCRIBE'
|
85
|
]
|