<commit_before># -*- coding: utf-8 -*-
# OpenID relying party library
# Copyright <NAME>, 2009
# Licensed under the Academic Free License, version 3

# This library implements OpenID Authentication 2.0,
# in the role of a relying party
# It has the following assumptions and limitations:
# - service discovery requires YADIS (HTML discovery not implemented)
# - only provider-directed mode (identifier_select) is supported
# - direct requests require https
# - as a signature algorithm, HMAC-SHA1 is requested

import urllib, BeautifulSoup, xml.etree.ElementTree
import cStringIO, base64, hmac, sha, datetime

# Don't use urllib2, since it breaks in 2.5
# for https://login.launchpad.net//+xrds

def parse_response(s):
    '''Parse a key-value form (OpenID section 4.1.1) into a dictionary'''
    res = {}
    for line in s.splitlines():
        k,v = line.split(':', 1)
        res[k] = v
    return res

def discover(url):
    '''Perform service discovery on the OP URL.
    Return list of service types, and the auth/2.0 URL,
    or None if discovery fails.'''
    opener = urllib.FancyURLopener()
    opener.addheader('Accept', "text/html; q=0.3, "+
                     "application/xhtml+xml; q=0.5, "+
                     "application/xrds+xml")
    res = opener.open(url)

    content_type = res.headers.gettype()

    # Yadis 6.2.5 option 2 and 3: header includes x-xrds-location
    xrds_loc = res.headers.get('x-xrds-location')
    if xrds_loc and content_type != 'application/xrds+xml':
        res.close()
        return discover(xrds_loc)

    if content_type == 'text/html':
        data = res.read()
        res.close()
        soup = BeautifulSoup.BeautifulSoup(data)
        # Yadis 6.2.5 option 1: meta tag
        meta = soup.findAll('meta', {'http-equiv':lambda v:v.lower()=='x-xrds-location'})
        if meta:
            xrds_loc = meta.attrs['content']
            return discover(xrds_loc)
        # OpenID 7.3, item 3: attempt html based discovery
        # TODO
        return None
            
    if content_type == 'application/xrds+xml':
        # Yadis 6.2.5 option 4
        doc = xml.etree.ElementTree.parse(res)
        res.close()
        for svc in doc.findall(".//{xri://$xrd*($v*2.0)}Service"):
            services = [x.text for x in svc.findall("{xri://$xrd*($v*2.0)}Type")]
            if not 'http://specs.openid.net/auth/2.0/server' in services:
                # We don't support discovery of Claimed Identifiers, only OP Identifiers
                continue
            uri = svc.find("{xri://$xrd*($v*2.0)}URI")
            if uri is None:
                return None # invalid service
            break
        else:
            return None # No OpenID 2.0 service found
    return services, uri.text

def associate(url):<commit_msg>Use httplib instead of urllib.<commit_after># -*- coding: utf-8 -*-
# OpenID relying party library
# Copyright <NAME>, 2009
# Licensed under the Academic Free License, version 3

# This library implements OpenID Authentication 2.0,
# in the role of a relying party
# It has the following assumptions and limitations:
# - service discovery requires YADIS (HTML discovery not implemented)
# - only provider-directed mode (identifier_select) is supported
# - direct requests require https
# - as a signature algorithm, HMAC-SHA1 is requested

import urlparse, httplib, BeautifulSoup, xml.etree.ElementTree
import cStringIO, base64, hmac, sha, datetime

# Don't use urllib2, since it breaks in 2.5
# for https://login.launchpad.net//+xrds

# Don't use urllib, since it sometimes selects HTTP/1.1 (e.g. in PyPI)
# and then fails to parse chunked responses.

def parse_response(s):
    '''Parse a key-value form (OpenID section 4.1.1) into a dictionary'''
    res = {}
    for line in s.splitlines():
        k,v = line.split(':', 1)
        res[k] = v
    return res

def discover(url):
    '''Perform service discovery on the OP URL.
    Return list of service types, and the auth/2.0 URL,
    or None if discovery fails.'''
    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
    assert not query and not fragment
    assert scheme == 'https'
    conn = httplib.HTTPSConnection(netloc)
    conn.putrequest("GET", path)
    conn.putheader('Accept', "text/html; q=0.3, "+
                   "application/xhtml+xml; q=0.5, "+
                   "application/xrds+xml")
    conn.endheaders()

    res = conn.getresponse()
    data = res.read()
    conn.close()

    content_type = res.msg.gettype()

    # Yadis 6.2.5 option 2 and 3: header includes x-xrds-location
    xrds_loc = res.msg.get('x-xrds-location')
    if xrds_loc and content_type != 'application/xrds+xml':
        return discover(xrds_loc)

    if content_type == 'text/html':
        soup = BeautifulSoup.BeautifulSoup(data)
        # Yadis 6.2.5 option 1: meta tag
        meta = soup.findAll('meta', {'http-equiv':lambda v:v.lower()=='x-xrds-location'})
        if meta:
            xrds_loc = meta.attrs['content']
            return discover(xrds_loc)
        # OpenID 7.3, item 3: attempt html based discovery
        # TODO
        return None
            
    if content_type == 'application/xrds+xml':
        # Yadis 6.2.5 option 4
        doc = xml.etree.ElementTree.fromstring(data)
        for svc in doc.findall(".//{xri://$xrd*($v*2.0)}Service"):
            services = [x.text for x in svc.findall("{xri://$xrd*($v*2.0)}Type")]
            if not 'http://specs.openid.net/auth/2.0/server' in services:
                # We don't support discovery of Claimed Identifiers, only OP Identifiers
                continue
            uri = svc.find("{xri://$xrd*($v*2.0)}URI")
            if uri is None:
                return None # invalid service
            break
        else:
            return None # No OpenID 2.0 service found
    return services, uri.text

def associate(url):
{'fraction_non_alphanumeric': 0.12025965152032798, 'fraction_numerical': 0.01742398360095661, 'mean_word_length': 2.487194758784991, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3035730', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1827, 'n_words': 550}