Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
3d65057d
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3d65057d
编写于
11月 19, 2015
作者:
M
Matt Zabriskie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Making isURLSameOrigin/cookies safe to use in all envs
上级
affe3aaa
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
146 addition
and
115 deletion
+146
-115
lib/adapters/http.js
lib/adapters/http.js
+2
-2
lib/adapters/xhr.js
lib/adapters/xhr.js
+4
-4
lib/helpers/buildUrl.js
lib/helpers/buildUrl.js
+1
-1
lib/helpers/cookies.js
lib/helpers/cookies.js
+49
-39
lib/helpers/isURLSameOrigin.js
lib/helpers/isURLSameOrigin.js
+68
-0
lib/helpers/urlIsSameOrigin.js
lib/helpers/urlIsSameOrigin.js
+0
-58
test/specs/helpers/buildUrl.spec.js
test/specs/helpers/buildUrl.spec.js
+11
-11
test/specs/helpers/isURLSameOrigin.spec.js
test/specs/helpers/isURLSameOrigin.spec.js
+11
-0
未找到文件。
lib/adapters/http.js
浏览文件 @
3d65057d
...
...
@@ -2,7 +2,7 @@
var
defaults
=
require
(
'
./../defaults
'
);
var
utils
=
require
(
'
./../utils
'
);
var
buildU
rl
=
require
(
'
./../helpers/buildUrl
'
);
var
buildU
RL
=
require
(
'
./../helpers/buildURL
'
);
var
transformData
=
require
(
'
./../helpers/transformData
'
);
var
http
=
require
(
'
http
'
);
var
https
=
require
(
'
https
'
);
...
...
@@ -50,7 +50,7 @@ module.exports = function httpAdapter(resolve, reject, config) {
var
options
=
{
host
:
parsed
.
hostname
,
port
:
parsed
.
port
,
path
:
buildU
rl
(
parsed
.
path
,
config
.
params
).
replace
(
/^
\?
/
,
''
),
path
:
buildU
RL
(
parsed
.
path
,
config
.
params
).
replace
(
/^
\?
/
,
''
),
method
:
config
.
method
,
headers
:
headers
,
agent
:
config
.
agent
...
...
lib/adapters/xhr.js
浏览文件 @
3d65057d
...
...
@@ -4,7 +4,7 @@
var
defaults
=
require
(
'
./../defaults
'
);
var
utils
=
require
(
'
./../utils
'
);
var
buildU
rl
=
require
(
'
./../helpers/buildUrl
'
);
var
buildU
RL
=
require
(
'
./../helpers/buildURL
'
);
var
parseHeaders
=
require
(
'
./../helpers/parseHeaders
'
);
var
transformData
=
require
(
'
./../helpers/transformData
'
);
...
...
@@ -29,7 +29,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// Create the request
var
request
=
new
(
XMLHttpRequest
||
ActiveXObject
)(
'
Microsoft.XMLHTTP
'
);
request
.
open
(
config
.
method
.
toUpperCase
(),
buildU
rl
(
config
.
url
,
config
.
params
,
config
.
paramsSerializer
),
true
);
request
.
open
(
config
.
method
.
toUpperCase
(),
buildU
RL
(
config
.
url
,
config
.
params
,
config
.
paramsSerializer
),
true
);
// Set the request timeout in MS
request
.
timeout
=
config
.
timeout
;
...
...
@@ -67,10 +67,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/urlIs
SameOrigin
'
);
var
isURLSameOrigin
=
require
(
'
./../helpers/isURL
SameOrigin
'
);
// Add xsrf header
var
xsrfValue
=
urlIs
SameOrigin
(
config
.
url
)
?
var
xsrfValue
=
isURL
SameOrigin
(
config
.
url
)
?
cookies
.
read
(
config
.
xsrfCookieName
||
defaults
.
xsrfCookieName
)
:
undefined
;
...
...
lib/helpers/buildUrl.js
浏览文件 @
3d65057d
...
...
@@ -20,7 +20,7 @@ function encode(val) {
* @param {object} [params] The params to be appended
* @returns {string} The formatted url
*/
module
.
exports
=
function
buildU
rl
(
url
,
params
,
paramsSerializer
)
{
module
.
exports
=
function
buildU
RL
(
url
,
params
,
paramsSerializer
)
{
if
(
!
params
)
{
return
url
;
}
...
...
lib/helpers/cookies.js
浏览文件 @
3d65057d
'
use strict
'
;
/**
* WARNING:
* This file makes references to objects that aren't safe in all environments.
* Please see lib/utils.isStandardBrowserEnv before including this file.
*/
var
utils
=
require
(
'
./../utils
'
);
module
.
exports
=
{
write
:
function
write
(
name
,
value
,
expires
,
path
,
domain
,
secure
)
{
var
cookie
=
[];
cookie
.
push
(
name
+
'
=
'
+
encodeURIComponent
(
value
));
if
(
utils
.
isNumber
(
expires
))
{
cookie
.
push
(
'
expires=
'
+
new
Date
(
expires
).
toGMTString
());
}
if
(
utils
.
isString
(
path
))
{
cookie
.
push
(
'
path=
'
+
path
);
}
if
(
utils
.
isString
(
domain
))
{
cookie
.
push
(
'
domain=
'
+
domain
);
}
if
(
secure
===
true
)
{
cookie
.
push
(
'
secure
'
);
}
document
.
cookie
=
cookie
.
join
(
'
;
'
);
},
read
:
function
read
(
name
)
{
var
match
=
document
.
cookie
.
match
(
new
RegExp
(
'
(^|;
\\
s*)(
'
+
name
+
'
)=([^;]*)
'
));
return
(
match
?
decodeURIComponent
(
match
[
3
])
:
null
);
},
remove
:
function
remove
(
name
)
{
this
.
write
(
name
,
''
,
Date
.
now
()
-
86400000
);
}
};
module
.
exports
=
(
utils
.
isStandardBrowserEnv
()
?
// Standard browser envs support document.cookie
(
function
()
{
return
{
write
:
function
write
(
name
,
value
,
expires
,
path
,
domain
,
secure
)
{
var
cookie
=
[];
cookie
.
push
(
name
+
'
=
'
+
encodeURIComponent
(
value
));
if
(
utils
.
isNumber
(
expires
))
{
cookie
.
push
(
'
expires=
'
+
new
Date
(
expires
).
toGMTString
());
}
if
(
utils
.
isString
(
path
))
{
cookie
.
push
(
'
path=
'
+
path
);
}
if
(
utils
.
isString
(
domain
))
{
cookie
.
push
(
'
domain=
'
+
domain
);
}
if
(
secure
===
true
)
{
cookie
.
push
(
'
secure
'
);
}
document
.
cookie
=
cookie
.
join
(
'
;
'
);
},
read
:
function
read
(
name
)
{
var
match
=
document
.
cookie
.
match
(
new
RegExp
(
'
(^|;
\\
s*)(
'
+
name
+
'
)=([^;]*)
'
));
return
(
match
?
decodeURIComponent
(
match
[
3
])
:
null
);
},
remove
:
function
remove
(
name
)
{
this
.
write
(
name
,
''
,
Date
.
now
()
-
86400000
);
}
};
})()
:
// Non standard browser env (web workers, react-native) lack needed support.
(
function
()
{
return
{
write
:
function
write
()
{},
read
:
function
read
()
{
return
null
;
},
remove
:
function
remove
()
{}
};
})()
);
lib/helpers/isURLSameOrigin.js
0 → 100644
浏览文件 @
3d65057d
'
use strict
'
;
var
utils
=
require
(
'
./../utils
'
);
module
.
exports
=
(
utils
.
isStandardBrowserEnv
()
?
// Standard browser envs have full support of the APIs needed to test
// whether the request URL is of the same origin as current location.
(
function
()
{
var
msie
=
/
(
msie|trident
)
/i
.
test
(
navigator
.
userAgent
);
var
urlParsingNode
=
document
.
createElement
(
'
a
'
);
var
originURL
;
/**
* Parse a URL to discover it's components
*
* @param {String} url The URL to be parsed
* @returns {Object}
*/
function
resolveURL
(
url
)
{
var
href
=
url
;
if
(
msie
)
{
// IE needs attribute set twice to normalize properties
urlParsingNode
.
setAttribute
(
'
href
'
,
href
);
href
=
urlParsingNode
.
href
;
}
urlParsingNode
.
setAttribute
(
'
href
'
,
href
);
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
return
{
href
:
urlParsingNode
.
href
,
protocol
:
urlParsingNode
.
protocol
?
urlParsingNode
.
protocol
.
replace
(
/:$/
,
''
)
:
''
,
host
:
urlParsingNode
.
host
,
search
:
urlParsingNode
.
search
?
urlParsingNode
.
search
.
replace
(
/^
\?
/
,
''
)
:
''
,
hash
:
urlParsingNode
.
hash
?
urlParsingNode
.
hash
.
replace
(
/^#/
,
''
)
:
''
,
hostname
:
urlParsingNode
.
hostname
,
port
:
urlParsingNode
.
port
,
pathname
:
(
urlParsingNode
.
pathname
.
charAt
(
0
)
===
'
/
'
)
?
urlParsingNode
.
pathname
:
'
/
'
+
urlParsingNode
.
pathname
};
}
originURL
=
resolveURL
(
window
.
location
.
href
);
/**
* Determine if a URL shares the same origin as the current location
*
* @param {String} requestURL The URL to test
* @returns {boolean} True if URL shares the same origin, otherwise false
*/
return
function
isURLSameOrigin
(
requestURL
)
{
var
parsed
=
(
utils
.
isString
(
requestURL
))
?
resolveURL
(
requestURL
)
:
requestURL
;
return
(
parsed
.
protocol
===
originURL
.
protocol
&&
parsed
.
host
===
originURL
.
host
);
};
})()
:
// Non standard browser envs (web workers, react-native) lack needed support.
(
function
()
{
return
function
isURLSameOrigin
()
{
return
true
;
};
})()
);
lib/helpers/urlIsSameOrigin.js
已删除
100644 → 0
浏览文件 @
affe3aaa
'
use strict
'
;
/**
* WARNING:
* This file makes references to objects that aren't safe in all environments.
* Please see lib/utils.isStandardBrowserEnv before including this file.
*/
var
utils
=
require
(
'
./../utils
'
);
var
msie
=
/
(
msie|trident
)
/i
.
test
(
navigator
.
userAgent
);
var
urlParsingNode
=
document
.
createElement
(
'
a
'
);
var
originUrl
;
/**
* Parse a URL to discover it's components
*
* @param {String} url The URL to be parsed
* @returns {Object}
*/
function
urlResolve
(
url
)
{
var
href
=
url
;
if
(
msie
)
{
// IE needs attribute set twice to normalize properties
urlParsingNode
.
setAttribute
(
'
href
'
,
href
);
href
=
urlParsingNode
.
href
;
}
urlParsingNode
.
setAttribute
(
'
href
'
,
href
);
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
return
{
href
:
urlParsingNode
.
href
,
protocol
:
urlParsingNode
.
protocol
?
urlParsingNode
.
protocol
.
replace
(
/:$/
,
''
)
:
''
,
host
:
urlParsingNode
.
host
,
search
:
urlParsingNode
.
search
?
urlParsingNode
.
search
.
replace
(
/^
\?
/
,
''
)
:
''
,
hash
:
urlParsingNode
.
hash
?
urlParsingNode
.
hash
.
replace
(
/^#/
,
''
)
:
''
,
hostname
:
urlParsingNode
.
hostname
,
port
:
urlParsingNode
.
port
,
pathname
:
(
urlParsingNode
.
pathname
.
charAt
(
0
)
===
'
/
'
)
?
urlParsingNode
.
pathname
:
'
/
'
+
urlParsingNode
.
pathname
};
}
originUrl
=
urlResolve
(
window
.
location
.
href
);
/**
* Determine if a URL shares the same origin as the current location
*
* @param {String} requestUrl The URL to test
* @returns {boolean} True if URL shares the same origin, otherwise false
*/
module
.
exports
=
function
urlIsSameOrigin
(
requestUrl
)
{
var
parsed
=
(
utils
.
isString
(
requestUrl
))
?
urlResolve
(
requestUrl
)
:
requestUrl
;
return
(
parsed
.
protocol
===
originUrl
.
protocol
&&
parsed
.
host
===
originUrl
.
host
);
};
test/specs/helpers/buildUrl.spec.js
浏览文件 @
3d65057d
var
buildU
rl
=
require
(
'
../../../lib/helpers/buildUrl
'
);
var
buildU
RL
=
require
(
'
../../../lib/helpers/buildURL
'
);
describe
(
'
helpers::buildU
rl
'
,
function
()
{
describe
(
'
helpers::buildU
RL
'
,
function
()
{
it
(
'
should support null params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
)).
toEqual
(
'
/foo
'
);
expect
(
buildU
RL
(
'
/foo
'
)).
toEqual
(
'
/foo
'
);
});
it
(
'
should support params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
foo
:
'
bar
'
})).
toEqual
(
'
/foo?foo=bar
'
);
});
it
(
'
should support object params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
foo
:
{
bar
:
'
baz
'
}
...
...
@@ -22,31 +22,31 @@ describe('helpers::buildUrl', function () {
it
(
'
should support date params
'
,
function
()
{
var
date
=
new
Date
();
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
date
:
date
})).
toEqual
(
'
/foo?date=
'
+
date
.
toISOString
());
});
it
(
'
should support array params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
foo
:
[
'
bar
'
,
'
baz
'
]
})).
toEqual
(
'
/foo?foo[]=bar&foo[]=baz
'
);
});
it
(
'
should support special char params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
foo
:
'
@:$,
'
})).
toEqual
(
'
/foo?foo=@:$,+
'
);
});
it
(
'
should support existing params
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo?foo=bar
'
,
{
expect
(
buildU
RL
(
'
/foo?foo=bar
'
,
{
bar
:
'
baz
'
})).
toEqual
(
'
/foo?foo=bar&bar=baz
'
);
});
it
(
'
should support "length" parameter
'
,
function
()
{
expect
(
buildU
rl
(
'
/foo
'
,
{
expect
(
buildU
RL
(
'
/foo
'
,
{
query
:
'
bar
'
,
start
:
0
,
length
:
5
...
...
@@ -57,7 +57,7 @@ describe('helpers::buildUrl', function () {
serializer
=
sinon
.
stub
();
params
=
{
foo
:
'
bar
'
};
serializer
.
returns
(
'
foo=bar
'
);
expect
(
buildU
rl
(
'
/foo
'
,
params
,
serializer
)).
toEqual
(
'
/foo?foo=bar
'
);
expect
(
buildU
RL
(
'
/foo
'
,
params
,
serializer
)).
toEqual
(
'
/foo?foo=bar
'
);
expect
(
serializer
.
calledOnce
).
toBe
(
true
);
expect
(
serializer
.
calledWith
(
params
)).
toBe
(
true
);
})
...
...
test/specs/helpers/
urlIs
SameOrigin.spec.js
→
test/specs/helpers/
isURL
SameOrigin.spec.js
浏览文件 @
3d65057d
var
urlIsSameOrigin
=
require
(
'
../../../lib/helpers/urlIs
SameOrigin
'
);
var
isURLSameOrigin
=
require
(
'
../../../lib/helpers/isURL
SameOrigin
'
);
describe
(
'
helpers::
urlIs
SameOrigin
'
,
function
()
{
describe
(
'
helpers::
isURL
SameOrigin
'
,
function
()
{
it
(
'
should detect same origin
'
,
function
()
{
expect
(
urlIs
SameOrigin
(
window
.
location
.
href
)).
toEqual
(
true
);
expect
(
isURL
SameOrigin
(
window
.
location
.
href
)).
toEqual
(
true
);
});
it
(
'
should detect different origin
'
,
function
()
{
expect
(
urlIs
SameOrigin
(
'
https://github.com/mzabriskie/axios
'
)).
toEqual
(
false
);
expect
(
isURL
SameOrigin
(
'
https://github.com/mzabriskie/axios
'
)).
toEqual
(
false
);
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录