Commits

Anonymous committed 9532234

Added a URI re-encode example middleware.

Comments (0)

Files changed (1)

     environ['SCRIPT_NAME'] = script_name
     environ['wsgi.uri_encoding'] = uri_encoding
 
+URI Recoding
+------------
+
+A middleware might re-encode the values if the `wsgi.uri_encoding` is
+`iso-8859-1` and only then.  If it performs re-encoding it must alter
+the value of `wsgi.uri_encoding` as well.  For example if an application
+accepts legacy URL as `iso-8859-7` encoded, this middleware might be
+used::
+
+    import codecs
+    iso_8859_1 = codecs.lookup('iso-8859-1')
+
+    def reencode(string):
+        return string.encode('iso-8859-1').decode('iso-8859-7')
+
+    def latin7_fallback_middleware(application):
+        def new_application(environ, start_response):
+            if codecs.lookup(environ['wsgi.uri_encoding']) == iso_8859_1:
+                environ['PATH_INFO'] = reencode(environ['PATH_INFO'])
+                environ['SCRIPT_NAME'] = reencode(environ['SCRIPT_NAME'])
+                environ['wsgi.uri_encoding'] = 'iso-8859-7'
+            return application(environ, start_response)
+        return new_application
+
 URI Encoding
 ------------