Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
57338d98
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
57338d98
编写于
12月 19, 2018
作者:
K
Kevin (Kun) "Kassimo" Qian
提交者:
Ryan Dahl
12月 19, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add illegal header name and value guards (#1375)
上级
22874d44
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
85 addition
and
1 deletion
+85
-1
js/headers.ts
js/headers.ts
+33
-0
js/headers_test.ts
js/headers_test.ts
+51
-0
js/testing/util.ts
js/testing/util.ts
+1
-1
未找到文件。
js/headers.ts
浏览文件 @
57338d98
...
...
@@ -2,6 +2,11 @@
import
*
as
domTypes
from
"
./dom_types
"
;
import
{
DomIterableMixin
}
from
"
./mixins/dom_iterable
"
;
// From node-fetch
// Copyright (c) 2016 David Frank. MIT License.
const
invalidTokenRegex
=
/
[^\^
_`a-zA-Z
\-
0-9!#$%&'*+.|~
]
/
;
const
invalidHeaderCharRegex
=
/
[^\t\x
20-
\x
7e
\x
80-
\x
ff
]
/
;
// tslint:disable-next-line:no-any
function
isHeaders
(
value
:
any
):
value
is
domTypes
.
Headers
{
return
value
instanceof
Headers
;
...
...
@@ -12,6 +17,8 @@ const headerMap = Symbol("header map");
// ref: https://fetch.spec.whatwg.org/#dom-headers
class
HeadersBase
{
private
[
headerMap
]:
Map
<
string
,
string
>
;
// TODO: headerGuard? Investigate if it is needed
// node-fetch did not implement this but it is in the spec
private
_normalizeParams
(
name
:
string
,
value
?:
string
):
string
[]
{
name
=
String
(
name
).
toLowerCase
();
...
...
@@ -19,6 +26,20 @@ class HeadersBase {
return
[
name
,
value
];
}
// The following name/value validations are copied from
// https://github.com/bitinn/node-fetch/blob/master/src/headers.js
// Copyright (c) 2016 David Frank. MIT License.
private
_validateName
(
name
:
string
)
{
if
(
invalidTokenRegex
.
test
(
name
))
{
throw
new
TypeError
(
`
${
name
}
is not a legal HTTP header name`
);
}
}
private
_validateValue
(
value
:
string
)
{
if
(
invalidHeaderCharRegex
.
test
(
value
))
{
throw
new
TypeError
(
`
${
value
}
is not a legal HTTP header value`
);
}
}
constructor
(
init
?:
domTypes
.
HeadersInit
)
{
if
(
init
===
null
)
{
throw
new
TypeError
(
...
...
@@ -31,6 +52,8 @@ class HeadersBase {
if
(
Array
.
isArray
(
init
))
{
for
(
const
[
rawName
,
rawValue
]
of
init
)
{
const
[
name
,
value
]
=
this
.
_normalizeParams
(
rawName
,
rawValue
);
this
.
_validateName
(
name
);
this
.
_validateValue
(
value
);
const
existingValue
=
this
[
headerMap
].
get
(
name
);
this
[
headerMap
].
set
(
name
,
...
...
@@ -42,14 +65,19 @@ class HeadersBase {
for
(
const
rawName
of
names
)
{
const
rawValue
=
init
[
rawName
];
const
[
name
,
value
]
=
this
.
_normalizeParams
(
rawName
,
rawValue
);
this
.
_validateName
(
name
);
this
.
_validateValue
(
value
);
this
[
headerMap
].
set
(
name
,
value
);
}
}
}
}
// ref: https://fetch.spec.whatwg.org/#concept-headers-append
append
(
name
:
string
,
value
:
string
):
void
{
const
[
newname
,
newvalue
]
=
this
.
_normalizeParams
(
name
,
value
);
this
.
_validateName
(
newname
);
this
.
_validateValue
(
newvalue
);
const
v
=
this
[
headerMap
].
get
(
newname
);
const
str
=
v
?
`
${
v
}
,
${
newvalue
}
`
:
newvalue
;
this
[
headerMap
].
set
(
newname
,
str
);
...
...
@@ -57,22 +85,27 @@ class HeadersBase {
delete
(
name
:
string
):
void
{
const
[
newname
]
=
this
.
_normalizeParams
(
name
);
this
.
_validateName
(
newname
);
this
[
headerMap
].
delete
(
newname
);
}
get
(
name
:
string
):
string
|
null
{
const
[
newname
]
=
this
.
_normalizeParams
(
name
);
this
.
_validateName
(
newname
);
const
value
=
this
[
headerMap
].
get
(
newname
);
return
value
||
null
;
}
has
(
name
:
string
):
boolean
{
const
[
newname
]
=
this
.
_normalizeParams
(
name
);
this
.
_validateName
(
newname
);
return
this
[
headerMap
].
has
(
newname
);
}
set
(
name
:
string
,
value
:
string
):
void
{
const
[
newname
,
newvalue
]
=
this
.
_normalizeParams
(
name
,
value
);
this
.
_validateName
(
newname
);
this
.
_validateValue
(
newvalue
);
this
[
headerMap
].
set
(
newname
,
newvalue
);
}
}
...
...
js/headers_test.ts
浏览文件 @
57338d98
...
...
@@ -177,3 +177,54 @@ test(function headerTypesAvailable() {
const
headers
=
newHeaders
();
assert
(
headers
instanceof
Headers
);
});
// tslint:disable-next-line:max-line-length
// Modified from https://github.com/bitinn/node-fetch/blob/7d3293200a91ad52b5ca7962f9d6fd1c04983edb/test/test.js#L2001-L2014
// Copyright (c) 2016 David Frank. MIT License.
test
(
function
headerIllegalReject
()
{
let
errorCount
=
0
;
try
{
new
Headers
({
"
He y
"
:
"
ok
"
});
}
catch
(
e
)
{
errorCount
++
;
}
try
{
new
Headers
({
"
Hé-y
"
:
"
ok
"
});
}
catch
(
e
)
{
errorCount
++
;
}
try
{
new
Headers
({
"
He-y
"
:
"
ăk
"
});
}
catch
(
e
)
{
errorCount
++
;
}
const
headers
=
new
Headers
();
try
{
headers
.
append
(
"
Hé-y
"
,
"
ok
"
);
}
catch
(
e
)
{
errorCount
++
;
}
try
{
headers
.
delete
(
"
Hé-y
"
);
}
catch
(
e
)
{
errorCount
++
;
}
try
{
headers
.
get
(
"
Hé-y
"
);
}
catch
(
e
)
{
errorCount
++
;
}
try
{
headers
.
has
(
"
Hé-y
"
);
}
catch
(
e
)
{
errorCount
++
;
}
try
{
headers
.
set
(
"
Hé-y
"
,
"
ok
"
);
}
catch
(
e
)
{
errorCount
++
;
}
assertEqual
(
errorCount
,
8
);
// 'o k' is valid value but invalid name
new
Headers
({
"
He-y
"
:
"
o k
"
});
});
js/testing/util.ts
浏览文件 @
57338d98
...
...
@@ -21,7 +21,7 @@ export function assertEqual(actual: any, expected: any, msg?: string) {
}
if
(
!
equal
(
actual
,
expected
))
{
console
.
error
(
"
assertEqual failed. actual =
"
,
"
assertEqual failed. actual =
"
,
actual
,
"
expected =
"
,
expected
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录