def nameprep(self, label): label = idna.nameprep(label) self.check_prohibiteds(label) if label[0] == '-': raise UnicodeError, "Invalid leading hyphen-minus" if label[-1] == '-': raise UnicodeError, "Invalid trailing hyphen-minus" return label
def nameprep(self, label): label = idna.nameprep(label) self.check_prohibiteds(label) if label[0] == u'-': raise UnicodeError("Invalid leading hyphen-minus") if label[-1] == u'-': raise UnicodeError("Invalid trailing hyphen-minus") return label
def __prepare_domain(data): """Prepare domainpart of the JID. :Parameters: - `data`: Domain part of the JID :Types: - `data`: `str` :raise JIDError: if the domain name is too long. """ # pylint: disable=R0912 if not data: raise JIDError("Domain must be given") data = str(data) if not data: raise JIDError("Domain must be given") if '[' in data: if data[0] == '[' and data[-1] == ']': try: addr = _validate_ip_address(socket.AF_INET6, data[1:-1]) return "[{0}]".format(addr) except ValueError as err: logger.debug("ValueError: {0}".format(err)) raise JIDError("Invalid IPv6 literal in JID domainpart") else: raise JIDError("Invalid use of '[' or ']' in JID domainpart") elif data[0].isdigit() and data[-1].isdigit(): try: addr = _validate_ip_address(socket.AF_INET, data) except ValueError as err: logger.debug("ValueError: {0}".format(err)) data = UNICODE_DOT_RE.sub(".", data) data = data.rstrip(".") labels = data.split(".") try: labels = [idna.nameprep(label) for label in labels] except UnicodeError: raise JIDError("Domain name invalid") for label in labels: if not STD3_LABEL_RE.match(label): raise JIDError("Domain name invalid") try: idna.ToASCII(label) except UnicodeError: raise JIDError("Domain name invalid") domain = ".".join(labels) if len(domain.encode("utf-8")) > 1023: raise JIDError("Domain name too long") return domain
def __set_domain(self,s): """Initialize `self.domain` :Parameters: - `s`: Unicode or UTF-8 domain part of the JID :raise JIDError: if the domain name is too long.""" if s is None: raise JIDError,"Domain must be given" if s: s = unicode(s) s=idna.nameprep(s) if len(s.encode("utf-8"))>1023: raise JIDError,"Domain name too long" object.__setattr__(self,"domain",s)
def test_nameprep(self): from encodings.idna import nameprep for pos, (orig, prepped) in enumerate(nameprep_tests): if orig is None: # Skipped continue # The Unicode strings are given in UTF-8 orig = unicode(orig, "utf-8") if prepped is None: # Input contains prohibited characters self.assertRaises(UnicodeError, nameprep, orig) else: prepped = unicode(prepped, "utf-8") try: self.assertEquals(nameprep(orig), prepped) except Exception,e: raise test_support.TestFailed("Test 3.%d: %s" % (pos+1, str(e)))
def __prepare_domain(data): """Prepare domainpart of the JID. :Parameters: - `data`: Domain part of the JID :Types: - `data`: `str` :raise JIDError: if the domain name is too long.""" if not data: raise JIDError("Domain must be given") data = data.rstrip(".") if not data: raise JIDError("Domain must be given") if '[' in data: if data[0] == '[' and data[-1] == ']': try: # decode... addr = socket.inet_pton(socket.AF_INET6, data[1:-1]) # ...and normalize return "[{0}]".format( socket.inet_ntop(socket.AF_INET6, addr)) except socket.error: raise JIDError("Invalid IPv6 literal in JID domainpart") else: raise JIDError("Invalid use of '[' or ']' in JID domainpart") elif data[0].isdigit() and data[-1].isdigit(): try: # try to decode as IPv4... addr = socket.inet_pton(socket.AF_INET, data) # ...and normalize return socket.inet_ntop(socket.AF_INET, addr) except socket.error: pass data = str(data) labels = data.split(".") labels = [idna.nameprep(label) for label in labels] domain = ".".join(labels) if len(domain.encode("utf-8")) > 1023: raise JIDError("Domain name too long") return domain
return "[{0}]".format(addr) except ValueError, err: logger.debug("ValueError: {0}".format(err)) raise JIDError(u"Invalid IPv6 literal in JID domainpart") else: raise JIDError(u"Invalid use of '[' or ']' in JID domainpart") elif data[0].isdigit() and data[-1].isdigit(): try: addr = _validate_ip_address(socket.AF_INET, data) except ValueError, err: logger.debug("ValueError: {0}".format(err)) data = UNICODE_DOT_RE.sub(u".", data) data = data.rstrip(u".") labels = data.split(u".") try: labels = [idna.nameprep(label) for label in labels] except UnicodeError: raise JIDError(u"Domain name invalid") for label in labels: if not STD3_LABEL_RE.match(label): raise JIDError(u"Domain name invalid") try: idna.ToASCII(label) except UnicodeError: raise JIDError(u"Domain name invalid") domain = u".".join(labels) if len(domain.encode("utf-8")) > 1023: raise JIDError(u"Domain name too long") return domain @staticmethod