aboutsummaryrefslogtreecommitdiff
path: root/rpki/http_simple.py
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2014-09-21 17:11:03 +0000
committerRob Austein <sra@hactrn.net>2014-09-21 17:11:03 +0000
commitfeadd1293e004d1acef12e39184cfdb1a280ea18 (patch)
treeae2188461fb0475719616b14b753d071560201ce /rpki/http_simple.py
parent9f8c367eac5db1f28e11b187eb48ad37b0ab3797 (diff)
Move synchronous HTTP client code to library.
svn path=/branches/tk705/; revision=5965
Diffstat (limited to 'rpki/http_simple.py')
-rw-r--r--rpki/http_simple.py39
1 files changed, 39 insertions, 0 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()