A few Unicode bugs solved

master
Stephane Bortzmeyer 3 years ago
parent 90145fec66
commit b804f5dd39
  1. 3
      DNSLG/Formatter.py
  2. 2
      DNSLG/Resolver.py
  3. 31
      DNSLG/__init__.py

@ -1007,7 +1007,8 @@ class HtmlFormatter(Formatter):
suppressXMLDeclaration=True,
outputEncoding=querier.encoding)
elif rdata.rdtype == dns.rdatatype.TXT:
text = str(" ".join(rdata.strings), "UTF-8")
text = b" ".join(rdata.strings)
text = text.decode() # May break if the content of TXT is not UTF-8? TODO
icontext.addGlobal ("text", text)
self.txt_template.expand (icontext, iresult,
suppressXMLDeclaration=True,

@ -73,7 +73,7 @@ class Resolver(object):
raise NoNameservers()
for ns in self.nameservers:
try:
message = dns.message.make_query(name.decode(), type, rdclass=klass,
message = dns.message.make_query(name, type, rdclass=klass,
use_edns=self.edns, payload=self.payload,
want_dnssec=self.do)
except TypeError: # Old DNS Python... Code here just as long as it lingers in some places

@ -11,6 +11,9 @@ from urllib.parse import parse_qs
import encodings.idna
import os
from datetime import datetime
import time
import logging
import logging.handlers
# http://www.dnspython.org/
import dns.resolver
@ -49,14 +52,6 @@ def send_response(start_response, status, output, type):
('Link', '<https://www.bortzmeyer.org/dns-lg-usage.html>; rel="service-doc"; type="text/html"')]
start_response(status, response_headers)
def punycode_of(domain):
labels = domain.split(".")
result = ""
for label in labels:
if label:
result += (encodings.idna.ToASCII(label).decode() + ".")
return (result)
class Querier(object):
def __init__(self, email_admin=None, url_doc=None, url_css=None, url_opensearch=None,
@ -68,6 +63,14 @@ class Querier(object):
google_code=None, description=None, description_html=None,
forbidden_suffixes=[]):
self.resolver = Resolver.Resolver(edns_payload=edns_size)
self.logger = logging.getLogger('DNSLG')
self.logger.setLevel(logging.DEBUG)
ft = logging.Formatter(fmt = 'DNSLG - %(levelname)s - %(asctime)s - %(message)s',
datefmt = '%Y-%m-%d %H:%M:%SZ')
ft.converter = time.gmtime
fh = logging.handlers.SysLogHandler(address="/dev/log", facility="local0")
fh.setFormatter(ft)
self.logger.addHandler(fh)
self.buckets = {}
self.base_url = base_url
self.whitelist = whitelist
@ -189,6 +192,9 @@ Disallow: /
else:
self.buckets[ip_prefix.cidr] = LeakyBucket(size=self.bucket_size)
args = path[1:]
args = bytes([ ord(c) for c in args]).decode() # The URL is
# raw bytes but given to us as a string, so we have to force
# interpretation as UTF-8 :-(
slashpos = args.find('/')
if slashpos == -1:
if reverse:
@ -236,11 +242,12 @@ Disallow: /
send_response(start_response, '403 Local domain is private',
output, plaintype)
return [output.encode()]
punycode_domain = punycode_of(domain)
punycode_domain = domain.encode("idna").decode()
if punycode_domain != domain:
qdomain = punycode_domain.encode("US-ASCII")
qdomain = punycode_domain
else:
qdomain = domain.encode("US-ASCII")
qdomain = domain
self.logger.info("Domain is %s, punycode is %s, qdomain is %s" % (domain, punycode_domain, qdomain))
try:
if format == "HTML":
formatter = Formatter.HtmlFormatter(domain)
@ -301,7 +308,7 @@ Disallow: /
send_response(start_response, '400 Unknown class', output,
plaintype)
except Resolver.NoSuchDomainName:
output = "Domain %s does not exist\n" % domain
output = "Domain %s/%s does not exist\n" % (domain, qdomain)
output = output.encode(self.encoding)
# TODO send back in the requested format (see issue #11)
send_response(start_response, '404 No such domain', output, plaintype)

Loading…
Cancel
Save