Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
61741925
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,发现更多精彩内容 >>
提交
61741925
编写于
2月 20, 2016
作者:
P
Paul Tremberth
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add "Content-Length: 0" for body-less HTTP/1.1 POST requests
GH-823 was fixed only for HTTP/1.0 (in GH-1089)
上级
2969e34e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
1 deletion
+47
-1
scrapy/core/downloader/handlers/http11.py
scrapy/core/downloader/handlers/http11.py
+15
-1
tests/test_downloader_handlers.py
tests/test_downloader_handlers.py
+32
-0
未找到文件。
scrapy/core/downloader/handlers/http11.py
浏览文件 @
61741925
...
...
@@ -209,7 +209,21 @@ class ScrapyAgent(object):
headers
=
TxHeaders
(
request
.
headers
)
if
isinstance
(
agent
,
self
.
_TunnelingAgent
):
headers
.
removeHeader
(
b
'Proxy-Authorization'
)
bodyproducer
=
_RequestBodyProducer
(
request
.
body
)
if
request
.
body
else
None
if
request
.
body
:
bodyproducer
=
_RequestBodyProducer
(
request
.
body
)
else
:
bodyproducer
=
None
# Setting Content-Length: 0 even for POST requests is not a
# MUST per HTTP RFCs, but it's common behavior, and some
# server require this otherwise returing HTTP 411
#
# RFC 7230#section-3.3.2:
# "a Content-Length header field is normally sent in a POST
# request even when the value is 0 (indicating an empty payload body)."
#
# Twisted Agent will not add "Content-Length: 0" by itself
if
method
==
b
'POST'
:
headers
.
addRawHeader
(
b
'Content-Length'
,
b
'0'
)
start_time
=
time
()
d
=
agent
.
request
(
...
...
tests/test_downloader_handlers.py
浏览文件 @
61741925
...
...
@@ -105,6 +105,15 @@ class FileTestCase(unittest.TestCase):
return
self
.
assertFailure
(
d
,
IOError
)
class
ContentLengthHeaderResource
(
resource
.
Resource
):
"""
A testing resource which renders itself as the value of the Content-Length
header from the request.
"""
def
render
(
self
,
request
):
return
request
.
requestHeaders
.
getRawHeaders
(
b
"content-length"
)[
0
]
class
HttpTestCase
(
unittest
.
TestCase
):
scheme
=
'http'
...
...
@@ -122,6 +131,7 @@ class HttpTestCase(unittest.TestCase):
r
.
putChild
(
b
"host"
,
HostHeaderResource
())
r
.
putChild
(
b
"payload"
,
PayloadResource
())
r
.
putChild
(
b
"broken"
,
BrokenDownloadResource
())
r
.
putChild
(
b
"contentlength"
,
ContentLengthHeaderResource
())
self
.
site
=
server
.
Site
(
r
,
timeout
=
None
)
self
.
wrapper
=
WrappingFactory
(
self
.
site
)
self
.
host
=
'localhost'
...
...
@@ -209,6 +219,28 @@ class HttpTestCase(unittest.TestCase):
d
.
addCallback
(
self
.
assertEquals
,
b
'example.com'
)
return
d
def
test_content_length_zero_bodyless_post_request_headers
(
self
):
"""Tests if "Content-Length: 0" is sent for bodyless POST requests.
This is not strictly required by HTTP RFCs but can cause trouble
for some web servers.
See:
https://github.com/scrapy/scrapy/issues/823
https://issues.apache.org/jira/browse/TS-2902
https://github.com/kennethreitz/requests/issues/405
https://bugs.python.org/issue14721
"""
def
_test
(
response
):
self
.
assertEquals
(
response
.
body
,
b
'0'
)
request
=
Request
(
self
.
getURL
(
'contentlength'
),
method
=
'POST'
,
headers
=
{
'Host'
:
'example.com'
})
return
self
.
download_request
(
request
,
Spider
(
'foo'
)).
addCallback
(
_test
)
d
=
self
.
download_request
(
request
,
Spider
(
'foo'
))
d
.
addCallback
(
lambda
r
:
r
.
body
)
d
.
addCallback
(
self
.
assertEquals
,
b
'0'
)
return
d
def
test_payload
(
self
):
body
=
b
'1'
*
100
# PayloadResource requires body length to be 100
request
=
Request
(
self
.
getURL
(
'payload'
),
method
=
'POST'
,
body
=
body
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录