Skip to content

Commit

Permalink
Added RequestHandler.get_arguments(), which does the same normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Darnell committed May 31, 2010
1 parent 548bd70 commit 982554e
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,33 @@ def get_argument(self, name, default=_ARG_DEFAULT, strip=True):
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):
Expand Down

0 comments on commit 982554e

Please sign in to comment.