I have http proxy project. There're some rewrite rules I use to change all actions and href's to this /follow/+URL
. I also change all src's URLs to this /proxy/+URL
, it helps me to assign the URLs between the links and e.g. images. It works fine with <img>
tag though. The problem I have consists in <iframe src=...>
tag. After I apply my code it changes src to '/proxy/+URL' but I want to change it to /follow/+URL
and I'm not sure how I can do that.
Here's my code
from urlparse import urlparse, urlunparse
import re, urllib
REWRITE_LINKS = re.compile(r'((?P<attr>action|href|src)=["\'](?P<uri>\S+?)["\'])', re.IGNORECASE)
def rewrite_links(content, mimetype = '', uri = ''):
uri = str(uri)
urip = urlparse(uri)
server_root = str(urlunparse((urip[0], urip[1], '/', '', '', '')))
working_dir = str(urlunparse((urip[0], urip[1], urip[2], '/', '', '')))
working_dir = '/'.join(working_dir.split('/')[:-1])
def repl_html(match):
attr, value = match.groupdict()['attr'], match.groupdict()['uri']
if value in ('',) or value.startswith('javascript:') or value.startswith('#'):
pass
else:
if value.find('://') == -1:
if value.startswith('./'):
value = working_dir + value[2:]
elif value.startswith('../'):
value = '/'.join(working_dir.split('/')[:-1]) + value[3:]
elif value.startswith('/'):
value = server_root + value[1:]
else:
value = server_root + value
#value = value.replace('/','|')
if attr.lower() == 'src':
value = '/proxy/' + value
else:
value = '/follow/' + value
return ' %s="%s" ' % (attr, value)
if mimetype.startswith('text/html'):
content = REWRITE_LINKS.sub(repl_html, content)
elif mimetype.startswith('text/css'):
pass
elif mimetype.startswith('application/x-javascript'):
pass
else:
pass
return content
Do you have any advices?