]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Added RequestHandler.get_arguments(), which does the same normalization
authorBen Darnell <bdarnell@beaker.local>
Mon, 31 May 2010 04:56:44 +0000 (21:56 -0700)
committerBen Darnell <bdarnell@beaker.local>
Mon, 31 May 2010 06:38:50 +0000 (23:38 -0700)
as get_argument() but returns a list for repeated arguments.

Based on changes by jehiah:
http://github.com/jehiah/tornado/commit/cbe2eeb1753de7d1a16a86c6a96b5f8e7c99dd52
http://github.com/jehiah/tornado/commit/af08ab067fb7ad1661fc56039004cbd86356716c

tornado/web.py

index 17289d55b1d15596900e26a351de09b158d7d333..42476e73b1238961f13933bbea146fdd0446ad91 100644 (file)
@@ -183,18 +183,33 @@ class RequestHandler(object):
         If default is not provided, the argument is considered to be
         required, and we throw an HTTP 404 exception if it is missing.
 
+        If the argument appears in the url more than once, we return the
+        last value.
+
         The returned value is always unicode.
         """
-        values = self.request.arguments.get(name, None)
-        if values is None:
+        args = self.get_arguments(name, strip=strip)
+        if not args:
             if default is self._ARG_DEFAULT:
                 raise HTTPError(404, "Missing argument %s" % name)
             return default
+        return args[-1]
+
+    def get_arguments(self, name, strip=True):
+        """Returns a list of the arguments with the given name.
+
+        If the argument is not present, returns an empty list.
+
+        The returned values are always unicode.
+        """
+        values = self.request.arguments.get(name, [])
         # Get rid of any weird control chars
-        value = re.sub(r"[\x00-\x08\x0e-\x1f]", " ", values[-1])
-        value = _unicode(value)
-        if strip: value = value.strip()
-        return value
+        values = [re.sub(r"[\x00-\x08\x0e-\x1f]", " ", x) for x in values]
+        values = [_unicode(x) for x in values]
+        if strip:
+            values = [x.strip() for x in values]
+        return values
+
 
     @property
     def cookies(self):