8
8
import cgi
9
9
import os
10
10
11
- from collections import defaultdict
12
- from urllib import parse
11
+ from urllib .parse import unquote_plus
13
12
14
13
15
14
class FieldStorage (cgi .FieldStorage ):
@@ -77,11 +76,11 @@ def __init__(self, fp=None, headers=None, outerboundary=b'',
77
76
78
77
def add_qs (self , qs ):
79
78
"""Add all non-existing parameters from the given query string."""
80
- values = defaultdict (list )
81
79
# split the query string in the same way as the current Python does it
82
80
try :
83
81
max_num_fields = self .max_num_fields
84
82
except AttributeError :
83
+ # parameter did not exist before Python 3.6.7
85
84
max_num_fields = None
86
85
try :
87
86
separator = self .separator
@@ -101,23 +100,27 @@ def add_qs(self, qs):
101
100
raise ValueError ('Max number of fields exceeded' )
102
101
# new splitting algorithm that only supports one separator
103
102
pairs = qs .split (separator )
103
+ if not pairs :
104
+ return # shortcut when there are no parameters
105
+ if self .list is None :
106
+ # This makes sure self.keys() are available, even
107
+ # when valid POST data wasn't encountered.
108
+ self .list = []
109
+ append = self .list .append
110
+ existing_names = set (self )
111
+ strict_parsing = self .strict_parsing
112
+ keep_blank_values = self .keep_blank_values
104
113
for name_value in pairs :
105
114
nv = name_value .split ('=' , 1 )
106
115
if len (nv ) != 2 :
107
- if self . strict_parsing :
116
+ if strict_parsing :
108
117
raise ValueError (f'bad query field: { name_value !r} ' )
118
+ # Ignore parameters with no equal sign if not strict parsing
109
119
continue
110
- name = parse .unquote (nv [0 ].replace ('+' , ' ' ))
111
- value = parse .unquote (nv [1 ].replace ('+' , ' ' ))
112
- if len (value ) or self .keep_blank_values :
113
- values [name ].append (value )
114
- if self .list is None :
115
- # This makes sure self.keys() are available, even
116
- # when valid POST data wasn't encountered.
117
- self .list = []
118
- for key in values :
119
- if key not in self :
120
- # Only append values that aren't already the FieldStorage;
121
- # this makes POSTed vars override vars on the query string.
122
- for value in values [key ]:
123
- self .list .append (cgi .MiniFieldStorage (key , value ))
120
+ value = unquote_plus (nv [1 ])
121
+ if value or keep_blank_values :
122
+ name = unquote_plus (nv [0 ])
123
+ if name not in existing_names :
124
+ # Only append values that aren't already the FieldStorage;
125
+ # this makes POSTed vars override vars on the query string.
126
+ append (cgi .MiniFieldStorage (name , value ))
0 commit comments