Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

correctly set the content type header; raise not found if the cert is…

… not found
  • Loading branch information...
commit 82df82150e391b619d5168e1097a308e7228ee11 1 parent 06e5d9d
Attila Oláh authored
Showing with 20 additions and 6 deletions.
  1. +20 −6 api/cert.py
View
26 api/cert.py
@@ -8,23 +8,37 @@
from . import ds
+MIME_TYPES = {
+ None: b'application/octet-stream',
+ b'json': b'application/json',
+ b'txt': b'text/plain',
+ b'xml': b'text/xml',
+}
+
+
+
class Cert(webapp.RequestHandler):
"""Single certificate request handler."""
@ndb.synctasklet
- def get(self, prime, suffix=''):
+ def get(self, prime, suffix=None):
"""Redirect to the batch of certs, if available."""
prime = long(prime)
entity = yield ds.batch_key(get_batch_hash(prime)).get_async()
if entity is None:
- # Certificate not found:
- self.response.out.write(u'Certificate not found.')
- self.response.status = 404
- raise ndb.Return()
+ raise self._not_found()
cert_data = entity.get_cert(prime, suffix or 'raw')
- #self.response.content_type = b'application/octet-stream'
+ if cert_data is None:
+ raise self._not_found()
+ self.response.content_type = MIME_TYPES[suffix]
self.response.out.write(cert_data)
+ def _not_found(self, code=404, message=u'Certificate not found.'):
+ """Not found shortcut."""
+ self.response.out.write(message)
+ self.response.status = code
+ return ndb.Return()
+
app = webapp.WSGIApplication([
(r'^/cert/(\d+)(?:\.(json|xml|txt))?$', Cert),
Please sign in to comment.
Something went wrong with that request. Please try again.