xhr.js
3.85 KB
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
var AWS = require('../core');
var EventEmitter = require('events').EventEmitter;
require('../http');
/**
* @api private
*/
AWS.XHRClient = AWS.util.inherit({
handleRequest: function handleRequest(httpRequest, httpOptions, callback, errCallback) {
var self = this;
var endpoint = httpRequest.endpoint;
var emitter = new EventEmitter();
var href = endpoint.protocol + '//' + endpoint.hostname;
if (endpoint.port !== 80 && endpoint.port !== 443) {
href += ':' + endpoint.port;
}
href += httpRequest.path;
var xhr = new XMLHttpRequest(), headersEmitted = false;
httpRequest.stream = xhr;
xhr.addEventListener('readystatechange', function() {
try {
if (xhr.status === 0) return; // 0 code is invalid
} catch (e) { return; }
if (this.readyState >= this.HEADERS_RECEIVED && !headersEmitted) {
emitter.statusCode = xhr.status;
emitter.headers = self.parseHeaders(xhr.getAllResponseHeaders());
emitter.emit(
'headers',
emitter.statusCode,
emitter.headers,
xhr.statusText
);
headersEmitted = true;
}
if (this.readyState === this.DONE) {
self.finishRequest(xhr, emitter);
}
}, false);
xhr.upload.addEventListener('progress', function (evt) {
emitter.emit('sendProgress', evt);
});
xhr.addEventListener('progress', function (evt) {
emitter.emit('receiveProgress', evt);
}, false);
xhr.addEventListener('timeout', function () {
errCallback(AWS.util.error(new Error('Timeout'), {code: 'TimeoutError'}));
}, false);
xhr.addEventListener('error', function () {
errCallback(AWS.util.error(new Error('Network Failure'), {
code: 'NetworkingError'
}));
}, false);
xhr.addEventListener('abort', function () {
errCallback(AWS.util.error(new Error('Request aborted'), {
code: 'RequestAbortedError'
}));
}, false);
callback(emitter);
xhr.open(httpRequest.method, href, httpOptions.xhrAsync !== false);
AWS.util.each(httpRequest.headers, function (key, value) {
if (key !== 'Content-Length' && key !== 'User-Agent' && key !== 'Host') {
xhr.setRequestHeader(key, value);
}
});
if (httpOptions.timeout && httpOptions.xhrAsync !== false) {
xhr.timeout = httpOptions.timeout;
}
if (httpOptions.xhrWithCredentials) {
xhr.withCredentials = true;
}
try { xhr.responseType = 'arraybuffer'; } catch (e) {}
try {
if (httpRequest.body) {
xhr.send(httpRequest.body);
} else {
xhr.send();
}
} catch (err) {
if (httpRequest.body && typeof httpRequest.body.buffer === 'object') {
xhr.send(httpRequest.body.buffer); // send ArrayBuffer directly
} else {
throw err;
}
}
return emitter;
},
parseHeaders: function parseHeaders(rawHeaders) {
var headers = {};
AWS.util.arrayEach(rawHeaders.split(/\r?\n/), function (line) {
var key = line.split(':', 1)[0];
var value = line.substring(key.length + 2);
if (key.length > 0) headers[key.toLowerCase()] = value;
});
return headers;
},
finishRequest: function finishRequest(xhr, emitter) {
var buffer;
if (xhr.responseType === 'arraybuffer' && xhr.response) {
var ab = xhr.response;
buffer = new AWS.util.Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
}
try {
if (!buffer && typeof xhr.responseText === 'string') {
buffer = new AWS.util.Buffer(xhr.responseText);
}
} catch (e) {}
if (buffer) emitter.emit('data', buffer);
emitter.emit('end');
}
});
/**
* @api private
*/
AWS.HttpClient.prototype = AWS.XHRClient.prototype;
/**
* @api private
*/
AWS.HttpClient.streamsApiVersion = 1;