Commit 3e48b3c6 authored by Alexandre's avatar Alexandre
Browse files

[DoH] Divide request in different steps

parent 4500e063
......@@ -184,9 +184,52 @@ class RequestDoH:
def close_handle(self):
self.handle.close()
def perform(self):
def prepare_query(self, curl_opt):
self.buffer = io.BytesIO()
curl_opt[pycurl.WRITEDATA] = self.buffer
self.create_handle(curl_opt)
def perform_query(self):
self.handle.perform()
def receive_query(self):
self.body = self.buffer.getvalue()
self.body_size = len(self.body)
self.buffer.close()
def check_response(self):
ok = True
self.rcode = self.handle.getinfo(pycurl.RESPONSE_CODE)
if self.rcode == 200:
ctype = self.handle.getinfo(pycurl.CONTENT_TYPE)
if ctype != "application/dns-message":
self.response = "Content type of the response (\"%s\") invalid" % ctype
ok = False
else:
if not head:
try:
self.response = dns.message.from_wire(self.body)
except dns.message.TrailingJunk: # Not DNS. Should
# not happen for a content type
# application/dns-message but who knows?
self.response = "ERROR Not proper DNS data, trailing junk \"%s\"" % self.body
ok = False
except dns.name.BadLabelType: # Not DNS.
self.response = "ERROR Not proper DNS data (wrong path in the URL?) \"%s\"" % self.body[:100]
ok = False
else:
if self.body_size == 0:
self.response = "HEAD successful"
else:
self.response = "ERROR Body length is not null \"%s\"" % self.body[:100]
ok = False
else:
ok = False
if self.body_size == 0:
self.response = "[No details]"
else:
self.response = self.body
class Connection:
def __init__(self, server, servername=None, connect=None, forceIPv4=False, forceIPv6=False,
......@@ -388,47 +431,11 @@ class ConnectionDoH(Connection):
self.prepare_test_head(qname, qtype)
else:
self.prepare_test_get(qname, qtype)
buffer = io.BytesIO()
self.curl_opt[pycurl.WRITEDATA] = buffer
self.request.create_handle(self.curl_opt)
self.request.perform()
size = None
rcode = self.request.handle.getinfo(pycurl.RESPONSE_CODE)
ok = True
if rcode == 200:
ctype = self.request.handle.getinfo(pycurl.CONTENT_TYPE)
if ctype != "application/dns-message":
response = "Content type of the response (\"%s\") invalid" % ctype
ok = False
else:
body = buffer.getvalue()
if not head:
try:
size = len(body)
response = dns.message.from_wire(body)
except dns.message.TrailingJunk: # Not DNS. Should
# not happen for a content type
# application/dns-message but who knows?
response = "ERROR Not proper DNS data, trailing junk \"%s\"" % body
ok = False
except dns.name.BadLabelType: # Not DNS.
response = "ERROR Not proper DNS data (wrong path in the URL?) \"%s\"" % body[:100]
ok = False
else:
if len(body) == 0:
response = "HEAD successful"
else:
response = "ERROR Body length is not null \"%s\"" % body[:100]
ok = False
else:
ok = False
body = buffer.getvalue()
if len(body) == 0:
response = "[No details]"
else:
response = body
buffer.close()
return (rcode, response, size)
self.request.prepare_query(self.curl_opt)
self.request.perform_query()
self.request.receive_query()
self.request.check_response()
return (self.request.rcode, self.request.response, self.request.body_size)
# Main program
me = os.path.basename(sys.argv[0])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment