Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
6326178b
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
6326178b
编写于
2月 22, 2021
作者:
E
Eugenio Lacuesta
提交者:
GitHub
2月 22, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
http2: acceptable protocol update, tests (#4994)
上级
4418f789
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
12 deletion
+22
-12
scrapy/core/http2/protocol.py
scrapy/core/http2/protocol.py
+14
-12
tests/test_http2_client_protocol.py
tests/test_http2_client_protocol.py
+8
-0
未找到文件。
scrapy/core/http2/protocol.py
浏览文件 @
6326178b
...
...
@@ -33,13 +33,16 @@ from scrapy.spiders import Spider
logger
=
logging
.
getLogger
(
__name__
)
PROTOCOL_NAME
=
b
"h2"
class
InvalidNegotiatedProtocol
(
H2Error
):
def
__init__
(
self
,
negotiated_protocol
:
str
)
->
None
:
def
__init__
(
self
,
negotiated_protocol
:
bytes
)
->
None
:
self
.
negotiated_protocol
=
negotiated_protocol
def
__str__
(
self
)
->
str
:
return
f
'InvalidNegotiatedProtocol: Expected h2 as negotiated protocol, received
{
self
.
negotiated_protocol
!
r
}
'
return
(
f
"Expected
{
PROTOCOL_NAME
!
r
}
, received
{
self
.
negotiated_protocol
!
r
}
"
)
class
RemoteTerminatedConnection
(
H2Error
):
...
...
@@ -52,7 +55,7 @@ class RemoteTerminatedConnection(H2Error):
self
.
terminate_event
=
event
def
__str__
(
self
)
->
str
:
return
f
'Re
moteTerminatedConnection: Re
ceived GOAWAY frame from
{
self
.
remote_ip_address
!
r
}
'
return
f
'Received GOAWAY frame from
{
self
.
remote_ip_address
!
r
}
'
class
MethodNotAllowed405
(
H2Error
):
...
...
@@ -60,7 +63,7 @@ class MethodNotAllowed405(H2Error):
self
.
remote_ip_address
=
remote_ip_address
def
__str__
(
self
)
->
str
:
return
f
"
MethodNotAllowed405:
Received 'HTTP/2.0 405 Method Not Allowed' from
{
self
.
remote_ip_address
!
r
}
"
return
f
"Received 'HTTP/2.0 405 Method Not Allowed' from
{
self
.
remote_ip_address
!
r
}
"
@
implementer
(
IHandshakeListener
)
...
...
@@ -231,13 +234,12 @@ class H2ClientProtocol(Protocol, TimeoutMixin):
self
.
transport
.
loseConnection
()
def
handshakeCompleted
(
self
)
->
None
:
"""We close the connection with InvalidNegotiatedProtocol exception
when the connection was not made via h2 protocol"""
protocol
=
self
.
transport
.
negotiatedProtocol
if
protocol
is
not
None
and
protocol
!=
b
"h2"
:
# Here we have not initiated the connection yet
# So, no need to send a GOAWAY frame to the remote
self
.
_lose_connection_with_error
([
InvalidNegotiatedProtocol
(
protocol
.
decode
(
"utf-8"
))])
"""
Close the connection if it's not made via the expected protocol
"""
if
self
.
transport
.
negotiatedProtocol
is
not
None
and
self
.
transport
.
negotiatedProtocol
!=
PROTOCOL_NAME
:
# we have not initiated the connection yet, no need to send a GOAWAY frame to the remote peer
self
.
_lose_connection_with_error
([
InvalidNegotiatedProtocol
(
self
.
transport
.
negotiatedProtocol
)])
def
_check_received_data
(
self
,
data
:
bytes
)
->
None
:
"""Checks for edge cases where the connection to remote fails
...
...
@@ -414,4 +416,4 @@ class H2ClientFactory(Factory):
return
H2ClientProtocol
(
self
.
uri
,
self
.
settings
,
self
.
conn_lost_deferred
)
def
acceptableProtocols
(
self
)
->
List
[
bytes
]:
return
[
b
'h2'
]
return
[
PROTOCOL_NAME
]
tests/test_http2_client_protocol.py
浏览文件 @
6326178b
...
...
@@ -5,6 +5,7 @@ import re
import
shutil
import
string
from
ipaddress
import
IPv4Address
from
unittest
import
mock
from
urllib.parse
import
urlencode
from
h2.exceptions
import
InvalidBodyLengthError
...
...
@@ -381,6 +382,13 @@ class Https2ClientProtocolTestCase(TestCase):
200
)
@
inlineCallbacks
def
test_invalid_negotiated_protocol
(
self
):
with
mock
.
patch
(
"scrapy.core.http2.protocol.PROTOCOL_NAME"
,
return_value
=
b
"not-h2"
):
request
=
Request
(
url
=
self
.
get_url
(
'/status?n=200'
))
with
self
.
assertRaises
(
ResponseFailed
):
yield
self
.
make_request
(
request
)
def
test_cancel_request
(
self
):
request
=
Request
(
url
=
self
.
get_url
(
'/get-data-html-large'
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录