1

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?

KennyPowers
  • 4,925
  • 8
  • 36
  • 51
  • 1
    As answered in http://stackoverflow.com/a/1732454/770830, one should not use regex to parse HTML. Pick any special tool: BeautifulSoup, lxml, whatever. – bereal Nov 21 '12 at 08:59

1 Answers1

1

You could try adding an extra named group:

<(?P<tagname>[^\s]+)\s[^>]*

to the REWRITE_LINKS line like this:

REWRITE_LINKS = re.compile(r'(<(?P<tagname>[^\s]+)\s[^>]*(?P<attr>action|href|src)=["\'](?P<uri>\S+?)["\'])', re.IGNORECASE)

then you can query this match in the if attr.lower() == 'src': conditional statement

garyh
  • 2,782
  • 1
  • 26
  • 28