forked from browserify/http-browserify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
137 lines (118 loc) · 3.24 KB
/
request.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var Stream = require('stream');
var Response = require('./response');
var concatStream = require('concat-stream');
var Base64 = require('Base64');
var util = require('util');
var Request = module.exports = function (xhr, params) {
var self = this;
self.writable = true;
self.xhr = xhr;
self.body = concatStream();
if (!params.host && params.hostname) {
params.host = params.hostname + (params.port ? ':' + params.port : '');
}
self.uri =
(params.scheme || 'http') + '://'
+ params.host
+ (params.path || '/')
;
xhr.open(
params.method || 'GET',
self.uri,
true
);
if (params.headers) {
var keys = objectKeys(params.headers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!self.isSafeRequestHeader(key)) continue;
var value = params.headers[key];
if (isArray(value)) {
for (var j = 0; j < value.length; j++) {
xhr.setRequestHeader(key, value[j]);
}
}
else xhr.setRequestHeader(key, value)
}
}
if (params.auth) {
//basic auth
this.setHeader('Authorization', 'Basic ' + Base64.btoa(params.auth));
}
var res = new Response;
res.on('close', function () {
self.emit('close');
});
res.on('ready', function () {
self.emit('response', res);
});
xhr.onreadystatechange = function () {
res.handle(xhr);
};
};
util.inherits(Request, Stream);
Request.prototype.setHeader = function (key, value) {
if (isArray(value)) {
for (var i = 0; i < value.length; i++) {
this.xhr.setRequestHeader(key, value[i]);
}
}
else {
this.xhr.setRequestHeader(key, value);
}
};
Request.prototype.write = function (s) {
this.body.write(s);
};
Request.prototype.destroy = function (s) {
this.xhr.abort();
this.emit('close');
};
Request.prototype.end = function (s) {
if (s !== undefined) this.body.write(s);
this.body.end()
this.xhr.send(this.body.getBody());
};
// Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html
Request.unsafeHeaders = [
"accept-charset",
"accept-encoding",
"access-control-request-headers",
"access-control-request-method",
"connection",
"content-length",
"cookie",
"cookie2",
"content-transfer-encoding",
"date",
"expect",
"host",
"keep-alive",
"origin",
"referer",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"user-agent",
"via"
];
Request.prototype.isSafeRequestHeader = function (headerName) {
if (!headerName) return false;
return indexOf(Request.unsafeHeaders, headerName.toLowerCase()) === -1;
};
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
};
var isArray = Array.isArray || function (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
var indexOf = function (xs, x) {
if (xs.indexOf) return xs.indexOf(x);
for (var i = 0; i < xs.length; i++) {
if (xs[i] === x) return i;
}
return -1;
};