diff options
author | Rob Austein <sra@hactrn.net> | 2014-12-19 17:38:55 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2014-12-19 17:38:55 +0000 |
commit | 17dc566f6ab144bb5be8ceaa23595c960d917fba (patch) | |
tree | f099b3b24cbf3b01e89072112efdfd20d4379e9f /rpki/http.py | |
parent | b2b0d58c3eecb66c5a36cd912ce7da422065a3b1 (diff) |
Accept either old or new MIME Content-Type in up-down server code, and
allow configuration of either old or new in up-down client code. For
the moment, client defaults to using the old Content-Type, to avoid
breaking deployed services, but this will change at some point.
See #747.
svn path=/trunk/; revision=6045
Diffstat (limited to 'rpki/http.py')
-rw-r--r-- | rpki/http.py | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/rpki/http.py b/rpki/http.py index 546dd310..71239c7f 100644 --- a/rpki/http.py +++ b/rpki/http.py @@ -38,9 +38,10 @@ import rpki.POW logger = logging.getLogger(__name__) -## @var rpki_content_type -# HTTP content type used for all RPKI messages. -rpki_content_type = "application/x-rpki" +## @var default_content_type +# HTTP content type used for RPKI messages. +# Can be overriden on a per-client or per-server basis. +default_content_type = "application/x-rpki" ## @var want_persistent_client # Whether we want persistent HTTP client streams, when server also supports them. @@ -487,6 +488,7 @@ class http_server(http_stream): def __init__(self, sock, handlers): self.handlers = handlers + self.received_content_type = None http_stream.__init__(self, sock = sock) self.expect_close = not want_persistent_server self.logger.debug("Starting") @@ -503,10 +505,10 @@ class http_server(http_stream): """ Helper method to search self.handlers. """ - for s, h in self.handlers: - if path.startswith(s): - return h - return None + for h in self.handlers: + if path.startswith(h[0]): + return h[1], h[2] if len(h) > 2 else (default_content_type,) + return None, None def handle_message(self): """ @@ -518,12 +520,13 @@ class http_server(http_stream): self.logger.debug("Received request %r", self.msg) if not self.msg.persistent: self.expect_close = True - handler = self.find_handler(self.msg.path) + handler, allowed_content_types = self.find_handler(self.msg.path) + self.received_content_type = self.msg.headers["Content-Type"] error = None if self.msg.cmd != "POST": error = 501, "No handler for method %s" % self.msg.cmd - elif self.msg.headers["Content-Type"] != rpki_content_type: - error = 415, "No handler for Content-Type %s" % self.headers["Content-Type"] + elif self.received_content_type not in allowed_content_types: + error = 415, "No handler for Content-Type %s" % self.received_content_type elif handler is None: error = 404, "No handler for URL %s" % self.msg.path if error is None: @@ -560,7 +563,7 @@ class http_server(http_stream): if code >= 400: self.expect_close = True msg = http_response(code = code, reason = reason, body = body, - Content_Type = rpki_content_type, + Content_Type = self.received_content_type, Connection = "Close" if self.expect_close else "Keep-Alive") self.push(msg.format()) if self.expect_close: @@ -936,7 +939,7 @@ class http_queue(object): # Map of (host, port) tuples to http_queue objects. client_queues = {} -def client(msg, url, callback, errback): +def client(msg, url, callback, errback, content_type = default_content_type): """ Open client HTTP connection, send a message, set up callbacks to handle response. @@ -961,7 +964,7 @@ def client(msg, url, callback, errback): callback = callback, errback = errback, Host = u.hostname, - Content_Type = rpki_content_type) + Content_Type = content_type) hostport = (u.hostname or "localhost", u.port or default_tcp_port) |