Skip to content

Commit bba08f5

Browse files
committed
Minor simplifications in FieldStorage
1 parent f8087fd commit bba08f5

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

WebUtils/FieldStorage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import cgi
1010

11-
from urllib import unquote
11+
from urllib import unquote_plus
1212

1313

1414
class FieldStorage(cgi.FieldStorage):
@@ -114,10 +114,10 @@ def add_qs(self, qs):
114114
nv.append('')
115115
else:
116116
continue
117-
if len(nv[1]) or keep_blank_values:
118-
name = unquote(nv[0].replace('+', ' '))
117+
if nv[1] or keep_blank_values:
118+
name = unquote_plus(nv[0])
119119
if name not in existing_names:
120120
# Only append values that aren't already the FieldStorage;
121121
# this makes POSTed vars override vars on the query string.
122-
value = unquote(nv[1].replace('+', ' '))
122+
value = unquote_plus(nv[1])
123123
append(cgi.MiniFieldStorage(name, value))

WebUtils/Tests/TestFieldStorage.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def testPostRequestWithBody(self):
4343
self.assertEqual(fs.getlist('e'), ['5', '6'])
4444
self.assertEqual(fs.getlist('f'), ['6'])
4545

46+
def testPostRequestWithSpacesInValues(self):
47+
fs = FieldStorage(fp=StringIO(), environ=dict(
48+
REQUEST_METHOD='POST', QUERY_STRING='a=b%20c+d'))
49+
self.assertEqual(fs.getfirst('a'), 'b c d')
50+
fs = FieldStorage(fp=StringIO('a=b%20c+d'), environ=dict(
51+
REQUEST_METHOD='POST'))
52+
self.assertEqual(fs.getfirst('a'), 'b c d')
53+
4654
def testPostRequestOverrides(self):
4755
fs = FieldStorage(
4856
fp=StringIO('b=8&c=9&d=4&e=5&e=6&f=6'), environ=dict(

0 commit comments

Comments
 (0)