1"""This is a temporary hack to fix an SSL bug in Python versions before 3.2. 2It is used automatically when you download an HTTPS URL using a Fetcher. 3 4Copyright: Python Software Foundation 5License: MIT License 6Downloaded from: http://pypi.python.org/pypi/backports.ssl_match_hostname/""" 7 8# pylint: disable=W0312 910importre11
16"""@type dn: str"""17pats=[]18forfragindn.split(r'.'):19iffrag=='*':20# When '*' is a fragment by itself, it matches a non-empty dotless21# fragment.22pats.append('[^.]+')23else:24# Otherwise, '*' matches any dotless fragment.25frag=re.escape(frag)26pats.append(frag.replace(r'\*','[^.]*'))27returnre.compile(r'\A'+r'\.'.join(pats)+r'\Z',re.IGNORECASE)
30"""Verify that *cert* (in decoded format as returned by31 SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 rules32 are mostly followed, but IP addresses are not accepted for *hostname*.3334 CertificateError is raised on failure. On success, the function35 returns nothing.36 @type hostname: str"""37ifnotcert:38raiseValueError("empty or no certificate")39dnsnames=[]40san=cert.get('subjectAltName',())41forkey,valueinsan:42ifkey=='DNS':43if_dnsname_to_pat(value).match(hostname):44return45dnsnames.append(value)46ifnotsan:47# The subject is only checked when subjectAltName is empty48forsubincert.get('subject',()):49forkey,valueinsub:50# XXX according to RFC 2818, the most specific Common Name51# must be used.52ifkey=='commonName':53if_dnsname_to_pat(value).match(hostname):54return55dnsnames.append(value)56iflen(dnsnames)>1:57raiseCertificateError("hostname %r "58"doesn't match either of %s"59%(hostname,', '.join(map(repr,dnsnames))))60eliflen(dnsnames)==1:61raiseCertificateError("hostname %r "62"doesn't match %r"63%(hostname,dnsnames[0]))64else:65raiseCertificateError("no appropriate commonName or "66"subjectAltName fields were found")