On Thu, Sep 23, 2010 at 11:17 AM, Ian Bicking <span dir="ltr"><<a href="mailto:ianb@colorstudy.com">ianb@colorstudy.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
If these headers accidentally contain non-Latin1 characters, the error isn't detectable until the header reaches the origin server doing the transmission encoding, and it'll likely be a dynamic (and therefore hard-to-debug) error.<br>
</blockquote></div><div><br>I don't see any reason why Location shouldn't be ASCII. Any header could have any character put in it, of course, there's just no valid case where Location shouldn't be a URL, and URLs are ASCII. Cookie can contain weirdness, yes. I would expect any library that abstracts cookies to handle this (it's certainly doable)... otherwise, this seems like one among many ways a person can do the wrong thing.<br>
</div></div></blockquote><div><br>Minor correction, Set-Cookie, not Cookie. Good practice is to stick to ASCII even there (all other techniques have a high risk of mojibake), so we're really considering legacy integration. Note that a similar problem is using [('Content-length', len(body))] -- which also results in a sometimes confusing error message well away from the application itself.<br>
<br>Generally without validation any data errors occur away from the application. A type error is not any different than an encoding error. Using bytes removes a possible encoding error, but IMHO has a greater chance of type errors (as bytes are not as natural as text in most cases). Validation can check all aspects, including encoding (simply by doing a test encoding).<br>
<br>Consider this hello world:<br><br>def app(environ, start_response):<br> body = b'Hello World'<br> start_response(b'200 OK', [(b'Content-Type', str(len(body)).encode('ascii'))])<br>
return [body]<br><br>str(len(body)).encode('ascii')?!? Yuck. Also no 2to3 fixup can help there. bytes(len(body)) does something weird.<br><br></div></div>-- <br>Ian Bicking | <a href="http://blog.ianbicking.org">http://blog.ianbicking.org</a><br>