Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc72b7f7

Browse files
bpo-34226: fix cgi.parse_multipart without content_length (GH-8530)
In Python 3.7 the behavior of parse_multipart changed requiring CONTENT-LENGTHheader, this fix remove this header as required and fix FieldStorageread_lines_to_outerboundary, by not using limit when it's negative,since by default it's -1 if not content-length and keeps substracting whatwas read from the file object.Also added a test case for this problem.(cherry picked from commitd8cf351)Co-authored-by: roger <rogerduran@gmail.com>
1 parent811e040 commitc72b7f7

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

‎Lib/cgi.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def parse_multipart(fp, pdict, encoding="utf-8", errors="replace"):
200200
ctype="multipart/form-data; boundary={}".format(boundary)
201201
headers=Message()
202202
headers.set_type(ctype)
203-
headers['Content-Length']=pdict['CONTENT-LENGTH']
203+
try:
204+
headers['Content-Length']=pdict['CONTENT-LENGTH']
205+
exceptKeyError:
206+
pass
204207
fs=FieldStorage(fp,headers=headers,encoding=encoding,errors=errors,
205208
environ={'REQUEST_METHOD':'POST'})
206209
return {k:fs.getlist(k)forkinfs}
@@ -736,7 +739,8 @@ def read_lines_to_outerboundary(self):
736739
last_line_lfend=True
737740
_read=0
738741
while1:
739-
ifself.limitisnotNoneand_read>=self.limit:
742+
743+
ifself.limitisnotNoneand0<=self.limit<=_read:
740744
break
741745
line=self.fp.readline(1<<16)# bytes
742746
self.bytes_read+=len(line)

‎Lib/test/test_cgi.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ def test_parse_multipart(self):
128128
'file': [b'Testing 123.\n'],'title': ['']}
129129
self.assertEqual(result,expected)
130130

131+
deftest_parse_multipart_without_content_length(self):
132+
POSTDATA='''--JfISa01
133+
Content-Disposition: form-data; name="submit-name"
134+
135+
just a string
136+
137+
--JfISa01--
138+
'''
139+
fp=BytesIO(POSTDATA.encode('latin1'))
140+
env= {'boundary':'JfISa01'.encode('latin1')}
141+
result=cgi.parse_multipart(fp,env)
142+
expected= {'submit-name': ['just a string\n']}
143+
self.assertEqual(result,expected)
144+
131145
deftest_parse_multipart_invalid_encoding(self):
132146
BOUNDARY="JfISa01"
133147
POSTDATA="""--JfISa01
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `cgi.parse_multipart` without content_length. Patch by Roger Duran

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp