<commit_before>import datetime
import re
import time
import urllib
import urllib2

from openid.constants import *
from openid.util import *
from openid.errors import *
from openid.association import *
from openid.parse import parseLinkAttrs

def normalize_url(url):
    assert isinstance(url, basestring), type(url)
    url = url.strip()
    if not (url.startswith('http://') or url.startswith('https://')):
        url = 'http://' + url
    return url


class SimpleHTTPClient(object):
    def get(self, url):
        f = urllib2.urlopen(url)
        try:
            data = f.read()
        finally:
            f.close()
        
        return (f.geturl(), data)

    def post(self, url, body):
        req = urllib2.Request(url, body)
        try:
            f = urllib2.urlopen(req)
            try:
                data = f.read()
            finally:
                f.close()
        except urllib2.HTTPError, why:
            if why.code == 400:
                try:
                    data = why.read()
                finally:
                    why.close()
                args = parsekv(data)
                error = args.get('error')
                if error is None:
                    raise ProtocolError("Unspecified Server Error: %r" %
                                        (args,))
                else:
                    raise ProtocolError("Server Response: %r" % (error,))
            else:
                raise 
            
        return (f.geturl(), data)


class OpenIDConsumer(object):
    # regexes for parsing out server url
    link_re = re.compile(r'<link(?P<linkinner>.*?)>', re.M|re.U|re.I)
    href_re = re.compile(r'.*?href\s*=\s*[\'"](?P<href>.*?)[\'"].*?',
                         re.M|re.U|re.I)
    
    def __init__(self, http_client=None, assoc_mngr=None):
        if http_client is None:
            http_client = SimpleHTTPClient()
        self.http_client = http_client
        
        if assoc_mngr is None:
            assoc_mngr = BaseAssociationManager(
                DiffieHelmanAssociator(http_client))
        self.assoc_mngr = assoc_mngr

    def handle_request(self, url, return_to, trust_root=None, immediate=False):
        """Returns the url to redirect to or None if no identity was found."""
        url = normalize_url(url)
        
        server_info = self.find_server(url)
        if server_info is None:
            return None
        
        identity, server_url = server_info
        
        redir_args = {"openid.identity" : identity,
                      "openid.return_to" : return_to,}

        if trust_root is not None:
            redir_args["openid.trust_root"] = trust_root

        if immediate:
            mode = "checkid_immediate"
        else:
            mode = "checkid_setup"

        redir_args['openid.mode'] = mode

        assoc_handle = self.assoc_mngr.associate(server_url)
        if assoc_handle is not None:
            redir_args["openid.assoc_handle"] = assoc_handle

        return append_args(server_url, redir_args)

    def handle_response(self, req):
        """Handles an OpenID GET request with openid.mode in the
        arguments. req should be a Request instance, properly
        initialized with the http arguments given, and the http method
        used to make the request.  Returns a """
        if not req.hasOpenIDParams():<commit_msg>[project @ Make sure redirect url is an instance of str]<commit_after>import datetime
import re
import time
import urllib
import urllib2

from openid.constants import *
from openid.util import *
from openid.errors import *
from openid.association import *
from openid.parse import parseLinkAttrs

def normalize_url(url):
    assert isinstance(url, basestring), type(url)
    url = url.strip()
    if not (url.startswith('http://') or url.startswith('https://')):
        url = 'http://' + url
    return url


class SimpleHTTPClient(object):
    def get(self, url):
        f = urllib2.urlopen(url)
        try:
            data = f.read()
        finally:
            f.close()
        
        return (f.geturl(), data)

    def post(self, url, body):
        req = urllib2.Request(url, body)
        try:
            f = urllib2.urlopen(req)
            try:
                data = f.read()
            finally:
                f.close()
        except urllib2.HTTPError, why:
            if why.code == 400:
                try:
                    data = why.read()
                finally:
                    why.close()
                args = parsekv(data)
                error = args.get('error')
                if error is None:
                    raise ProtocolError("Unspecified Server Error: %r" %
                                        (args,))
                else:
                    raise ProtocolError("Server Response: %r" % (error,))
            else:
                raise 
            
        return (f.geturl(), data)


class OpenIDConsumer(object):
    # regexes for parsing out server url
    link_re = re.compile(r'<link(?P<linkinner>.*?)>', re.M|re.U|re.I)
    href_re = re.compile(r'.*?href\s*=\s*[\'"](?P<href>.*?)[\'"].*?',
                         re.M|re.U|re.I)
    
    def __init__(self, http_client=None, assoc_mngr=None):
        if http_client is None:
            http_client = SimpleHTTPClient()
        self.http_client = http_client
        
        if assoc_mngr is None:
            assoc_mngr = BaseAssociationManager(
                DiffieHelmanAssociator(http_client))
        self.assoc_mngr = assoc_mngr

    def handle_request(self, url, return_to, trust_root=None, immediate=False):
        """Returns the url to redirect to or None if no identity was found."""
        url = normalize_url(url)
        
        server_info = self.find_server(url)
        if server_info is None:
            return None
        
        identity, server_url = server_info
        
        redir_args = {"openid.identity" : identity,
                      "openid.return_to" : return_to,}

        if trust_root is not None:
            redir_args["openid.trust_root"] = trust_root

        if immediate:
            mode = "checkid_immediate"
        else:
            mode = "checkid_setup"

        redir_args['openid.mode'] = mode

        assoc_handle = self.assoc_mngr.associate(server_url)
        if assoc_handle is not None:
            redir_args["openid.assoc_handle"] = assoc_handle

        return str(append_args(server_url, redir_args))

    def handle_response(self, req):
        """Handles an OpenID GET request with openid.mode in the
        arguments. req should be a Request instance, properly
        initialized with the http arguments given, and the http method
        used to make the request.  Returns a """
        if not req.hasOpenIDParams():
{'fraction_non_alphanumeric': 0.10014749262536873, 'fraction_numerical': 0.002359882005899705, 'mean_word_length': 1.7756856324191568, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5467665', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1958, 'n_words': 508}