Created
March 8, 2010 03:49
-
-
Save mcfunley/324834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
from cStringIO import StringIO | |
except ImportError: | |
from StringIO import StringIO | |
import re | |
hex_digit = re.compile('[0-9a-fA-F]') | |
def urldecode(s): | |
""" | |
This is a fairly literal translation of php_url_decode in the | |
/ext/standard/url.c file in the PHP 5.2.6 source. There is, apparently, | |
no python built-in that does this. | |
By default, PHP urlencodes/decodes cookies. So we need to deal with | |
that with any that we interact with. | |
""" | |
buf = StringIO() | |
l = len(s) | |
i = 0 | |
while l: | |
c = s[i] | |
if c == '+': | |
buf.write(' ') | |
elif c == '%' and l >= 2: | |
a, b = s[i+1], s[i+2] | |
if hex_digit.match(a) and hex_digit.match(b): | |
buf.write(chr(int(a+b, 16))) | |
i += 2 | |
l -= 2 | |
else: | |
buf.write(c) | |
i += 1 | |
l -= 1 | |
return buf.getvalue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment