diff options
Diffstat (limited to 'rpki')
-rw-r--r-- | rpki/http_simple.py | 39 | ||||
-rw-r--r-- | rpki/rootd.py | 21 | ||||
-rw-r--r-- | rpki/up_down.py | 4 |
3 files changed, 42 insertions, 22 deletions
diff --git a/rpki/http_simple.py b/rpki/http_simple.py index 8f609e46..4a05d607 100644 --- a/rpki/http_simple.py +++ b/rpki/http_simple.py @@ -20,6 +20,8 @@ need the full-blown rpki.http asynchronous code. """ import logging +import httplib +import urlparse import BaseHTTPServer logger = logging.getLogger(__name__) @@ -83,3 +85,40 @@ def server(handlers, port, host = ""): rpki_handlers = handlers BaseHTTPServer.HTTPServer((host, port), RequestHandler).serve_forever() + + +class BadURL(Exception): + "Bad contact URL" + +class RequestFailed(Exception): + "HTTP returned failure" + +class BadContentType(Exception): + "Wrong HTTP Content-Type" + + +def client(url, query): + """ + Issue single a query and return the response. + + Might want to add CMS processing here, not sure yet. + """ + + u = urlparse.urlparse(url) + + if u.scheme not in ("", "http") or u.username or u.password or u.params or u.query or u.fragment: + raise BadURL("Unusable URL %s", url) + + http = httplib.HTTPConnection(u.hostname, u.port or httplib.HTTP_PORT) + + http.request("POST", u.path, query, {"Content-Type" : rpki_content_type}) + + r = http.getresponse() + + if r.status != 200: + raise RequestFailed("HTTP request failed with status %r reason %r" % (r.status, r.reason)) + + if r.getheader("Content-Type") != rpki_content_type: + raise BadContentType("HTTP Content-Type %r, expected %r" % (r.getheader("Content-Type"), rpki_content_type)) + + return r.read() diff --git a/rpki/rootd.py b/rpki/rootd.py index 4be38a0c..a9562881 100644 --- a/rpki/rootd.py +++ b/rpki/rootd.py @@ -249,17 +249,7 @@ class main(object): for q_pdu in q_msg: logger.info("Sending %s to pubd", q_pdu.get("uri")) q_der = rpki.publication.cms_msg_no_sax().wrap(q_msg, self.rootd_bpki_key, self.rootd_bpki_cert, self.rootd_bpki_crl) - logger.debug("Sending request to pubd") - http = httplib.HTTPConnection(self.pubd_host, self.pubd_port) - http.request("POST", self.pubd_path, q_der, {"Content-Type" : rpki.http_simple.rpki_content_type}) - r = http.getresponse() - if r.status != 200: - raise rpki.exceptions.HTTPRequestFailed("HTTP request to pubd failed with status %r reason %r" % (r.status, r.reason)) - if r.getheader("Content-Type") != rpki.http_simple.rpki_content_type: - raise rpki.exceptions.HTTPRequestFailed("HTTP request to pubd failed, got Content-Type %r, expected %r" % ( - r.getheader("Content-Type"), rpki.http_simple.rpki_content_type)) - logger.debug("Received response from pubd") - r_der = r.read() + r_der = rpki.http_simple.client(self.pubd_url, q_der) r_cms = rpki.publication.cms_msg_no_sax(DER = r_der) r_msg = r_cms.unwrap((self.bpki_ta, self.pubd_bpki_cert)) self.pubd_cms_timestamp = r_cms.check_replay(self.pubd_cms_timestamp, self.pubd_url) @@ -452,15 +442,6 @@ class main(object): self.pubd_url = self.cfg.get("pubd-contact-uri") - u = urlparse.urlparse(self.pubd_url) - if u.scheme not in ("", "http") or u.username or u.password or u.params or u.query or u.fragment: - logger.error("Unusable URL %s", self.pubd_url) - sys.exit(1) - - self.pubd_host = u.hostname - self.pubd_port = u.port or httplib.HTTP_PORT - self.pubd_path = u.path - rpki.http_simple.server(host = self.http_server_host, port = self.http_server_port, handlers = self.handler) diff --git a/rpki/up_down.py b/rpki/up_down.py index 82abcebb..41f5ec2b 100644 --- a/rpki/up_down.py +++ b/rpki/up_down.py @@ -60,7 +60,7 @@ error_response_codes = { 2001 : "Internal Server Error - Request not performed" } -def generate_error_response(r_pdu, status = 2001, description = None): +def generate_error_response(r_msg, status = 2001, description = None): """ Generate an error response. If STATUS is given, it specifies the numeric code to use, otherwise we default to "internal error". @@ -69,7 +69,7 @@ def generate_error_response(r_pdu, status = 2001, description = None): """ assert status in error_response_codes - del r_msg[:len(r_msg)] + del r_msg[:] r_msg.set("type", "error_response") SubElement(r_msg, tag_status).text = str(status) se = SubElement(r_msg, tag_description) |