Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var http = module.exports;
2
var EventEmitter = require('events').EventEmitter;
3
var Request = require('./lib/request');
4
var url = require('url')
5
6
http.request = function (params, cb) {
7
if (typeof params === 'string') {
8
params = url.parse(params)
9
}
10
if (!params) params = {};
11
if (!params.host && !params.port) {
12
params.port = parseInt(window.location.port, 10);
13
}
14
if (!params.host && params.hostname) {
15
params.host = params.hostname;
16
}
17
18
if (!params.protocol) {
19
if (params.scheme) {
20
params.protocol = params.scheme + ':';
21
} else {
22
params.protocol = window.location.protocol;
23
}
24
}
25
26
if (!params.host) {
27
params.host = window.location.hostname || window.location.host;
28
}
29
if (/:/.test(params.host)) {
30
if (!params.port) {
31
params.port = params.host.split(':')[1];
32
}
33
params.host = params.host.split(':')[0];
34
}
35
if (!params.port) params.port = params.protocol == 'https:' ? 443 : 80;
36
37
var req = new Request(new xhrHttp, params);
38
if (cb) req.on('response', cb);
39
return req;
40
};
41
42
http.get = function (params, cb) {
43
params.method = 'GET';
44
var req = http.request(params, cb);
45
req.end();
46
return req;
47
};
48
49
http.Agent = function () {};
50
http.Agent.defaultMaxSockets = 4;
51
52
var xhrHttp = (function () {
53
if (typeof window === 'undefined') {
54
throw new Error('no window object present');
55
}
56
else if (window.XMLHttpRequest) {
57
return window.XMLHttpRequest;
58
}
59
else if (window.ActiveXObject) {
60
var axs = [
61
'Msxml2.XMLHTTP.6.0',
62
'Msxml2.XMLHTTP.3.0',
63
'Microsoft.XMLHTTP'
64
];
65
for (var i = 0; i < axs.length; i++) {
66
try {
67
var ax = new(window.ActiveXObject)(axs[i]);
68
return function () {
69
if (ax) {
70
var ax_ = ax;
71
ax = null;
72
return ax_;
73
}
74
else {
75
return new(window.ActiveXObject)(axs[i]);
76
}
77
};
78
}
79
catch (e) {}
80
}
81
throw new Error('ajax not supported in this browser')
82
}
83
else {
84
throw new Error('ajax not supported in this browser');
85
}
86
})();
87
88
http.STATUS_CODES = {
89
100 : 'Continue',
90
101 : 'Switching Protocols',
91
102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
92
200 : 'OK',
93
201 : 'Created',
94
202 : 'Accepted',
95
203 : 'Non-Authoritative Information',
96
204 : 'No Content',
97
205 : 'Reset Content',
98
206 : 'Partial Content',
99
207 : 'Multi-Status', // RFC 4918
100
300 : 'Multiple Choices',
101
301 : 'Moved Permanently',
102
302 : 'Moved Temporarily',
103
303 : 'See Other',
104
304 : 'Not Modified',
105
305 : 'Use Proxy',
106
307 : 'Temporary Redirect',
107
400 : 'Bad Request',
108
401 : 'Unauthorized',
109
402 : 'Payment Required',
110
403 : 'Forbidden',
111
404 : 'Not Found',
112
405 : 'Method Not Allowed',
113
406 : 'Not Acceptable',
114
407 : 'Proxy Authentication Required',
115
408 : 'Request Time-out',
116
409 : 'Conflict',
117
410 : 'Gone',
118
411 : 'Length Required',
119
412 : 'Precondition Failed',
120
413 : 'Request Entity Too Large',
121
414 : 'Request-URI Too Large',
122
415 : 'Unsupported Media Type',
123
416 : 'Requested Range Not Satisfiable',
124
417 : 'Expectation Failed',
125
418 : 'I\'m a teapot', // RFC 2324
126
422 : 'Unprocessable Entity', // RFC 4918
127
423 : 'Locked', // RFC 4918
128
424 : 'Failed Dependency', // RFC 4918
129
425 : 'Unordered Collection', // RFC 4918
130
426 : 'Upgrade Required', // RFC 2817
131
428 : 'Precondition Required', // RFC 6585
132
429 : 'Too Many Requests', // RFC 6585
133
431 : 'Request Header Fields Too Large',// RFC 6585
134
500 : 'Internal Server Error',
135
501 : 'Not Implemented',
136
502 : 'Bad Gateway',
137
503 : 'Service Unavailable',
138
504 : 'Gateway Time-out',
139
505 : 'HTTP Version Not Supported',
140
506 : 'Variant Also Negotiates', // RFC 2295
141
507 : 'Insufficient Storage', // RFC 4918
142
509 : 'Bandwidth Limit Exceeded',
143
510 : 'Not Extended', // RFC 2774
144
511 : 'Network Authentication Required' // RFC 6585
145
};
146