2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-14 18:42:33 +03:00

Merge branch 'master' of github.com:mzabriskie/axios into xDomainRequestSupport

Conflicts:
	lib/adapters/xhr.js
This commit is contained in:
Vineet Hawal
2015-11-20 15:21:04 +05:30
19 changed files with 439 additions and 321 deletions
+22 -6
View File
@@ -2,11 +2,12 @@
var defaults = require('./../defaults');
var utils = require('./../utils');
var buildUrl = require('./../helpers/buildUrl');
var buildURL = require('./../helpers/buildURL');
var transformData = require('./../helpers/transformData');
var http = require('http');
var https = require('https');
var http = require('follow-redirects').http;
var https = require('follow-redirects').https;
var url = require('url');
var zlib = require('zlib');
var pkg = require('./../../package.json');
var Buffer = require('buffer').Buffer;
@@ -50,7 +51,7 @@ module.exports = function httpAdapter(resolve, reject, config) {
var options = {
host: parsed.hostname,
port: parsed.port,
path: buildUrl(parsed.path, config.params).replace(/^\?/, ''),
path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
method: config.method,
headers: headers,
agent: config.agent
@@ -59,12 +60,27 @@ module.exports = function httpAdapter(resolve, reject, config) {
// Create the request
var transport = parsed.protocol === 'https:' ? https : http;
var req = transport.request(options, function (res) {
// uncompress the response body transparently if required
var stream = res;
switch(res.headers['content-encoding']) {
case 'gzip':
case 'compress':
case 'deflate': {
// add the unzipper to the body stream processing pipeline
stream = stream.pipe(zlib.createUnzip());
// remove the content-encoding in order to not confuse downstream operations
delete res.headers['content-encoding'];
}
}
var responseBuffer = [];
res.on('data', function (chunk) {
stream.on('data', function (chunk) {
responseBuffer.push(chunk);
});
res.on('end', function () {
stream.on('end', function () {
var data = Buffer.concat(responseBuffer);
if (config.responseType !== 'arraybuffer') {
data = data.toString('utf8');
+8 -8
View File
@@ -4,9 +4,10 @@
var defaults = require('./../defaults');
var utils = require('./../utils');
var buildUrl = require('./../helpers/buildUrl');
var buildURL = require('./../helpers/buildURL');
var parseHeaders = require('./../helpers/parseHeaders');
var transformData = require('./../helpers/transformData');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
module.exports = function xhrAdapter(resolve, reject, config) {
// Transform request data
@@ -27,12 +28,12 @@ module.exports = function xhrAdapter(resolve, reject, config) {
delete requestHeaders['Content-Type']; // Let the browser set it
}
var adapter = (XMLHttpRequest || ActiveXObject),
loadEvent = 'onreadystatechange',
xDomain = false;
var adapter = (XMLHttpRequest || ActiveXObject);
var loadEvent = 'onreadystatechange';
var xDomain = false;
// For IE 8/9 CORS support
if(config.xDomain && window.XDomainRequest){
if(!isURLSameOrigin(config.url) && window.XDomainRequest){
adapter = window.XDomainRequest;
loadEvent = 'onload';
xDomain = true;
@@ -40,7 +41,6 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Create the request
var request = new adapter('Microsoft.XMLHTTP');
request.open(config.method.toUpperCase(), buildUrl(config.url, config.params, config.paramsSerializer), true);
// Set the request timeout in MS
@@ -78,10 +78,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Specifically not if we're in a web worker, or react-native.
if (utils.isStandardBrowserEnv()) {
var cookies = require('./../helpers/cookies');
var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
// Add xsrf header
var xsrfValue = urlIsSameOrigin(config.url) ?
var xsrfValue = isURLSameOrigin(config.url) ?
cookies.read(config.xsrfCookieName || defaults.xsrfCookieName) :
undefined;