'Handle sslstrip on HTTP responses that contain data.
This goes through and replaces URLs in the response content.'

def replace_ok(self, response):

    resp = util.http.parse_response(response)
    headers = dict(resp.getheaders())
    old_length = int(headers.get('content-length', 0))
    contents = resp.read(old_length)
    new_contents = ''
    prev = 0
    urls = re.finditer('https://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', contents)
    for match in urls:
        url = match.group(0)
        callback = self.build_report_callback(url)
        url = ('http://' + url[8:])
        new_url = ClientReportDetection.add_callback_url(callback, url, timeout=20)
        new_contents += (contents[prev:match.start()] + new_url)
        prev = match.end()
        self.log(logging.DEBUG, ('Replacing    %s    with    %s' % (match.group(0), new_url)))
    new_contents += contents[prev:]
    headers['content-length'] = len(new_contents)
    version = ('HTTP/1.0' if (resp.version == 10) else 'HTTP/1.1')
    message = ((('{version}    200    OK\r\n' + '\r\n'.join([('%s:    %s' % (k, v)) for (k, v) in headers.items()])) + '\r\n\r\n') + '{data}')
    data = message.format(version=version, data=new_contents)
    total_consumed = ((response.index('\r\n\r\n') + 4) + old_length)
    if (total_consumed < len(response)):
        data += response[total_consumed:]
    return data
