import re


def _htmlPingbackURI(fileObj):
    """Given an interable object returning text, search it for a pingback URI
    based upon the search parameters given by the pingback specification.

    Namely, it should match the regex:
      <link rel="pingback" href="([^"]+)" ?/?>
    (source: http://www.hixie.ch/specs/pingback/pingback)

    We could search the text using an actual HTML parser easily enough, or
    expand the regex to be a little more forgiving, but for the moment we'll
    follow the spec."""
    regex = re.compile('<link rel="pingback" href="([^"]+)" ?/?>', re.I) # might as well be case-insensitive
    for line in fileObj:
        m = regex.search(line)
        if m != None:
            uri = m.group(1)
            # The pingback spec demands we expand the four allowed entities,
            # but no more.
            uri = uri.replace("&lt;", "<")
            uri = uri.replace("&gt;", ">")
            uri = uri.replace("&quot;", '"')
            uri = uri.replace("&amp;", "&")
            return uri
    return None