~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Lib/cgi.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 17:53:26 UTC
  • mfrom: (39025.1.14 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921175326-92vaej2hc3yuecxb
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
import sys
39
39
import os
40
40
import urllib
41
 
import mimetools
42
 
import rfc822
43
41
import UserDict
 
42
import urlparse
 
43
 
 
44
from warnings import filterwarnings, catch_warnings, warn
 
45
with catch_warnings():
 
46
    if sys.py3kwarning:
 
47
        filterwarnings("ignore", ".*mimetools has been removed",
 
48
                        DeprecationWarning)
 
49
    import mimetools
 
50
    if sys.py3kwarning:
 
51
        filterwarnings("ignore", ".*rfc822 has been removed", DeprecationWarning)
 
52
    import rfc822
 
53
 
44
54
try:
45
55
    from cStringIO import StringIO
46
56
except ImportError:
165
175
    return parse_qs(qs, keep_blank_values, strict_parsing)
166
176
 
167
177
 
 
178
# parse query string function called from urlparse,
 
179
# this is done in order to maintain backward compatiblity.
 
180
 
168
181
def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
169
 
    """Parse a query given as a string argument.
170
 
 
171
 
        Arguments:
172
 
 
173
 
        qs: URL-encoded query string to be parsed
174
 
 
175
 
        keep_blank_values: flag indicating whether blank values in
176
 
            URL encoded queries should be treated as blank strings.
177
 
            A true value indicates that blanks should be retained as
178
 
            blank strings.  The default false value indicates that
179
 
            blank values are to be ignored and treated as if they were
180
 
            not included.
181
 
 
182
 
        strict_parsing: flag indicating what to do with parsing errors.
183
 
            If false (the default), errors are silently ignored.
184
 
            If true, errors raise a ValueError exception.
185
 
    """
186
 
    dict = {}
187
 
    for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
188
 
        if name in dict:
189
 
            dict[name].append(value)
190
 
        else:
191
 
            dict[name] = [value]
192
 
    return dict
 
182
    """Parse a query given as a string argument."""
 
183
    warn("cgi.parse_qs is deprecated, use urlparse.parse_qs \
 
184
            instead",PendingDeprecationWarning)
 
185
    return urlparse.parse_qs(qs, keep_blank_values, strict_parsing)
 
186
 
193
187
 
194
188
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
195
 
    """Parse a query given as a string argument.
196
 
 
197
 
    Arguments:
198
 
 
199
 
    qs: URL-encoded query string to be parsed
200
 
 
201
 
    keep_blank_values: flag indicating whether blank values in
202
 
        URL encoded queries should be treated as blank strings.  A
203
 
        true value indicates that blanks should be retained as blank
204
 
        strings.  The default false value indicates that blank values
205
 
        are to be ignored and treated as if they were  not included.
206
 
 
207
 
    strict_parsing: flag indicating what to do with parsing errors. If
208
 
        false (the default), errors are silently ignored. If true,
209
 
        errors raise a ValueError exception.
210
 
 
211
 
    Returns a list, as G-d intended.
212
 
    """
213
 
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
214
 
    r = []
215
 
    for name_value in pairs:
216
 
        if not name_value and not strict_parsing:
217
 
            continue
218
 
        nv = name_value.split('=', 1)
219
 
        if len(nv) != 2:
220
 
            if strict_parsing:
221
 
                raise ValueError, "bad query field: %r" % (name_value,)
222
 
            # Handle case of a control-name with no equal sign
223
 
            if keep_blank_values:
224
 
                nv.append('')
225
 
            else:
226
 
                continue
227
 
        if len(nv[1]) or keep_blank_values:
228
 
            name = urllib.unquote(nv[0].replace('+', ' '))
229
 
            value = urllib.unquote(nv[1].replace('+', ' '))
230
 
            r.append((name, value))
231
 
 
232
 
    return r
233
 
 
 
189
    """Parse a query given as a string argument."""
 
190
    warn("cgi.parse_qsl is deprecated, use urlparse.parse_qsl instead",
 
191
            PendingDeprecationWarning)
 
192
    return urlparse.parse_qsl(qs, keep_blank_values, strict_parsing)
234
193
 
235
194
def parse_multipart(fp, pdict):
236
195
    """Parse multipart input.
456
415
        self.strict_parsing = strict_parsing
457
416
        if 'REQUEST_METHOD' in environ:
458
417
            method = environ['REQUEST_METHOD'].upper()
 
418
        self.qs_on_post = None
459
419
        if method == 'GET' or method == 'HEAD':
460
420
            if 'QUERY_STRING' in environ:
461
421
                qs = environ['QUERY_STRING']
474
434
                headers['content-type'] = "application/x-www-form-urlencoded"
475
435
            if 'CONTENT_TYPE' in environ:
476
436
                headers['content-type'] = environ['CONTENT_TYPE']
 
437
            if 'QUERY_STRING' in environ:
 
438
                self.qs_on_post = environ['QUERY_STRING']
477
439
            if 'CONTENT_LENGTH' in environ:
478
440
                headers['content-length'] = environ['CONTENT_LENGTH']
479
441
        self.fp = fp or sys.stdin
631
593
    def read_urlencoded(self):
632
594
        """Internal: read data in query string format."""
633
595
        qs = self.fp.read(self.length)
 
596
        if self.qs_on_post:
 
597
            qs += '&' + self.qs_on_post
634
598
        self.list = list = []
635
 
        for key, value in parse_qsl(qs, self.keep_blank_values,
636
 
                                    self.strict_parsing):
 
599
        for key, value in urlparse.parse_qsl(qs, self.keep_blank_values,
 
600
                                            self.strict_parsing):
637
601
            list.append(MiniFieldStorage(key, value))
638
602
        self.skip_lines()
639
603
 
645
609
        if not valid_boundary(ib):
646
610
            raise ValueError, 'Invalid boundary in multipart form: %r' % (ib,)
647
611
        self.list = []
 
612
        if self.qs_on_post:
 
613
            for key, value in urlparse.parse_qsl(self.qs_on_post,
 
614
                                self.keep_blank_values, self.strict_parsing):
 
615
                self.list.append(MiniFieldStorage(key, value))
 
616
            FieldStorageClass = None
 
617
 
648
618
        klass = self.FieldStorageClass or self.__class__
649
619
        part = klass(self.fp, {}, ib,
650
620
                     environ, keep_blank_values, strict_parsing)