From: Ben Darnell Date: Mon, 31 May 2010 04:56:44 +0000 (-0700) Subject: Added RequestHandler.get_arguments(), which does the same normalization X-Git-Tag: v1.0.0~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=982554e710344b498887020071b921a3e63a17ac;p=thirdparty%2Ftornado.git Added RequestHandler.get_arguments(), which does the same normalization 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 --- diff --git a/tornado/web.py b/tornado/web.py index 17289d55b..42476e73b 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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):