From 1ba255659ee9c0d2ed33cdcc07473be092f541cb Mon Sep 17 00:00:00 2001 From: ianb Date: Sun, 12 Jun 2005 18:21:41 +0000 Subject: Added function for parsing QUERY_STRING --- paste/wsgilib.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'paste') diff --git a/paste/wsgilib.py b/paste/wsgilib.py index 4f53ac1..20604b1 100644 --- a/paste/wsgilib.py +++ b/paste/wsgilib.py @@ -6,6 +6,7 @@ from Cookie import SimpleCookie from cStringIO import StringIO import mimetypes import os +import cgi __all__ = ['get_cookies', 'add_close', 'raw_interactive', 'interactive', 'construct_url', 'error_body_response', @@ -28,6 +29,28 @@ def get_cookies(environ): environ['paste.cookies'] = (cookies, header) return cookies +def parse_querystring(environ): + """ + Parses a query string into a list like ``[(name, value)]``. + Caches this value in case parse_querystring is called again + for the same request. + + You can pass the result to ``dict()``, but be aware that keys that + appear multiple times will be lost (only the last value will be + preserved). + """ + source = environ.get('QUERY_STRING', '') + if not source: + return [] + if 'paste.parsed_querystring' in environ: + parsed, check_source = environ['paste.parsed_querystring'] + if check_source == source: + return parsed + parsed = cgi.parse_qs(source, keep_blank_values=True, + strict_parsing=False) + environ['paste.parsed_querystring'] = (parsed, source) + return parsed + class add_close: """ An an iterable that iterates over app_iter, then calls -- cgit v1.2.1