Blame view

node_modules/stream-http/index.js 1.84 KB
aaac7fed   liuqimichale   add
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
  var ClientRequest = require('./lib/request')
  var response = require('./lib/response')
  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)
  
  	// Normally, the page is loaded from http or https, so not specifying a protocol
  	// will result in a (valid) protocol-relative url. However, this won't work if
  	// the protocol is something else, like 'file:'
  	var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
  
  	var protocol = opts.protocol || defaultProtocol
  	var host = opts.hostname || opts.host
  	var port = opts.port
  	var path = opts.path || '/'
  
  	// Necessary for IPv6 addresses
  	if (host && host.indexOf(':') !== -1)
  		host = '[' + host + ']'
  
  	// This may be a relative url. The browser should always be able to interpret it correctly.
  	opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
  	opts.method = (opts.method || 'GET').toUpperCase()
  	opts.headers = opts.headers || {}
  
  	// 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.ClientRequest = ClientRequest
  http.IncomingMessage = response.IncomingMessage
  
  http.Agent = function () {}
  http.Agent.defaultMaxSockets = 4
  
  http.globalAgent = new http.Agent()
  
  http.STATUS_CODES = statusCodes
  
  http.METHODS = [
  	'CHECKOUT',
  	'CONNECT',
  	'COPY',
  	'DELETE',
  	'GET',
  	'HEAD',
  	'LOCK',
  	'M-SEARCH',
  	'MERGE',
  	'MKACTIVITY',
  	'MKCOL',
  	'MOVE',
  	'NOTIFY',
  	'OPTIONS',
  	'PATCH',
  	'POST',
  	'PROPFIND',
  	'PROPPATCH',
  	'PURGE',
  	'PUT',
  	'REPORT',
  	'SEARCH',
  	'SUBSCRIBE',
  	'TRACE',
  	'UNLOCK',
  	'UNSUBSCRIBE'
  ]