paulb@654 | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
paulb@507 | 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@507 | 3 | <html xmlns="http://www.w3.org/1999/xhtml"><head> |
paulb@507 | 4 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@654 | 5 | <title>Encoding and Decoding Path Values</title> |
paulb@507 | 6 | <link href="styles.css" rel="stylesheet" type="text/css" /></head> |
paulb@507 | 7 | <body> |
paulb@507 | 8 | <h1>Encoding and Decoding Path Values</h1><p>On some occasions it can |
paulb@507 | 9 | be necessary to manually decode path values, producing genuine Unicode |
paulb@507 | 10 | objects, and then to encode them, producing plain strings that can be |
paulb@507 | 11 | used in response headers and other places. For such occasions, some |
paulb@507 | 12 | transaction methods are available:</p><div class="WebStack"> |
paulb@507 | 13 | <h3>WebStack API - Encoding and Decoding Path Values</h3> |
paulb@507 | 14 | <p>WebStack provides the following methods to transform path values:</p> |
paulb@654 | 15 | <dl><dt><code>decode_path</code></dt><dd>This method accepts a path containing "URL encoded" information (as defined in the <a href="paths.html">"URLs and Paths"</a> |
paulb@507 | 16 | document) and, using an optional encoding parameter, returns a Unicode |
paulb@507 | 17 | object containing genuine character values in place of the "URL |
paulb@507 | 18 | encoded" values.</dd><dt><code>encode_path</code></dt><dd>This method |
paulb@507 | 19 | accepts a Unicode object containing the path and an optional encoding |
paulb@654 | 20 | parameter; it reverses the process carried out by the <code>decode_path</code> method.</dd></dl> |
paulb@654 | 21 | </div><p>Generally, the <code>decode_path</code> method is of little interest; its only relatively common application might be to decode query strings:</p><pre>qs = trans.get_query_string() # eg. "a=%E6"<br />new_qs = trans.decode_path(qs, "iso-8859-1") # producing "a=?"</pre><p>Such operations are generally better performed using the <a href="parameters.html">request parameter methods</a>.</p><p>The <code>encode_path</code> |
paulb@507 | 22 | method is slightly more useful: since various transaction methods |
paulb@507 | 23 | return values which have already been transformed into Unicode objects, |
paulb@507 | 24 | we must consider the use of <code>encode_path</code> to produce values |
paulb@507 | 25 | which are suitable for feeding into other methods. For example, having |
paulb@507 | 26 | obtained a path, we may wish to cause a <a href="redirection.html">redirect</a> to another location |
paulb@654 | 27 | based on that path:</p><pre>path = trans.get_path_without_query("iso-8859-1") # eg. "/app/resource"<br />path += "/???;"<br />new_path = trans.encode_path(path, "iso-8859-1") # producing "/app/resource/%E6%F8%E5"<br />trans.redirect(new_path)</pre><p>It |
paulb@507 | 28 | is essential to encode the path in such situations because the |
paulb@507 | 29 | underlying mechanisms do not support the full range of Unicode |
paulb@654 | 30 | characters. Some cases where this limitation exists are listed in the <a href="encodings.html">"Character Encodings"</a> document.</p></body></html> |